MySQL跨主機複製表

轉載 https://blog.csdn.net/yabingshi_tech/article/details/44645719

方法一: SQLyog 複製

方法二 

一:實驗環境


--這裏把主機名爲target_pc的機器當做源端,把主機名爲source_pc的機器當做目標端。
 

不要被主機名混淆了。

二:實驗步驟

這裏介紹遷移所有數據庫和遷移單個數據庫時的數據遷移步驟。

2.1:遷移所有數據庫

2.1.1 遷移前環境

遷移前,源端有以下數據庫:

  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)  

遷移前,目標端的有以下數據庫:

  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. [email protected]'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 驗證

  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 準備測試數據

在源端新建一個數據庫:

  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. [email protected]'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

  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>   


--注意:當遷移某個數據庫時,必須在目標端先創建好數據庫才行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章