溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MongoDB主從的實(shí)現(xiàn)方式

發(fā)布時(shí)間:2021-08-18 10:14:22 來源:億速云 閱讀:159 作者:chen 欄目:MongoDB數(shù)據(jù)庫(kù)

這篇文章主要介紹“MongoDB主從的實(shí)現(xiàn)方式”,在日常操作中,相信很多人在MongoDB主從的實(shí)現(xiàn)方式問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”MongoDB主從的實(shí)現(xiàn)方式”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

簡(jiǎn)單的介紹一下MongoDB主從實(shí)現(xiàn)的方式

環(huán)境:

操作系統(tǒng):CentOS 6.7

MongoDB:  3.4.5

主數(shù)據(jù)目錄:/data/mongomaster 

從數(shù)據(jù)目錄: /data/mongoslave

MongoDB 管理命令環(huán)境變量:

export PATH="$PATH:/home/mongodb/mongodb/mongodb-3.4.5/bin"

啟動(dòng)主

mongod   --dbpath=/data/mongomaster/  --port 10000  --master  --logpath=/data/mongomaster/masterlog   --logappend     --fork

bout to fork child process, waiting until server is ready for connections.
forked process: 3114
child process started successfully, parent exiting
[root@bogon data]#

啟動(dòng)從

mongod   --dbpath=/data/mongoslave/   --logpath=/data/mongoslave/slave.log  --logappend    --port  10002 --slave --source 192.168.1.130:10000   --fork
about to fork child process, waiting until server is ready for connections.
forked process: 3144
child process started successfully, parent exiting

測(cè)試 在主庫(kù)上面創(chuàng)建mongo庫(kù) winne集合插入文檔數(shù)據(jù)

> use  mongo
switched to db mongo
> show  dbs
admin   0.000GB
local   0.003GB
master  0.002GB
> db
mongo
> for(i=0;i<10000;i++)db.mongo.insert({"name" : "linux" }, {"object" : "docker"}, {"age" :i})
WriteResult({ "nInserted" : 1 })
>

從庫(kù)測(cè)試

> show  dbs
2017-05-20T14:14:42.526+0800 E QUERY    [thread1] Error: listDatabases failed:{
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:769:19
shellHelper@src/mongo/shell/utils.js:659:15
@(shellhelp2):1:1,

報(bào)錯(cuò)了什么情況呢?找找資料什么的,關(guān)鍵信息not master and slaveOk=false 和NotMasterNoSlaveOk 隨便一搜,結(jié)果好多這個(gè)報(bào)錯(cuò)呢,都是說主從這種架構(gòu)默認(rèn)從庫(kù)不支持讀寫,像Redis等是不支持寫,MongoDB是更神奇,直接不能讀,從從庫(kù)上簡(jiǎn)單的配置一下,再看看。

2017-05-20T14:17:40.067+0800 E QUERY    [thread1] SyntaxError: illegal character @(shell):1:1
> rs.slaveOk()
> show  dbs
admin   0.000GB
local   0.000GB
master  0.002GB
mongo   0.000GB

2017-05-20T14:17:40.067+0800 E QUERY    [thread1] SyntaxError: illegal character @(shell):1:1
> rs.slaveOk()
> show  dbs
admin   0.000GB
local   0.000GB
master  0.002GB
mongo   0.000GB

經(jīng)過驗(yàn)證數(shù)據(jù)同步

到此,關(guān)于“MongoDB主從的實(shí)現(xiàn)方式”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI