Configure MongoDB configuration items in the configuration file and back up and restore the database, mongodb backup and recovery

Source: Internet
Author: User
Tags mongodump mongorestore

Configure MongoDB configuration items in the configuration file and back up and restore the database, mongodb backup and recovery
I. mongoDB command line startup option Configuration

When mongodb is started using the command line, we can select some options to change the configuration. The specific options are as follows:

1. -- dbpath: Each mongodb operation requires an independent data storage directory. If multiple mongodb instances are enabled on different ports but the data storage directory is the same, an error is returned:

 

 

 

2. -- port: Different listening ports of the program. The default value is 27017.

-- Port = 27017 modify different ports to start multiple mongdb Processes

 


 

3. -- fork: Create a daemon mongodb Process

4. -- logpath: Specify the log writing path strength.

-- Logpath = "mongodb. log" is stored in the/bin directory installed in mongodb by default.

5. -- config: Configuration File

-- Config = 'configuration file path strength and configuration file'

(1) using the configuration file will simplify startup. Because one configuration is used multiple times, a configuration file is created in the directory. The configuration file creates an instance as follows:

# "#": Its annotation Function

Dbpath = E: \ DB \ mongodb \ data

Port = 27017

Fork = true

Logpath = mongdb. log

Logappend = true

// By default, logs are cleared from the previous logs. If you want to append logs, you can use logappend = true.

Start the process after Configuration:

 

6. Disable the service: db. shutdownServer ()

 

Ii. data backup and recovery:

Mongo Database Backup Recovery

1. file backup (cold backup)

Back up the data directory of mongodb directly. You need to stop the mongo server from copying Data Directories for complete and effective backup.

2. Tool backup (hot backup)

Mongodump exports data in binary format,

Mongoexport exports data in csv or json format. mongodump can export a database or all databases on the MongoDB service. Therefore, mongodump is a larger backup tool.

(1) mongodump back up a database

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


// The log shows that mongodump generates a directory. The first directory is the database name, And the next layer is the backup file corresponding to each set.


(2) A collection in the backup database

Mongodump-h 127.0.0.1-d test-c mytest-o E: \ DB \ mongodb \ data. dmp

 

// Mongodump and the-c (full name: collection) parameter can be used to back up a specified set.


(3) back up the entire instance

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

// Mongodump if the-d parameter is not specified, the entire MongoDB instance will be backed up. Under the data. dmp directory, a directory with the same number of databases as the mongo instance is generated. The directory names correspond to the database names on the MongoDB instance respectively.

 

Restore: Use the mongorestore command

(1) restore a database

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

-D: database name used

Add the directory you just exported to directly restore all tables.

If-c is used to restore a table

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

// Import the data in the specified directory to the resdb database.


// After the import is successful, log on to the production database: show dbs

 


(2) restore a set

Mongorestore-ddbname-ccollectinname E: \ DB \ mongo \ data

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

// Import the export set mytest. bson under the specified directory to the restesss set under the test database

 

// After the import, we log on to the production and check all the sets in the database test: db. getCollectionNames (). We found that there is an imported set.

 

Iii. Export and Import

1. Export tool export Export

The export tool in Mongodb can export a collection to a file in JSON or CSV format. You can use parameters to specify the data items to be exported, or export data based on the specified conditions. The usage of mongoexport is as follows:

(1), export -- help // view the usage of the export tool

Parameter description:

-H: Specifies the IP address of the Database Host.

-U: Specifies the database user name.

-P: Specifies the Database Password.

-D: Specifies the database name.

-C: Specifies the collection name.

-F: Specifies the columns to export.

-O: Specifies the file name to be exported.

-Q: specify the criteria for filtering the exported data.

 

Instance: There is a students set in the test database:

This document contains three fields: classid, age, and name.

(2) Export export-d test-c students-o students. dat

After the command is executed, check that a students. dat file is generated in the directory.

Parameter description:

-D: Specifies the library used. In this example, It is test.

-C: Specifies the set to be exported. In this example, It is students.

-O: Specifies the file name to be exported. In this example, It is students. dat.

// As shown in the preceding results, the specified export style is not displayed when exporting data. By default, data in JSON format is exported. To export CSV data, use the -- csv parameter as follows:

 

(3) Export export-d test-c students -- csv-f classid, name, age-o students_csv.dat

Parameter description:

-Csv: indicates that the file to be exported is in csv format.

-F: Specifies the data of the three columns: classid, name, and age to be exported.

// As shown in the preceding results, export successfully exported data to the students_csv.dat file in csv format.

 

2. Import tool Export Import

The volume import tool in Mongodb can import the content in a specific format file to the specified collection. This tool can import JSON or CSV data. The specific usage is as follows:

(1) Export Import -- help

Parameter description:

-H: Specifies the IP address of the Database Host.

-U: Specifies the database user name.

-P: Specifies the Database Password.

-D: Specifies the database name.

-C: Specifies the collection name.

-F: Specifies the columns to import.

Instance:

(2) Export Import-d test-c students. dat

Parameter description:

-D: Specifies the database name, which is test in this example.

-C: Specifies the collection name. In this example, It is students.

Students. dat: Imported file name

// The preceding example shows how to import the content in a JSON file. to import the content in a CSV file, use the -- type parameter to specify the import format, as shown below:

Import the previously exported students_csv.dat file.

(3) Export Import-d test-c students -- type csv -- headerline -- file students_csv.dat

Parameter description:

-Type: Specifies the format of the file to be imported.

-Headerline: specifies that the first row is the column name and does not need to be imported.

-File: Specifies the file to be imported.

 

4. fl data modified in the memory to the disk

1. fsync and lock

Force refresh all changes to the disk and maintain a global lock. Then copy the data directory for backup. The system performance is affected during refresh, And the read is blocked during the lock process.


> Use admin

Switched to db admin

> Db. runCommand ({"fsync": 1, "lock": 1 });

{

"Info": "now locked against writes, use db. fsyncUnlock () tounlock ",

"SeeAlso ":

"OK": 1

}

Restore

-- Repair release d startup option. After the release of release d is abnormal, you can use the repair parameter to start it. Repair Process: verify all data, ignore corrupted data, and recreate all indexes. At the same time, the data space is organized.

Online repair:

> Use db

Switched to db

> Db. repairDatabase ()

{"OK": 1}

>

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.