轻松备份与恢复:Mongo数据库导出与导入实战 (mongo数据库到处)

Mongo数据库备份和恢复非常重要。备份是保证数据安全的之一步,在数据意外丢失和服务器出现问题时,可以使用备份来恢复数据。在Mongo数据库中备份很容易,只需要将数据导出到一个文件中,需要用到的时候再将文件导入,恢复数据即可。

本文将介绍Mongo数据库的导出和导入操作。我们需要安装MongoDB。安装好后,我们需要连接MongoDB数据库以进行操作。

连接MongoDB

使用mongo命令连接到MongoDB实例。输入以下命令:

“`

$ mongo

“`

这将连接到默认端口上的MongoDB实例。如果您有指定的主机和端口,请使用以下命令:

“`

$ mongo –host :

“`

如果MongoDB实例中启用了验证机制,请提供凭据:

“`

$ mongo -u -p

“`

在连接到MongoDB实例后,我们将在本地机器上导出和导入数据。

Mongo数据库导出

Mongo数据库导出是将MongoDB中的数据保存到文件中。这将创建一个存档文件,该文件可以用于恢复数据或将数据迁移到其他MongoDB实例。

在导出MongoDB数据之前,您需要选择导出的数据库和。为此,我们使用`use`命令。

“`

> use mydb

“`

在这个例子中,我们将使用`mydb`数据库的情况。现在,我们将手动导出数据。使用以下命令:

“`

> mongoexport –db mydb –collection mycollection –out backup.json

“`

这将导出`mycollection`的所有数据,并将其保存在`backup.json`文件中。该文件将在当前工作目录中创建。

Mongo数据库导入

Mongo数据库导入是将MongoDB导出文件中的数据插入到MongoDB中。要导入数据,我们只需要使用`mongorestore`命令。在这个例子中,我们假设您已经将导出文件`backup.json`放在您当前的工作目录中。运行以下命令:

“`

> mongorestore –db mydb –collection mycollection backup.json

“`

这将把`backup.json`文件中的数据导入到`mycollection`中。如果`mycollection`不存在,则MongoDB将创建该并将数据插入其中。

结论

相关问题拓展阅读:

mongodb用mongoexport命令导出数据为什么一直显示百分之零

一、导出棚判工具mongoexport

Mongodb中的mongoexport工具可以把一个collection导出成ON格式或CSV格式的文件。可以陪和穗通过参数指定导出的数据项,也可以根据指定的条件导出数据。mongoexport具体用法如下所示:

Shell代码

# ./芦卜bin/mongoexport –help

Export MongoDB data to CSV, TSV or ON files.

options:

–help produce help message

-v be more verbose (include multiple times for more

verbosity e.g. -vvvvv)

–version print the program’s version and exit

-h argmongo host to connect to ( /s1,s2 for

sets)

–port arg server port. Can also use –host hostname:port

–ipvenable IPv6 support (disabled by default)

-u arg username

-p arg password

–dbpath argdirectly 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

–directoryperdbif dbpath specified, each db is in a separate

directory

–journal enable journaling

-d argdatabase to use

-c arg collection to use (some commands)

-f argcomma separated list of field names e.g. -f

name,age

–fieldFile argfile with fields names – 1 per line

-q argquery filter, as a ON string

–csvexport to csv instead of json

-o argoutput file; if not specified, stdout is used

–jsonArray output to a json array rather than one object per

line

-k arg (=1) use secondaries for export if available, default

true

参数说明:

-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导出那些列

-o:指明到要导出的文件名

-q:指明导出数据的过滤条件

实例:test库中存在着一个students,中数据如下:

Js代码

> db.students.find()

{ “_id” : ObjectId(“fea81e5”), “classid” : 1, “age” : 20, “name” : “kobe” }

{ “_id” : ObjectId(“a50fea81e6”), “classid” : 1, “age” : 23, “name” : “nash” }

{ “_id” : ObjectId(“a50fea81e7”), “classid” : 2, “age” : 18, “name” : “james” }

