= MySQL Replication = == How to Set Up Replication == 目的: 我們假定A中心有一台Server Master1 ,而B中心有一台Server Master2. 為了方便我們在更新 Master1 的資料庫時, B中心的 Master2 資料庫也能同時更新而做到資料同步.[[BR]] 實驗環境: Debian GNU/Linux Etch 4.0r4 with kernel 2.6.18[[BR]] === Part1: 安裝MySQL 5.0 === '''步驟一 :''' 安裝 MySQL (假定 Master1 & Master2 都還沒有安裝 MySQL database)[[BR]] 使用指令[[BR]] ''rider@ocean:~$ sudo apt-get install mysql-server-5.0 mysql-client-5.0''[[BR]] '''步驟二 :''' 為了要確保同步設定有效,我們讓MySQL可以在該網路介面上(interface)任意開啟監聽的埠(port)並藉由編輯 /etc/mysql/my.cnf 將 bind-address註釋起來. 使用指令[[BR]] ''rider@ocean:~$ sudo vim /etc/mysql/my.cnf''[[BR]] {{{ # 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 使用指令[[BR]] ''rider@ocean:~$ sudo su''[[BR]] ''root@ocean:~$ /etc/init.d/mysql restart''[[BR]] '''步驟四 :''' 檢查MySQL所開啟的連接埠 使用指令[[BR]] ''root@ocean:~$ netstat -tap | grep mysql''[[BR]] {{{ tcp 0 0 *:mysql *:* LISTEN 21678/mysqld }}} '''步驟五 :''' 設定 root@localhost 的MySQL使用者密碼 使用指令[[BR]] ''root@ocean:~$ mysqladmin -u root password yourrootsqlpassword''[[BR]] '''/* @Master1 */''' [[BR]] '''步驟六 :''' 設定 root@master1.nchc.org.tw的MySQL使用者密碼 使用指令[[BR]] ''root@ocean:~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword''[[BR]] '''步驟七 :''' 設定一組同步使用者 slave2_user 給 Master2 能存取 Master1 的MySQL 資料庫的權限 使用指令[[BR]] ''root@ocean:~$ mysql -u root -p''[[BR]] -> 登入MySQL ''Enter password: rootmysqlpasswd''[[BR]] -> 輸入root 的 MySQL 登入密碼 ''GRANT REPLICATION SLAVE ON *.* TO 'slave2_user'@'%' IDENTIFIED BY 'slave2_password';[[BR]]FLUSH PRIVILEGES;[[BR]]quit;[[BR]]'' -> 設定使用者: slave2_user 的同步權限並給予密碼 '''/* @Master2 */''' [[BR]] '''步驟八 :''' 設定 root@master2.nchc.org.tw的MySQL使用者密碼,與設定一組同步使用者 slave1_user 給 Master1 能存取 Master2 的MySQL 資料庫的權限(重複步驟六&七: 設定 Master1 MySQL的方法) 使用指令[[BR]] ''root@ocean:~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword''[[BR]] ''root@ocean:~$ mysql -u root -p''[[BR]] -> 登入MySQL ''Enter password: rootmysqlpasswd''[[BR]] -> 輸入root 的 MySQL 登入密碼 ''GRANT REPLICATION SLAVE ON *.* TO 'slave1_user'@'%' IDENTIFIED BY 'slave1_password';[[BR]]FLUSH PRIVILEGES;[[BR]]quit;[[BR]]'' -> 設定使用者: slave1_user 的同步權限並給予密碼 === 注意事項 ===