MongoDB数据迁移

时间:2024-12-08 18:34:20

将集合user从192.168.1.12:27017导入到192.168.1.120:27017

数据的导出:mongoexport

数据的导入:mongoimport

导出集合user的过程:

[root@prod01 ]#cd /data/package/mongodb/bin

[root@prod01 bin]# ./mongoexport -h192.168.1.12:27017 --username=root --password=xxxxxx --authenticationDatabase=admin -d userinfo -c user -o user.dat
2018-04-24T14:05:58.942+0800 connected to: 10.15.23.142:27017
2018-04-24T14:05:59.942+0800 [........................] userinfo.user 0/41904 (0.0%)
2018-04-24T14:06:00.942+0800 [........................] userinfo.user 0/41904 (0.0%)
2018-04-24T14:06:01.942+0800 [........................] userinfo.user 0/41904 (0.0%)
2018-04-24T14:06:02.942+0800 [####....................] userinfo.user 8000/41904 (19.1%)
2018-04-24T14:06:03.942+0800 [####....................] userinfo.user 8000/41904 (19.1%)
2018-04-24T14:06:04.942+0800 [####....................] userinfo.user 8000/41904 (19.1%)
2018-04-24T14:06:05.942+0800 [####....................] userinfo.user 8000/41904 (19.1%)
2018-04-24T14:06:06.947+0800 [#########...............] userinfo.user 16000/41904 (38.2%)
2018-04-24T14:06:07.949+0800 [#########...............] userinfo.user 16000/41904 (38.2%)
2018-04-24T14:06:08.945+0800 [#########...............] userinfo.user 16000/41904 (38.2%)
2018-04-24T14:06:09.942+0800 [#########...............] userinfo.user 16000/41904 (38.2%)
2018-04-24T14:06:10.942+0800 [#############...........] userinfo.user 24000/41904 (57.3%)
2018-04-24T14:06:11.942+0800 [#############...........] userinfo.user 24000/41904 (57.3%)
2018-04-24T14:06:12.942+0800 [#############...........] userinfo.user 24000/41904 (57.3%)
2018-04-24T14:06:13.942+0800 [#############...........] userinfo.user 24000/41904 (57.3%)
2018-04-24T14:06:14.942+0800 [##################......] userinfo.user 32000/41904 (76.4%)
2018-04-24T14:06:15.942+0800 [##################......] userinfo.user 32000/41904 (76.4%)
2018-04-24T14:06:16.942+0800 [##################......] userinfo.user 32000/41904 (76.4%)
2018-04-24T14:06:17.942+0800 [######################..] userinfo.user 40000/41904 (95.5%)
2018-04-24T14:06:18.771+0800 [########################]
2018-04-24T14:06:18.771+0800 exported 41908 records
[root@prod01 bin]#ls

user.dat

导出文件位于bin目录

-h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
    --port=<port> server port (can also use --host hostname:port)

-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
  --authenticationDatabase=<database-name> database that holds the user's credentials
  --authenticationMechanism=<mechanism> authentication mechanism to use

-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use

-o, --out=<filename> output file; if not specified, stdout is used
--jsonArray output to a JSON array rather than one object per line
--pretty output JSON formatted to be human-readable
--noHeaderLine export CSV data without a list of field names at the first line

导入集合:

./mongoimport -h192.168.1.120:27017 --username=root --password=xxxxxx --authenticationDatabase=admin -d userinfo -c user  user.dat