初識mongodb一二三(二)
阿新 • • 發佈:2017-11-14
mongodb;mongodb主從
二mongodb主從配置
在之前我們使用mysql數據庫或者redis時大家廣泛用到,采用雙機備份後主節點掛掉了後從節點可以接替主機繼續服務。所以這種模式比單節點的高可用性要好很多。
1、環境準備
實際應用中,需要兩臺機器一主一從。我這裏因資源問題,使用一臺機器解決。
192.168.221.161:27021當做master
192.168.221.161:27022當做slave
2、分別建立兩個文件夾/data/db_master,/data/db_slave
3、分別配置兩個配置文件mongodb_master.conf,mongodb_salve.conf
[root@MidApp mongodb]# cat mongodb_master.conf dbpath=/data/db_master logpath=/usr/local/mongodb/logs/mongodb_master.log logappend=true port=27021 fork=true auth=false nohttpinterface=false bind_ip=192.168.221.161 journal=true quiet=true master=true
[root@MidApp mongodb]# cat mongodb_salve.conf dbpath=/data/db_slave logpath=/usr/local/mongodb/logs/mongodb_slave.log logappend=true port=27022 fork=true auth=false nohttpinterface=false bind_ip=192.168.221.161 journal=true quiet=true slave=true source=192.168.221.161:27021
4. 啟動master節點
[root@MidApp mongodb]# mongod -f mongodb_master.conf about to fork child process, waiting until server is ready for connections. forked process: 55489 child process started successfully, parent exiting
Master上輸出日誌內容如下:
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 28021 2017-11-14T09:02: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/_tmp 2017-11-14T09:02:40.263-0800 I STORAGE [FileAllocator] done allocating datafile /data/db_master/local.0, size: 64MB, took 0.001 secs 2017-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 secs 2017-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. 啟動從節點
[root@MidApp mongodb]# mongod -f mongodb_salve.conf about to fork child process, waiting until server is ready for connections. forked process: 55577 child process started successfully, parent exiting
Slave上輸出日誌如下:
2017-11-14T09:05:10.757-0800 I CONTROL [initandlisten] allocator: tcmalloc 2017-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 28022 2017-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/_tmp 2017-11-14T09:05:11.045-0800 I STORAGE [FileAllocator] done allocating datafile /data/db_slave/local.0, size: 64MB, took 0.001 secs 2017-11-14T09:05:11.066-0800 I NETWORK [initandlisten] waiting for connections on port 27022
6. 驗證一下主從復制
登錄master插入數據:
[root@MidApp mongodb]# mongo 192.168.221.161:27021 MongoDB shell version: 3.0.6 connecting to: 192.168.221.161:27021/test > show dbs local 1.078GB > use test switched to db test > db.testdb.insert({"test1":"item1"}) WriteResult({ "nInserted" : 1 }) > db.testdb.find() { "_id" : ObjectId("5a0b23702c969986fca777ed"), "test1" : "item1" }
登錄slave節點,驗證數據:
[root@MidApp mongodb]# mongo 192.168.221.161:27022 MongoDB shell version: 3.0.6 connecting to: 192.168.221.161:27022/test > show dbs 2017-11-14T09:07: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節點默認是不可讀的,需要先執行這句命令才能查詢數據 > show dbs local 0.078GB > show dbs local 0.078GB test 0.078GB > db.testdb.find() { "_id" : ObjectId("5a0b23702c969986fca777ed"), "test1" : "item1" }
從節點上可以通過命令db.printReplicationInfo()查看服務狀態:
> db.printReplicationInfo() this is a slave, printing slave replication info. source: 192.168.221.161:27021 syncedTo: Tue Nov 14 2017 09:12:04 GMT-0800 (PST) 7 secs (0 hrs) behind the freshest member (no primary available at the moment)
至此,mongodb的主從架構已經完成了。為防止機器負載過大,也可以配置一主多從服務,master節點只負責寫操作,slave節點只提供讀操作。
不過,存在幾個疑問:
a. 從服務器可以當做主服務器嗎,也就是從服務器可寫嗎?
試一下:
> db.testdb.insert({"test3":"item3"}) WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
可以看到,slave節點是不可寫的
b.如果master掛掉,從服務器會自動接管主服務,變為可寫嗎?
把master進程殺掉:
kill -9 `ps -ef|grep mongod|grep -v grep|awk ‘{print $2}‘`
測試slave節點:
> db.testdb.insert({"test3":"item3"}) WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
發現,還是不可寫!
這種情況只能手動幹預了。。。
繼續思考一個問題:
我們怎麽實現主節點掛了之後能夠自動切換?下一篇接著學習mongodb的集群搭建
本文出自 “清渺淡寫” 博客,請務必保留此出處http://qingmiao.blog.51cto.com/7286083/1981606
初識mongodb一二三(二)