您好,登錄后才能下訂單哦!
第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).
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。