Master and slave configuration
MongoDB's Master-slave mode configuration is as follows
1.keyFile
Generate Key_file
OpenSSL rand-base64 741 > Mongo_key
Place Mongo_key in the places where master and slave MongoDB user can access.
Set permissions
Set up
Onwer chown Mongodb:nogroup Mongo_key
2.master Configuration
edit/etc/mongodb.conf, set as follows
Master = True
keyfile =/path/to/mongo_key
3.slave Configuration
Slave = true
Source = <ip of master> only
= Bookstore
keyfile =/usr/local/bookstore/mongo_key
If you are synchronizing only one db, configure only items, and if multiple db needs to sync, comment out only.
The above settings can start Master and slave, if the configuration has misunderstood startup failure, such as key_file permissions are incorrect, specific errors can be viewed/var/log/mongodb/mongodb.log. If correct, you can modify some records on Master to verify.
4. Configuration changes
Master-slave configuration is simpler, but it is not as easy to modify a configuration that is already running correctly. For example, to change the master address, or to remove only configuration. Because MongoDB is started, the configuration is written to DB, Master is configured in the slaves table of local db, and slave is configured in the sources table of local db. If you change the conf file individually, the direct reboot fails, and the log shows the configuration conflict in db. If you modify local DB directly, the modifications will be overwritten quickly.
5. Methods of operation
Cancel slave configuration reboot. That is, comment out the slave = True line.
Manually modify the sources table, such as changing master's IP or removing only items.
Modify the Conf file, open slave = true, and modify other items, such as changing master's IP or removing only items.
Restart MongoDB again.
6. Other issues
Manually force synchronization:
If slave is out of sync for special reasons and master, you need to manually force the synchronization by running the following command on slave:
Use admin
db.runcommand ({resync:1})
If the data falls behind more, it will take a long time.
Key point analysis of master-slave synchronization
The primary server and the server must be enabled for security authentication:--auth, the master server and the admin database from the server must have global users, and then the master server's local database and the local data from the server are named Repl with the same password as the username.
Note: Local database This database is not synchronized, and mainly holds the synchronized information. In the MongoDB2.0.2 version test, replication is possible from the server's admin database when there is no global user (Deven: That's the way we do it,
No user is established from the Server Admin database, although there is no user in admin, the client connects to this server without authentication (that is, the--auth parameter is invalid), but the--auth from the server must be specified. Now that--auth is enabled, you should add a user to the admin from the server.
First, create the account named Repl in Master's local database and set the password;
Create an account named Repl from the same server as master, and set the same password as master;
Then create a global account (Admin account, open Sync)
1. Create a global account
> Use admin
switched to DB admin
> Db.adduser ("Root", "Zhuima")
warning:the ' addUser ' shell Helper is DE Precated. Please use ' CreateUser ' instead
successfully added User: {"user": "Root", "roles": ["Root"]}
2. Create an account named Repl under the 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 DE Precated. 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# Open authentication Mode
master = True # Specifies that the MongoDB is master mode
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 # Open authentication Mode
slave = True # Specifies that MongoDB is slave mode
Source = 192.168.58.30 : 27017 # Specify master server
5. Check Sync situation
> Db.printreplicationinfo ()
is a slave, printing slave replication info.
Source: 192.168.58.30:27017
Syncedto:fri 2014 10:22:31 gmt+0200 (CEST) = secs
ago (0.01hrs)
# this refers to the length of the delay.