Setting up a MongoDB replica set

Source: Internet
Author: User

Reference post: https://www.jianshu.com/p/ca620842868a

First, installation Preparation
1. Modify the System configuration
#关闭selinux, Firewall
#修改/etc/security/limits.conf Add the following:

    • Soft Nofile 65536
    • Hard Nofile 65536
    • Soft Nproc 65536
    • Hard Nproc 65536
      #修改/etc/security/limits.d/20-nproc.conf
    • Soft Nproc 65536
      3. Create account, Directory
      Groupadd-g MONGO
      Useradd-u 600-g MONGO MONGO

Second, installation steps
1. Unzip the file
Tar xvf mongodb-linux-x86_64-enterprise-rhel70-3.6.2.tgz-c/usr/local/
Cd/usr/local
MV Mongodb-linux-x86_64-enterprise-rhel70-3.6.2/mongodb
CD MongoDB
Mkdir-p data/db
mkdir Log
Touch Log/mongod.log
mkdir etc
Touch etc/mongod.conf
Chown-r Mongo.mongo/usr/local/mongodb
2. Configure config file on each machine separately
Vim etc/mongod.conf
DBPath =/usr/local/mongodb/data/db # Specify the database path
LogPath =/usr/local/mongodb/log/mongod.log # specify MongoDB log file
Logappend = true # Write log with Append
Port = 27017 #设置端口号为27017
BIND_IP = 192.168.1.221
Fork = True #设置以守护进程的方式启动mongod
Replset = Bsbgpsrs #设置副本集的名字为myrs, replset name of the same replica cluster
3. Start the replica set on each machine separately
Su-mongo
#修改环境变量
Path= $PATH: $HOME/bin:/usr/local/mongodb/bin
Export PATH
#启动
Yum install-y net-snmp* gcc-c++
Nohup mongod-f/usr/local/mongodb/etc/mongod.conf >/dev/null 2>&1 &
4. Initialize the replica set
Login to the MongoDB backend Management shell of any one machine:
Cd/usr/local/mongodb
./bin/mongo
Use admin
Config = {
"_id": "Bsbgpsrs",
"Members": [
{"_id": 0, "host": "192.168.198.224:27017"},
{"_id": 1, "host": "192.168.198.225:27017"},
{"_id": 2, "host": "192.168.198.226:27017", arbiteronly:true}
]
}
Rs.initiate (config); #初始化配置
5. View replica set status
Rs.status ();
6. Add Validation
Use admin
Db.createuser ({User: "admin", pwd: "mongo/2018", roles:["root"]})
Db.auth ("admin", "mongo/2018")

Third, test the replica set
1. Test copy Set data copy function
At this point 192.168.198.224 is the master node on my machine and we use MONGO to log in to the shell.

Cd/usr/local/mongodb
./bin/mongo
Use Test #创建test数据库
Db.testdb.insert ({"Name": "Yaya"}); #插入数据
We log in to the shell with a replica node, and here I am 192.168.198.225:

Cd/usr/local/mongodb
./bin/mongo
Use test
Show tables
This will cause an error:

[Thread1] Error:listcollections failed: {
"OK": 0,
"ErrMsg": "Not Master and Slaveok=false",
"Code": 13435,
"codename": "Notmasternoslaveok"
}
Because MongoDB defaults to read and write data from the primary node, it is not allowed to read on the replica node, and the replica node needs to be read.

Myrs:secondary> Db.getmongo (). Setslaveok ();
You can now read the data:

Repset:secondary> Db.testdb.find ();
Console output: {"_id": ObjectId ("59676d711881041abab44477"), "name": "Yaya"}
Therefore, the functionality of data replication is available.

2. Test the failover function of the replica set
Kill the Mongod process 192.168.198.224 the primary node:

myrs:primary> Use admin
Myrs:primary> Db.shutdownserver ()
As you can see, 192.168.198.225 is originally a secondary node and is automatically converted to the primary node at this time. Can be viewed through rs.status ().

Iv. Other Settings
1, clear large pages in/etc/rc.d/rc.local add the following:
If Test-f/sys/kernel/mm/transparent_hugepage/enabled; Then
echo Never >/sys/kernel/mm/transparent_hugepage/enabled
Fi
If Test-f/sys/kernel/mm/transparent_hugepage/defrag; Then
echo Never >/sys/kernel/mm/transparent_hugepage/defrag
Fi
chmod +x/etc/rc.d/rc.local
2. Mongodb C Drive Installation
Tar xvf mongo-c-driver-1.6.1.tar.gz
CD MONGO-C-DRIVER-1.6.1/
./configure-disable-automatic-init-and-cleanup
Make
Make install

Setting up a MongoDB replica set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.