Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB Replica Set building

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Part 0 Business Information

Business name: xyz

Port number: 28017

Part 1 preparation of the environment

Create a mongodb user

# useradd mongodb

Create a data directory

# mkdir-pv / data1/mongodb/28017/data

# chown-R mongodb.mongodb / data1/mongodb/28017/data/

Create configuration, log file directory

# mkdir-pv / home/mongodb/mongodb/xyz/28017/ {conf,logs,pid}

Prepare the configuration file

# 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}

Part 2 adjusting operating system parameters

Switch to mongodb user.

# su-mongodb

Start the mongodb process.

$/ usr/local/mongodb/bin/mongod-config / home/mongodb/mongodb/xyz/28017/conf/mongodb.conf

Adjust the operating system parameters according to the WARNING prompted by the log.

$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]

Adjust system parameters

# 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

Close the mongodb process.

$/ 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...

Start the mongodb process again.

$numactl-- interleave=all / usr/local/mongodb/bin/mongod-- config / home/mongodb/mongodb/xyz/28017/conf/mongodb.conf

Part 3 initialize the replica set

Keep replSet on and turn off keyFile and auth parameters.

$/ usr/local/mongodb/bin/mongo-port 28017-host localhost

> config = {_ id: "xyz", members: [{_ id: 0, host: "192.168.1.157members 28017"}, {_ id: 1, host: "192.168.1.158virtual 28017"}, {_ id: 2, host: "192.168.1.159members 28017"}]}

{

"_ id": "xyz"

"members": [

{

"_ id": 0

"host": "192.168.1.157purl 28017"

}

{

"_ id": 1

"host": "192.168.1.158purl 28017"

}

{

"_ id": 2

"host": "192.168.1.159purl 28017"

}

]

}

> rs.initiate (config)

{"ok": 1}

Create an administrative user

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"

}

]

}

Close the mongodb process and prepare the key file.

$echo 'L6rE59OEHWGeQAIIZHy3uXNj1cxu9lFuCJxZSejf' > / home/mongodb/mongodb/xyz/28017/conf/mongodb.key

$chmod 600 / home/mongodb/mongodb/xyz/28017/conf/mongodb.key

Enable the keyFile and auth parameters, start the mongodb process, and log in.

/ usr/local/mongodb/bin/mongo-- port 28017-- host localhost-- username root-- password root_password-- authenticationDatabase admin

Part 4 deployment of business

Create a business user

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"

}

]

}

Business user abc_user login.

/ 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"}

If you are interested, please follow Subscription account's Database Best practices (DBBestPractice).

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report