Build a mongodb pseudo-replica set in three minutes

1. Purpose
This article mainly explains the "how to" problem, how to build a mongodb pseudo-cluster in the shortest time

2. Download mongodb and unzip it

wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.8.tgz
tar -zxvf mongodb-linux-x86_64-2.4.8.tgz

3. Prepare the mongodb.conf configuration file
Because there is no configuration file in the downloaded mongodb package, we need to manually create a configuration file, and the installation here uses the key to build a replica set to improve security. How to generate Please Baidu by yourself for the key.
The following is the content of the mongodb.conf configuration file

# 数据存放目录
dbpath=/opt/Link/platform/mongodb/data

# 日志存放路径
logpath=/opt/Link/platform/mongodb/logs/mongodb.log  

# 进程文件, 方便停止mongodb
pidfilepath=/opt/Link/platform/mongodb/logs/mongodb.pid  

# SOCK 文件路径
unixSocketPrefix=/opt/Link/platform/mongodb

# 为每一个数据库按照数据库名建立文件夹存放
directoryperdb=true

# 以追加的方式记录日志
logappend=true  

# replica set的名字
replSet=prodRep001

# mongodb所绑定的ip地址
bind_ip=0.0.0.0

# mongodb进程所使用的端口号, 默认为27017
port=27017

# 数据库引擎
storageEngine=wiredTiger

# 登录需要认证,命令行指定(副本集中如果指定了--keyFile其实已经包含了auth了)
auth=true

# mongodb操作日志文件的最大大小. 单位为Mb, 默认为硬盘剩余空间的5%
oplogSize=10000

# 以后台方式运行进程
fork=true  

# 不预先分配存储
noprealloc=true  

# 开启httpinterface
httpinterface=true

# 开启rest支持更多的状态显示
rest = true

#开启auth之后,各个mongodb直接需要通过keyFile相互认证,副本集上的各个mongodb的keyFile必须相同
keyFile=/opt/Link/platform/mongodb/mongodb-keyfile

#maxConns=20000

Some of these configuration items may change due to different versions. Check the problem during startup and delete some unnecessary configuration sections.

Fourth, put the prepared conf file and key file under the decompression package of mongo

5. Create the data folder, the logs folder, and modify the execution permissions of the key

mkdir logs;
mkdir data;
chmod 400 mongo-keyfile

After doing the above operations, you will see the following files
write picture description here

6. Responsible for decompressing two copies of the package

cp -rf mongodb1 mongodb2
cp -rf mongodb1 mongodb3

write picture description here

7. Enter the configuration of the mongodb2 and mongodb3 files and modify the port. The three nodes cannot use the same port

Eight, start all nodes

src/mongod -f mongodb.conf

The startup success information is as follows:
write picture description here

write picture description here

9. Use the administrator account to log in to the original mongodb through tools or command lines, and run the following commands in mongodb. The following is the mongodb command

use admin
 config = {_id: 'prodRep001', members: [ 
    {_id: 0, host: '127.0.0.1:27001'}, 
    {_id: 1, host: '127.0.0.1:27002'}, 
    {_id: 2, host: '127.0.0.1:27003',arbiterOnly:true}] #仲裁结点
 }
rs.initiate(config)

10. View the status of the replica set

rs.status()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326126796&siteId=291194637