mysql使用mysqldump 与 mysqlimport进行数据库迁移
1.导入数据库
1)source
source /home/platvt/product/pc2_create_tables.sql
2)mysqlimport
使用mysqlimport导入数据:
出现情况:
(1)出现 Error: 1146
[platvt@qa-qd-62-180 tmp]$ mysqlimport -uroot -p --fields-terminated-by='\t' test students
Enter password:
mysqlimport: Error: 1146, Table 'test.students' doesn't exist, when using table: students
Enter password:
mysqlimport: Error: 1146, Table 'test.students' doesn't exist, when using table: students
(2)出现 Error: 29
[platvt@qa-qd-62-180 tmp]$ mysqlimport -uroot -p --fields-terminated-by='\t' test students
Enter password:
mysqlimport: Error: 29, File '/data/mysqldata/mydata/test/students' not found (Errcode: 2), when using table: students
Enter password:
mysqlimport: Error: 29, File '/data/mysqldata/mydata/test/students' not found (Errcode: 2), when using table: students
[platvt@qa-qd-62-180 tmp]$ ps -ef | grep mysql
root 7068 1 0 Jul10 ? 00:00:00 /bin/sh ./mysqld_safe
mysql 7324 7068 0 Jul10 ? 00:34:59 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/data/mysqldata/mydata --user=mysql --log-error=/usr/local/mysql/log/error.log --open-files-limit=8192 --pid-file=/usr/local/mysql/sock/mysql.pid --socket=/usr/local/mysql/sock/mysql.sock --port=3306
root 7068 1 0 Jul10 ? 00:00:00 /bin/sh ./mysqld_safe
mysql 7324 7068 0 Jul10 ? 00:34:59 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/data/mysqldata/mydata --user=mysql --log-error=/usr/local/mysql/log/error.log --open-files-limit=8192 --pid-file=/usr/local/mysql/sock/mysql.pid --socket=/usr/local/mysql/sock/mysql.sock --port=3306
(3)出现 Error: 13
[platvt@qa-qd-62-180 tmp]$ mysqlimport -uroot -p --fields-terminated-by='\t' test /home/platvt/tmp/students
Enter password:
mysqlimport: Error: 13, Can't get stat of '/home/platvt/tmp/students' (Errcode: 13), when using table: students
Enter password:
mysqlimport: Error: 13, Can't get stat of '/home/platvt/tmp/students' (Errcode: 13), when using table: students
[platvt@qa-qd-62-180 tmp]$ mysqlimport -uroot -p --fields-terminated-by='\t' --local test /home/platvt/tmp/students
Enter password:
test.students: Records: 5 Deleted: 0 Skipped: 1 Warnings: 0
Enter password:
test.students: Records: 5 Deleted: 0 Skipped: 1 Warnings: 0
2、导出数据库
1)导出语法
mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql
C:\Documents and Settings\keju.wangkj>mysqldump -h ip -unapoli_ccbu_test -p1qaz@2wsx napoli_ccbu_test --skip-l
ock-tables > D:\documents\stanley\ccbu_napoli_test.sql
ock-tables > D:\documents\stanley\ccbu_napoli_test.sql
2)导出某用户下面的所有数据库:
>mysqldump -hip -uoffer -poffer --all-database --skip-lock-tables > D:\documents\stanley\cobar_all.sql
3)导出一个表
mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql
4)导出一个数据库结构:-d
mysqldump -u wcnc -p -d --add-drop-table smgp_apps_wcnc >d:wcnc_db.sql
-d 没有数据 --add-drop-table 在每个create语句之前增加一个drop table
mysqldump -h ip -ustanley -pstanley stanley -d --add-drop-table> pc2_create_tables.sql
- mysqldump -unormandy -p*** -h10.20.36.26 normandy_dev --skip-lock-tables --add-drop-table -d > 10.20.36.26_normandy_dev.sql
3、导入导出实战
需求:
1、导出的表和导入的表结构相同
2、导入的表里面存在数据,且有字增长的字段
解决方案:
建立临时表:create table tmp as select 列 from table;
只导出数据,不导出表结构:mysqldum -t
4、到处成excel表格
方法1:命令行导出
C:\>mysql -h172.29.63.17 -unapoli -p*** -e "select * from napoli.queues" > D:\documents\projects\napoli\queues_17.xls
mysql -h172.29.63.17 -unapoli -p*** -e "select * from napoli.machines" > D:\documents\projects\napoli\machines_17.xls
mysql -h172.29.63.17 -unapoli -p*** -e "select * from napoli.physical_queue" > D:\documents\projects\napoli\physical_queue_17.xls
mysql -h172.29.63.17 -unapoli -p*** -e "select * from napoli.queue_details" > D:\documents\projects\napoli\queue_details_17.xls
mysql -h172.29.63.17 -unapoli -p*** -e "select * from napoli.virtual_topics" > D:\documents\projects\napoli\virtual_topics_17.xls
mysql -h172.29.63.17 -unapoli -p*** -e "select * from napoli.vtopic_queue" > D:\documents\projects\napoli\vtopic_queue_17.xls
方法2:使用工具
【注意】
1、只导出一个某用户下面的一个数据库(一个用户下面可能存在多个数据库)
mysqldump -h10.20.*.* -unapoli_itu_test -pnapoli_itu_test napoli --skip-lock-tables > /home/leonardo/product/napoli_create_tables_with_data.sql
2、参看淘宝DBA的mysql导入导出: http://www.taobaodba.com/html/558_loaddata.html