In the above article, we learned how to use MySQL to learn MongoDB authorization and permissions. In this article, we will continue our learning journey and learn how to back up and restore the two.
It is important to back up your database when database tables are lost or damaged. In the event of a system crash, you must be able to restore as much data as possible from your table to the State at the time of the crash.
1. MySQL backup and recovery
MySQL backup methods are generally divided into the following three types:
Directly copy database files
Back up a database using mysqlhotcopy
Back up a database using mysqldump
(1) directly copy database files
It is the most direct, fast, and convenient, but basically cannot implement Incremental backup. To ensure data consistency, run the following SQL statement before the back-to-back file:
Flush tables with read lock;
That is, the data in the memory is refreshed to the disk, and the data table is locked to ensure that no new data is written during the copy process. In this way, the backup data recovery is also very simple, just copy it back to the original database directory.
However, for an Innodb table, you also need to back up its log file, that is, the ib_logfile * file. Because when the Innodb table is corrupted, these log files can be recovered.
(2) Use mysqlhotcopy to back up the database
Mysqlhotcopy is a perl program. It uses lock tables, flush tables, and cp or scp to quickly back up databases. It is the fastest way to back up a database or a single table, but it can only run on a local server, and mysqlhotcopy can only back up MyISAM tables, there is no way to use Innodb tables.
(3) Use mysqldump to back up the database
Mysqldump is an SQL-level backup. It imports data tables into SQL script files and is suitable for upgrading between different MySQL versions. This is also the most popular backup method.
2. MongoDB backup and recovery
MongoDB provides two commands to back up (mongodump) and recover (mongorestore) databases.
(1) mongodump Backup Tool
Let's take a look at the help information of this tool:
- [root@localhost bin]# ./mongodump --help
- options:
- --help produce help message
- -v [ --verbose ] be more verbose (include multiple times for more
- verbosity e.g. -vvvvv)
- -h [ --host ] arg mongo host to connect to ( /s1,s2 for
- sets)
- --port arg server port. Can also use --host hostname:port
- --ipv6 enable IPv6 support (disabled by default)
- -u [ --username ] arg username
- -p [ --password ] arg password
- --dbpath arg directly access mongod database files in the given
- path, instead of connecting to a mongod server -
- needs to lock the data directory, so cannot be used
- if a mongod is currently accessing the same path
- --directoryperdb if dbpath specified, each db is in a separate
- directory
- -d [ --db ] arg database to use
- -c [ --collection ] arg collection to use (some commands)
- -o [ --out ] arg (=dump) output directory or "-" for stdout
- -q [ --query ] arg json query
- --oplog Use oplog for point-in-time snapshotting
- --repair try to recover a crashed database
- [root@localhost bin]#
For example, there is a database named "foo" in our system. Below we will demonstrate how to back up this database:
- [root@localhost bin]# ./mongodump -d foo -o /data/dump
- connected to: 127.0.0.1
- DATABASE: foo to /data/dump/foo
- foo.system.indexes to /data/dump/foo/system.indexes.bson
- 3 objects
- foo.system.users to /data/dump/foo/system.users.bson
- 1 objects
- foo.t2 to /data/dump/foo/t2.bson
- 1 objects
- foo.t1 to /data/dump/foo/t1.bson
- 2 objects
- [root@localhost bin]#
After the tool returns information, we can see that the data in foo has been backed up into a bson file. Next we will verify it in the backup directory:
- [Root @ localhost dump] # ll/data/dump/foo/
- Total 16
- -Rw-r -- 1 root 193 04-22 :55 system. indexes. bson
- -Rw-r -- 1 root 91 04-22 :55 system. users. bson
- -Rw-r -- 1 root 66 04-22 :55 t1.bson
- -Rw-r -- 1 root 49 04-22 :55 t2.bson
- [Root @ localhost dump] #
The results show that the table in the foo database has been successfully backed up. Next we will demonstrate how to restore the backup back.
(2) mongorestore restoration Tool
Let's take a look at the help information of this tool:
- [root@localhost bin]# ./mongorestore --help
- usage: ./mongorestore [options] [directory or filename to restore from]
- options:
- --help produce help message
- -v [ --verbose ] be more verbose (include multiple times for more
- verbosity e.g. -vvvvv)
- -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)
- --port arg server port. Can also use --host hostname:port
- --ipv6 enable IPv6 support (disabled by default)
- -u [ --username ] arg username
- -p [ --password ] arg password
- --dbpath arg directly access mongod database files in the given
- path, instead of connecting to a mongod server -
- needs to lock the data directory, so cannot be used
- if a mongod is currently accessing the same path
- --directoryperdb if dbpath specified, each db is in a separate
- directory
- -d [ --db ] arg database to use
- -c [ --collection ] arg collection to use (some commands)
- --objcheck validate object before inserting
- --filter arg filter to apply before inserting
- --drop drop each collection before import
- --oplogReplay replay oplog for point-in-time restore
- [root@localhost bin]#
For example, we deleted the "foo" database first:
- [root@localhost bin]# ./mongo
- MongoDB shell version: 1.8.1
- connecting to: test
- > use foo
- switched to db foo
- > db.dropDatabase();
- { "dropped" : "foo", "ok" : 1 }
- > show dbs
- admin 0.0625GB
- local (empty)
- test 0.0625GB
- >
Next we will demonstrate how to restore this database:
- [root@localhost bin]# ./mongorestore --directoryperdb /data/dump
- connected to: 127.0.0.1
- Sun Apr 22 12:01:27 /data/dump/foo/t1.bson
- Sun Apr 22 12:01:27 going into namespace [foo.t1]
- Sun Apr 22 12:01:27 2 objects found
- Sun Apr 22 12:01:27 /data/dump/foo/t2.bson
- Sun Apr 22 12:01:27 going into namespace [foo.t2]
- Sun Apr 22 12:01:27 1 objects found
- Sun Apr 22 12:01:27 /data/dump/foo/system.users.bson
- Sun Apr 22 12:01:27 going into namespace [foo.system.users]
- Sun Apr 22 12:01:27 1 objects found
- Sun Apr 22 12:01:27 /data/dump/foo/system.indexes.bson
- Sun Apr 22 12:01:27 going into namespace [foo.system.indexes]
- Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.system.users", key: { _id: 1 }, v: 0 }
- Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t2", key: { _id: 1 }, v: 0 }
- Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t1", key: { _id: 1 }, v: 0 }
- Sun Apr 22 12:01:27 3 objects found
- [root@localhost bin]#
Using the tool to return information, we can see that the data in foo has been recovered. Next we will go to the library to verify:
- [root@localhost bin]# ./mongo
- MongoDB shell version: 1.8.1
- connecting to: test
- > use foo
- switched to db foo
- > show collections;
- system.indexes
- system.users
- t1
- t2
- >
It turns out that the foo database table has been successfully restored.