mysqldump合并mysql多个数据库
1 新建目标数据库,空数据库。
2 选择一个源数据库,导出数据结构。windows可以切换到mysql的bin目录下面,
mysqldump -u root -p -d --add-drop-table dbname >d:/myql/dbname_db.sql
-d 没有数据 --add-drop-table 在每个create语句之前增加一个drop table
3 导入数据结构。进入mysql数据库控制台,如mysql -u root -p password>use 数据库,
然后使用source命令,后面参数为脚本文件(如这里用到的.sql) mysql>source d:/dbname.sql
4 开始导具体表数据
mysqldump -u root -p -t dbname users>d:/myql/user.sql ,#只导出表数据不导表结构,添加“-t”命令参数
然后对应目标库source d:/myql/user.sql
5 处理主键冲突的问题
不同数据库同一个表主键冲突,需要删除该条数据。删除报错的话
23:20:16 delete from table.user where id=1234 Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. Cannot use range access on index 'PRIMARY' due to type or collation conversion on field 'id' To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect. 0.000 sec
设置一下安全模式即可。SET SQL_SAFE_UPDATES = 0;
6 主键冲突也可以提前查询一下
select id from sourcedb1.user where id in (select id from todb.user);
然后把查询到的重复数据提前删除
delete from sourcedb1.user where id in (select id from sourcedb1.user where id in (select id from todb.user)),
此时会提示错误Error Code: 1093. You can't specify target table 'user' for update in FROM clause 0.000 sec。
意思是不能在同一表中查询的数据作为同一表的更新数据。
我们加一个中间表即可
delete from sourcedb1.user where id in (select temptable.temp from (select id as temp from sourcedb1.user where id in (select id from todb.user))temptable);
7 内存过小的问题
Error Code: 1206. The total number of locks exceeds the lock table size 102.750 sec
show variables like "%_buffer%";
SET GLOBAL innodb_buffer_pool_size=32*1024*1024*1024;