First knowledge of MongoDB 123 (ii)

Source: Internet
Author: User

two mongodb master-slave configuration

in the past weUseMySQLDatabaseorRediswhen everyone is widely used, the use of dual-machine backup of the main node hung off after the node can take over the host to continue service. So this mode is much better than the high availability of a single node.

1. Environment Preparation

In practical applications, it takes two machines a master one from. I use a machine to solve this problem.

192.168.221.161:27021 as master

192.168.221.161:27022 as slave

2. Create two folder /data/db_master, respectively, /data/db_slave

3. Configure two configuration file mongodb_master.conf, respectively, mongodb_salve.conf

[email protected] mongodb]# cat mongodb_master.conf dbpath=/data/db_masterlogpath=/usr/local/mongodb/logs/mongodb_ Master.loglogappend=trueport=27021fork=trueauth=falsenohttpinterface=falsebind_ip=192.168.221.161journal= Truequiet=truemaster=true


[email protected] mongodb]# cat mongodb_salve.conf dbpath=/data/db_slavelogpath=/usr/local/mongodb/logs/mongodb_ Slave.loglogappend=trueport=27022fork=trueauth=falsenohttpinterface=falsebind_ip=192.168.221.161journal= truequiet=trueslave=truesource=192.168.221.161:27021


4. start the master node

[[email protected] mongodb]# mongod-f mongodb_master.conf about to fork child process, waiting until server was ready for Connections.forked Process:55489child Process started successfully, parent exiting


the output log content on Master is as follows:

2017-11-14t09:02:39.999-0800 i control  [initandlisten] options: { config:   "mongodb_master.conf", master: true, net: { bindip:  "192.168.221.161",  http: { enabled: true }, port: 27021 }, processManagement:  { fork: true }, security: { authorization:  "Disabled"  }, storage:  { dbPath:  "/data/db_master", journal: { enabled: true } },  systemlog: { destination:  "File", logappend: true, path:  "/usr/local/ Mongodb/logs/mongodb_master.log ", quiet: true } }2017-11-14t09:02:40.014-0800 i  index    [initandlisten] allocating new ns file /data/db_master/ Local.ns, filling with zeroes ... 2017-11-14t09:02:40.018-0800 i network  [websvr] admin web console waiting for connections on port 280212017-11-14t09:0 2:40.254-0800 i storage  [fileallocator] allocating new datafile /data /db_master/local.0, filling with zeroes ... 2017-11-14t09:02:40.254-0800 i storage  [fileallocator] creating directory  /data/db_master/_tmp2017-11-14t09:02:40.263-0800 i storage  [fileallocator] done  allocating datafile /data/db_master/local.0, size: 64mb,  took 0.001  secs2017-11-14t09:02:40.318-0800 i repl     [initandlisten] **** **2017-11-14t09:02:40.318-0800 i repl     [initandlisten] creating  REPLICATION OPLOG OF SIZE: 990MB ... 2017-11-14t09:02:40.319-0800 i storage  [fileallocator] allocating new  Datafile /data/db_mastEr/local.1, filling with zeroes ... 2017-11-14t09:02:40.349-0800 i storage  [fileallocator] done allocating  datafile /data/db_master/local.1, size: 1024mb,  took 0.029  secs2017-11-14t09:02:40.354-0800 i repl     [initandlisten] ****** 2017-11-14t09:02:40.358-0800 i network  [initandlisten] waiting for  connections on port 27021


5. start the slave node

[[email protected] mongodb]# mongod-f mongodb_salve.conf about to fork child process, waiting until server was ready for C Onnections.forked Process:55577child Process started successfully, parent exiting


the output log on the Slave is as follows:

