first, the environment preparation
1, Centos7
2, mongodb3.4.10
3. Three machine IPs are: 192.168.1.100, 192.168.1.135, 192.168.1.136
second, the installation of MONGDB database
1, respectively in three machines for mongdb installation, specific reference to the previous tutorial MongoDB installation and Configuration
2. Start MongoDB on three machines respectively, the specific command is as follows
[Email protected] mongodb]#/bin/mongod-f mongodb-config/server.conf--replset Mongotestfuben
Where –replset represents the replica cluster parameters, Mongotestfuben is the replica set name, where the name can be arbitrarily taken, and the other two machines will be the same
If you start successfully you will see a prompt similar to the following
About-to-fork child process, waiting until server was ready for connections.
Forked process:3805
Child process started successfully, parent exiting
iii. Configuring a MongoDB replica set
The first thing to do is to initialize the MongoDB replica set by simply initializing the replica set at any one of the servers.
1, connect any one, such as IP 100, into the MONGO shell operation
You can directly use the MONGO command in the bin directory, such as/bin/mongo -port 27017
Also can use the remote client, I installed the MongoDB client on the remote computer, and configured the environment variable, the operation is more convenient, so the remote computer's cmd window to connect 100 of the MongoDB server
C:\Users\zxp>mongo 192.168.220.100:27017
MongoDB shell version v3.4.9
connecting to: 192.168.220.100:27017
MongoDB server version: 3.4.10
2、进入admin库
use admin
3. MONGO Copy Configuration
Copy the following configuration directly or before you edit it in advance.
config={_id: "Mongotestfuben", Members:[{_id:1,host: "192.168.220.100:27017"},{_id:2,host: "192.168.220.135:27017" },{_id:3,host: "192.168.220.136:27017"}]}
Then execute the following command
Rs.initiate (config)
Display {"OK": 1}, which indicates success of initialization
This makes it possible to use a replica cluster.
Finally, add a few more orders.
rs.add("host:port")
+ = Add a node
rs.remove("host:port")
= Delete Member
rs.config()
= = View Replica set configuration
rs.reconfig(config)
+ = Reconfigure the replica set according to the Config configuration object
rs.isMaster 查看主节点信息等
Reference: MongoDB3.4.2 cluster setup in Centos 7
MongoDB replica Set related commands
The construction of MongoDB cluster