MongoDB database backup and recovery operation example _mongodb

Source: Internet
Author: User
Tags auth mongodb mongodump mongorestore

It's written in front.

This article has already assumed that you have installed the MongoDB (2.6) and has opened the auth.

User

First we add the users needed to back up and recover the data, and this user needs to have ReadWrite and Useradmin permissions

Copy Code code as follows:

$ MONGO
$ use admin
$ db.auth ("admin", "youradminpasswd");
$ use backupdb
$ db.adduser ({User: "Backup", pwd: "passwd", Roles: ["ReadWrite", "Useradmin"]})

Backup

Note: This command is executed in the console
We use Mongodump for data backup (note: Mongodump does not back up the content in the local database).

Mongodump can be backed up by data in the following two ways:

Connect to Mongod or MONGOs

Direct access to data files

The tool can back up the entire server, a single database, or a single collection data, or only some of the data in the collection through a query statement.

If you execute mongodump directly without any parameters, it connects to the MongoDB instance on the local (127.0.0.1 or localhost) 27017 port and creates a backup named dump.

Copy Code code as follows:

$ mongodump--host mongodb.example.net--port 27017--db test--collection Some--username backup--password passwd

This will enable Mongodump to connect to the Mongod on the mongodb.example.net:27017 and back up somecollection in dbtest to the dump directory

Note: Starting with the Mongodb2.2 version, the data format used by Mongodump is incompatible with the Mongod instance of the previous version. So don't use the new version (>=2.2) Mongodump to back up old data.


Recovery

Data backed up using mongodump needs to be recovered using Mongorestore.

Mongorestore Data recovery methods and mongodump correspond to, but also divided into two kinds:

Connect to Mongod or MONGOs

Write directly to the data file

Mongorestore can either restore the entire backup or recover a portion of it.

Copy Code code as follows:

$ mongorestore--host mongodb.example.net--port 27017--db test--collection Some--username backup--password password/ Data/backup

The above data is recovered from the/data/backup, which restores only the TestDB somecollection to mongodb.example.net:27017. If you do not specify--host and--portoption, then Mongorestore will use localhost:27017 by default.

Note: When restoring, you must create new data to be restored on the database that you want to restore and the user--drop you belong to delete the original database in the library and restore the database up
This command is performed in the console

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.