docker mongo replica set configuration (1)

1. Generate a certificate

Non-docker environment (the current directory data)

 cd  /data/

mkdir core
cd core
//生成keyfile
openssl rand -base64 741 > mongodb-keyfile
chmod 600 mongodb-keyfile
mongod.conf added (note json format) . 1 Security:
 2 keyFile: " / Data / Core / MongoDB-KeyFile " . 3 Replication: . 4 replSetName: RS

 

 

 

Copy mongodb-keyfile mongo server to another
execution

chmod 600 mongodb-keyfile

If you need to perform a docker

chown 999 mongodb-keyfile

 

 

2. Run mongo docker

 docker run --name mongo -p 27017:27017 --restart=always -v /etc/localtime:/etc/localtime:ro -v /data/mongo:/data/db -v /data/core/mongodb-keyfile:/opt/mongodb-keyfile -d mongo:4.0.3 --smallfiles --keyFile=/opt/mongodb-keyfile --replSet "rs"
 
 --smallfiles 开启验证
 --keyFile 设置统一的key

 

 

3, Log mongoDB

Copy the code
Run 1 the following 
2 rs.initiate ({_id: "rs0 ", members: [{_id: 0, host: "172.17.0.2:27017"}, {_id: 1, host: "172.17.0.3:27017"} , {the _id: 2, Host: "172.17.0.4:27017"}]}) 
. 3 may be used {the _id: 0, Host: '127.0.0.1: 10002', priority:}. 1 (a plurality of the nodes designated master node priority high will become the master node)
4 _id: copy the name of the set. Mongod must be consistent with the start --replSet
5 members: a list of the members of the configuration file.
6 members [n] ._ id: id used to identify the members can not be repeated
7 members [n] .host: members of the corresponding host: port obtained in the fourth step ip
Copy the code

 

4, using rs.status (); View cluster status

 

5, do not read and write permissions from the default library

Need to enter the execution rs.slaveOk from mongoDB library (); or db.getMongo () setSlaveOk ();. 
But but but! ! ! 
Only take effect when the times! Only take effect when the times! Only take effect when the times! 
If you need permanent effect may in turn execute the following command to 
the Find / - name .mongorc.js 
vim / Home / Sea / .mongorc.js 
add content 
rs.slaveOk ();

 

 

Mandatory replacement master node approach
http://www.mongoing.com/docs/tutorial/force-member-to-be-primary.html

Copy the code
Note 1: 
2 in the building process, a problem encountered 
3 when stopped after the two main slave nodes used in rs.stepDown (120) to switch from an existing node to the master node lead to loss of the primary node may in this case the turning off of a restart from a node, then the master node will automatically be elected, a copy rule set is a must have two available databases to be able to automatically elected from the master node. 
Another 4: This does not involve voting node, interested can search related documents themselves can be 
more than five say one: This is version 4.0.3, 4.0 version of the following associated command may fail! // tested, individual operational reasons, need to perform rs.reconfig (); when adding "protocolVersion": 1   
such as this: rs.reconfig ({_id: "rs0 ", "protocolVersion": 1, members: [{_id : 0, host: "192.168.1.64:27017",priority:2} , {_id: 1, host:" 192.168.1.50:27017 "}, {_id: 2, host:" 192.168.1.51:27017 "}] })
If you need to add a non-primary node performs { "force": true} e.g. rs.reconfig (cfg, { "force" : true})
Copy the code

 

 

 



Guess you like

Origin www.cnblogs.com/lshan/p/12035285.html