資料庫遷移的總結
需求:將遠程伺服器IP:172.10.10.1上的資料庫database1上的數據遷移到伺服器IP:172.10.10.2的資料庫database2的表table中,要求字段要對應
1、本地登錄遠程伺服器,並下載數據
遠程登錄伺服器 ssh 172.10.10.1 查看此伺服器上的mysql進程 ps -ef | grep mysql //一臺伺服器上可能有多個mysql進程,主要通過端口及sock文件進行區分 登錄端口為3307的mysql伺服器 /home/work/oem/mysql_oem/bin/mysql -h localhost -uroot -P 3307 --socket=/home/work/oem/mysql_oem/mysql.sock 查看3307的mysql伺服器上的數據的編碼形式 show variables like 'character%'; 查看3307的mysql伺服器上資料庫 show databases 將端口為3307的mysql伺服器上的數據按照latin1編碼dump下來 /home/work/oem/mysql_oem/bin/mysqldump --default-character-set=latin1 -h localhost -uroot -P 3307 --socket=/home/work/oem/mysql_oem/mysql.sock database1 > ~/oem/redmine_oem/public/database1.sql
同樣將172.10.10.2中的資料庫database2的表table下下來
2、本地進行遷移
將database1.sql下載到本地 wget url 在本地創建資料庫 create schema database1 將數據導入到本地的資料庫database1中 mysql -uroot -p*** -hlocalhost database1 < /home/sunzhijie/sql/database1.sql or mysql> source path
按照要求,相應的字段做對應
a) Insert into......select .....from .. b) update ..... set .... where... c) substring(string,pos) 取string的自pos位置起的子串 d) concat(string1, string2):將string1與string2連接
3、本地遷移成功的話,上線
a) 將線上的系統進行備份
b) 查看目標資料庫
c) 進行本地的一系列操作