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 replication set management

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

Share

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

1. Configuration allows data to be read from the node

The slave node of the default MongoDB replication set cannot read data, and you can use the rs.slaveOk () command to allow data to be read from the slave node.

Abc:PRIMARY > show dbs # can read data on the primary node

Admin 0.000GB

Config 0.000GB

Local 0.000GB

School 0.000GB

Abc:PRIMARY > exit

Bye

[root@localhost logs] # mongo-- port 27018 # enters the slave node with port 27018

MongoDB shell version v3.6.7

Connecting to: mongodb://127.0.0.1:27018/

Abc:SECONDARY > show dbs # View the database

2018-09-13T14:55:03.037+0800 E QUERY [thread1] Error: listDatabases failed: {# unable to read data

OperationTime: Timestamp (1536821694, 1)

"ok": 0

"errmsg": "not master and slaveOk=false"

Abc:SECONDARY > rs.slaveOk () # use the command rs.slaveOk () command to allow data to be read from the slave node

Abc:SECONDARY > show dbs

Admin 0.000GB

Config 0.000GB

Local 0.000GB

School 0.000GB

Abc:SECONDARY >

two。 View replication status information

Abc:SECONDARY > rs.help ()

Rs.printReplicationInfo () check oplog size and time range

Rs.printSlaveReplicationInfo () check replica set members and replication lag

Abc:SECONDARY > rs.printReplicationInfo () # View log size and time range

Configured oplog size: 990MB

Log length start to end: 3482secs (0.97hrs)

Oplog first event time Thu Sep 13 2018 14:12:02 GMT+0800 (CST)

Oplog last event time Thu Sep 13 2018 15:10:04 GMT+0800 (CST)

Now Thu Sep 13 2018 15:10:06 GMT+0800 (CST)

Abc:SECONDARY > rs.printSlaveReplicationInfo () # View the data copied from the node

Source: 192.168.213.184:27018

SyncedTo Thu Sep 13 2018 15:11:54 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

Source: 192.168.213.184:27019

SyncedTo Thu Sep 13 2018 15:11:54 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

It can be seen that the arbitration node does not have data replication.

3.. Change the oplog siz

Oplog, which stands for opreations, is stored in the local database. The new operation in oplog automatically replaces the old operation to ensure that the oplog does not exceed the default size. By default. The oplog size takes up 5% of the disk space of a 64-bit instance. Try to ensure that the oplog of the master node is large enough to store operation records for a long time.

(1) first shut down the slave node server, exit from the replication set, and temporarily become a single instance

Abc:SECONDARY > use admin

Switched to db admin

Abc:SECONDARY > db.shutdownServer () # disable the service

Server should be down...

[root@localhost logs] # vim / etc/mongod2.conf # change the configuration file of instance 2

Port: 27028 # Port number change

# replication:

# replSetName: abc # Log out of replication set

(2) enter the database with the port number of 27028

[root@localhost logs] # mongod-f / etc/mongod2.conf

About to fork child process, waiting until server is ready for connections.

Forked process: 40575

Child process started successfully, parent exiting

[root@localhost logs] # mongo-- port 27028

MongoDB shell version v3.6.7

Connecting to: mongodb://127.0.0.1:27028/

(3) make a full backup of oplog

[root@localhost logs] # mongodump-port 27028-db local-collection 'oplog.rs'

2018-09-13T15:30:19.876+0800 writing local.oplog.rs to

2018-09-13T15:30:19.881+0800 done dumping local.oplog.rs (376documents)

(4) Delete the original log file

> use local

Switched to db local

> show tables

Me

Oplog.rs

Replset.election

Replset.minvalid

Replset.oplogTruncateAfterPoint

Startup_log

System.replset

System.rollback.id

> db.oplog.rs.drop ()

True

> db.runCommand ({create: "oplog.rs", capped:true,size: (2 * 1024 * 1024 * 1024)}) # prototype creation oplog.rs specified size

{"ok": 1}

> use admin

Switched to db admin

> db.shutdownServer () # disable the service

Server should be down...

(5) restore the independent instance mongodb2 to the replication set and log in.

> exit

Bye

[root@localhost logs] # vim / etc/mongod2.conf # restore stand-alone instance mongodb2 to replication set

Port: 27018 # change the port number back to 27018

Replication: # enable replication set

ReplSetName: abc

OplogSizeMB: 2048 # specify oplog size

[root@localhost logs] # mongod-f / etc/mongod2.conf

About to fork child process, waiting until server is ready for connections.

Forked process: 40835

Child process started successfully, parent exiting

[root@localhost logs] # mongo-- port 27018

MongoDB shell version v3.6.7

Connecting to: mongodb://127.0.0.1:27018/

Abc:SECONDARY > rs.printReplicationInfo ()

Configured oplog size: 2048MB

Log length start to end: 90secs (0.03hrs)

Oplog first event time Thu Sep 13 2018 15:44:15 GMT+0800 (CST)

Oplog last event time Thu Sep 13 2018 15:45:45 GMT+0800 (CST)

Now Thu Sep 13 2018 15:45:54 GMT+0800 (CST)

4. Deploy certified replication

(1)

Abc:PRIMARY > use admin

Switched to db admin

Abc:PRIMARY > db.createUser ({"user": "root", "pwd": "123"," roles ": [" root "]}) # create user root set password to 123

Successfully added user: {"user": "root", "roles": ["root"]}

(2) enable authentication in the configuration file of each instance

Abc:PRIMARY > exit

Bye

[root@localhost logs] # vim / etc/mongod.conf

Security:

KeyFile: / usr/bin/abckey1 # verify the file path

ClusterAuthMode: keyFile # authentication mode, file verification

[root@localhost logs] # vim / etc/mongod2.conf

Security:

KeyFile: / usr/bin/abckey2

ClusterAuthMode:keyFile

[root@localhost logs] # vim / etc/mongod3.conf

Security:

KeyFile: / usr/bin/abckey3

ClusterAuthMode:keyFile

[root@localhost logs] # vim / etc/mongod4.conf

Security:

KeyFile: / usr/bin/abckey4

ClusterAuthMode:keyFile

[root@localhost logs] # cd / usr/bin/

[root@localhost bin] # echo "abckey" > abckey1 # generate 4 key files

[root@localhost bin] # echo "abckey" > abckey2

[root@localhost bin] # echo "abckey" > abckey3

[root@localhost bin] # echo "abckey" > abckey4

(3) restart 4 instances

[root@localhost bin] # chmod 600 abc* # set file abc permissions to 600

[root@localhost bin] # mongod-f / etc/mongod.conf # start the service

About to fork child process, waiting until server is ready for connections.

Forked process: 41828

Child process started successfully, parent exiting

[root@localhost bin] # mongod-f / etc/mongod2.conf-- shutdown

Killing process with pid: 40835

[root@localhost bin] # mongod-f / etc/mongod2.conf

About to fork child process, waiting until server is ready for connections.

Forked process: 42252

Child process started successfully, parent exiting

[root@localhost bin] # mongod-f / etc/mongod3.conf-- shutdown

Killing process with pid: 4881

[root@localhost bin] # mongod-f / etc/mongod3.conf

About to fork child process, waiting until server is ready for connections.

Forked process: 42451

Child process started successfully, parent exiting

[root@localhost bin] # mongod-f / etc/mongod4.conf-- shutdown

Killing process with pid: 4909

[root@localhost bin] # mongod-f / etc/mongod4.conf

About to fork child process, waiting until server is ready for connections.

Forked process: 42634

Child process started successfully, parent exiting

(4) Log in to the primary node server to verify

[root@localhost bin] # mongo-- port 27018

MongoDB shell version v3.6.7

Connecting to: mongodb://127.0.0.1:27018/

MongoDB server version: 3.6.7

Abc:PRIMARY > show dbs # View the database on the master node

2018-09-13T16:49:14.542+0800 E QUERY [thread1] Error: listDatabases failed: {# cannot be queried

OperationTime: Timestamp (1536828545, 1)

"ok": 0

Abc:PRIMARY > rs.status () # View the status of each node, but cannot query it

{

OperationTime: Timestamp (1536828575, 1)

"ok": 0

"errmsg": "not authorized on admin to execute command {replSetGetStatus: 1.0,$ clusterTime: {clusterTime: Timestamp (1536828545, 1), signature: {hash: BinData (0, 40060B8D2AC8AC1AE68D47E9332835D2040120C2), keyId: 6600587920397041666}}, $db:\" admin\ "}

"code": 13

"codeName": "Unauthorized"

"$clusterTime": {

ClusterTime: Timestamp (1536828575, 1)

"signature": {

"hash": BinData (0, "gSi7raqiqfKJKSF42wlgu2rvggE=")

"keyId": NumberLong ("6600587920397041666")

}

}

}

Abc:PRIMARY > use admin # enter admin database

Switched to db admin

Abc:PRIMARY > db.auth ("root", "123") # for authentication

one

Abc:PRIMARY > show dbs # and then check the database

Admin 0.000GB

Config 0.000GB

Local 0.000GB

School 0.000GB

(5) enter the slave node server for verification

[root@localhost bin] # mongo-- port 27019

MongoDB shell version v3.6.7

Connecting to: mongodb://127.0.0.1:27019/

MongoDB server version: 3.6.7

Abc:SECONDARY > show dbs # View the database

2018-09-13T16:55:14.429+0800 E QUERY [thread1] Error: listDatabases failed: {

OperationTime: Timestamp (1536828905, 1)

"ok": 0

Abc:SECONDARY > rs.slaveOk ()

Abc:SECONDARY > use admin # for authentication

Switched to db admin

Abc:SECONDARY > db.auth ("root", "123")

one

Abc:SECONDARY > show dbs

Admin 0.000GB

Config 0.000GB

Local 0.000GB

School 0.000GB

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