Mongodb provides a lot of flexibilities to the application developers and system administrators. As we know mongodb is an nosql database server. Standard Sql database servers provides different tools to dump and backup given database. Mongodb provides mongodump
tool for dump and backup operations.
Mongodb为应用程序开发人员和系统管理员提供了很多灵活性。 我们知道mongodb是一个nosql数据库服务器。 标准Sql数据库服务器提供了不同的工具来转储和备份给定的数据库。 Mongodb提供了用于转储和备份操作的mongodump
工具。
检查MongoDB服务状态 (Check MongoDB Service Status)
First we should check the status of the mongodb. We can use systemctl
command which is provided by popular Linux distributions.
首先,我们应该检查mongodb的状态。 我们可以使用流行Linux发行版提供的systemctl
命令。
$ systemctl status mongod
转储MongoDb数据库(Dump MongoDb Database)
We only need to specify the hostname of the mongodb database server. We will use --host
option in order to specify the hostname. In this example we will dump remote host named poftut1
. This will backup all databases to the file named dump
.
我们只需要指定mongodb数据库服务器的主机名即可。 我们将使用--host
选项来指定主机名。 在此示例中,我们将转储名为poftut1
远程主机。 这会将所有数据库备份到名为dump
的文件中。
$ mongodump --host poftut1
仅转储指定的数据库 (Only Dump Specified Database)
We do not need to dump all databases. We can specify the database we want to dump. We will use --db
option in order to specify the database name. In this example we will only dump the database named school
我们不需要转储所有数据库。 我们可以指定我们要转储的数据库。 我们将使用--db
选项来指定数据库名称。 在此示例中,我们仅转储名为school
的数据库
$ mongodump --host poftut1 --db school
仅转储特定集合 (Only Dump Specific Collection)
We can also specify specific collection. We will use --collection
option in order to specify the collection’s name. In this example we will dump the collection named students
in database school
.
我们还可以指定特定的集合。 我们将使用--collection
选项来指定集合的名称。 在此示例中,我们将名为“ students
的集合转储到数据库school
。
$ mongodump --host poftut1 --db school --collection students
转储和压缩 (Dump and Compress)
Dumping big databases or collections will create a lot data with big size. If we need to do dump multiple times in a day the disk will be full. We can compress the dumped data by using gzip
or similar compression tools. We will redirect the output of dump to the gzip with --out -
option. In this example we also set the compressed dump name with the current date.
转储大型数据库或集合将创建大量大数据。 如果我们需要在一天内多次转储,则磁盘将满。 我们可以使用gzip
或类似的压缩工具来压缩转储的数据。 我们将使用--out -
选项将转储的输出重定向到gzip。 在此示例中,我们还将压缩转储名称设置为当前日期。
$ mongodump --host poftut1 --out - | gzip > dump_`date "+%Y-%m-%d"`.gz
For more information about compression look following tutorials.
有关压缩的更多信息,请参见以下教程。
http://www.poftut.com/7z-command-tutorial-examples-compress-extract-files-linux/
http://www.poftut.com/7z-command-tutorial-examples-compress-extract-files-linux/
http://www.poftut.com/gzip-command-tutorial-examples-linux/
http://www.poftut.com/gzip-command-tutorial-examples-linux/
翻译自: https://www.poftut.com/dump-mongodb-using-mongodump-tool-examples/