MongoDB's replica set mode has been greatly improved by the earlier Master-slave mode, since the previous Master-slave mode does not support the host outage after switching to the slave, has gradually been eliminated. And now the official recommendation of the replica set mode, support multiple nodes coexist, when the main node down from the highest weight of the sub-node, switch to the main node, is very easy to use, the following is the author of some of the construction steps and considerations, as well as fault handling and so on.
Precautions:
1. MongoDB no longer supports 32-bit operating systems, so be sure to install a 64-bit system.
2, the process of building a replica set, you must shut down the firewall, whether it is SELinux or iptables, or will error, display can not communicate, "errmsg": "Replsetinitiate quorum check failed because not all Proposed set members responded affirmatively:192.168.145.136:27017 failed and No route to host.
3, the MongoDB version of each node must be consistent, otherwise the build is not successful, using the Rs.status () command to view the time will show "Statestr": "(not reachable/healthy)."
Installation steps:
1. Write MongoDB's yum source configuration file Mongodb-org-3.4.repo:
[mongodb-org-3.4]
Name=mongodb Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
Gpgcheck=1
Enabled=1
Gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
2. Yum Install-y mongodb-org
It will automatically download the 3.4 version of MongoDB
Build steps:
1. After installing MongoDB on three machines, rewrite the configuration file/etc/mongod.conf:
Replication
# #oplog大小
Oplogsizemb:20
# #复制集名称
Replsetname:raffaele_mongo
2. Restart Service:/etc/init.d/mongod restart
3, select a machine as the main node, enter the machine MongoDB, execute the following command:
>useadmin
>config={_id: "Raffaele_mongo", Members:[{_id:0,host: "192.168.145.135:27017"},{_id:1,host: " 192.168.145.132:27017 "},{_id:2,host:" 192.168.145.136:27017 "}]}
>rs.initiate (config)
>rs.add ("192.168.145.132")
>rs.add ("192.168.145.136")
Rs.status ()//View status
If two from the status of "Statestr": "STARTUP", you need to do the following
>var config={_id: "Raffaele_mongo", Members:[{_id:0,host: "192.168.145.135:27017"},{_id:1,host: " 192.168.145.132:27017 "},{_id:2,host:" 192.168.145.136:27017 "}]}
>rs.reconfig (config)
viewing Rs.status () again will find the state changed from secondary
Troubleshooting:
1. When viewing individual node states using the Rs.status () command, the secondary node always displays "Statestr": "(not Reachable/healthy)".
This is due to the inconsistency between the master node and the MongoDB version of the secondary node, you can replace the Yum source file, and re-install-y mongodb-org, so that the MongoDB version can be updated.
2, E QUERY [Thread1] syntaxerror:missing) after argument list @ (Shell): 1:14
Syntax error, this problem can be directly on the Internet or official documents to search the use and format of the command, of course, the personal feel MongoDB Official document is very verbose.
3, operation of any command on the sub-node, display
E QUERY [Thread1] error:listdatabases failed:{
"OK": 0,
"ErrMsg": "Not Master and Slaveok=false",
"Code": 13435,
"codename": "Notmasternoslaveok"
} :
You only need to execute the command on the secondary node: Rs.slaveok ()
This article is from "The Miracle Teenager" blog, please be sure to keep this source http://raffaelexr.blog.51cto.com/8555551/1942735
MongoDB Replica Set Build