咱們知道mongodb如今生產環境大部分仍是使用的複製集來做爲主流,據我瞭解。不少公司如今都是使用複製集的方式,而咱們經過mongos能夠巧妙的解決連接的問題,咱們先從環境提及:linux
server1:mongod primary+mongod config+mongos server2:mongod primary+mongod config+mongos server3:mongod primary+mongod config+mongos
其實就是一個分片複製集,只不過這裏咱們啓動了一個分片節點,全部的數據庫和集合並無採用分片的操做,咱們的全部連接能夠經過三個mongos連接,三個mongos和mongo confir是獨立的,目的是爲了當其中一組down掉之後出現單點的問題。mongodb
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:加入環境變量json
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:關閉配置SELinuxapp
vi /etc/selinux/config
配置:tcp
[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 statusspa
vi mongodb27017.conf
6:啓動code
mongod -f /home/mongodb/db27017/mongodb27017.cnf
7:爲主實例配置超級管理員帳號:maxiangqianserver
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