{ “_id” : ObjectId(“a50fea81e8”), “classid” : 2, “age” : 19, “name” : “wade” }

{ “_id” : ObjectId(“fea81e9”), “classid” : 2, “age” : 19, “name” : “bosh” }

{ “_id” : ObjectId(“fea81ea”), “classid” : 2, “age” : 25, “name” : “allen” }

{ “_id” : ObjectId(“b50fea81eb”), “classid” : 1, “age” : 19, “name” : “howard” }

{ “_id” : ObjectId(“503114a750fea81ec”), “classid” : 1, “age” : 22, “name” : “paul” }

{ “_id” : ObjectId(“503114cd50fea81ed”), “classid” : 2, “age” : 24, “name” : “shane” }

由上可以看出文档中存在着3个字段:classid、age、name

1.直接导出数据到文件中

Shell代码

# ./bin/mongoexport -d test -c students -o students.dat

connected to: 127.0.0.1

exported 9 records

命令执行完后使用ll命令查看,发现目录下生成了一个students.dat的文件

Shell代码

-rw-r–r– 1 root rootAug 21 00:05 students.dat

查看该文件信息,具体信息如下:

Shell代码

# cat students.dat

{ “_id” : { “$oid” : “fea81e5” }, “classid” : 1, “age” : 20, “name” : “kobe” }

{ “_id” : { “$oid” : “a50fea81e6” }, “classid” : 1, “age” : 23, “name” : “nash” }

{ “_id” : { “$oid” : “a50fea81e7” }, “classid” : 2, “age” : 18, “name” : “james” }

{ “_id” : { “$oid” : “a50fea81e8” }, “classid” : 2, “age” : 19, “name” : “wade” }

{ “_id” : { “$oid” : “fea81e9” }, “classid” : 2, “age” : 19, “name” : “bosh” }

{ “_id” : { “$oid” : “fea81ea” }, “classid” : 2, “age” : 25, “name” : “allen” }

{ “_id” : { “$oid” : “b50fea81eb” }, “classid” : 1, “age” : 19, “name” : “howard” }

{ “_id” : { “$oid” : “503114a750fea81ec” }, “classid” : 1, “age” : 22, “name” : “paul” }

{ “_id” : { “$oid” : “503114cd50fea81ed” }, “classid” : 2, “age” : 24, “name” : “shane” }

参数说明:

-d:指明使用的库,本例中为test

-c:指明要导出的,本例中为students

-o:指明要导出的文件名,本例中为students.dat

从上面的结果可以看出,我们在导出数据时没有显示指定导出样式 ,默认导出了ON格式的数据。如果我们需要导出CSV格式的数据,则需要使用–csv参数,具体如下所示:

Shell代码

# ./bin/mongoexport -d test -c students –csv -f classid,name,age -o students_csv.dat

connected to: 127.0.0.1

exported 9 records

# cat students_csv.dat

classid,name,age

1.0,”kobe”,20.0

1.0,”nash”,23.0

2.0,”james”,18.0

2.0,”wade”,19.0

2.0,”bosh”,19.0

2.0,”allen”,25.0

1.0,”howard”,19.0

1.0,”paul”,22.0

2.0,”shane”,24.0

#

参数说明:

-csv:指明要导出为csv格式

-f:指明需要导出classid、name、age这3列的数据

由上面结果可以看出,mongoexport成功地将数据根据csv格式导出到了students_csv.dat文件中。

二、导入工具mongoimport

Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入ON格式数据,也可以导入CSV格式数据。具体使用如下所示:

Shell代码

# ./bin/mongoimport –help

options:

–help produce help message

-v be more verbose (include multiple times for more

verbosity e.g. -vvvvv)

–version print the program’s version and exit

-h argmongo host to connect to ( /s1,s2 for sets)

–port argserver port. Can also use –host hostname:port

–ipv enable IPv6 support (disabled by default)

-u arg username

-p arg password

–dbpath argdirectly 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

–directoryperdbif dbpath specified, each db is in a separate

directory

–journal enable journaling

-d argdatabase to use

-c arg collection to use (some commands)

