Objective
This article mainly relates to the content: database backup and recovery, for any database, backup is crucial; data import and export; database repair; Database status monitoring tool mongostat; Other commands are summarized here when using the process.
1 . Backup and recovery of databases
1.1 Copying data files
backup : You must use this method to ensure that the data file does not change, you can use the Fsynclock command,> Db.fsynclock ()
Once the command is run, the database is locked, any writes are suppressed, and the data is synchronized to the disk. When the command returns, you can copy the data file, such as backing up a database named MyDB, just copy all files named mydb.*, and if you use the-DIRECTORYPERDB option, simply copy the entire data directory for that database. After the copy is complete, use > Db.fsyncunlock () to unlock the database.
Note: If authentication is enabled, do not shut down the shell during calls to Fsynclock and Fsyncunlock or you can only restart the Mongod service.
Recovery : Ensure that the Mongod service does not start, the directory to be recovered is empty, copy the backed up data files to the database directory, and finally start the Mongod service.
1.2 Use Mongodump
using Mongodump, you can back up the database separately, but the backup and recovery is slow. You can use Mongodump–help to view available options.
Backup:
Backing up the entire Foo database
Mongodump–h 127.0.0.1:30000–d Foo–o/data/dbback/foo
Back up the Bar collection in the Foo database
mongodump–h 127.0.0.1:30000–d foo–c Foo.bar–o/data/dbback/foo/bar
Back up multiple collections, Bar,info collections in Foo, separated by commas
mongodump–h 127.0.0.1:30000–d foo–c foo.bar, Foo.infi–o/data/dbback/foo/cols
Recovery:
With the Mongorestore command, there are two options worth mentioning:
--drop: Deletes an existing collection before recovery, or directly into an existing collection
--noobjcheck: Ignore validation steps before inserting objects into a collection
Recovering a single database
mongorestore–h 127.0.0.1:30000–d foo–dir/data/dbback/foo
Recovering a single collection
mongorestore–h 127.0.0.1:30000–d foo–c Foo.bar–dir/data/dbback/foo/bar
Recovering multiple collections
mongorestore–h 127.0.0.1:30000–d foo–c foo.bar,foo.infi–dir/data/dbback/foo/cols
1.3 Backup Replica set
Backup directly using the copy data file , which does not require any modification when backing up the replica set.
1.4 backing up a shard cluster
First step: Turn off the equalizer,
Step Two: Connect MONGOs perform Mongodump backup
Step three: Enable the Equalizer,
If you restore a backup, use Mongorestore to connect to a MONGOs to execute
The same as backing up a single shard and backing up a single server.
2 . Import and export of data
Data import and Export is two commands: Mongoimport and Mongoexport
Mongoimport, you can import CSV, TSV, or JSON-formatted data into MongoDB, with a few useful options:
--headerline: For CSV,TSV format files, use the first row as the field name
--drop: Delete collection before import, or append after collection
--ignoreblanks: Empty fields are not imported
Mongoimport–h 127.0.0.1:30000–d foo–c Foo.bar–type csv–headerline < CSVFile
Mongoexport, you can export MongoDB data to a file in json,csv format. A few of these useful options:
--q: Specify query criteria to export data, default to {}
--f: Specify the name of the exported field
Mongoexport–h 127.0.0.1:30000–d foo–c foo.bar–q {}–type=csv > Bar.csv
3 . Database Status Monitoring
use Mongostat to output the current status of the service per second, output the main field description table, and use method Mongostat–port 30000.
use db.currentop () to see what actions the database is currently performing.
Table 1 Mongostat Field Description
Insert |
Number of insertions per second |
Query |
Number of queries per second |
Update |
Number of updates per second |
Delete |
Number of deletions per second |
Getmore |
Number of query cursors per second Getmore |
Command |
Command tree executed per second, in addition to additions and deletions, there are other statistical commands |
Flushes |
Number of times per second that Fsync data is written to disk |
Mapped |
The amount of data that is mapped into memory by mmap, in units of M |
Vsize |
Virtual memory usage, Unit m |
Res |
Amount of physical memory used, Unit m |
Faults |
The number of access failures per second, when the faults is high, is generally the following for several reasons: 1. Insufficient disk space 2. Insufficient memory 3. Disk IO reaches bottleneck |
Qr|qw |
Indicates read, write queue, high concurrency when queue values generally rise |
Ar|aw |
Indicates the number of active read, write clients |
Netin |
Number of bytes per second entered over the network |
Netout |
Bytes output over the network per second |
Conn |
Number of connections opened by the server, including input and output connections |
mongodb-Server Management