Today there is time to do a bit mongoDB replica set, I met a lot of pits, wrote this article for future access!
This tutorial is the installation and testing in the windows environment (I am a native + two virtual machines)
Native: 10.53.8.159
A virtual machine: 10.53.8.110
Virtual machine two: 10.53.8.112
MongoDB version is as follows:
Some basic setup I will not say, for example, three can ping each other, turn off the firewall.
1. From the official website to download the installation package
https://www.mongodb.com/download-center/community
2. Follow the tutorial to install
Installation Guide online a lot https://www.runoob.com/mongodb/mongodb-window-install.html
Note: After installation, do not follow it, "to create the directory" because the data, log directory has been created.
After installation, my directory is as follows
Path: E: \ MongoDB \ Server \ 4.0
3. On the remaining two virtual machines have their own install it again mongoDB.
My installation path and native consistent, E: \ MongoDB \ Server \ 4.0 I hope you also consistent, so you get familiar to casual.
4. This editing machine E: \ MongoDB \ Server \ 4.0 \ bin \ mongod.cfg profile
Profiles using yml format, the format needs to be careful not to mistake, otherwise an error code 1053
Modify two points:
1.bindIp: 127.0.0.1 =======> bindIp: 0.0.0.0 This is mongoDB which can be controlled from ip landing, 0.0.0.0, then it can be from any ip
2. The # sign in front of replication release, modified as follows
replication:
oplogSizeMB: 128
replSetName: mySet
enableMajorityReadConcern: true
The same step 4, edit a virtual machine, II E: \ MongoDB \ Server \ 4.0 \ bin \ mongod.cfg profile
6. Start making bat file
In the native E: \ \ \ under bin MongoDB Server \ 4.0, New mongo_start.bat (create txt file and then change the suffix txt to bat) which reads as follows
mongod.exe --config "E:\MongoDB\Server\4.0\bin\mongod.cfg"
7. with step 6, a virtual machine, two also make bat file
8. Double-click the machine, a virtual machine, the two starting mongoDB bat replica set.
9. In the machine, into the E: \ MongoDB \ Server \ 4.0 \ bin directory, the command line is connected with cmd mongoDB.
mongo --port 27017
10. Define the members of the replica set configuration
Note: _id must mongod.cfg profile replSetName identical.
rsconf = {
_id: "mySet",
members: [
{
_id: 0,
host: "10.53.8.159:27017"
},
{
_id: 1,
host: "10.53.8.110:27017"
},
{
_id: 2,
host: "10.53.8.112:27017"
}
]
}
11. Perform initialization commands
rs.initiate(rsconf)
10 correspond to the name defined rsconf step.
OK it is 1, indicating successfully created.
12. View configuration information
rs.conf()
12. Use visualization tools may also see information
13. 10.53.8.159 insert a data connection
DB . COL . INSERT ({ title : 'Tutorial MongoDB' , Description : 'MongoDB is a database Nosql' , by : 'novice tutorial' , URL : 'http://www.runoob.com' , Tags : [ 'MongoDB ' , ' Database ' , ' the NoSQL ' ], Likes : 100 })
14. a 10.53.8.110,10.53.8.112 can be confirmed on the data, so far replica set configuration and tested successfully.
Remember:
1. Before starting the replica set, ensure data directory is empty
2. Ensure that the path is not wrong
3. Make sure the three machine can ping each other. (Sometimes bad network will fail)
Attach official documentation: English, able to read, absolutely great harvest.
https://docs.mongodb.com/v4.0/tutorial/deploy-replica-set/
Have any questions, comments contact me!