我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

MongoDBReplicaSet搭建

第0部分 业务信息

创新互联建站专业IDC数据服务器托管提供商,专业提供成都服务器托管,服务器租用,服务器托管服务器托管,成都多线服务器托管等服务器托管服务。

业务名称: 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搭建


网站栏目:MongoDBReplicaSet搭建
网站链接:http://mswzjz.cn/article/gesgdp.html

其他资讯