MONGO database master-slave backup service cluster construction

Source: Internet
Author: User
Tags mongodb mongodb server

MongoDB master-slave backup

Environment : CentOS 7, MongoDB x86_64

MongoDB official website download page :https://www.mongodb.com/download-center#community

Download finished, extract to the specified directory, I will now put it in the/opt directory (green version, do not install)

In bin/This directory, is the relevant tool of MongoDB,

Common Tool Description:

Bsondump--Dumps files in Bson format to JSON-formatted data

MONGO--mongo Client (for connection to MongoDB)

Mongod--MONGODB data plus Primary server (for booting MONGO)

Mongodump--mongodb Database Backup tool

Mongoimport--For importing data files to MongoDB

Mongostore--recovery

Just a few simple lists ...

MongoDB is to specify the location of the database files and log files, so I created three folders with the same data directory structure on the/home directory

Note: MongoDB constructs a master-slave backup with at least 3 server data, otherwise the master-slave backup cannot be implemented. Here I will only use the same host, specify a different port and implementation

Three MongoDB server master-slave backup, in production now as long as the IP to the real IP is OK

Command:

mongod--bind_ip server_ip --port Port --dbpath data File save path --logpath log file/mongodb.log C7>--replset server cluster name

Three MONGO database server started successfully,

Note: The first port started on: 1000, data file saved in/home/data1/db

Second console port in: data file saved in/home/data2/db

Third console port in the /home/data3/db data file saved in

Key: Last parameter:--replset [name] The name after this parameter is the same, otherwise it cannot be communicated

The server is built successfully, then the configuration is initialized:

Use MONGO--host IP--port to connect to servers on the service

Connect up to three MongoDB servers and choose one of the initialization options:

Note: There is nothing you can do if you do not enter the initialization and configuration at this time

Hint slaveok= false

Initialize command:

Rs.initiate ()

Initial success, check server status:

To view the current status of the server:

Rs.status ()

You can see "statestr": "PRIMARY" see this option to indicate that this server is already a primary server, and then simply add the IP and port from the server to this server

To add a replica set to the primary server:


Rs.add (' From server IP: Port ')

Successfully added two slave servers:

Note: Above as long as no error on the successful addition, you can see the lower left corner of the cursor:mserver:primary> that it is the primary server

At this point: at the end of the configuration in the master server, switch to execute from server: Rs.slaveok () This command is done.

After the primary server is initialized and the slave is successfully added

Executing from the server:

Rs.slaveok ()

Final Result:

Note: Only the primary server can write data, from the server can only read, when the primary server rock, from the server randomly one automatically switch to the main server, and the Rock Machine

When the server is repaired, the boot automatically switches to the slave server and backs up the missing data from the primary server.

  


MONGO database master-slave backup service cluster construction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.