Get the App
SLTechnology News&Howtos  ›  Servers  › 

Yum installation mongodb configuration replication set (master-slave replication)

Shulou Source: shulou.com Published: 2022-06-03 00:08:17 09月19日 Update

one。 Configure yum Warehouse

Vim / etc/yum.repos.d/mongodb-org-4.2.repo # New Warehouse File

[mongodb-org-4.2] name=MongoDB Repositorybaseurl= https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

The function of several bags

1.mongodb-org-server: the main program contains the MongoDB daemon and related configuration and initialization scripts.

2.mongodb-org-mongos: a daemon that contains mongos. (used by slicing sub-library)

3.mongodb-org-shell: contains mongo shell.

4.mongodb-org-tools: tools containing MongoDB: mongoimport, bsondump, mongodump, mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop.

two。 Install yum install mongodb-org-y

three。 Configure replication set

First, install the two nodes. When configuring the configuration of each node, the configuration is different from the listening IP, and the configuration is the same.

Vim / etc/mongo.conf

SystemLog: destination: file logAppend: true path: / var/log/mongodb/mongod.logstorage: dbPath: / var/lib/mongo journal: enabled: trueprocessManagement: fork: true # fork and run in background pidFilePath: / var/run/mongodb/mongod.pid # location of pidfile timeZoneInfo: / usr/share/zoneinfonet: port: 27017 bindIp: 192.168.0.11replication: # enable the replication set function replSetName: test-rc # the name must not be written in the top box or an error will be reported

Mongo-host 192.168.0.9 # enter the node

Rs.status () # View replication set status

Both nodes show that the replication set starts normally as shown in the figure above.

Cfg= {"_ id": "test-rc", "members": [{"_ id": 0, "host": "192.168.0.9 id 27017"}, {"_ id": 1, "host": "192.168.0.11 test-rc 27017"}]}

# define initialization parameters to add two host hosts to the test-rc replication set

Rs.initiate (cfg) # starts the replication set feature (initializes the configuration to ensure that there is no data from the slave node)

Rs.status () to view replication set status

Third, add new nodes and delete nodes

Now the new node 192.168.0.13 has installed mongodb and added the replication set to the configuration and started it.

Log in to the primary node

Rs.add ("192.168.0.13 27017") # add a new node

Re.status ()

Looking at the replication set status, you can see that the 192.168.0.13 node is already online.

Rs.remove ("192.168.0.13 27017") # Delete a node

If you look at it with rs.status (), you can find that there is no information about the 192.168.0.13 node.

Fourth, manually switch nodes

Rs.freeze (30) # suspend for 30s not to participate in the election

Rs.stepDown (6010) # hand over the location of the master node, maintain the slave node state for not less than 60 seconds, and wait 30 seconds to synchronize the master node and slave node logs (for switching)

Fifth, the slave node is readable.

Db.getMongo () .setSlaveOk () # sets slave node readability

Primary node

Slave node

6. Backup and restore

Mongodump-- port 20000-- host 192.168.0.9-d testdb-o / root/test

# only back up the testdb database and back up to the / root/test/ path. Note that there are two--

Mongorestore-d K1 / root/test/testdb/

# restore the backup data in / root/test/testdb/ to K1 database

Mongodump-- port 20000-- host 192.168.0.9-o / root/all

# back up all data

Mongoexport-- port 20000-- host 192.168.0.9-d testdb-c table1-o / root/table1.json

# only back up the table1 table in the testdb database

Mongoimport-- host 192.168.0.14-d K1-c table2-- file=/root/table1.json

# restore / root/table1.json data to K1 database table2 table

Tags: Node configuration backup data two status warehouse function database process install switch image above host main program location function information sub-library parameters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi vpn Apple OPPO Reno