麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 數(shù)據(jù)庫(kù) > MongoDB > 正文

MongoDB 主從復(fù)制實(shí)例講解

2020-03-14 13:04:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

主從復(fù)制可以用來(lái)做數(shù)據(jù)庫(kù)的備份,故障恢復(fù),讀寫分離。

本實(shí)驗(yàn)使用Mongodb 3.2版本,我們先查看一下mongod的幫助

[root@localhost mongodb]# mongod --help.....省略Master/slave options (old; use replica sets instead): --master               master mode --slave                slave mode --source arg             when slave: specify master as                     <server:port> --only arg              when slave: specify a single database                     to replicate --slavedelay arg           specify delay (in seconds) to be used                     when applying master ops to slave --autoresync             automatically resync if slave data is                     stale.....省略

主從復(fù)制已經(jīng)是過(guò)期的功能,目前使用副本集代替。主從復(fù)制和副本集的區(qū)別,可以簡(jiǎn)單的理解成主從復(fù)制不能自動(dòng)故障轉(zhuǎn)移,副本集中的集群在主節(jié)點(diǎn)宕機(jī)后,可以使用選舉的策略選擇一個(gè)新的主節(jié)點(diǎn)出來(lái)。實(shí)現(xiàn)自動(dòng)的故障轉(zhuǎn)移。

從節(jié)點(diǎn)可以是一個(gè),也可以是多個(gè)。

下面我們?cè)谝慌_(tái)機(jī)器上,使用兩個(gè)實(shí)例的方式實(shí)現(xiàn)主從復(fù)制。

建立數(shù)據(jù)庫(kù)目錄

[root@localhost data]# mkdir -p /application/mongodb/data/{master,slave}

2. 啟動(dòng)master實(shí)例

[root@localhost data]# mongod --dbpath=/application/mongodb/data/master/ --port 27017 --master

--master 指定該實(shí)例是主服務(wù)器 。

3. 啟動(dòng)從實(shí)例

[root@localhost ~]# mongod --dbpath=/application/mongodb/data/slave/ --port 27018 --slave --source 127.0.0.1:27017

--slave 指定該實(shí)例為從服務(wù)器
--source 指定主服務(wù)器是誰(shuí)?

從服務(wù)器啟動(dòng)后,即不斷的想主服務(wù)器請(qǐng)求同步數(shù)據(jù)

2016-01-16T10:30:10.208+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:11.210+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:12.211+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:14.196+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:15.197+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:16.199+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:17.202+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:18.204+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:19.207+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:30:20.209+0800 I REPL   [replslave] syncing from host:127.0.0.1:27017

至此,主從復(fù)制已經(jīng)配置完成,就是這么的簡(jiǎn)單。

對(duì)于從服務(wù)器,還有三個(gè)參數(shù)需要解釋一下。

  --only arg

 從節(jié)點(diǎn)指定只復(fù)制某個(gè)特定的數(shù)據(jù)庫(kù)(默認(rèn)復(fù)制所有數(shù)據(jù)庫(kù))

  --slavedelay arg

指定從服務(wù)器延遲多久時(shí)間再同步,此選項(xiàng)在主服務(wù)器發(fā)生人為操作失誤時(shí),比較有用。發(fā)現(xiàn)錯(cuò)誤時(shí),從服務(wù)器還沒(méi)有同步錯(cuò)誤。這樣可以避免錯(cuò)誤的發(fā)生。

  --autoresync

如果從節(jié)點(diǎn)的數(shù)據(jù)與主節(jié)點(diǎn)發(fā)生斷裂(某些oplog中的數(shù)據(jù)還未被同步,即被覆蓋了),那么該選項(xiàng)將是從節(jié)點(diǎn)自動(dòng)的從新從頭開(kāi)始同步數(shù)據(jù)庫(kù)。

下面我們驗(yàn)證一下,數(shù)據(jù)的同步是否有效。
在主庫(kù)中插入數(shù)據(jù)。

