一、环境准备
CentOS 7.5 3.10.0-862.el7.x86_64
mongodb 3.4.2
IP: 192.168.75.50
192.168.75.51
192.168.75.52
下载压缩包
https://www.mongodb.com/download-center/community
# 在3台机器上分别执行
systemctl stop firewalld && systemctl disable firewalld
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
cd /
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.6.tgz
tar zxvf mongodb-linux-x86_64-rhel70-3.4.2.tgz
mv mongodb-linux-x86_64-rhel70-3.4.2 mongodb
cd mongodb
mkdir conf logs db
cd conf
touch mongodb.conf
vim mongodb.conf # add below lines
dbpath=/mongodb/db
logpath=/mongodb/logs/mongodb.log
port=27017
fork=true
replSet=mongoTest
cd ..
bin/mongod -f conf/mongodb.conf
#about to fork child process, waiting until server is ready for connections.
#forked process: 1528
#child process started successfully, parent exiting
三、在主库上配置mongodb副本集
cd /mongodb/bin
./mongo -port 27017
集群配置
use admin
config={_id:"mongoTest",members:[{_id:0,host:"192.168.75.50:27017"},{_id:1,host:"192.168.75.51:27017"},{_id:2,host:"192.168.75.52:27017"}]}
{
"_id" : "mongoTest",
"members" : [
{
"_id" : 0,
"host" : "192.168.75.50:27017"
},
{
"_id" : 1,
"host" : "192.168.75.51:27017"
},
{
"_id" : 2,
"host" : "192.168.75.52:27017"
}
]
}
rs.initiate(config)
{ "ok" : 1 }
四、验证
# 主库上执行 192.168.75.50
mongoTest:PRIMARY> use db
switched to db db
mongoTest:PRIMARY> show tables
mongoTest:PRIMARY> db.tt.insert({"testkey":1});
WriteResult({ "nInserted" : 1 })
mongoTest:PRIMARY> db.tt.find()
{ "_id" : ObjectId("5eb645ae0e061116881a5dd4"), "testkey" : 1 }
# secondary库上执行 192.168.75.51
cd /mongodb/bin
./mongo localhost:27017
mongoTest:SECONDARY> rs.slaveOk();
mongoTest:SECONDARY> use db
switched to db db
mongoTest:SECONDARY> db.tt.find()
{ "_id" : ObjectId("5eb645ae0e061116881a5dd4"), "testkey" : 1 }
# secondary库上执行 192.168.75.52
cd /mongodb/bin
./mongo localhost:27017
rs.slaveOk();
mongoTest:SECONDARY> use db
switched to db db
mongoTest:SECONDARY> db.tt.find()
{ "_id" : ObjectId("5eb645ae0e061116881a5dd4"), "testkey" : 1 }
mongoTest:SECONDARY> show tables
tt
mongoTest:SECONDARY> db.tt.find()
{ "_id" : ObjectId("5eb645ae0e061116881a5dd4"), "testkey" : 1 }
{ "_id" : ObjectId("5eb64a110e061116881a5dd5"), "testkey2" : 2 }
五、配置mongo命令slave可读
vi ~/.mongorc.js # 增加如下内容
rs.slaveOk();
六、开机自启动
编辑mongod.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/mongodb/bin/mongod --config /mongodb/conf/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/mongodb/bin/mongod --shutdown --config /mongodb/bin/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
将mongod.service上传到3台机器的 /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mongod
systemctl status mongod
netstat -lant
登录主节点
cd /mongodb/bin
./mongo -port 27017
mongoTest:PRIMARY> show dbs
admin 0.000GB
db 0.000GB
local 0.000GB
mongoTest:PRIMARY> db.tt.find()
mongoTest:PRIMARY> use db
switched to db db
mongoTest:PRIMARY> db.tt.find()
{ "_id" : ObjectId("5eb645ae0e061116881a5dd4"), "testkey" : 1 }
{ "_id" : ObjectId("5eb64a110e061116881a5dd5"), "testkey2" : 2 }
七、primary和secondary切换
rs.stepDown()
rs.isMaster()
配置节点的优先级priority
mongoTest:PRIMARY> conf=rs.config()
{
"_id" : "mongoTest",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "192.168.75.50:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.75.51:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.75.52:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 60000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5eb6446417b712d0196fa8e8")
}
}
mongoTest:PRIMARY> conf.members[0].priority=10
10
mongoTest:PRIMARY> conf.members[1].priority=5
5
mongoTest:PRIMARY> conf.members[2].priority=1
1
mongoTest:PRIMARY> rs.reconfig(conf)
{ "ok" : 1 }
八、常用mongodb操作
use db
db.tt.insert({"testkey1":1})
db.tt.remove({"testkey1":1})
db.tt.remove({}) #删除tt的全部数据
db.tt.find()
db.tt.drop() # 删除tt
mongoTest:PRIMARY> db.createCollection("ss");
{ "ok" : 1 }
mongoTest:PRIMARY> show collections;
ss
tt
mongoTest:PRIMARY> db.ss.insert({"ss_key1":1})
WriteResult({ "nInserted" : 1 })