2017-11-14t09:05:10.757-0800 i control  [initandlisten] allocator:  tcmalloc2017-11-14t09:05:10.757-0800 i control  [initandlisten] options: {  config:  "mongodb_salve.conf", net: { bindip:  "192.168.221.161",  http: {  enabled: true }, port: 27022 }, processManagement: { fork:  true }, security: { authorization:  "Disabled"  }, slave: true,  source:  "192.168.221.161:27021", storage: { dbpath:  "/data/db_slave",  journal:  { enabled: true } }, systemLog: { destination:  "File",  logappend: true, path:  "/usr/local/mongodb/logs/mongodb_slave.log", quiet: true  } }2017-11-14t09:05:10.759-0800 i index    [initandlisten] allocating  new ns file /datA/db_slave/local.ns, filling with zeroes ... 2017-11-14t09:05:10.763-0800 i network  [websvr] admin web console  Waiting for connections on port 280222017-11-14t09:05:11.039-0800 i storage   [fileallocator] allocating new datafile /data/db_slave/local.0, filling  with zeroes ... 2017-11-14t09:05:11.039-0800 i storage  [fileallocator] creating directory  /data/db_slave/_tmp2017-11-14t09:05:11.045-0800 i storage  [fileallocator] done  allocating datafile /data/db_slave/local.0, size: 64mb,  took 0.001  secs2017-11-14t09:05:11.066-0800 i network  [initandlisten] waiting for  connections on port 27022


6. Verify the Master-slave replication

login Master to Insert data:

[Email protected] mongodb]# MONGO 192.168.221.161:27021 MongoDB Shell version:3.0.6connecting to: 192.168.221.161:27021/test> show dbslocal 1.078gb> use testswitched to db test> Db.testdb.insert ({"Test1": " Item1 "}) Writeresult ({" ninserted ": 1}) > Db.testdb.find () {" _id ": ObjectId (" 5a0b23702c969986fca777ed ")," test1 ":" I Tem1 "}


Log in to the slave node and verify the data:

[[Email protected] mongodb]# mongo 192.168.221.161:27022mongodb shell version:  3.0.6connecting to: 192.168.221.161:27022/test> show dbs2017-11-14t09:0 7:45.745-0800 e query    error: listdatabases failed:{  "Note"  :  "From execcommand",  "OK"  : 0,  "errmsg"  :  "Not master"  }    at Error  (<anonymous>)     at  mongo.getdbs  (src/mongo/shell/mongo.js:47:15)     at shellHelper.show  (src/ mongo/shell/utils.js:630:33)     at shellHelper  (src/mongo/shell/utils.js:524:36)     at  (SHELLHELP2): 1:1 at src/mongo/shell/mongo.js:47> db.getmongo (). Setslaveok () #由于slave节点默认是不可读的, you need to execute this command before you can query the data > show dbslocal  0.078gb> show  dbslocal  0.078gbtest&nbsP;  0.078gb> db.testdb.find () {  "_id"  : objectid ("5a0b23702c969986fca777ed"),   "Test1"  :  "Item1" &NBSP;}


from the node, you can view the service status by Command Db.printreplicationinfo ():

> Db.printreplicationinfo () This is a slave, printing slave replication info.source:192.168.221.161:27021syncedto: Tue Nov 09:12:04 GMT-0800 (PST) 7 secs (0 hrs) behind the freshest member (no primary available at the moment)


atthis point, MongoDB 's master-slave architecture has been completed. To prevent excessive machine load, you can also configure a master multi-slave service, theMaster node is only responsible for write operations, theslave node only provides read operations.

However, there are several questions:

A. can the server be the master server, i.e. can I write from the server?

Try it:

> Db.testdb.insert ({"Test3": "Item3"}) Writeresult ({"Writeerror": {"code": Undefined, "errmsg": "Not Master"}})


As you can see,the slave node is not writable

B. if master hangs up, does the slave server automatically take over the master service and becomes writable?

to kill the master process:

Kill-9 ' Ps-ef|grep mongod|grep-v Grep|awk ' {print $} '


To test the slave node:

> Db.testdb.insert ({"Test3": "Item3"}) Writeresult ({"Writeerror": {"code": Undefined, "errmsg": "Not Master"}})


Find, or not write!

This situation can only be manually intervened ...

Keep thinking about a problem:

How can we automatically switch after the primary node is hung? Next Learn about MongoDB cluster building


This article is from the "Qing Miao write" blog, please be sure to keep this source http://qingmiao.blog.51cto.com/7286083/1981606

First knowledge of MongoDB 123 (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.