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

Configure mongodb replica set

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

Share

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

Host Information:

[root@my2 db] # cat / etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

192.168.56.101 my1.ml.com my1

192.168.56.102 my2.ml.com my2

192.168.56.103 my3.ml.com my3

Turn off large page memory:

Echo never > > / sys/kernel/mm/transparent_hugepage/enabled

Echo never > > / sys/kernel/mm/transparent_hugepage/defrag

Create a directory on each host:

Mkdir / data/db-p

Edit the configuration file on each host:

Vim / data/db/mongodb.conf

Storage:

DbPath: "/ data/db"

DirectoryPerDB: true

Journal:

Enabled: true

WiredTiger:

EngineConfig:

CacheSizeGB: 1

SystemLog:

Destination: file

Path: "/ data/db/mongodb.log"

LogRotate: rename

LogAppend: true

TimeStampFormat: iso8601-local

OperationProfiling:

SlowOpThresholdMs: 200

Mode: slowOp

Replication:

OplogSizeMB: 10240

ReplSetName: "repsetzhou"

ProcessManagement:

Fork: true

Net:

BindIp: 0.0.0.0

Port: 27017

Security:

KeyFile: "/ data/db/mongodb-keyfile"

Authorization: "enabled"

Generate certification files:

[root@my1 db] # openssl rand-base64 800 > / data/db/mongodb-keyfile

Scp to other nodes:

[root@my1 db] # scp / data/db/mongodb-keyfile 192.168.56.102:/data/db/

Mongodb-keyfile 100% 1390 1.4KB/s 00:00

[root@my1 db] # scp / data/db/mongodb-keyfile 192.168.56.103:/data/db/

Mongodb-keyfile

/ usr/local/mongodb/bin/mongod-- config / data/db/mongodb.conf

Log in to mongodb on 192.168.56.101 to configure and initialize the replication set:

/ usr/local/mongodb/bin/mongo

> use admin

Switched to db admin

Configure the replication set:

Config = {

_ id: "repsetzhou"

Members: [

{_ id: 0, host: "192.168.56.101 purl 27017"}

{_ id: 1, host: "192.168.56.102 purl 27017"}

{_ id: 2, host: "192.168.56.103 purl 27017"}

]

}

Initialize the configuration set:

Rs.initiate (config)

Action:

> config = {

... _ id: "repsetzhou"

... Members: [

... {_ id: 0, host: "192.168.56.101 purl 27017"}

... {_ id: 1, host: "192.168.56.102 purl 27017"}

... {_ id: 2, host: "192.168.56.103 purl 27017"}

...]

...}

{

"_ id": "repsetzhou"

"members": [

{

"_ id": 0

"host": "192.168.56.101purl 27017"

}

{

"_ id": 1

"host": "192.168.56.102 purl 27017"

}

{

"_ id": 2

"host": "192.168.56.103 purl 27017"

}

]

}

>

> rs.initiate (config)

{"ok": 1}

Login confirmation:

Repsetzhou:PRIMARY > use admin

Switched to db admin

Repsetzhou:PRIMARY > rs.config ()

{

"_ id": "repsetzhou"

"version": 1

"protocolVersion": NumberLong (1)

"members": [

{

"_ id": 0

"host": "192.168.56.101purl 27017"

"arbiterOnly": false

"buildIndexes": true

"hidden": false

"priority": 1

"tags": {

}

"slaveDelay": NumberLong (0)

"votes": 1

}

{

"_ id": 1

"host": "192.168.56.102 purl 27017"

"arbiterOnly": false

"buildIndexes": true

"hidden": false

"priority": 1

"tags": {

}

"slaveDelay": NumberLong (0)

"votes": 1

}

{

"_ id": 2

"host": "192.168.56.103 purl 27017"

"arbiterOnly": false

"buildIndexes": true

"hidden": false

"priority": 1

"tags": {

}

"slaveDelay": NumberLong (0)

"votes": 1

}

]

"settings": {

"chainingAllowed": true

"heartbeatIntervalMillis": 2000

"heartbeatTimeoutSecs": 10

"electionTimeoutMillis": 10000

"getLastErrorModes": {

}

"getLastErrorDefaults": {

"w": 1

"wtimeout": 0

}

"replicaSetId": ObjectId ("5b9724ff670e7d5d4d9c6c7e")

}

}

Repsetzhou:PRIMARY >

Use admin

Db.createUser (

{

User: "admin"

Pwd: "admin123"

Roles: [{role: "userAdminAnyDatabase", db: "admin"}]

}

);

Repsetzhou:PRIMARY > use admin

Switched to db admin

Repsetzhou:PRIMARY > db.createUser (

... {

... User: "admin"

... Pwd: "admin123"

... Roles: [{role: "userAdminAnyDatabase", db: "admin"}]

...}

Successfully added user: {

"user": "admin"

"roles": [

{

"role": "userAdminAnyDatabase"

"db": "admin"

}

]

}

Authorization:

Db.grantRolesToUser ("admin", ["clusterAdmin"])

Db.grantRolesToUser ("admin", ["readWriteAnyDatabase"])

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