温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB Replica Set搭建

发布时间:2020-07-16 13:06:06 来源:网络 阅读:614 作者:coveringindex 栏目:MongoDB数据库

第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).

MongoDB Replica Set搭建

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI