快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

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

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

MongoDBReplicaSet搭建-创新互联

第0部分 业务信息

成都创新互联成都企业网站建设服务,提供成都做网站、网站建设网站开发,网站定制,建网站,网站搭建,网站设计,成都响应式网站建设,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:028-86922220

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:MongoDBReplicaSet搭建-创新互联
文章网址:http://6mz.cn/article/djoopj.html

其他资讯