我们知道mongodb现在生产环境大部分还是使用的复制集来作为主流,据我了解。很多公司现在都是使用复制集的方式,而我们通过mongos可以巧妙的解决链接的问题,我们先从环境说起:
server1:mongod primary+mongod config+mongos server2:mongod primary+mongod config+mongos server3:mongod primary+mongod config+mongos
其实就是一个分片复制集,只不过这里我们启动了一个分片节点,所有的数据库和集合并没有采用分片的操作,我们的所有链接可以通过三个mongos链接,三个mongos和mongo confir是独立的,目的是为了当其中一组down掉以后出现单点的问题。
server1:mongodb+config+mongos server2:mongodb+config+mongos
两个mongod是一个复制集,两个config+mongos是各自独立的。
tar -zxvf mongodb-linux-x86_64-rhel62-3.4.2.tgz mkdir /home/maxiangqian/ mv mongodb-linux-x86_64-rhel62-3.4.2 /home/maxiangqian/
2:加入环境变量
export PATH=/home/maxiangqian/mongodb-linux-x86_64-rhel62-3.4.2/bin:$PATH
3:创建用户和目录
[root@localhost mongodb]# mkdir -p /home/mongodb/db27017/data [root@localhost mongodb]# mkdir -p /home/mongodb/db27017/log [root@localhost mongodb]# mkdir -p /home/mongodb/db27017/tmp [root@localhost mongodb]# mkdir -p /home/mongodb/db27017/key
4:关闭配置SELinux
vi /etc/selinux/config
配置:
[root@localhost log]# /sbin/iptables -I INPUT -p tcp --dport 27017 -j ACCEPT [root@localhost log]# /sbin/iptables -I INPUT -p tcp --dport 27018 -j ACCEPT [root@localhost log]# /sbin/iptables -I INPUT -p tcp --dport 27019 -j ACCEPT [root@localhost log]# /etc/rc.d/init.d/iptables save
查看状态:/etc/init.d/iptables status
vi mongodb27017.conf
6:启动
mongod -f /home/mongodb/db27017/mongodb27017.cnf
7:为主实例配置超级管理员账号:maxiangqian
use admin db.createUser( { user: "maxiangqian", pwd: "maxiangqian", roles: [ { role: "root", db: "admin" } ] } )
openssl rand -base64 756 > /home/mongodb/db27017/key/autokey chmod 400 /home/mongodb/db27017/key/autokey cp /home/mongodb/db27017/key/autokey /home/sa/ scp -P 9880 autokey sa@172.16.16.35:/home/sa/
9:初始化复制集
config = { _id : "mongodbtestmxq", members : [ { _id : 0, host : "172.16.16.34:27017" }, { _id : 1, host : "172.16.16.35:27017" } ] }
创建复制集配置对象,其中id=warringstates是表示复制集的名称
rs.initiate(config)
10:验证复制集
mongodbtestmxq:PRIMARY> use maxiangqian switched to db maxiangqian mongodbtestmxq:PRIMARY> db.maxiagnqian.insert({"id":1,"name":"maxiangqian"}) WriteResult({ "nInserted" : 1 }) mongodbtestmxq:PRIMARY> db.maxiagnqian.find() { "_id" : ObjectId("58eb1aaddf342c94989755e7"), "id" : 1, "name" : "maxiangqian" }
从节点验证:
mongodbtestmxq:SECONDARY> use maxiangqian switched to db maxiangqian mongodbtestmxq:SECONDARY> db.maxiagnqian.find() { "_id" : ObjectId("58eb1aaddf342c94989755e7"), "id" : 1, "name" : "maxiangqian" }
发现这个认证复制集搭建是OK的。
mongod -f /home/mongodb/config/mongoconfig27018.conf
然后我们看一下配置文件:
[root@localhost config]# cat mongoconfig27018.conf dbpath=/home/mongodb/config/data/ configsvr=true port=27018 keyFile=/home/sa/autokey fork = true logpath = /home/mongodb/config/log/mongo.logi replSet=configmxq
初始化复制集,也要加认证,添加管理员账号:
config = { _id : "configmxq", members : [ { _id : 0, host : "172.16.16.34:27018" }, { _id : 1, host : "172.16.16.35:27018" } ] } rs.initiate(config)
12:启动mongos
[root@localhost mongos]# cat mongos27019.conf port=27019 configdb=configmxq/172.16.16.34:27018,172.16.16.35:27018 keyFile=/home/sa/autokey fork = true logpath = /home/mongodb/mongos/log/mongo.log
然后直接进行启动:
mongos -f /home/mongodb/mongos/mongos27019.conf
13:进入mongos进行分片的设置:
mongos> use admin switched to db admin mongos> db.auth("maxiangqian","maxiangqian") 1 mongos> show dbs admin 0.000GB config 0.000GB maxiangqian 0.000GB mongos> db.maxiangqian.find() mongos> use maxiangqian switched to db maxiangqian mongos> db.maxiangqian.find() mongos> db.maxiangqian.insert({"id":2,"name":"maxiangqian2"}) WriteResult({ "nInserted" : 1 }) 从主库直接插入一条数据: mongodbtestmxq:PRIMARY> db.maxiangqian.insert({"id":3,"name":"maxiangqian3"}) 然后返回mongos直接进行查看: mongos> db.maxiangqian.find() { "_id" : ObjectId("58eb34a9289626fe9f9c2586"), "id" : 2, "name" : "maxiangqian2" } { "_id" : ObjectId("58eb35516c6683eeb8a72b9a"), "id" : 3, "name" : "maxiangqian3" }
看到数据是没问题的。应该是配置好了。到这里,也算配置结束了。
下面还是贴一下基本的配置文件吧,方便大家查阅:
dbpath = /home/mongodb/db27017/data/ logpath = /home/mongodb/db27017/log/mongo.log pidfilepath = /home/mongodb/db27017/tmp/mongo.pid keyFile = /home/sa/autokey directoryperdb = true logappend = true smallfiles = true fork = true auth = true port = 27017 replSet = mongodbtestmxq maxConns = 10240 shardsvr=true
2:config配置文件
dbpath=/home/mongodb/config/data/ configsvr=true port=27018 keyFile=/home/sa/autokey fork = true logpath = /home/mongodb/config/log/mongo.log replSet=configmxq
3:mongos配置文件
port=27019 configdb=configmxq/172.16.16.34:27018,172.16.16.35:27018 keyFile=/home/sa/autokey fork = true logpath = /home/mongodb/mongos/log/mongo.log