[root@localhost ~]# mongo 127.0.0.1:27017MongoDB shell version: 3.2.1connecting to: 127.0.0.1:27017/test> db.user.insert({"name":"jack","age":40,"job":"moive star"})WriteResult({ "nInserted" : 1 })> db.user.insert({"name":"vicent","age":25,"job":"teacher"})WriteResult({ "nInserted" : 1 })

登錄從數(shù)據(jù)庫(kù),檢查數(shù)據(jù)是否同步

[root@localhost ~]# mongo 127.0.0.1:27018MongoDB shell version: 3.2.1connecting to: 127.0.0.1:27018/test> > db.user.find(){ "_id" : ObjectId("5699af720102a61caffb76e8"), "name" : "jack", "age" : 40, "job" : "moive star" }{ "_id" : ObjectId("5699af920102a61caffb76e9"), "name" : "vicent", "age" : 25, "job" : "teacher" }

可以看到數(shù)據(jù)已經(jīng)同步啦~

默認(rèn)情況下,要想在從庫(kù)開(kāi)啟查詢功能,必須告知服務(wù)器,你接受從服務(wù)器的數(shù)據(jù)(有可能同步有延遲,數(shù)據(jù)不一致,你能夠接受這種不一致)

> show collections2016-01-16T10:52:04.363+0800 E QUERY  [thread1] Error: listCollections failed: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :_getErrorWithCode@src/mongo/shell/utils.js:23:13DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:746:1DB.prototype.getCollectionInfos@src/mongo/shell/db.js:758:15DB.prototype.getCollectionNames@src/mongo/shell/db.js:769:12shellHelper.show@src/mongo/shell/utils.js:695:9shellHelper@src/mongo/shell/utils.js:594:15@(shellhelp2):1:1

執(zhí)行rs.slaveOK

> rs.slaveOk()> show collectionsuser>

在從服務(wù)的local數(shù)據(jù)庫(kù)中有個(gè)sources集合,記錄了主服務(wù)的信息

> use localswitched to db local> show collectionsmesourcesstartup_log> db.sources.find().pretty(){  "_id" : ObjectId("5699aaafa33311c25ab793df"),  "host" : "127.0.0.1:27017",  "source" : "main",  "syncedTo" : Timestamp(1452913003, 1)}

我們?cè)俅螁?dòng)從庫(kù)時(shí),就無(wú)需指定source參數(shù)啦。

[root@localhost ~]# mongod --dbpath=/application/mongodb/data/slave/ --port 27018 --slave2016-01-16T10:57:45.965+0800 I CONTROL [initandlisten] MongoDB starting : pid=21820 port=27018 dbpath=/application/mongodb/data/slave/ slave=1 64-bit host=localhost.localdomain2016-01-16T10:57:45.967+0800 I CONTROL [initandlisten] db version v3.2.12016-01-16T10:57:45.968+0800 I CONTROL [initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b22016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 20132016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] allocator: tcmalloc2016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] modules: none2016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] build environment:2016-01-16T10:57:45.969+0800 I CONTROL [initandlisten]   distmod: rhel622016-01-16T10:57:45.969+0800 I CONTROL [initandlisten]   distarch: x86_642016-01-16T10:57:45.969+0800 I CONTROL [initandlisten]   target_arch: x86_642016-01-16T10:57:45.969+0800 I CONTROL [initandlisten] options: { net: { port: 27018 }, slave: true, storage: { dbPath: "/application/mongodb/data/slave/" } }2016-01-16T10:57:46.010+0800 I -    [initandlisten] Detected data files in /application/mongodb/data/slave/ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.2016-01-16T10:57:46.011+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),2016-01-16T10:57:48.485+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2016-01-16T10:57:48.486+0800 I CONTROL [initandlisten] 2016-01-16T10:57:48.488+0800 I CONTROL [initandlisten] 2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] **    We suggest setting it to 'never'2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] 2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] **    We suggest setting it to 'never'2016-01-16T10:57:48.490+0800 I CONTROL [initandlisten] 2016-01-16T10:57:48.493+0800 I FTDC   [initandlisten] Initializing full-time diagnostic data capture with directory '/application/mongodb/data/slave/diagnostic.data'2016-01-16T10:57:48.494+0800 I NETWORK [initandlisten] waiting for connections on port 270182016-01-16T10:57:48.495+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker2016-01-16T10:57:49.497+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:57:50.503+0800 I REPL   [replslave] sleep 1 sec before next pass2016-01-16T10:57:51.504+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:57:52.505+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:57:54.295+0800 I REPL   [replslave] syncing from host:127.0.0.1:270172016-01-16T10:57:55.296+0800 I REPL   [replslave] syncing from host:127.0.0.1:27017

