单实例:
1)建立mongodb测试文件
#存放整个mongodb文件 mkdri -p /data/mongodbtest/single #mongodb数据文件 mkdir /data/mongodbtest/data
2)mongodb安装
cd /data/mongodbtest/single tar mongodb-linux-x86_64-rhel62-3.0.6.tgz mv mongodb-linux-x86_64-rhel62-3.0.6 mongodb
3)启动单实例mongodb
/data/mongodbtest/single/mongodb/bin/mongod --dbpath /data/mongodbtest/single/data
输出日志如下,表示已成功:
[initandlisten] db version v3.0.6 [initandlisten] git version:1ef45a23a4c5e3480ac919b28afcba3c615488f2 [initandlisten] build info: Linuxip-10-67-194-123 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011x86_64 BOOST_LIB_VERSION=1_49 [initandlisten] allocator: tcmalloc [initandlisten] options: { storage: { dbPath:"/data/mongodbtest/single/data/" } } [initandlisten] waiting for connections on port27017
Web访问:localhost:28017
注:第一次启动会有点久
主从模式:
ip:192.168.30.131 作为主节点master
ip:192.168.30.132 作为从节点slave
master创建文件夹:/data/mongodbtest/master
slave 创建文件夹:/data/mongodbtest/slave
启动master:
/data/mongodbtest/single/mongodb/bin/mongod --dbpath/data/mongodbtest/master -master
输出日志如下,表示已成功:
[initandlisten] MongoDBstarting :pid=3978port=27017 dbpath=/data/mongodbtest/master master=1 64-bit host=hadoop03 …… [initandlisten] options: { master: true,storage: { dbPath: "/data/mongodbtest/master" } }
启动slave:
/data/mongodbtest/single/mongodb/bin/mongod --dbpath/data/mondbtest/slave -slave -source 192.168.30.131:27017
输出日志如下,表示已成功:
[initandlisten] MongoDBstarting :pid=3521port=27017 dbpath=../slave slave=1 64-bit host=hadoop04 …… [initandlisten] options: { slave: true,source: "192.168.30.131:27017", storage: { dbPath:"../slave" } } …… [replslave] repl: syncing fromhost:192.168.30.131:27017
测试主从复制
在主节点上连接到终端:
/data/mongodbtest/single/monodb/bin/mongo 127.0.0.1
> use test; switched to db test >db.testdb.insert({"test1":"testvall"}) WriteResult({ "nInserted" : 1 }) >db.testdb.find() { "_id" :ObjectId("5601085dc618d904c883a5e2"), "test1" :"testvall" }
检查从主机的数据:
/data/mongodbtest/single/monodb/bin/mongo 127.0.0.1
> use test; switched to db test >db.testdb.find(); { "_id" :ObjectId("5601085dc618d904c883a5e2"), "test1" :"testvall" }
从slave主机上查看服务状态:
>db.printReplicationInfo() this is a slave, printing slave replicationinfo. source: 192.168.30.131:27017 syncedTo:Tue Sep 22 2015 15:57:59 GMT+0800 (CST) 13secs (0 hrs) behind the freshest member (no primary available at the moment)
一主多从模式:
和主从模式一样,只是再添加从主机而已,这里就不多讲了
总结上述模式的缺陷:
a) 主节点挂了能否自动切换连接?目前需要手工切换。
b) 主节点的写压力过大如何解决?
c) 从节点每个上面的数据都是对数据库全量拷贝,从节点压力会不会过大?
d) 就算对从节点路由实施路由访问策略能否做到自动扩展?
转载于:https://blog.51cto.com/kupig/1697635