赞
踩
目录
4-1 mysql下world库的city表,导入mongodb
一、导出工具 mongoexport
Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。 可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。 (1)版本差异较大 (2)异构平台数据迁移
mongoexport具体用法如下所示:
$ mongoexport --help 参数说明: -h:指明数据库宿主机的IP -u:指明数据库的用户名 -p:指明数据库的密码 -d:指明数据库的名字 -c:指明collection的名字 -f:指明要导出那些列 -o:指明到要导出的文件名 -q:指明导出数据的过滤条件 --authenticationDatabase admin1-1 单表备份至json格式
注:备份文件的名字可以自定义,默认导出了JSON格式的数据。
mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log -o /mongodb/log.json
1-2 单表备份至csv格式
如果我们需要导出CSV格式的数据,则需要使用----type=csv参数: mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log --type=csv -f uid,name,age,date -o /mongodb/log.csv
二、导入工具 mongoimport
Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。具体使用如下所示:
$ mongoimport --help 参数说明: -h:指明数据库宿主机的IP -u:指明数据库的用户名 -p:指明数据库的密码 -d:指明数据库的名字 -c:指明collection的名字 -f:指明要导入那些列 -j, --numInsertionWorkers=<number> number of insert operations to run concurrently (defaults to 1) //并行
三、数据恢复
3-1 恢复json格式表数据到log1
mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d testdb -c log1 /mongodb/log.json
3-2 恢复csv格式的文件到log2
上面演示的是导入JSON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过--type参数指定导入格式,具体如下所示:
注意: (1)csv格式的文件头行,有列名字 mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d testdb -c log2 --type=csv --headerline --file /mongodb/log.csv (2)csv格式的文件头行,没有列名字 mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d testdb -c log3 --type=csv -f id,name,age,date --file /mongodb/log1.csv --headerline:指明第一行是列名,不需要导入。 补:导入CSV文件P_T_HISTXN_20160616.csv(csv文件中没有列明,fields指定列明) $ tail -5 P_T_HISTXN_20160616.csv
四、异构平台迁移案例
4-1 mysql下world库的city表,导入mongodb
4-1-1 mysql开启安全路径
vim /etc/my.cnf --->添加以下配置 secure-file-priv=/tmp --重启数据库生效 /etc/init.d/mysqld restart4-1-2 导出mysql的city表数据
source /root/world.sql select * from world.city into outfile '/tmp/city1.csv' fields terminated by ',';4-1-3 处理备份文件
desc world.city ID | int(11) | NO | PRI | NULL | auto_increment | | Name | char(35) | NO | | | | | CountryCode | char(3) | NO | MUL | | | | District | char(20) | NO | | | | | Population vim /tmp/city.csv ----> 添加第一行列名信息 ID,Name,CountryCode,District,Population4-1-4 在mongodb中导入备份
mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d world -c city --type=csv -f ID,Name,CountryCode,District,Population --file /tmp/city1.csv use world db.city.find({CountryCode:"CHN"});
mysql world共100张表,全部迁移到mongodb
select * from world.city into outfile '/tmp/world_city.csv' fields terminated by ','; 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';导入: 提示,使用infomation_schema.columns + information_schema.tables
4-2 mysql导出csv
select * from test_info into outfile '/tmp/test.csv' fields terminated by ',' ------字段间以,号分隔 optionally enclosed by '"' ------字段用"号括起 escaped by '"' ------字段中使用的转义符为" lines terminated by '\r\n'; ------行以\r\n结束4-3 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';
五、python 基于复制集的数据库连接
from pymongo import MongoReplicaSetClient conn = MongoReplicaSetClient("10.0.0.200:28017,10.0.0.200:28017,10.0.0.200:28017", replicaset='my_repl') print(conn) print (conn) print (conn.primary) print (conn.seeds) print (conn.secondaries) print (conn.read_preference) print (conn.server_info())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。