安装
直接下载官方程序
在三台机器上如下配置,其中文件名称,ip,端口可作相应更改。
1、拷贝程序到/opt/下,创建分片配置文件。
#shard21.conf dbpath=/data/shard21 logpath = /data/shard21.log pidfilepath = /opt/mongodb/shard21.pid directoryperdb = true logappend = true replSet = shard2 bind_ip=10.105.33.6 port = 27017 oplogSize = 100 fork = true noprealloc=true |
2、创建mongos配置文件
dbpath = /data/config logpath =/data/config.log logappend = true bind_ip = 10.105.33.6 port = 20000 fork = true |
启动
在三台机器分别执行如下,文件名称,ip地址相应修改。
/opt/mongodb/bin/mongod --shardsvr -f /opt/mongodb/shard21.conf /opt/mongodb/bin/mongod --configsvr -f /opt/mongodb/config.conf /opt/mongodb/bin/mongos --configdb 10.237.168.179:20000,10.131.154.192:20000,10.105.36.235:20000 --port 30000 --logpath /data/route.log --logappend --fork
|
配置
设置副本
use admin config={_id:"shard2",members:[{"_id":0,"host":"10.105.33.6:27017,","priority":2},{"_id":1,"host":",10.247.40.202:27017","priority":1},{"_id":2,"host":"10.131.158.204:27017","arbiterOnly":true}]} rs.initiate(config)
|
添加分片
use admin db.runCommand({addshard:"shard2/10.105.33.6:27017,10.247.40.202:27017,10.131.158.204:27017",name:"shard2",maxsize:20480})
db.runCommand({addshard:"shard4/10.105.49.216:27017,10.131.185.59:27017,10.131.224.105:27017",name:"shard4",maxsize:0})
db.runCommand({enablesharding : "lbstest"}) db.runCommand({"shardCollection":"lbstest.test",key:{"line_way":1,"user_id":1}})
db.runCommand({listshards:1}); |
地理信息库测试
use lbstest;
db.test.ensureIndex({“location”:”2d”})
db.test.ensureIndex({“location”:” 2dsphere”})
db.test.ensureIndex({"line_way":1})
db.test.ensureIndex({"user_id":1})
db.test.ensureIndex({"line_way":1,"user_id":1});
db.test.insert({"user_id":3,"location":{"lat":22.111,"long":33.222},"target":"t3","line_way":"xiamen_2_zhangzhou"});
mongo c++驱动
下载mongo驱动
下载boost库
sh bootstrap.sh sudo ./b2 --with-filesystem --with-regex --with-system --with-thread --with-signals --with-regex install |
mongo c++驱动安装
sudo yum install scons openssl-devel libgsasl libgsasl-develcyrus-sasl cyrus-sasl-devel
进入mongo驱动库 sudo scons --prefix=/opt/mongo-driver --ssl --use-sasl-client install |