官方网站:
http://www.mongodb.org/
关于mongodb的主从,官方已经不建议使用,目前建议使用副本集;
但由于公司现有环境的原因,如果改用副本集,则需要修改数据,受限于现实的种种原因,只能做主从同步配置,所以这里记录一下mongodb的主从;
mongodb的主从要注意的事项:
1.主服务器挂掉,从服务器不会自动取代主服务器。
2.主服务器挂掉后重启,从服务器则会重新同步数据,之前同步过来的数据被初始化。
3.综合以上两点,mongodb要随时做好数据备份,以免造成无法弥补的数据丢失!!!
MongoDB 安裝,主从配置
一 MongoDB 安装
[root@zabbix_server src]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.0.tgz
[root@zabbix_server src]# tar xzvf mongodb-linux-x86_64-2.0.0.tgz
[root@zabbix_server src]# mv mongodb-linux-x86_64-2.0.0 /usr/local/mongodb
[root@zabbix_server src]# mkdir /usr/local/mongodb/data
[root@zabbix_server src]# touch /usr/local/mongodb/logs
[root@zabbix_server bin]# /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork
[root@zabbix_server bin]#./mongo
MongoDB shell version: 2.0.0
connecting to:test
> use test;
switched to db test
> exit
bye
[root@zabbix_server bin]# netstat -anlpt | grep mongo
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 11504/mongod
tcp 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN 11504/mongod
注意:
如果报错
-bash: /usr/local/mongodb/bin/mongod: cannot execute binary file
说明你的服务器和mongodb 的版本不对应, 如果服务器是64位,下载x86_64的mongodb ,如果服务器是32位的, 下载i686的mongodb/
二 MongoDB 主从配置
1)实验环境
主:192.168.0.14
从:192.168.0.64
2)时间同步
两台机器做时间同步
[root@zabbix_server src]# ntpdate time.windows.com
3)启动服务
master:192.168.0.14
启动命令
/usr/local/mongodb/bin/mongod --master --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork
slave:192.168.0.64
启动命令
/usr/local/mongodb/bin/mongod -slave -source 192.168.0.14:27017 --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork
4)测试主从
在主上插入数据
测试1:
测试1:
1 在主库上插入数据
[root@cacti bin]#./mongo
MongoDB shell version: 2.0.0
connecting to:test
> db.foo.save({"id":123456,"name":'lizonggang'})
> db.foo.find({"id":123456})
{ "_id": ObjectId("51c3f4b21399022afd992f39"),"id": 123456,"name":"lizonggang" }
>
2 在从上查看数据(已有在主上插入的数据,说明主从成功)
[root@zabbix_server bin]#./mongo
MongoDB shell version: 2.0.0
connecting to:test
> db.foo.find({"id":123456})
{ "_id": ObjectId("51c3f4b21399022afd992f39"),"id": 123456,"name":"lizonggang" }
>
测试2:
1 在主库上插入数据
> use elaindb
switched to db elaindb
> db.test.save({title:"This is a test!"})
> db.test.find();
{ "_id": ObjectId("51c3f8f11a90c8da157c7f64"),"title":"This is a test!" }
>
2 在从库上验证数据
[root@zabbix_server bin]#./mongo
MongoDB shell version: 2.0.0
connecting to:test
> use elaindb
switched to db elaindb
> db.test.find();
{ "_id": ObjectId("51c3f8f11a90c8da157c7f64"),"title":"This is a test!" }
> db.printReplicationInfo();
this is a slave, printing slave replication info.
source: 192.168.0.14:27017
syncedTo: Fri Jun 21 2013 14:59:10 GMT+0800 (CST)
= 9 secs ago (0hrs)
>
> db.printSlaveReplicationInfo();
source: 192.168.0.14:27017
syncedTo: Fri Jun 21 2013 14:59:30 GMT+0800 (CST)
= 16 secs ago (0hrs)
>
测试3:
在这个状态下,slave 下是不可以插入数据的
> db.test.save({title:"This is slave test!"})
not master
5) 一些查询状态的语句
1)查询库
> show dbs;
elaindb 0.0625GB
local 0.125GB
test 0.0625GB
2)在从库上查询主库的地址
> use local;
switched to db local
> db.sources.find()
{ "_id": ObjectId("51c3f5dd568d10529950b10d"),"host":"192.168.0.14:27017","source":"main","syncedTo": { "t": 1371797300000,"i": 1 } }
>
3)查看主从复制状态
> db.printReplicationInfo();
configured oplog size: 47.6837158203125MB
log length start to end: 1375secs (0.38hrs)
oplog first event time: Fri Jun 21 2013 14:30:35 GMT+0800 (CST)
oplog last event time: Fri Jun 21 2013 14:53:30 GMT+0800 (CST)
now: Fri Jun 21 2013 14:53:32 GMT+0800 (CST)
>
4)查看Collection状态
> db.printCollectionStats();
foo
{
"ns":"test.foo",
"count": 1,
"size": 56,
"avgObjSize": 56,
"storageSize": 4096,
"numExtents": 1,
"nindexes": 1,
"lastExtentSize": 4096,
"paddingFactor": 1,
"flags": 1,
"totalIndexSize": 8176,
"indexSizes": {
"_id_": 8176
},
"ok": 1
}
---
system.indexes
{
"ns":"test.system.indexes",
"count": 1,
"size": 64,
"avgObjSize": 64,
"storageSize": 4096,
"numExtents": 1,
"nindexes": 0,
"lastExtentSize": 4096,
"paddingFactor": 1,
"flags": 0,
"totalIndexSize": 0,
"indexSizes": {
},
"ok": 1
}
---
>
6)应急
如果在应用中主挂掉了怎么办?
这个情况就需要,停止从库, 把从库以主库的启动命令起来就可以了。
1)停掉从库
kill -9 进程号
2)删除locat下文件
rm mongodb/db/locat.*-rf
3)启动从库
/usr/local/mongodb/bin/mongod --master --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork
7) MongoDB 增,删,改,查
[root@cacti bin]#./mongo
MongoDB shell version: 2.0.0
connecting to:test
> db.test.insert({"Name":"ymind","age": 8 }); #在test数据库里插入数据
>
> db.test.find({"Name":"ymind" } ); #查询数据
{ "_id": ObjectId("51c4187090db4e4751a3cd05"),"Name":"ymind","age": 8 }
> db.test.find({"age": 8} ); #查询数据
{ "_id": ObjectId("51c4187090db4e4751a3cd05"),"Name":"ymind","age": 8 }
>
>
> db.test.update({"Name":"ymind"}, { "$set": { "age":"5" } } ); #修改数据
> db.test.find({"Name":"ymind" } );
{ "Name":"ymind","_id": ObjectId("51c4187090db4e4751a3cd05"),"age":"5" }
>
> db.test.remove({"Name":"ymind" } ); #删除数据
> db.test.find({"Name":"ymind" } );
>
>
转载于:https://blog.51cto.com/summer37/1874903