-f arg comma separated list of field names e.g. -f name,age

–fieldFile argfile with fields names – 1 per line

–ignoreBlanksif given, empty fields in csv and tsv will be ignored

–type argtype of file to import. default: json (json,csv,tsv)

–file argfile to import from; if not specified stdin is used

–drop drop collection first

–headerlineCSV,TSV only – use first line as headers

–upsert insert or update objects that already exist

–upsertFields arg comma-separated fields for the query part of the

upsert. You should make sure this is indexed

–stopOnErrorstop importing at first error rather than continuing

–jsonArrayload a json array, not one item per line. Currently

limited to 4MB.

参数说明:

-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导入那些列

示例:先删除students中的数据,并验证

Js代码

> db.students.remove()

> db.students.find()

>

然后再导入上面导出的students.dat文件中的内容

Shell代码

# ./bin/mongoimport -d test -c students students.dat

connected to: 127.0.0.1

imported 9 objects

#

参数说明:

-d:指明数据库名,本例中为test

-c:指明collection名,本例中为students

students.dat:导入的文件名

查询students中的数据

Js代码

> db.students.find()

{ “_id” : ObjectId(“fea81e5”), “classid” : 1, “age” : 20, “name” : “kobe” }

{ “_id” : ObjectId(“a50fea81e6”), “classid” : 1, “age” : 23, “name” : “nash” }

{ “_id” : ObjectId(“a50fea81e7”), “classid” : 2, “age” : 18, “name” : “james” }

{ “_id” : ObjectId(“a50fea81e8”), “classid” : 2, “age” : 19, “name” : “wade” }

{ “_id” : ObjectId(“fea81e9”), “classid” : 2, “age” : 19, “name” : “bosh” }

{ “_id” : ObjectId(“fea81ea”), “classid” : 2, “age” : 25, “name” : “allen” }

{ “_id” : ObjectId(“b50fea81eb”), “classid” : 1, “age” : 19, “name” : “howard” }

{ “_id” : ObjectId(“503114a750fea81ec”), “classid” : 1, “age” : 22, “name” : “paul” }

{ “_id” : ObjectId(“503114cd50fea81ed”), “classid” : 2, “age” : 24, “name” : “shane” }

>

证明数据导入成功

上面演示的是导入ON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过–type参数指定导入格式,具体如下所示:

先删除数据

Js代码

> db.students.remove()

> db.students.find()

>

再导入之前导出的students_csv.dat文件

Shell代码

# ./bin/mongoimport -d test -c students –type csv –headerline –file students_csv.dat

connected to: 127.0.0.1

imported 10 objects

#

参数说明:

-type:指明要导入的文件格式

-headerline:指明之一行是列名,不需要导入

-file:指明要导入的文件

查询students,验证导入是否成功:

Js代码

> db.students.find()

{ “_id” : ObjectId(“c632cd118ad8”), “classid” : 1, “name” : “kobe”, “age” : 20 }

{ “_id” : ObjectId(“c632cd118ad9”), “classid” : 1, “name” : “nash”, “age” : 23 }

{ “_id” : ObjectId(“c632cd118ada”), “classid” : 2, “name” : “james”, “age” : 18 }

{ “_id” : ObjectId(“c632cd118adb”), “classid” : 2, “name” : “wade”, “age” : 19 }

{ “_id” : ObjectId(“c632cd118adc”), “classid” : 2, “name” : “bosh”, “age” : 19 }

{ “_id” : ObjectId(“c632cd118add”), “classid” : 2, “name” : “allen”, “age” : 25 }

{ “_id” : ObjectId(“c632cd118ade”), “classid” : 1, “name” : “howard”, “age” : 19 }

{ “_id” : ObjectId(“c632cd118adf”), “classid” : 1, “name” : “paul”, “age” : 22 }

{ “_id” : ObjectId(“c632cd118ae0”), “classid” : 2, “name” : “shane”, “age” : 24 }

关于mongo数据库到处的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » 轻松备份与恢复:Mongo数据库导出与导入实战 (mongo数据库到处)