标签:
官方网站:
http://www.mongodb.org/
MongoDB 安裝,主从配置
一 MongoDB 安装
1 [root@zabbix_server src]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.0.tgz 2 [root@zabbix_server src]# tar xzvf mongodb-linux-x86_64-2.0.0.tgz 3 [root@zabbix_server src]# mv mongodb-linux-x86_64-2.0.0 /usr/local/mongodb 4 5 [root@zabbix_server src]# mkdir /usr/local/mongodb/data 6 [root@zabbix_server src]# touch /usr/local/mongodb/logs 7 8 [root@zabbix_server bin]# /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork 9 10 11 12 [root@zabbix_server bin]# ./mongo 13 MongoDB shell version: 2.0.0 14 connecting to: test 15 > use test; 16 switched to db test 17 > exit 18 bye 19 20 21 [root@zabbix_server bin]# netstat -anlpt | grep mongo 22 tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 11504/mongod 23 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: 2 1 在主库上插入数据 3 [root@cacti bin]# ./mongo 4 MongoDB shell version: 2.0.0 5 connecting to: test 6 > db.foo.save({"id":123456,"name":‘lizonggang‘}) 7 > db.foo.find({"id":123456}) 8 { "_id" : ObjectId("51c3f4b21399022afd992f39"), "id" : 123456, "name" : "lizonggang" } 9 > 10 11 2 在从上查看数据(已有在主上插入的数据,说明主从成功) 12 [root@zabbix_server bin]# ./mongo 13 MongoDB shell version: 2.0.0 14 connecting to: test 15 > db.foo.find({"id":123456}) 16 { "_id" : ObjectId("51c3f4b21399022afd992f39"), "id" : 123456, "name" : "lizonggang" } 17 >
测试2:
1 1 在主库上插入数据 2 > use elaindb 3 switched to db elaindb 4 > db.test.save({title:"This is a test!"}) 5 > db.test.find(); 6 { "_id" : ObjectId("51c3f8f11a90c8da157c7f64"), "title" : "This is a test!" } 7 > 8 9 2 在从库上验证数据 10 [root@zabbix_server bin]# ./mongo 11 MongoDB shell version: 2.0.0 12 connecting to: test 13 > use elaindb 14 switched to db elaindb 15 > db.test.find(); 16 { "_id" : ObjectId("51c3f8f11a90c8da157c7f64"), "title" : "This is a test!" } 17 > db.printReplicationInfo(); 18 this is a slave, printing slave replication info. 19 source: 192.168.0.14:27017 20 syncedTo: Fri Jun 21 2013 14:59:10 GMT+0800 (CST) 21 = 9 secs ago (0hrs) 22 > 23 > db.printSlaveReplicationInfo(); 24 source: 192.168.0.14:27017 25 syncedTo: Fri Jun 21 2013 14:59:30 GMT+0800 (CST) 26 = 16 secs ago (0hrs) 27 >
测试3:
1 在这个状态下,slave 下是不可以插入数据的 2 > db.test.save({title:"This is slave test!"}) 3 not master
5) 一些查询状态的语句
1)查询库
1 > show dbs; 2 elaindb 0.0625GB 3 local 0.125GB 4 test 0.0625GB
2)在从库上查询主库的地址
1 > use local; 2 switched to db local 3 > db.sources.find() 4 { "_id" : ObjectId("51c3f5dd568d10529950b10d"), "host" : "192.168.0.14:27017", "source" : "main", "syncedTo" : { "t" : 1371797300000, "i" : 1 } } 5 >
3)查看主从复制状态
1 > db.printReplicationInfo(); 2 configured oplog size: 47.6837158203125MB 3 log length start to end: 1375secs (0.38hrs) 4 oplog first event time: Fri Jun 21 2013 14:30:35 GMT+0800 (CST) 5 oplog last event time: Fri Jun 21 2013 14:53:30 GMT+0800 (CST) 6 now: Fri Jun 21 2013 14:53:32 GMT+0800 (CST) 7 >
4)查看Collection状态
1 > db.printCollectionStats(); 2 foo 3 { 4 "ns" : "test.foo", 5 "count" : 1, 6 "size" : 56, 7 "avgObjSize" : 56, 8 "storageSize" : 4096, 9 "numExtents" : 1, 10 "nindexes" : 1, 11 "lastExtentSize" : 4096, 12 "paddingFactor" : 1, 13 "flags" : 1, 14 "totalIndexSize" : 8176, 15 "indexSizes" : { 16 "_id_" : 8176 17 }, 18 "ok" : 1 19 } 20 --- 21 system.indexes 22 { 23 "ns" : "test.system.indexes", 24 "count" : 1, 25 "size" : 64, 26 "avgObjSize" : 64, 27 "storageSize" : 4096, 28 "numExtents" : 1, 29 "nindexes" : 0, 30 "lastExtentSize" : 4096, 31 "paddingFactor" : 1, 32 "flags" : 0, 33 "totalIndexSize" : 0, 34 "indexSizes" : { 35 36 }, 37 "ok" : 1 38 } 39 --- 40 >
6)应急
如果在应用中主挂掉了怎么办?
这个情况就需要,停止从库, 把从库以主库的启动命令起来就可以了。
1)停掉从库
kill -9 进程号
2)删除locat下文件
1 rm mongodb/db/locat.* -rf
3)启动从库
1 /usr/local/mongodb/bin/mongod --master --dbpath=/usr/local/mongodb/data
--logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork
6) Mongdb Web 查看界面
7) MongoDB 增,删,改,查
1 [root@cacti bin]# ./mongo 2 MongoDB shell version: 2.0.0 3 connecting to: test 4 > db.test.insert({ "Name" : "ymind", "age" : 8 }); #在test数据库里插入数据 5 > 6 > db.test.find({ "Name" : "ymind" } ); #查询数据 7 { "_id" : ObjectId("51c4187090db4e4751a3cd05"), "Name" : "ymind", "age" : 8 } 8 > db.test.find({ "age" : 8} ); #查询数据 9 { "_id" : ObjectId("51c4187090db4e4751a3cd05"), "Name" : "ymind", "age" : 8 } 10 > 11 > 12 > db.test.update({"Name" : "ymind"}, { "$set" : { "age" : "5" } } ); #修改数据 13 > db.test.find({ "Name" : "ymind" } ); 14 { "Name" : "ymind", "_id" : ObjectId("51c4187090db4e4751a3cd05"), "age" : "5" } 15 > 16 > db.test.remove({ "Name" : "ymind" } ); #删除数据 17 > db.test.find({ "Name" : "ymind" } ); 18 > 19 >
标签:
原文地址:http://www.cnblogs.com/huangxu/p/5622832.html