Master and slave synchronization configuration in MongoDB and launch commands related to mongod

  • 2020-06-01 11:14:37
  • OfStack

MongoDB master slave synchronization Settings
For the installation and startup parameters of MongoDB, please refer to the "Ubuntu installation MongoDB" and "Mongodb startup command mongod parameters" I reprinted before.

Master-slave setup

Master : Port : 8001
Slave : Port : 8001

Start the Master

mongod --dbpath /data/masterdb/ --master --oplogSize 64 --port 8001 --fork --logpath /var/logs/mongodb/mongod.log

Start the Slave
mongod --dbpath /data/slavedb/ --slave --source --oplogSize 64 --port 8001 --fork --logpath /var/logs/mongodb/mongod.log

Looking at Slave log, you can see that the data has been copied from Master

tail -f /var/logs/mongodb/mongod.log 
Thu Feb 27 22:18:20 repl: main@ 
Thu Feb 27 22:18:20 repl: sleep 2sec before next pass 
Thu Feb 27 22:18:22 repl: main@ 
Thu Feb 27 22:18:22 repl: sleep 2sec before next pass 
Thu Feb 27 22:18:24 repl: main@ 
Thu Feb 27 22:18:24 repl: sleep 2sec before next pass 
Thu Feb 27 22:18:26 repl: main@ 
Thu Feb 27 22:18:26 repl: sleep 2sec before next pass 
Thu Feb 27 22:18:28 repl: main@ 
Thu Feb 27 22:18:28 repl: sleep 2sec before next pass 
Thu Feb 27 22:18:30 repl: main@ 
Thu Feb 27 22:18:30 repl: sleep 2sec before next pass 
Thu Feb 27 22:18:32 repl: main@ 
Thu Feb 27 22:18:32 repl: sleep 2sec before next pass 

Stop using MongoDB

mongo -port 8001 
use admin 

Repair MongoDB

mongod --dbpath /data/masterdb/ repair 
mongod --dbpath /data/slavedb/ repair 

Mongodb launch command mongod parameter description

The main parameters of mongod are:

The basic configuration


--quiet # quiet output
--port arg # specifies the service port number, with the default port 27017
--bind_ip arg # binding service IP. If bound, it can only be accessed locally, and all IP are not specified by default
--logpath arg # specifies the MongoDB log file. Note that the specified file is not a directory
--logappend # logs with appends
-- the full path to pidfilepath arg # PID File, if not set, no PID file
-- the full path of the private key of the keyFile arg # cluster, only valid for the Replica Set architecture
--unixSocketPrefix arg # UNIX domain socket substitution directory,(default /tmp)
--fork # runs MongoDB as a daemon, creating a server process
--auth # enables validation
--cpu # shows CPU's CPU utilization and iowait periodically
--dbpath arg # specifies the database path
--diaglog arg # diaglog option 0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb # sets that each database will be saved in a separate directory
-- with the logging option enabled, the data operations of MongoDB will be written to files in the journal folder
--journalOptions arg # enables log diagnostics
--ipv6 # enables the IPv6 option
--jsonp # allows the JSONP form to be accessed via HTTP (with security implications)
-- the maximum number of simultaneous connections maxConns arg # defaults to 2000
--noauth # does not enable validation
--nohttpinterface # closes http interface, and port 27018 is closed by default
--noprealloc # disables preallocation of data files (often affecting performance)
--noscripting # disable the scripting engine
--notablescan # does not allow table scans
--nounixsocket # disables Unix socket listening
--nssize arg (=16) # set the letter database.ns file size (MB)
--objcheck # after receiving customer data, check the validity,
--profile arg # file parameter 0=off 1=slow, 2=all
--quota # limits the number of files per database and sets the default to 8
--quotaFiles arg # number of files allower per db, requires --quota
--rest # opens simple rest API
--repair # fixes all databases run repair on all dbs
--repairpath arg # fixes the directory of files generated by the library, with the default directory name dbpath
--slowms arg (=100) # value of slow for profile and console log
--smallfiles # USES a smaller default file
--syncdelay arg (=60) # number of seconds to write data to disk (0=never, not recommended)
--sysinfo # prints 1 diagnostic system information
--upgrade # if you need to upgrade the database * Replicaton parameter


--fastsync # enables the slave library replication service from an dbpath database that is a snapshot of the master library and can be used to quickly enable synchronization
--autoresync # if the slave library is much worse than the master, automatically resynchronize,
--oplogSize arg # sets the size of oplog (MB) * as the master/slave parameter


--master # main library mode
--slave # slave mode
--source arg # slave library port number
--only arg # specifies the database replication for single 1
--slavedelay arg # sets the delay time for synchronizing the master from the library * Replica set(replica set) option:


--replSet arg # sets the replica set name * Sharding(sharding) option

--configsvr # declares that this is the config service for 1 cluster, default port 27019, default directory /data/configdb
--shardsvr # declares that this is a sharding of 1 cluster, with the default port 27018
--noMoveParanoia # turns off bigotry for moveChunk data saving
The above parameters can be written into the mongod.conf configuration document, for example:

dbpath = /data/mongodb
logpath = /data/mongodb/mongodb.log
logappend = true
port = 27017
fork = true
auth = true

e. g:

./mongod -shardsvr -replSet shard1 -port 16161 -dbpath /data/mongodb/data/shard1a -oplogSize 100 -logpath /data/mongodb/logs/shard1a.log -logappend -fork -rest

Related articles: