轉載 https://blog.csdn.net/yabingshi_tech/article/details/44645719
方法一: SQLyog 複製
一:實驗環境
--這裏把主機名爲target_pc的機器當做源端,把主機名爲source_pc的機器當做目標端。
不要被主機名混淆了。
二:實驗步驟
2.1:遷移所有數據庫
2.1.1 遷移前環境
遷移前,源端有以下數據庫:
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | dan |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 5 rows in set (0.00 sec)
遷移前,目標端的有以下數據庫:
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 4 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 拷貝備份文件到目標端
- [root@target_pc databasefile]# scp all_databases_20150325.bak 192.168.8.225:/backup/databasefile/
- The authenticity of host '192.168.8.225 (192.168.8.225)' can't be established.
- RSA key fingerprint is ed:ee:f6:e6:f5:3b:76:ed:18:fa:2d:eb:73:83:0e:13.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '192.168.8.225' (RSA) to the list of known hosts.
- [email protected]'s password:
- all_databases_20150325.bak 100% 598KB 598.3KB/s 00:00
- [root@target_pc databasefile]#
2.2.3 在目標端還原所有數據庫
[root@source_pc databasefile]# mysql -u root -p < all_databases_20150325.bak
Enter password:
2.2.4 驗證
- [root@source_pc databasefile]# mysql -u root -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 3
- Server version: 5.6.20 MySQL Community Server (GPL)
- Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | dan |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 5 rows in set (0.00 sec)
- mysql> use dan;
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- mysql> show tables;
- +---------------+
- | Tables_in_dan |
- +---------------+
- | t |
- | t2 |
- +---------------+
- 2 rows in set (0.00 sec)
- mysql> select * from t;
- +------+
- | id |
- +------+
- | 3 |
- | 2 |
- +------+
- 2 rows in set (0.00 sec)
--注意:當遷移所有數據庫時,不用提前在目標端創建好所有數據庫。
2.2:遷移某個數據庫
2.2.1 準備測試數據
在源端新建一個數據庫:
- mysql> create database jiao;
- Query OK, 1 row affected (0.01 sec)
- mysql> use jiao;
- Database changed
- mysql> create table t(id int);
- Query OK, 0 rows affected (0.05 sec)
- mysql> insert into t(id) values(1);
- Query OK, 1 row affected (0.00 sec)
- mysql> insert into t(id) values(2);
- Query OK, 1 row affected (0.00 sec)
- mysql> commit;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from t;
- +------+
- | id |
- +------+
- | 1 |
- | 2 |
- +------+
- 2 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 拷貝備份文件到目標端
- [root@target_pc databasefile]# scp jiao_20150325.bak 192.168.8.225:/backup/databasefile/
- [email protected]'s password:
- jiao_20150325.bak 100% 1757 1.7KB/s 00:00
- [root@target_pc databasefile]#
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
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 9
- Server version: 5.6.20 MySQL Community Server (GPL)
- Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | dan |
- | jiao |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 6 rows in set (0.00 sec)
- mysql> use jiao;
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- mysql> select * from t;
- +------+
- | id |
- +------+
- | 1 |
- | 2 |
- +------+
- 2 rows in set (0.00 sec)
- mysql>
--注意:當遷移某個數據庫時,必須在目標端先創建好數據庫才行。