MongoDB Replica Set搭建
第0部分 業務資訊
業務名稱: xyz
埠號: 28017
第1部分 準備環境
建立mongodb使用者
# useradd mongodb
建立資料目錄
# mkdir -pv /data1/mongodb/28017/data
# chown -R mongodb.mongodb /data1/mongodb/28017/data/
建立配置, 日誌檔案目錄
# mkdir -pv /home/mongodb/mongodb/xyz/28017/{conf,logs,pid}
準備配置檔案
# cat /home/mongodb/mongodb/xyz/28017/conf/mongodb.conf
##General options
port = 28017
maxConns = 1024
logpath = /home/mongodb/mongodb/xyz/28017/logs/mongodb.log
logappend = false
logRotate = rename
timeStampFormat = iso8601-local
pidfilepath = /home/mongodb/mongodb/xyz/28017/pid/mongodb.pid
#keyFile = /home/mongodb/mongodb/xyz/28017/conf/mongodb.key
httpinterface = false
nounixsocket = true
fork = true
#auth = true
slowms = 100
profile = 1
##Replication options
oplogSize = 5120
##Replica set options
replSet = xyz
##Storage options
storageEngine = wiredTiger
dbpath = /data1/mongodb/28017/data/
directoryperdb = true
journal = true
##WiredTiger options
wiredTigerCacheSizeGB = 5
# chown -R mongodb.mongodb /home/mongodb/mongodb/xyz/28017/{conf,logs,pid}
第2部分 調整作業系統引數
切換到mongodb使用者.
# su - mongodb
啟動mongodb程序.
$ /usr/local/mongodb/bin/mongod --config /home/mongodb/mongodb/xyz/28017/conf/mongodb.conf
根據日誌提示的WARNING, 調整作業系統引數.
$ cat /home/mongodb/mongodb/xyz/28017/logs/mongodb.log
2018-11-07T17:03:40.974+0800 I CONTROL [initandlisten]
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten] ** WARNING: You are running on a NUMA machine.
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten] ** We suggest launching mongod like this to avoid performance problems:
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten] ** numactl --interleave=all mongod [other options]
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten]
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten]
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten]
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 32000 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2018-11-07T17:03:40.975+0800 I CONTROL [initandlisten]
調整系統引數
# echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
# echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
# tail -n 4 /etc/security/limits.conf
mongodb soft nofile 64000
mongodb hard nofile 64000
mongodb soft nproc 64000
mongodb hard nproc 64000
關閉mongodb程序.
$ /usr/local/mongodb/bin/mongo --port 28017 --host localhost
MongoDB shell version v3.4.3
connecting to: mongodb://localhost:28017/
MongoDB server version: 3.4.3
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
再次啟動mongodb程序.
$ numactl --interleave=all /usr/local/mongodb/bin/mongod --config /home/mongodb/mongodb/xyz/28017/conf/mongodb.conf
第3部分 初始化副本集
保持開啟replSet, 關閉keyFile和auth引數.
$ /usr/local/mongodb/bin/mongo --port 28017 --host localhost
> config = {_id: "xyz", members: [{_id: 0, host: "192.168.1.157:28017"}, {_id: 1, host: "192.168.1.158:28017"}, {_id: 2, host: "192.168.1.159:28017"}]}
{
"_id" : "xyz",
"members" : [
{
"_id" : 0,
"host" : "192.168.1.157:28017"
},
{
"_id" : 1,
"host" : "192.168.1.158:28017"
},
{
"_id" : 2,
"host" : "192.168.1.159:28017"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
建立管理使用者
xyz:PRIMARY> use admin
switched to db admin
xyz:PRIMARY> db.createUser({user: "root", pwd: "root_password", roles: [{role: "root", db: "admin"}]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
關閉mongodb程序, 準備key檔案.
$ echo 'L6rE59OEHWGeQAIIZHy3uXNj1cxu9lFuCJxZSejf' > /home/mongodb/mongodb/xyz/28017/conf/mongodb.key
$ chmod 600 /home/mongodb/mongodb/xyz/28017/conf/mongodb.key
開啟keyFile和auth引數, 啟動mongodb程序, 登陸.
$ /usr/local/mongodb/bin/mongo --port 28017 --host localhost --username root --password root_password --authenticationDatabase admin
第4部分 部署業務
建立業務使用者
xyz:PRIMARY> use abc
switched to db abc
xyz:PRIMARY> db.createUser({user : "abc_user", pwd : "xyzabc", roles : [{role : "readWrite", db : "abc"}]})
Successfully added user: {
"user" : "abc_user",
"roles" : [
{
"role" : "readWrite",
"db" : "abc"
}
]
}
業務使用者abc_user登陸.
$ /usr/local/mongodb/bin/mongo --port 28017 --host localhost --username abc_user --password xyzabc --authenticationDatabase abc
xyz:PRIMARY> db
test
xyz:PRIMARY> use abc
switched to db abc
xyz:PRIMARY> db.tt.insert({"name" : "abc"})
WriteResult({ "nInserted" : 1 })
xyz:PRIMARY> show collections
tt
xyz:PRIMARY> db.tt.find()
{ "_id" : ObjectId("5be3ffb4832bb6db59e4e60f"), "name" : "abc" }
若感興趣可關注訂閱號”資料庫最佳實踐”(DBBestPractice).