DBA MongoDB 数据迁徙

mongoexport

​ 将数据导出成JSON或者CSV格式,用于MongoDB中的数据向别的数据库产品进行迁移。

​ 命令行中执行:

T > mongoexport --help

​ 参数说明:

参数 描述
-h 指明数据库宿主机的IP
-u 指明数据库的用户名
-p 指明数据库的密码
-d 指明数据库的名字
-c 指明collection的名字
-f 指明要导出那些列
-o 指明到要导出的文件名
-q 指明导出数据的过滤条件
--port 端口号
--uri 使用链接字符串进行链接
--authenticationDatabase 验证库

​ 示例,单表备份成JSON格式,从从库备份,需要管理员用户:

T > mongoexport -uroot -p密码 --port 27018 --authenticationDatabase admin -d 库名 -c 表名 -o /mongodb/表名.json

​ 示例,单表中某些字段备份成CSV格式,需要加--type=csv参数:

T > mongoexport -uroot -p密码 --port 27017 --authenticationDatabase admin -d 库名 -c 表名 --type=csv -f 列1,列2,列3 -o /mongodb/表名.csv

mongoimport

​ 将格式为JSON或者CSV格式的数据文件进行导入,用于别的数据库产品中的数据向MongoDB进行迁移。

​ 命令行中执行:

T > mongoimport --help

​ 参数说明:

参数 描述
-h 指明数据库宿主机的IP
-u 指明数据库的用户名
-p 指明数据库的密码
-d 指明数据库的名字
-c 指明collection的名字
-f 指明要导入那些列
-j 并行导入,默认为1,使用 --numInsertionWorkers设置并行导入的数量
--port 端口号
--uri 使用链接字符串进行链接
--authenticationDatabase 验证库
--headerline 不导入第一行(列名)
--file 要恢复的备份文件

​ 示例,恢复JSON格式表到某一库中(推荐先恢复到测试库查看效果):

T > mongoimport -uroot -p密码 --port 28017 --authenticationDatabase admin -d 库名 -c 表名 /mongodb/备份文件.json

​ 示例,恢复CSV格式表到某一库中,其中--headerline为第一行是列名,不需要导入::

T > mongoimport -uroot -p密码 --port 28017 --authenticationDatabase admin -d 库名 -c 表名 --type=csv --headerline --file  /mongodb/备份文件.csv

​ 示例,恢复CSV格式表中的记录到某一已有的集合中,没有第一行的列名字,自己指定:

T > mongoimport  -uroot -p密码 --port 28017 --authenticationDatabase admin -d 库名 -c 表名 --type=csv -f 列1,列2,列3 --file  /mongodb/备份文件.csv

异构迁移

单表迁徙

​ 从MySQL迁移到MongoDB。

​ 将MySQL中的world数据库下的city表进行导出,并且导入到MongoDB。

​ 第一步,设置MySQL输出文件的安全路径:

T > vim /etc/my.cnf

secure-file-priv=
# =空为所有路径都是安全路径

​ 重启数据库生效:

T > systemctl restart mysqld.service

​ 第二步,导出MySQL中world库下的city表:

M > SELECT * FROM world.city INTO OUTFILE "/tmp/city.csv" FIELDS TERMINATED BY ",”;

​ 第三步,处理备份文件,首先查看city表的表结构:

M > DESC world.city

+-------------+----------+------+-----+--------+-----------------+
|  ID         | int(11)  | NO   | PRI |   NULL  | auto_increment |
| Name        | char(35) | NO   |     |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
+-------------+----------+------+-----+--------+-----------------+

​ 使用vim添加第一行的列名信息:

T > vim /tmp/city.csv

ID,Name,CountryCode,District

​ 第四步,在MongoDB中导入备份文件:

T > mongoimport -uroot -p密码 --port 28017 --authenticationDatabase admin -d world -c city --type=csv -f ID,Name,CountryCode,District --file /tmp/city1.csv

​ 进入该库查看内容:

> use world
> db.city.find({CountryCode:"CHN"});

其他知识

​ 假如world下共有100张表,全部迁移到MongoDB中:

M > select table_name ,group_concat(column_name) from columns where table_schema='world' group by table_name;

M > select * from world.city into outfile '/tmp/world_city.csv' fields terminated by ',';

M > select concat("select * from ",table_schema,".",table_name ," into outfile '/tmp/",table_schema,"_",table_name,".csv' fields terminated by ',';")
from information_schema.tables where table_schema ='world';

​ 在MySQL向文件输出时,可指定的语句如下:

select * from test_info   
into outfile '/tmp/test.csv'   
fields terminated by ','    # 字段间以,号分隔
optionally enclosed by '"'   # 字段用"号括起
escaped by '"'           # 字段中使用的转义符为"
lines terminated by '\r\n';  # 行以\r\n结束

​ 在MySQL中导入CSV文件时,可指定的语句如下:

load data infile '/tmp/test.csv'   
into table test_info    
fields terminated by ','  
optionally enclosed by '"' 
escaped by '"'   
lines terminated by '\r\n'; 
posted @ 2021-03-20 15:29  云崖君  阅读(53)  评论(0编辑  收藏  举报