MongoDB configuration items are configured in the configuration file and the database is backed up and restored

Source: Internet
Author: User
Tags mongodump mongorestore

One, MongoDB command line startup option configuration

MongoDB command line start when we can choose some options to change the configuration, the specific options are as follows:

1 、--dbpath: Each mongodb will need a separate data storage directory, if you start multiple MongoDB on different ports, but the data storage directory is the same error:

2 、--Port: Is the program's different listening port, the default is 27017

--port=27017 modifying different ports to start multiple MONGDB processes


3 、--Fork: Create a daemon MongoDB process

4 、--LogPath: Specifies the path that the log writes out

--logpath= "Mongodb.log" is placed by default in the/bin directory of the MongoDB installation

5 、--Config: Configuration file

--config= ' profile path and configuration file '

(1), the use of the configuration file will simplify the start, because once the configuration is used multiple times, in the directory to create a configuration file, the configuration file creation example is as follows:

# "#" its comment function

Dbpath=e:\db\mongodb\data

port=27017

Fork=true

Logpath=mongdb.log

Logappend=true

By default, the log clears the previous log, writes, and if you want to append, you can use the Logappend=true

To start the process after configuration:

6. Close service: Db.shutdownserver ()

Second, the data backup recovery:

MONGO Database Backup Recovery

1. File backup (Cold)

Directly back up the MongoDB data directory. You need to stop MONGO server copy data directory for a full and effective backup.

2. Tool Backup (hot standby)

Mongodump is to export the data in binary form,

The data format exported by Mongoexport is in CSV or JSON format; mongodump can export a database, or all the databases on the entire MongoDB service, so Mongodump is a much larger backup tool.

(1), mongodump back up a database

Mongodump-h 127.0.0.1-d Test-o E:\DB\mongodb\data\testdump.dmp


As seen from the log, the results of mongodump generate a directory, the first level directory is the database name, and the next layer is the corresponding backup file for each collection.


(2), back up a collection in the database

mongodump-h 127.0.0.1-d Test -C mytest-o E:\DB\mongodb\data. DMP


Mongodump plus the-C (full name: Collection) parameter backs up the specified collection.


(3), back up the entire instance

Mongodump-h 127.0.0.1-o E:\DB\mongodb\data.dmp

Mongodump If you do not specify the-d parameter, the entire MongoDB instance is just backed up. A directory with the same number of databases in the MONGO instance is generated under the directory data. DMP , and the directory name corresponds to the database name on the instance of MongoDB.

Recovery: Using the Mongorestore command

(1), restore a database

Mongorestore-d dbname E:\DB\mongo\data

-d database name used

Add directly to the directory you just exported, so that all tables are restored directly

If-C is the recovery of a table

Example: mongorestore-d resdb E:\DB\mongodb\data\dump.dmp\test

is to import the data from the specified directory into the RESDB database, and if not, it will be automatically created


After the import is successful we log in to the production database: Show DBS


(2), restore a collection

m ongorestore-d Dbname-ccollectinname E:\DB\mongo\data

Example: Mongorestore-h 127.0.0.1-d test-c restesss E:\DB\mongodb\data\test\mytest.bson

Imports the exported collection Mytest.bson under the specified directory into the Restesss collection under the test database

After the import we look at all the collections under test this database: Db.getcollectionnames (), we found that there are imported collections

third, export import

1. Export tool Mongoexport

The Mongoexport tool in MongoDB can export a collection to a file in JSON or CSV format. You can specify the exported data item by parameter, or you can export the data based on the specified criteria. The specific usage of Mongoexport is as follows:

(1), Mongoexport--help//view Mongoexport tool how to use

Parameter description:

-H: Indicates the IP of the database host

-U: Indicates the user name of the database

-P: Indicates the password for the database

-D: Indicates the name of the database

-C: Indicates the name of the collection

-F: Indicates which columns to export

-O: Indicates the file name to be exported

-Q: Indicates the filtering criteria for the exported data

Example: A students collection exists in the test library:

There are 3 fields in the document: ClassID, age, name

(2), mongoexport-d test-c students-o Students.dat

After the command is executed, a Students.dat file is generated under the Discovery directory.

Parameter description:

-D: Indicates which library to use, in this case test

-C: Indicates the collection to be exported, in this case students

-O: Indicates the name of the file to be exported, in this case Students.dat

As can be seen from the above results, we did not display the specified export style when exporting the data, and the data in JSON format was exported by default. If we need to export data in CSV format, we need to use the--csv parameter, as follows:

(3), mongoexport-d test-c students--csv-f classid,name,age-o Students_csv.dat

Parameter description:

-csv: Indicates that you want to export to CSV format

-F: Indicates that data for the 3 columns of ClassID, name, and age need to be exported

As can be seen from the above results, Mongoexport successfully exported the data to the Students_csv.dat file according to the CSV format.

2. Import Tool Mongoimport

The Mongoimport tool in MongoDB can import content from a particular format file into the specified collection. The tool can import JSON-formatted data or import CSV format data. The specific use is as follows:

(1), Mongoimport--help

Parameter description:

-H: Indicates the IP of the database host

-U: Indicates the user name of the database

-P: Indicates the password for the database

-D: Indicates the name of the database

-C: Indicates the name of the collection

-F: Indicates which columns to import

Instance:

(2), mongoimport-d test-c students Students.dat

Parameter description:

-D: Indicates the database name, in this case test

-C: Specify collection name, in this case students

Students.dat: Imported file name

The above shows the contents of the file imported in JSON format, if you want to import the contents of the CSV format file, you need to specify the import format through the--type parameter, as follows:

Re-import the previously exported Students_csv.dat file

(3), mongoimport-d test-c students--type csv--headerline--file students_csv.dat

Parameter description:

-type: Indicates the file format to import

-headerline: Indicates that the first row is a column name and does not need to be imported

-file: Indicates the file to import

Four, brush the memory of the modified data to disk

1. Fsync and lock

Forces all changes to disk to be refreshed, and maintains a global lock. Then copy the data directory for backup. During a refresh, the system performance is affected and the lock is blocked during the read.


> Use admin

Switched to DB admin

>db.runcommand ({"Fsync": 1, "Lock": 1});

{

"Info": "Now locked against writes, use Db.fsyncunlock () Tounlock",

"Seealso":

"OK": 1

}

Recovery

--repair mongod startup option, Mongod can be started with the repair parameter after exiting unexpectedly. Fix the process: Verify all the data, ignore the corrupted data, and rebuild all the indexes. At the same time, the data space is collated.

Online Repair:

> Use DB

Switched to DB DB

>db.repairdatabase ()

{"OK": 1}

>

MongoDB configuration items are configured in the configuration file and the database is backed up and restored

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.