wiki:MySQL_Replication

Version 2 (modified by rider, 16 years ago) (diff)

--

MySQL Replication

How to Set Up Replication

目的: 我們假定A中心有一台Server Master1 ,而B中心有一台Server Master2. 為了方便我們在更新 Master1 的資料庫時, B中心的 Master2 資料庫也能同時更新而做到資料同步.

實驗環境: Debian GNU/Linux Etch 4.0r4 with kernel 2.6.18

Part1: 安裝MySQL 5.0

步驟一 : 安裝 MySQL (假定 Master1 & Master2 都還沒有安裝 MySQL database)

使用指令
rider@ocean:~$ sudo apt-get install mysql-server-5.0 mysql-client-5.0

步驟二 : 為了要確保同步設定有效,我們讓MySQL可以在該網路介面上(interface)任意開啟監聽的埠(port)並藉由編輯 /etc/mysql/my.cnf 將 bind-address註釋起來.

使用指令
rider@ocean:~$ sudo vim /etc/mysql/my.cnf

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
# bind-address            = 127.0.0.1

步驟三 : 重新啟動 MySQL

使用指令
rider@ocean:~$ sudo su
root@ocean:~$ /etc/init.d/mysql restart

步驟四 : 檢查MySQL所開啟的連接埠

使用指令
root@ocean:~$ netstat -tap | grep mysql

tcp        0      0 *:mysql                 *:*                     LISTEN      21678/mysqld

步驟五 : 設定 root@localhost 的MySQL使用者密碼

使用指令
root@ocean:~$ mysqladmin -u root password yourrootsqlpassword

/* @Master1 */

步驟六 : 設定 root@…的MySQL使用者密碼

使用指令
root@ocean:~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword

步驟七 : 設定一組同步使用者 slave2_user 給 Master2 能存取 Master1 的MySQL 資料庫的權限

使用指令
root@ocean:~$ mysql -u root -p
-> 登入MySQL

Enter password: rootmysqlpasswd
-> 輸入root 的 MySQL 登入密碼

GRANT REPLICATION SLAVE ON *.* TO 'slave2_user'@'%' IDENTIFIED BY 'slave2_password';
FLUSH PRIVILEGES;
quit;
-> 設定使用者: slave2_user 的同步權限並給予密碼

/* @Master2 */

步驟八 : 設定 root@…的MySQL使用者密碼,與設定一組同步使用者 slave1_user 給 Master1 能存取 Master2 的MySQL 資料庫的權限(重複步驟六&七: 設定 Master1 MySQL的方法)

使用指令

root@ocean:~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword

root@ocean:~$ mysql -u root -p
-> 登入MySQL

Enter password: rootmysqlpasswd
-> 輸入root 的 MySQL 登入密碼

GRANT REPLICATION SLAVE ON *.* TO 'slave1_user'@'%' IDENTIFIED BY 'slave1_password';
FLUSH PRIVILEGES;
quit;
-> 設定使用者: slave1_user 的同步權限並給予密碼

注意事項