MONGODB集群架设

服务器及软件相关信息:
192.168.21.223 master
192.168.21.208 slave
192.168.21219 arbiter
1.下载资源,并同步到三台机上
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.4.tgz
scp mongodb-linux-x86_64-rhel70-4.0.4.tgz root@192.168.21.208:/usr/local/src
scp mongodb-linux-x86_64-rhel70-4.0.4.tgz root@192.168.21.219:/usr/local/src

2.解压并安装
tar xf mongodb-linux-x86_64-rhel70-4.0.4.tgz
mv mongodb-linux-x86_64-rhel70-4.0.4 /usr/local/mongodb
mkdir -p /usr/local/mongodb/{db,conf,log}

3.编辑配置文件
master:
vim /usr/local/mongodb/conf/mongodb.conf
port=27017 ##端口号
dbpath=/usr/local/mongodb/db ##数据路径
logpath=/usr/local/mongodb/log/mongodb.log ##日志路径
logappend=true ##日志追加
fork=true ##后台
bind_ip=192.168.21.223,127.0.0.1 ##监听地址
maxConns=5000
replSet=picasso ##复制集名称
slave:
port=27017 ##端口号
dbpath=/usr/local/mongodb/db ##数据路径
logpath=/usr/local/mongodb/log/mongodb.log ##日志路径
logappend=true ##日志追加
fork=true ##后台
bind_ip=192.168.21.208,127.0.0.1 ##监听地址
maxConns=5000
replSet=picasso ##复制集名称
arbiter:
port=27017 ##端口号
dbpath=/usr/local/mongodb/db ##数据路径
logpath=/usr/local/mongodb/log/mongodb.log ##日志路径
logappend=true ##日志追加
fork=true ##后台
bind_ip=192.168.21.219,127.0.0.1 ##监听地址
maxConns=5000
replSet=picasso ##复制集名称

启动三台机:
ln -s /usr/local/mongodb/bin/* /usr/sbin/
mongod -f /usr/local/mongodb/conf/mongodb.conf

分别执行如下语句:
[root@localhost ~]# mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { “id” : UUID(“1b6da791-1155-465e-b6c5-95eed42a77fc”) }
MongoDB server version: 4.0.4
Server has startup warnings:
use admin
config={_id:’picasso’,members:[{_id:0,host:’192.168.21.223:27017′},{_id:1,host:’192.168.21.208:27017′},{_id:2,host:’192.168.21.219:27017′,arbiterOnly:true}]}

第一台MASTER上执行
rs.initiate(config)
picasso:SECONDARY> rs.initiate(config)
{
“operationTime” : Timestamp(1544097319, 1),
“ok” : 0,
“errmsg” : “already initialized”,
“code” : 23,
“codeName” : “AlreadyInitialized”,
“$clusterTime” : {
“clusterTime” : Timestamp(1544097319, 1),
“signature” : {
“hash” : BinData(0,”AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
}
}
picasso:PRIMARY>

验证
rs.status()
从库查数据报错
picasso:SECONDARY> use qincx
switched to db qincx
picasso:SECONDARY> db.users.find()
Error: error: {
“operationTime” : Timestamp(1544100050, 1),
“ok” : 0,
“errmsg” : “not master and slaveOk=false”,
“code” : 13435,
“codeName” : “NotMasterNoSlaveOk”,
“$clusterTime” : {
“clusterTime” : Timestamp(1544100050, 1),
“signature” : {
“hash” : BinData(0,”AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0):
开启从库查数据后再次查看:
picasso:SECONDARY> rs.slaveOk(true)
picasso:SECONDARY> use qincx
switched to db qincx
picasso:SECONDARY> db.users.find()
{ “_id” : ObjectId(“5c091898c44fb85e94e59f52”), “name” : “qincx”, “age” : 40 }
picasso:SECONDARY>

复制集设置认证
openssl rand -base64 755 > /usr/local/mongodb/mongodb-keyfile
chmod 600 /usr/local/mongodb/mongodb-keyfile
scp mongodb-keyfile root@192.168.21.208:/usr/local/mongodb/
scp mongodb-keyfile root@192.168.21.219:/usr/local/mongodb/

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>