MySQL如何跨机器迁移数据?
經(jīng)常會遇到如此需求,需把A主機上的MySQL數(shù)據(jù)庫所有遷移到B主機上,或者部分數(shù)據(jù)庫,所以接下來將介紹遷移所有數(shù)據(jù)庫和遷移單個數(shù)據(jù)庫時的數(shù)據(jù)遷移步驟。
1 實驗環(huán)境
A主機(源主機):
- IP地址:192.168.0.191
- 系統(tǒng)版本:CentOS 6.8
- MySQL版本:5.5
- 源主機有以下數(shù)據(jù)庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | laucyun | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec) 查看數(shù)據(jù)庫laucyun內(nèi)容: mysql> show tables; +-------------------+ | Tables_in_laucyun | +-------------------+ | users | +-------------------+ 1 row in set (0.00 sec)mysql> select * from users; +----+----------+----------+----------------+ | id | username | password | email | +----+----------+----------+----------------+ | 1 | laucyun | 123456 | liu@liuker.xyz | +----+----------+----------+----------------+ 1 row in set (0.00 sec) 其中,數(shù)據(jù)庫laucyun中有數(shù)據(jù)庫表users,users表中有一條記錄。
B主機(目標主機):
- IP地址:192.168.0.192
- 系統(tǒng)版本:CentOS 6.8
- MySQL版本:5.5
- 目標主機有以下數(shù)據(jù)庫: mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec)
源主機比目標主機多一個名為laucyun的數(shù)據(jù)庫。
OK,開始環(huán)境介紹完后,先介紹遷移所有數(shù)據(jù)。
2 遷移所有數(shù)據(jù)庫
目前實驗環(huán)境中,源主機比目標主機多一個名為laucyun的數(shù)據(jù)庫。
在源主機備份所有數(shù)據(jù)庫:
$ mysqldump -uroot -p --all-databases > /home/all_databases.bak提示輸入root用戶的密碼,備份文件信息如下:
$ ll total 552 -rw-r--r-- 1 root root 561449 Dec 31 13:02 all_databases.bak拷貝備份文件all_databases.bak到目標主機:
$ scp all_databases.bak root@192.168.0.192:/home/在目標主機還原所有數(shù)據(jù)庫:
$ mysql -uroot -p < /home/all_databases.bak提示輸入root用戶的密碼。
驗證是否成功:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | laucyun | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec)mysql> use laucyun; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed mysql> show tables; +-------------------+ | Tables_in_laucyun | +-------------------+ | users | +-------------------+ 1 row in set (0.00 sec)mysql> select * from users; +----+----------+----------+----------------+ | id | username | password | email | +----+----------+----------+----------------+ | 1 | laucyun | 123456 | liu@liuker.xyz | +----+----------+----------+----------------+ 1 row in set (0.00 sec)由此可見,目標主機上已經(jīng)備份成功了源主機中所有數(shù)據(jù)庫。
注意:當遷移所有數(shù)據(jù)庫時,不用提前在目標主機創(chuàng)建好所有數(shù)據(jù)庫。
3?遷移某個數(shù)據(jù)庫
目前實驗環(huán)境中,源主機比目標主機多一個名為laucyun的數(shù)據(jù)庫。
在源主機備份數(shù)據(jù)庫laucyun:
$ mysqldump -uroot -p laucyun > /home/laucyun.bak提示輸入root用戶的密碼,備份文件信息如下:
$ ll total 4 -rw-r--r-- 1 root root 1982 Dec 31 13:52 laucyun.bak拷貝備份文件laucyun.bak到目標主機:
$ scp laucyun.bak root@192.168.0.192:/home/在目標主機中創(chuàng)建好數(shù)據(jù)庫laucyun:
mysql> CREATE DATABASE `laucyun` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; Query OK, 1 row affected (0.00 sec)在目標主機還原數(shù)據(jù)庫laucyun:
$ mysql -uroot -p laucyun < /home/laucyun.bak提示輸入root用戶的密碼。
驗證是否成功:
mysql> show tables; +-------------------+ | Tables_in_laucyun | +-------------------+ | users | +-------------------+ 1 row in set (0.00 sec)mysql> select * from users; +----+----------+----------+----------------+ | id | username | password | email | +----+----------+----------+----------------+ | 1 | laucyun | 123456 | liu@liuker.xyz | +----+----------+----------+----------------+ 1 row in set (0.00 sec)由此可見,目標主機上已經(jīng)備份成功了源主機中數(shù)據(jù)庫laucyun。
注意:當遷移某個數(shù)據(jù)庫時,需要提前在目標主機中創(chuàng)建好該數(shù)據(jù)庫。
總結(jié)
以上是生活随笔為你收集整理的MySQL如何跨机器迁移数据?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爱斯基摩结构
- 下一篇: 前端:实现手机左右滑动效果