主從庫(kù)之間利用oplog日志進(jìn)行同步。oplog存在于主庫(kù)的local數(shù)據(jù)庫(kù),oplog.$main集合。

> use localswitched to db local> db.oplog.$main.find({"op":"i"}).sort({"ts":-1}).pretty(){  "ts" : Timestamp(1452916694, 1),  "h" : NumberLong(0),  "v" : 2,  "op" : "i",  "ns" : "test.user",  "o" : {    "_id" : ObjectId("5699bfd6647c735cb3a50e0c"),    "name" : "zhangcong"  }}{  "ts" : Timestamp(1452913156, 1),  "h" : NumberLong(0),  "v" : 2,  "op" : "i",  "ns" : "test.user",  "o" : {    "_id" : ObjectId("5699b204358c4672cad1cc6e"),    "name" : "zhangdd",    "age" : 30,    "job" : "teacher"  }}{  "ts" : Timestamp(1452912530, 1),  "h" : NumberLong(0),  "v" : 2,  "op" : "i",  "ns" : "test.user",  "o" : {    "_id" : ObjectId("5699af920102a61caffb76e9"),    "name" : "vicent",    "age" : 25,    "job" : "teacher"  }}{  "ts" : Timestamp(1452912498, 2),  "h" : NumberLong(0),  "v" : 2,  "op" : "i",  "ns" : "test.user",  "o" : {    "_id" : ObjectId("5699af720102a61caffb76e8"),    "name" : "jack",    "age" : 40,    "job" : "moive star"  }}

該集合屬于固定集合。在一定時(shí)間后,舊日志會(huì)被覆蓋。如果日志已經(jīng)被覆蓋,從庫(kù)還沒(méi)有來(lái)的及同步。那么從庫(kù)就無(wú)法再同步數(shù)據(jù)了。只有使用--autoresync讓其重新同步數(shù)據(jù)。

備注:命令行參數(shù)指定的參數(shù)值,可以寫到config文件中,啟動(dòng)時(shí)使用

mongod --config /path/to/file.conf

mongod 2.4以后的版本使用YAML的格式來(lái)編寫配置文件。關(guān)于主從復(fù)制的配置如何在配置文件中聲明,官方文件沒(méi)有給出方法。試了幾種寫法都不正確。 因?yàn)閙ongodb使用副本集代替了主從復(fù)制,從而可能配置文件不再支持主從復(fù)制。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到MongoDB頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 一级做a爱片性色毛片高清 国产精品色在线网站 | 天天操天天骑 | 91看片成人| 亚州综合网 | 免费观看一级黄色片 | 黄色av网站在线观看 | 国语自产免费精品视频在 | 久久手机在线视频 | 99精彩视频在线观看 | 国产精品久久久久久模特 | 日日草夜夜 | 亚洲视频综合网 | 羞羞答答tv | 黄色免费在线电影 | 一级一级一级毛片 | www噜噜偷拍在线视频 | 黄色免费高清网站 | 久久精品2019中文字幕 | 久久99精品久久久久久秒播放器 | 欧美日韩在线播放 | 久久国产精品区 | 久久精品国产99国产精品亚洲 | 久草视频在线看 | 欧美一区二区三区久久精品视 | 国产精品视频专区 | 欧美人xx | 国产在线地址 | 精品一区二区三区欧美 | 亚洲午夜影院在线观看 | 成人污在线| 久久久www成人免费毛片 | 欧美特黄一级高清免费的香蕉 | 久久久av亚洲男天堂 | 宅男噜噜噜66国产免费观看 | 久久超 | www.国产一区.com | 黄视频免费在线 | 自拍偷拍999 | 国产午夜电影 | 欧美一级毛片大片免费播放 | 色播视频在线播放 |