MongoDB replica set replication

MongoDB is a popular NoSQL database. It provides the scalability, reliability and isolation of replica set replication for data. Let's talk about the configuration of replica set replication in general.

1. Environment

Centos 7
MongoDB 3.6

2.机器配置
----------------------------------------------------------------------------
IP hostname port path is arbiter
----------------------------------------------------------------------------
192.168.11.100 mongodbr0 27017 /data/replset1/r0 false
192.168.11.101 mongodbr1 27017 /data/replset1/r1 false
192.168.11.102 mongodbr2 27017 /data/replset1/r2 true

3. Configuration step
3.1 Open the port or close the firewall
firewall-cmd --permanent --add-port=27017/tcp
firewall-cmd --reload
3.2 Create a corresponding directory on each machine
mkdir -p /data/replset1/r0 |r1|r2 ##Different machines correspond to different directories, refer to the path
mkdir -p /data/replset1/key
mkdir -p /data/replset1/log in the second part

Modify the hosts
vi /etc/hosts of each machine and add the following

192.168.11.100 mongodbr0
192.168.11.101 mongodbr1
192.168.11.102 mongodbr2

3.3 Copy the MongoDB installation package to each machine, and decompress it.
For example, in this example, decompress it to /usr/local/mongodb
3.4 Initialize the replica set
and enter /usr/local/mongodb/bin/ on each machine and
execute the following commands
respectively./ mongod --port 27017 --dbpath /data/replset1/r0 --logpath /data/replset1/log/r0.log --logappnd --fork --bind_id localhost,192.168.11.100 --replSet replset1

./mongod --port 27017 --dbpath /data/replset1/r1 --logpath /data/replset1/log/r1.log --logappnd --fork --bind_id localhost,192.168.11.101 --replSet replset1

./mongod --port 27017 --dbpath /data/replset1/r2 --logpath /data/replset1/log/r2.log --logappnd --fork --bind_id localhost,192.168.11.102 --replSet replset1

Note: --bind_id must specify the bound IP, because each machine may have multiple IPs, so this parameter must be present, otherwise an error may be reported

3.5 Start copying
into any machine, take mongodbr0 as an example
3.5.1
/usr/local/mongodb/bin/mongo --port 27017
3.5.2
a. Run rs.initiate()
b. Run rs.add("mongodbr1 :27017")
c. Run rs.addArb("mongodbr2:27017") #Arbitration set

At this point, the replica set configuration is complete

3.6 Test
3.6.1 Test data replication
a. Enter mongodbr0, run /usr/local/mongodb/bin/mongo --port 27017
b. Enter test by default
c. Insert new data eg db.foo.insert({"name ":"zhangshan","gender":"Male","age":"20","address":"suzhou"})
d. Enter mongodbr1, run /usr/local/mongodb/bin/mongo --port 27017
e. Since r1 is secondary, it cannot directly access the data, you need to run rs.slaveOk() or db.getMongo().setSlaveOk()
f. Execute db.foo.find(); the data just inserted will be displayed here
3.6.2 Failover test
a. Enter mongodbr0, run /usr/local/mongodb/bin/mongo --port 27017
b. Execute use admin to enter admin
c. Execute db.shutdownServer()
d. Enter mongodbr1, run /usr/ local/mongodb/bin/mongo --port 27017
e. At this time, you will find that this machine has become primary replset1:PRIMARY 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324534599&siteId=291194637