MongoDB's Master Slave master slave mode configuration and master slave replication key resolution

  • 2020-05-30 21:15:00
  • OfStack

A master-slave configuration
The master-slave mode for mongodb is configured as follows
1.keyFile
Generate key_file


 openssl rand -base64 741 > mongo_key

Place mongo_key where master and slave mongodb user can access, respectively.
Set the permissions


chmod 700 mongo_key

Set up the


onwer chown mongodb:nogroup mongo_key

2. master configuration
Edit /etc/ mongodb.conf with the following Settings


master = true
keyFile = /path/to/mongo_key

3. slave configuration


slave = true
source = <ip of master>
only = bookstore
keyFile = /usr/local/bookstore/mongo_key

Configure the only item if only one db is synchronized, and comment out the only item if more than one DB needs to be synchronized.
Activate master and slave set after the completion of the above, if there is a misunderstanding boot failure configuration, such as key_file permission is not correct, the error can view/var/log/mongodb/mongodb log. If correct, you can modify some records on master for verification.
4. Configuration changes
The master-slave configuration is relatively simple, but it is not as simple as you might think to modify the configuration that is already running correctly. For example, to change the master address, or to remove the only configuration. Because when mongodb is started, the configuration is written to DB, master is in local DB's slaves table, slave is in local DB's sources table. If you change the conf file alone, a direct restart will fail, and the configuration conflicts shown in log and DB. If you modify local DB directly, the changes will be overwritten quickly.
5. Operation method
Cancel the slave configuration restart. That is, comment out the line slave = true.
Manually modify the sources table, such as changing the ip of master or removing the only entries.
Modify the conf file, open slave = true, and modify other items, such as changing ip for master or removing the only item.
Restart mongodb again.
6. Other questions
Manually forced synchronization:
If slave and master are out of sync for special reasons, you need to manually force synchronization by running the following command on slave:


use admin
db.runCommand( { resync: 1 } )

If the data lag a lot, it will take a long time.

Master and slave synchronization point resolution
Master server and slave server must have security authentication enabled :--auth, master server and slave server must have global users in admin database, and then master server's local database and slave server's local data are both named repl with the same password.
Note: local: local database this database will not be synchronized and will mainly store synchronized information. During MongoDB2.0.2 testing, replication was also possible when there were no global users in the admin database from the server (Deven: we used this approach,
The slave server admin database does not have a user), although there is no user in admin, the client connection to this server is not validated (that is, the auth parameter is invalid), but the slave server's --auth must be specified. Now that you have enabled --auth, you should add one user to admin from the server.

First, create an account named repl in master's local database and set the password.
Create an account named repl from the server and the master1 sample, and set the password as the master sample;
Then create a global account (manage account, turn on synchronization)
1. Create a global account


> use admin
switched to db admin
> db.addUser("root","zhuima")
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "root", "roles" : [ "root" ] }

2. Create an account named repl in master/slave local database and set the same password


> use local
switched to db local
> db.addUser("repl","zhuima")
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "repl", "roles" : [ "dbOwner" ] }

3.master configuration file


[root@redis ~]# sed -e '/^$/d;/^#/d' /etc/mongodb.conf 
bind_ip = 192.168.58.30
port = 27017
fork = true
pidfilepath = /var/run/mongodb/mongodb.pid
logpath = /var/log/mongodb/mongodb.log
dbpath =/mydata/data
journal = true
auth = true#  Enable authentication mode 
master = true          #  Specify the mongodb for master model 

4.slave configuration file


[root@localhost ~]$ sed -e '/^$/d;/^#/d' /etc/mongodb.conf 
bind_ip = 192.168.58.10
port = 27017
fork = true
pidfilepath = /var/run/mongodb/mongodb.pid
logpath = /var/log/mongodb/mongodb.log
dbpath = /mydata/data
journal = true
auth = true  #  Enable authentication mode 
slave = true  #  Specify the mongodb for slave model 
source = 192.168.58.30:27017  #  The specified master The server 

5. Check the synchronization


chmod 700 mongo_key
0


Related articles: