MongoDB master-slave configuration and backup

Source: Internet
Author: User
Tags mkdir mongodb mongodump mongorestore

This article will introduce the next MongoDB master-slave configuration and backup

MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for WEB applications.
MongoDB is a product between a relational database and a non-relational database, and is the most versatile and most like relational database in a non-relational database.

Implementation principle of Master-slave server

First, the master node will record the service's write-related operations, read and do not record, these operations are recorded in the local database Oplog. $admin This collection, this is a fixed set, size is configurable, mainly through the configuration oplogsize this parameter to implement, Unit is m, the size of the disk is generally about 5% of the remaining space. Because it is a fixed set so when the fixed set is full of logs, the new log will overwrite the oldest log, if this value is set unreasonable, resulting in data quickly overwritten, and from the node has not been updated, This results in a situation where data is out of sync. The local database that is set as the primary node has oplog. $admin and slave. The slave records the information from the node.
The data synchronization from the node and the master node is mainly from the node to connect the master node, request the operation log of the master node, and perform the same operation on the data sub-table to achieve the synchronization of the data. From the local database of the node, there are two collections of source and me. Source is the information that records the master node, me is the identity of the record from the node

Environment configuration
我这里准备了2台机器作为演示机172.16.0.138    master172.16.0.139    salve系统版本为 centos 7.2
Deployment Package Address

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.3.tgz

Unzip the file
[[email protected]]# tar xf mongodb-linux-x86_64-rhel70-3.6.3.tgz [[email protected]]# cd mongodb-linux-x86_64-rhel70-3.6.3/
Create a Data Catalog
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/data -p[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/logs -p
Configuration file Modification
# 设置数据文件的存放目录dbpath=/opt/mongodb/data# 设置日志文件的存放目录及其日志文件名logpath=/opt/mongodb/logs/mongodb.log# 设置端口号(默认的端口号是 27017)master=true# 设置为以守护进程的方式运行,即在后台运行fork=true#监听网卡bind_ip= 0.0.0.0#服务端口port=27017oplogSize=2048
Start the main library
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongod -f mongodb.conf  about to fork child process, waiting until server is ready for connections.forked process: 5702child process started successfully, parent exiting[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ps -ef |grep mongodbroot      5702     1  2 14:38 ?        00:00:00 ./bin/mongod -f mongodb.confroot      5729  5601  0 14:39 pts/0    00:00:00 grep --color=auto mongodb
Configure from Library
部署包从主库上拷贝过来
Create a Data Catalog
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/data -p[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/logs -p
Modifying a configuration file
# 设置数据文件的存放目录dbpath=/opt/mongodb/data# 设置日志文件的存放目录及其日志文件名logpath=/opt/mongodb/logs/mongodb.log# 设置为以守护进程的方式运行,即在后台运行fork=true#服务端口port=27017bind_ip= 0.0.0.0slave=truesource=172.16.0.138:27017autoresync=true
Start from library
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongod -f mongodb.conf about to fork child process, waiting until server is ready for connections.forked process: 10233child process started successfully, parent exiting[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ps -ef |grep mongodbroot     10302     1  0 15:03 ?        00:00:09 ./bin/mongod -f mongodb.confroot     10369 10210  0 15:38 pts/0    00:00:00 grep --color=auto mongodb到这里基本主从就配置完了,你可以查看主节点的local数据库里有没有slave,oplog.$admin,从节点中有没有source,me这几个集合接下来你可以主节点创建数据库插入数据看看从节点是否同步过去了.这些都可以通过查看日志来查看的
Validation results
    • Main Library
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongoMongoDB shell version v3.6.3connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.6.3> db.printReplicationInfo();configured oplog size:   2048MBlog length start to end: 1517secs (0.42hrs)oplog first event time:  Mon Apr 16 2018 14:38:53 GMT+0800 (CST)oplog last event time:   Mon Apr 16 2018 15:04:10 GMT+0800 (CST)now:                     Mon Apr 16 2018 15:04:11 GMT+0800 (CST)
    • From the Library
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongoMongoDB shell version v3.6.3connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.6.3Server has startup warnings: > db.printSlaveReplicationInfo()source: 172.16.0.138:27017    syncedTo: Mon Apr 16 2018 15:04:30 GMT+0800 (CST)    7 secs (0 hrs) behind the freshest member (no primary available at the moment)
Synchronization test
    • Inserting data on the main library
> use testsalveswitched to db testsalve> dbtestsalve> db.testsalve.insert({"name" : "测试同步"})WriteResult({ "nInserted" : 1 })> show collectionstestsalve> db.testsalve.find().pretty(){ "_id" : ObjectId("5ad44d090dd23b7a5c1a983f"), "name" : "测试同步" }
    • See if syncing from the gallery
> rs.slaveOk();> show dbs;admin      0.000GBconfig     0.000GBlocal      0.000GBtestsalve  0.000GB> use testsalveswitched to db testsalve> db.testsalve.find().pretty(){ "_id" : ObjectId("5ad44d090dd23b7a5c1a983f"), "name" : "测试同步" }# 数据已同步过来
Attention
salve节点默认是无法读写的,如果非要解决,方法如下:在从库执行> rs.slaveOk();
Backup
    • MongoDB Database Backup
1、语法:        mongodump -h dbhost -d dbname -o dbdirectory        参数说明:            -h: MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017            -d: 需要备份的数据库实例,例如:test            -o: 备份的数据存放位置,例如:/home/mongodump/,当然该目录需要提前建立,这个目录里面存放该数据库实例的备份数据。
    • Example
#以下命令备份了testsalve库到/opt/目录下[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongodump -h 127.0.0.1:27017 -d testsalve -o /opt/2018-04-16T15:19:54.779+0800    writing testsalve.testsalve to 2018-04-16T15:19:54.780+0800    done dumping testsalve.testsalve (1 document)#备份出来的文件[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ll /opt/testsalve/total 8-rw-r--r-- 1 root root  45 Apr 16 15:19 testsalve.bson-rw-r--r-- 1 root root 133 Apr 16 15:19 testsalve.metadata.json
Recovery
    • MongoDB Database Recovery

1、语法: mongorestore -h dbhost -d dbname --dir dbdirectory 参数或名: -h: MongoDB所在服务器地址 -d: 需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2 --dir: 备份数据所在位置,例如:/opt --drop: 恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除。

    • Example
#先删除当前库 > Use testsalveswitched to DB testsalve> db.dropdatabase () {"Dropped": "Testsalve", "OK": 1} #执行恢复 [[EMAIL&N Bsp;protected] mongodb-linux-x86_64-rhel70-3.6.3]#./bin/mongorestore-h 127.0.0.1:27017-d testsalve--dir/opt/ testsalve/2018-04-16t15:23:27.416+0800 the--db and--collection args should only being used when restoring from a BSON fi Le. Other uses is deprecated and would not be exist in the future; Use--nsinclude instead2018-04-16t15:23:27.417+0800 building a list of collections to restore From/opt/testsalve DIR20 18-04-16t15:23:27.418+0800 reading metadata for Testsalve.testsalve from/opt/testsalve/ testsalve.metadata.json2018-04-16t15:23:27.440+0800 restoring Testsalve.testsalve from/opt/testsalve/ testsalve.bson2018-04-16t15:23:27.449+0800 no indexes to restore2018-04-16t15:23:27.449+0800 finished restoring test Salve.testsalve (1 document) 2018-04-16t15:23:27.449+0800 done# view restore results > show dbs;admin 0.000GBconfig 0.000GBl Ocal 0.000GBtestsalve 0.000gb> use testsalveswitched to DB testsalve> db.testsalve.find (). Pretty () {"_id": ObjectId ("5ad44d0 90dd23b7a5c1a983f ")," name ":" Test Synchronization "}

MongoDB master-slave configuration and backup

Related Article

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.