mysql-mysql跨机器数据迁移(命令方式)

一:实验环境


--这里把主机名为target_pc的机器当做源端,把主机名为source_pc的机器当做目标端。
 

不要被主机名混淆了。

二:实验步骤

这里介绍迁移所有数据库和迁移单个数据库时的数据迁移步骤。

2.1:迁移所有数据库

2.1.1 迁移前环境

迁移前,源端有以下数据库:

[sql]  view plain  copy
  1. mysql> show databases;  
  2. +--------------------+  
  3. Database           |  
  4. +--------------------+  
  5. | information_schema |  
  6. | dan                |  
  7. | mysql              |  
  8. | performance_schema |  
  9. | test               |  
  10. +--------------------+  
  11. rows in set (0.00 sec)  

迁移前,目标端的有以下数据库:

[sql]  view plain  copy
  1. mysql> show databases;  
  2. +--------------------+  
  3. Database           |  
  4. +--------------------+  
  5. | information_schema |  
  6. | mysql              |  
  7. | performance_schema |  
  8. | test               |  
  9. +--------------------+  
  10. rows in set (0.01 sec)  

 

源端比目标端多一个dan数据库。

--目标端是刚安装好的mysql,默认就有这4个数据库。

2.1.2 在源端备份所有数据库 

[root@target_pc databasefile]# mysqldump -u root -p --all-databases > /backup/databasefile/all_databases_20150325.bak

2.2.2 拷贝备份文件到目标端

[plain]  view plain  copy
  1. [root@target_pc databasefile]# scp all_databases_20150325.bak 192.168.8.225:/backup/databasefile/  
  2. The authenticity of host '192.168.8.225 (192.168.8.225)' can't be established.  
  3. RSA key fingerprint is ed:ee:f6:e6:f5:3b:76:ed:18:fa:2d:eb:73:83:0e:13.  
  4. Are you sure you want to continue connecting (yes/no)? yes  
  5. Warning: Permanently added '192.168.8.225' (RSA) to the list of known hosts.  
  6.    
  7.    
  8.    
  9.    
  10. root@192.168.8.225's password:   
  11. all_databases_20150325.bak                                                                                                                                      100%  598KB 598.3KB/s   00:00      
  12. [root@target_pc databasefile]#   


2.2.3 在目标端还原所有数据库 

[root@source_pc databasefile]# mysql -u root -p < all_databases_20150325.bak 

Enter password: 

2.2.4 验证

[sql]  view plain  copy
  1. [root@source_pc databasefile]# mysql -u root -p  
  2. Enter password:   
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  4. Your MySQL connection id is 3  
  5. Server version: 5.6.20 MySQL Community Server (GPL)  
  6.    
  7. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.  
  8.    
  9. Oracle is a registered trademark of Oracle Corporation and/or its  
  10. affiliates. Other names may be trademarks of their respective  
  11. owners.  
  12.    
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  14.    
  15. mysql> show databases;  
  16. +--------------------+  
  17. Database           |  
  18. +--------------------+  
  19. | information_schema |  
  20. | dan                |  
  21. | mysql              |  
  22. | performance_schema |  
  23. | test               |  
  24. +--------------------+  
  25. rows in set (0.00 sec)  
  26.    
  27.    
  28. mysql> use dan;  
  29. Reading table information for completion of table and column names  
  30. You can turn off this feature to get a quicker startup with -A  
  31.    
  32. Database changed  
  33. mysql> show tables;  
  34. +---------------+  
  35. | Tables_in_dan |  
  36. +---------------+  
  37. | t             |  
  38. | t2            |  
  39. +---------------+  
  40. rows in set (0.00 sec)  
  41.    
  42. mysql> select * from t;  
  43. +------+  
  44. | id   |  
  45. +------+  
  46. |    3 |  
  47. |    2 |  
  48. +------+  
  49. rows in set (0.00 sec)  

--注意:当迁移所有数据库时,不用提前在目标端创建好所有数据库。

 

2.2:迁移某个数据库

2.2.1 准备测试数据

在源端新建一个数据库:

[sql]  view plain  copy
  1. mysql> create database jiao;  
  2. Query OK, 1 row affected (0.01 sec)  
  3.    
  4. mysql> use jiao;  
  5. Database changed  
  6. mysql> create table t(id int);  
  7. Query OK, 0 rows affected (0.05 sec)  
  8.    
  9. mysql> insert into t(id) values(1);  
  10. Query OK, 1 row affected (0.00 sec)  
  11.    
  12. mysql> insert into t(id) values(2);  
  13. Query OK, 1 row affected (0.00 sec)  
  14.    
  15. mysql> commit;  
  16. Query OK, 0 rows affected (0.00 sec)  
  17.    
  18. mysql> select * from t;  
  19. +------+  
  20. | id   |  
  21. +------+  
  22. |    1 |  
  23. |    2 |  
  24. +------+  
  25. rows in set (0.00 sec)  

 

2.2.2 在源端备份新增的这个数据库

[root@target_pc databasefile]# mysqldump -u root -p jiao > /backup/databasefile/jiao_20150325.bak

Enter password: 

2.2.3 拷贝备份文件到目标端

[plain]  view plain  copy
  1. [root@target_pc databasefile]# scp jiao_20150325.bak 192.168.8.225:/backup/databasefile/  
  2. root@192.168.8.225's password:   
  3. jiao_20150325.bak                                                                                                                                             100% 1757     1.7KB/s   00:00      
  4. [root@target_pc databasefile]#   
  5.    

2.2.4 在目标端创建好该数据库

mysql> create database jiao;

Query OK, 1 row affected (0.00 sec)

 

2.2.5 在目标端还原该数据库

[root@source_pc databasefile]# mysql -u root -p < jiao_20150325.bak

Enter password: 

 

2.2.6 验证

 

[root@source_pc databasefile]# mysql -u root -p

[sql]  view plain  copy
  1. Enter password:   
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  3. Your MySQL connection id is 9  
  4. Server version: 5.6.20 MySQL Community Server (GPL)  
  5.    
  6. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.  
  7.    
  8. Oracle is a registered trademark of Oracle Corporation and/or its  
  9. affiliates. Other names may be trademarks of their respective  
  10. owners.  
  11.    
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  13.    
  14. mysql> show databases;  
  15. +--------------------+  
  16. Database           |  
  17. +--------------------+  
  18. | information_schema |  
  19. | dan                |  
  20. | jiao               |  
  21. | mysql              |  
  22. | performance_schema |  
  23. | test               |  
  24. +--------------------+  
  25. rows in set (0.00 sec)  
  26.    
  27. mysql> use jiao;  
  28. Reading table information for completion of table and column names  
  29. You can turn off this feature to get a quicker startup with -A  
  30.    
  31. Database changed  
  32. mysql> select * from t;  
  33. +------+  
  34. | id   |  
  35. +------+  
  36. |    1 |  
  37. |    2 |  
  38. +------+  
  39. rows in set (0.00 sec)  
  40.    
  41. mysql>   
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值