MongoDB master-slave configuration

Netkiller NoSQL Notebook

MongoDB, Cassandra, Redis, CouchBase, Hypertable...

Mr. Neo Chan, Chen Jingfeng (BG7NYT)


Xishan Meidi, Minzhi Street, Longhua New District, Shenzhen, Guangdong, China
518131
+86 13113668890

The document was created on 2012-11-16

Copyright Notice

Please contact the author for reprinting. When reprinting, please be sure to indicate the original source of the article, author information and this statement.

Documentation source:
http://netkiller.github.io
http://netkiller.sourceforge.net

Scan the QR code on WeChat to enter the Netkiller WeChat subscription account

Group: 128659835 Please indicate "reader"

 

 

2.3. Replication

Many tutorials use manual configuration of master-slave replication. I do not recommend you to start this way. Please use the scheme of modifying the /etc/mongod.conf configuration file.

Create master:

mongod –port 27017 –dbpath /var/lib/mongodb –master

 Created from:

mongod –port 27017 –dbpath /var/lib/mongodb –slave –source master_ip_address:27017
		

2.3.1. Master

sed -i "s/#master = true/master = true/" /etc/mongod.conf

systemctl restart  mongod
			

2.3.2. Slave

sed -i "s/#slave = true/slave = true/" /etc/mongod.conf
sed -i "s/#source = arg/source = mongodb.master.example.com/" /etc/mongod.conf

systemctl restart  mongod
			

2.3.3. 测试

进入 Master

			
[root@localhost ~]# mongo
MongoDB shell version: 2.6.11
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings:
2015-11-14T15:51:21.215+0800 [initandlisten]
2015-11-14T15:51:21.215+0800 [initandlisten] ** WARNING: Readahead for /var/lib/mongodb is set to 4096KB
2015-11-14T15:51:21.215+0800 [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
2015-11-14T15:51:21.215+0800 [initandlisten] **          http://dochub.mongodb.org/core/readahead
>
>
> db.foo.save({'name':'neo','address':{'city':'shenzhen','post':518000},'phone':[13113668890,13322993040]})
WriteResult({ "nInserted" : 1 })
> db.foo.find();
{ "_id" : ObjectId("5646e881a11081d5998bf70c"), "name" : "neo", "address" : { "city" : "shenzhen", "post" : 518000 }, "phone" : [ 13113668890, 13322993040 ] }
>
			
			

进入 Slave

			
[root@localhost ~]# mongo
MongoDB shell version: 2.6.11
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings:
2015-11-14T15:51:23.668+0800 [initandlisten]
2015-11-14T15:51:23.668+0800 [initandlisten] ** WARNING: Readahead for /var/lib/mongodb is set to 4096KB
2015-11-14T15:51:23.668+0800 [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
2015-11-14T15:51:23.668+0800 [initandlisten] **          http://dochub.mongodb.org/core/readahead
> db.foo.find();
{ "_id" : ObjectId("5646e881a11081d5998bf70c"), "name" : "neo", "address" : { "city" : "shenzhen", "post" : 518000 }, "phone" : [ 13113668890, 13322993040 ] }
>
			
			

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944112&siteId=291194637