所谓同机多实例是指在同一台服务器上部署多个mongod实例,本部署教程在2.4、2.6和2.8版本下均可使用,本博主均已测试通过。需要额外注意的是2点:
1、需要同步服务器时间
2、使用numactl启动mongod、mongos。
另外Config文件的设置请参考本博客的“Mongodb设置”。
1 部署过程
1.1 同步服务器时间
部署前必须同步服务器时间,否则容易出现奇怪问题,如”wait**s for distributed lock”等。同步服务器时间需要先设置同步点,即找台服务器启动时间同步服务。然后再在各个服务器上同步该同步点:
ntpdate 10.1.1.2
这里10.1.1.2是同步点服务器。如果不同步时间出现一例错误如下:升级mongos2.8会一直等待锁:
1.2 启动mongod
需要注意的是,因为本部署为多mongod同时运行在一台服务器上,所以需要使用numactl启动mongod,numactl的作用是让操作系统识别不同的mongod以便进行管理,避免造成冲突。
在10.1.101.8上执行:
numactl --interleave=all bin/mongod –f conf/sd1.conf
numactl --interleave=all bin/mongod –f conf/sd2.conf
numactl --interleave=all bin/mongod –f conf/sd3.conf
在另外两台机器上也执行上述命令
1.3 设置副本集
在10.1.101.8上执行
/usr/local/mongodb/bin/mongo localhost:27001
use admin
config={_id:"set1",members:[{_id:0,host:"10.1.101.8:27001"},{_id:1,host:"10.1.101.9:27001"},{_id:2,host:"10.1.101.10:27001","arbiterOnly":true}]};
rs.initiate(config)
在10.1.101.9上执行
/usr/local/mongodb/bin/mongo localhost:27002
use admin
config={_id:"set2",members:[{_id:0,host:"10.1.101.9:27002"},{_id:1,host:"10.1.101.10:27002"},{_id:2,host:"10.1.101.8:27002","arbiterOnly":true}]};
rs.initiate(config)
在10.1.101.10上执行
/usr/local/mongodb/bin/mongo localhost:27003
use admin
config={_id:"set3",members:[{_id:0,host:"10.1.101.10:27003"},{_id:1,host:"10.1.101.8:27003"},{_id:2,host:"10.1.101.9:27003","arbiterOnly":true}]};
rs.initiate(config)
执行完上述命令后,可运行rs.config()查看副本集。
1.4 启动config server
10.1.101.8,9,10启动config
numactl --interleave=all bin/mongod –f conf/configsvr.conf
其余两台机器也执行上述命令。
可使用netstat –lntp | grep mongo查看已启动进程。
1.5 启动mongos
10.1.101.8启动mongos
numactl --interleave=all bin/mongos –f conf/mongos.conf
1.6 配置shard
/usr/local/mongodb/bin/mongolocalhost:6080
useadmin
db.runCommand({addshard:"set1/10.1.101.8:27001,10.1.101.9:27001,10.1.101.10:27001"})
db.runCommand({addshard:"set2/10.1.101.8:27002,10.1.101.9:27002,10.1.101.10:27002"})
db.runCommand({addshard:"set3/10.1.101.8:27003,10.1.101.9:27003,10.1.101.10:27003"})
db.runCommand({enablesharding:"docinfo"})
db.runCommand({shardcollection:"docinfo.docinfo",key:{_id:1}})
db.runCommand({enablesharding:"doctext"})
db.runCommand({shardcollection:"doctext.doctext",key:{_id:1}})
1.7 停掉balancer
use config
sh.stopBalancer()
或使用以下命令
db.settings.update({_id:"balancer"},{$set:{stopped:true}},true);
2 注意事项
大部分注意事项在上文中已经提及。上述配置过程基本和本博客文章:“mongodb的配置”一致,请参考。