This article takes about 9 minutes to read

An overview of the

If you’re not familiar with Docker, check out my previous post. This article mainly talks about the following four contents:

  • Preamble – Talk about the database upgrade plan
  • A master from
  • The two from the Lord
  • One master, one subordinate, one arbitration

Originally, WE wanted to use the latest MongoDB Docker image, but the latest image is bound to localhost by default (see the official website) to connect to the database, of course, we can also modify. For convenience, I will directly use Mongo 3.4, because 3.4 version does not bind localhost to save some trouble.

Preamble – Talk about the database upgrade plan

Before learning cluster, we will talk about the database upgrade plan, I personally think there are some stages as follows, if I have a mistake in cognition, also ask readers to point out.

Phase one description: early stage of development, application and the database on the same server faults: - application with database for resources - database hang up, and the application can't provide service - cannot provide data disaster backup - read and write in the same node, pressure - throughput is small, limited in their ability to provide services - trouble-free restore functionCopy the code
Description of Phase 2: The database is separated from applications on a server. Disadvantages: - The database is down and applications cannot provide services. - Data Dr And backup cannot be providedCopy the code
Description of Phase 3: The database has a master/slave structure, with one database being primary and one database being duplicate. Disadvantages: - The database is down, and applications cannot provide services. - Read and write operations are performed on the same node, resulting in high pressureCopy the code
Description of Phase 4: One master, two (multiple) slave, read/write separation Disadvantages: - Small throughput, limited service capability - Multiple database nodes, relatively high economic costCopy the code
Description of stage five: Fragmentation, horizontal expansion disadvantages: - There are many database nodes, and the economic cost increases relativelyCopy the code

A master from

version: '2'Services: master: image: the mongo: 3.4 volumes: - / data/mongodbml/master: / data/dbcommand: the mongod -- dbpath/data/db - the master slaver: image: mongo: 3.4 volumes: - / data/mongodbml/slaver: / data/dbcommand: mongod --dbpath /data/db --slave --source master:27017
    links:
      - master
Copy the code

No need to create the corresponding file directory, directly run the YML file. After running the yML file, do the following initialization: go to the master mongo command line:

docker-compose exec master mongo
Copy the code

Insert a piece of data:

use test
db.test.insert({msg: "this message is from master", ts: new Date()})
Copy the code

Enter the slaver mongo command line:

docker-compose exec slaver mongo
Copy the code

View replica set information:

rs.slaveOk()
use test
db.test.find()
Copy the code

Rs. SlaveOk () function

db.getMongo().setSlaveOk()
This allows the current connection to allow read operations to run on secondary members. See the readPref() method for more fine-grained control over read preference in the mongo shell.
Copy the code

db.test.insert({msg: 'this is from slaver', ts: new Date()})
Copy the code

Insert failed, and an error message is displayed.

Pros and cons: Master-slave structure. When the master fails, the slave is not elected as the master, so this structure only serves to back up data

The two from the Lord

version: '2'Services: rs1: image: the mongo: 3.4 volumes: - / data/mongodbtest/replset/rs1: / data/dbcommand: the mongod -- dbpath/data/db - replSet myset rs2: image: mongo: 3.4 volumes: - / data/mongodbtest/replSet/rs2: / data/dbcommand: the mongod -- dbpath/data/db - replSet myset rs3: image: mongo: 3.4 volumes: - / data/mongodbtest/replSet/rs3: / data/dbcommand: mongod --dbpath /data/db --replSet myset
Copy the code

No need to create the corresponding file directory, directly run the YML file. After running the YML file, do the following initialization:

docker-compose exec rs1 mongo
Copy the code

Initialize each node:

rs.initiate()
rs.add('rs2:27017')
rs.add('rs3:27017')
Copy the code

View configuration and copy status

rs.conf() 
rs.status() 
Copy the code

Insert information to primary node:

docker-compose exec rs1 mongo
use test
db.test.insert({msg: 'this is from primary', ts: new Date()})
Copy the code

Check whether information is synchronized in the replica set:

docker-compose exec rs2 mongo
rs.slaveOk()
use test
db.test.find()
Copy the code
docker-compose execRs3 mongo rs.slaveok () // By default only primary can read and write usetest
db.test.find()
Copy the code

Failure test:

docker-compose stop rs1
Copy the code

View the other nodes separately: Notice the master and slave identifiers after entering the mongo command line

docker-compose exec rs2 mongo
Copy the code
docker-compose exec rs3 mongo
Copy the code

The advantages and disadvantages:

  1. Can be read and write separation
  2. Have failover capability

One master, one subordinate, one arbitration

version: '2'Services: master: image: the mongo: 3.4 volumes: - / data/mongodb3node/replset/rs1: / data/dbcommand: mongod --dbpath /data/db --replSet newset --oplogSize 128
  slave:
    image: mongo:3.4
    volumes:
      - /data/mongodb3node/replset/rs2:/data/db
    command: mongod --dbpath /data/db --replSet newset --oplogSize 128
  myarbiter:
    image: mongo:3.4
    command: mongod --dbpath /data/db --replSet newset --smallfiles --oplogSize 128
Copy the code

No need to create the corresponding file directory, directly run the YML file. After running the YML file, do the following initialization:

docker-compose exec rs1 mongo
Copy the code

Initialize each node:

rs.initiate()
rs.add('slave:27017')
rs.add('myarbiter:27017'.true)// Set as a quorum nodeCopy the code

View configuration and copy status

rs.conf() 
rs.status() 
Copy the code

Insert information to primary node:

docker-compose exec rs1 mongo
use test
db.test.insert({msg: 'this is from primary', ts: new Date()})
Copy the code

Check whether information is synchronized in the replica set:

docker-compose exec rs2 mongo
rs.slaveOk()
use test
db.test.find()
Copy the code
docker-compose execRs3 mongo rs.slaveok () // By default only primary can read and write usetest
db.test.find()
Copy the code

Failure test:

docker-compose stop master
Copy the code

View information about other nodes:

docker-compose exec slave mongo
Copy the code

docker-compose exec myarbiter mongo
Copy the code

The advantages and disadvantages:

  1. Have failover capability
  2. The quorum node plays an election role and saves some resources

Check out the next article on MongoDB sharding


Follow wechat official account, reply [Docker resources], get docker basic video tutorial