redhat7.0下mongodb主从搭配

1,首先安装mongodb,见之前博文

2,在主节点执行下面的命令

mongod --dbpath=/var/lib/mongo --master --oplogSize 64 --port 27017

看到下面的信息即启动成功

2015-05-18T09:51:12.111+0800 I STORAGE  [initandlisten] ** WARNING: Readahead for /var/lib/mongo is set to 4096KB
2015-05-18T09:51:12.111+0800 I STORAGE  [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
2015-05-18T09:51:12.111+0800 I STORAGE  [initandlisten] **          http://dochub.mongodb.org/core/readahead
2015-05-18T09:51:12.112+0800 I JOURNAL  [initandlisten] journal dir=/var/lib/mongo/journal
2015-05-18T09:51:12.112+0800 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2015-05-18T09:51:12.162+0800 I JOURNAL  [durability] Durability thread started
2015-05-18T09:51:12.162+0800 I JOURNAL  [journal writer] Journal writer thread started
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] MongoDB starting : pid=29245 port=27017 dbpath=/var/lib/mongo master=1 64-bit host=localhost.localdomain
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] 
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] 
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] 
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] 
2015-05-18T09:51:12.162+0800 I CONTROL  [initandlisten] db version v3.0.2
2015-05-18T09:51:12.163+0800 I CONTROL  [initandlisten] git version: 6201872043ecbbc0a4cc169b5482dcf385fc464f
2015-05-18T09:51:12.163+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2015-05-18T09:51:12.163+0800 I CONTROL  [initandlisten] build info: Linux ip-10-169-109-10 3.10.0-121.el7.x86_64 #1 SMP Tue Apr 8 10:48:19 EDT 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-05-18T09:51:12.163+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2015-05-18T09:51:12.163+0800 I CONTROL  [initandlisten] options: { master: true, net: { port: 27017 }, replication: { oplogSizeMB: 64 }, storage: { dbPath: "/var/lib/mongo" } }

3,从节点执行下面的命令即可

mongod --dbpath=/var/lib/mongo --port=37017--slave


看到下面日志即启动成功

015-05-18T11:26:21.321+0800 I REPL     [replslave] repl:  end sync_pullOpLog syncedTo: May 18 11:26:16 55595bd8:1
2015-05-18T11:26:21.322+0800 I REPL     [replslave] repl: syncing from host:192.168.1.1:27017
2015-05-18T11:26:31.322+0800 I REPL     [replslave] repl:   applied 1 operations
2015-05-18T11:26:31.322+0800 I REPL     [replslave] repl:  end sync_pullOpLog syncedTo: May 18 11:26:26 55595be2:1
2015-05-18T11:26:31.322+0800 I REPL     [replslave] repl: syncing from host:192.168.1.1:27017
2015-05-18T11:26:41.322+0800 I REPL     [replslave] repl:   applied 1 operations
2015-05-18T11:26:41.322+0800 I REPL     [replslave] repl:  end sync_pullOpLog syncedTo: May 18 11:26:36 55595bec:1
2015-05-18T11:26:41.322+0800 I REPL     [replslave] repl: syncing from host:192.168.1.1:27017
2015-05-18T11:26:51.322+0800 I REPL     [replslave] repl:   applied 1 operations
2015-05-18T11:26:51.322+0800 I REPL     [replslave] repl:  end sync_pullOpLog syncedTo: May 18 11:26:46 55595bf6:1
2015-05-18T11:26:51.322+0800 I REPL     [replslave] repl: syncing from host:192.168.1.1:27017
2015-05-18T11:27:01.323+0800 I REPL     [replslave] repl:   applied 1 operations
2015-05-18T11:27:01.323+0800 I REPL     [replslave] repl:  end sync_pullOpLog syncedTo: May 18 11:26:56 55595c00:1
2015-05-18T11:27:01.323+0800 I REPL     [replslave] repl: syncing from host:192.168.1.1:27017
2015-05-18T11:27:11.323+0800 I REPL     [replslave] repl:   applied 1 operations
2015-05-18T11:27:11.323+0800 I REPL     [replslave] repl:  end sync_pullOpLog syncedTo: May 18 11:27:06 55595c0a:1
2015-05-18T11:27:11.323+0800 I REPL     [replslave] repl: syncing from host:192.168.1.1:27017
2015-05-18T11:27:21.323+0800 I REPL     [replslave] repl:   applied 1 operations
2015-05-18T11:27:21.323+0800 I REPL     [replslave] repl:  end sync_pullOpLog syncedTo: May 18 11:27:16 55595c14:1
2015-05-18T11:27:21.323+0800 I REPL     [replslave] repl: syncing from host:192.168.1.1:27017
2015-05-18T11:27:31.323+0800 I REPL     [replslave] repl:   applied 1 operations
2015-05-18T11:27:31.323+0800 I REPL     [replslave] repl:  end sync_pullOpLog syncedTo: May 18 11:27:26 55595c1e:1
2015-05-18T11:27:31.323+08


4,主库新增一条记录

> use test
switched to db test
> db.blog.save({title:"new article"}) 
WriteResult({ "nInserted" : 1 })

5,从库进行查询

>  db.blog.find()
{ "_id" : ObjectId("55595022e26fd5794a58bdd3"), "title" : "new article" }


主从同时都有了这条记录,即配置成功


注意事项:

1,报错如下:

repl: --source 192.168.1.1:27017 != 192.168.1.1:27017 from local.sources collection
原因:第一次执行从库命令的时候,错误的把source写成从库的ip,第二次修改正确的主库ip。由于从库已经在local表里存储了之前的source ip,以后在执行就会报错
解决办法:执行下面命令,删除错误的信息即可

 db.sources.remove({'host':'192.168.1.1:27017'})

2,从库命设置同步时机

mongod --dbpath=/var/lib/mongo --port=37017--slave --source=192.168.1.1:27017 autoresync=true 



add by joeyon1985


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。