Changes between Version 3 and Version 4 of MySQL_Replication


Ignore:
Timestamp:
Oct 7, 2008, 5:51:08 PM (16 years ago)
Author:
rider
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • MySQL_Replication

    v3 v4  
    1414''rider@ocean:~$ sudo apt-get install mysql-server-5.0 mysql-client-5.0''[[BR]]
    1515
    16 '''步驟二 :''' 為了要確保同步設定有效,我們讓MySQL可以在該網路介面上(interface)任意開啟監聽的埠(port)並藉由編輯 /etc/mysql/my.cnf 將 bind-address註釋起來.
    17 
    18 使用指令[[BR]]
    19 ''rider@ocean:~$ sudo vim /etc/mysql/my.cnf''[[BR]]
     16'''步驟二 :''' 為了要確保同步設定有效,我們讓MySQL可以在該網路介面上(interface)任意開啟監聽的埠(port)並藉由編輯 /etc/mysql/my.cnf 將 bind-address註釋起來
     17
     18使用指令[[BR]]
     19''rider@ocean1:~$ sudo vim /etc/mysql/my.cnf''[[BR]]
    2020{{{
    2121# Instead of skip-networking the default is now to listen only on
     
    2727
    2828使用指令[[BR]]
    29 ''rider@ocean:~$ sudo su''[[BR]]
    30 ''root@ocean:~$ /etc/init.d/mysql restart''[[BR]]
     29''rider@ocean1:~$ sudo su''[[BR]]
     30''root@ocean1:~$ /etc/init.d/mysql restart''[[BR]]
    3131
    3232'''步驟四 :''' 檢查MySQL所開啟的連接埠
    3333
    3434使用指令[[BR]]
    35 ''root@ocean:~$ netstat -tap | grep mysql''[[BR]]
     35''root@ocean1:~$ netstat -tap | grep mysql''[[BR]]
    3636{{{
    3737tcp        0      0 *:mysql                 *:*                     LISTEN      21678/mysqld
     
    4141
    4242使用指令[[BR]]
    43 ''root@ocean:~$ mysqladmin -u root password yourrootsqlpassword''[[BR]]
     43''root@ocean1:~$ mysqladmin -u root password yourrootsqlpassword''[[BR]]
    4444
    4545'''/* @Master1 */''' [[BR]]
     
    4747
    4848使用指令[[BR]]
    49 ''root@ocean:~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword''[[BR]]
     49''root@ocean1:~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword''[[BR]]
    5050
    5151'''步驟七 :''' 設定一組同步使用者 slave2_user 給 Master2 能存取 Master1 的MySQL 資料庫的權限
    5252
    5353使用指令[[BR]]
    54 ''root@ocean:~$ mysql -u root -p''[[BR]] -> 登入MySQL
     54''root@ocean1:~$ mysql -u root -p''[[BR]] -> 登入MySQL
    5555
    5656''Enter password: rootmysqlpasswd''[[BR]] -> 輸入root 的 MySQL 登入密碼
    5757
     58''mysql>''[[BR]]
    5859''GRANT REPLICATION SLAVE ON *.* TO 'slave2_user'@'%' IDENTIFIED BY 'slave2_password';[[BR]]FLUSH PRIVILEGES;[[BR]]quit;[[BR]]'' -> 設定使用者: slave2_user 的同步權限並給予密碼
    5960
     
    6364使用指令[[BR]]
    6465
    65 ''root@ocean:~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword''[[BR]]
    66 
    67 ''root@ocean:~$ mysql -u root -p''[[BR]] -> 登入MySQL
     66''root@ocean2:~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword''[[BR]]
     67
     68''root@ocean2:~$ mysql -u root -p''[[BR]] -> 登入MySQL
    6869
    6970''Enter password: rootmysqlpasswd''[[BR]] -> 輸入root 的 MySQL 登入密碼
    7071
     72''mysql>''[[BR]]
    7173''GRANT REPLICATION SLAVE ON *.* TO 'slave1_user'@'%' IDENTIFIED BY 'slave1_password';[[BR]]FLUSH PRIVILEGES;[[BR]]quit;[[BR]]'' -> 設定使用者: slave1_user 的同步權限並給予密碼
    7274
     
    7880使用指令[[BR]]
    7981
    80 ''root@ocean:~$ mysql -u root -p''[[BR]] -> 登入MySQL
    81 
     82''root@ocean2:~$ mysql -u root -p''[[BR]] -> 登入MySQL
     83
     84''mysql>''[[BR]]
    8285''CREATE DATABASE exampledb;[[BR]]quit;[[BR]]''
    8386
     
    9295
    9396'''/* @Master1 */''' [[BR]]
    94 '''步驟一 :''' 編輯 /etc/mysql/my.cnf -> 從[mysqld]開始新增以下的選項,並且將有衝突的相關選項給註釋掉.
    95 
    96 使用指令[[BR]]
    97 
    98 ''root@ocean:~$ vim /etc/mysql/my.cnf''[[BR]]
     97'''步驟一 :''' 編輯 /etc/mysql/my.cnf -> 從[mysqld]開始新增以下的選項,並且將有衝突的相關選項給註釋掉
     98
     99使用指令[[BR]]
     100
     101''root@ocean1:~$ vim /etc/mysql/my.cnf''[[BR]]
    99102
    100103{{{
     
    135138使用指令[[BR]]
    136139
    137 ''root@ocean:~$ /etc/init.d/mysql restart''[[BR]]
     140''root@ocean1:~$ /etc/init.d/mysql restart''[[BR]]
    138141
    139142
     
    143146使用指令[[BR]]
    144147
    145 ''root@ocean:~$ vim /etc/mysql/my.cnf''[[BR]]
     148''root@ocean2:~$ vim /etc/mysql/my.cnf''[[BR]]
    146149
    147150{{{
     
    179182}}}
    180183
    181 ''root@ocean:~$ /etc/init.d/mysql restart''[[BR]]
    182 
    183 '''/* @Master1 */''' [[BR]]
    184 '''步驟四 :''' 我們將鎖定在 Master1 上的 exampledb 資料庫, 且查詢 Master1 的主機狀態資訊而將 exampledb 給 dump(傾倒)出來 (因為我們待會要將傾倒出來的 exampledb 匯入到 Master2去,這樣兩各nodes的資料庫都將會有相同的資料), 最後將 exampledb 資料庫給解鎖(unlock)以便後續使用.[[BR]]
    185 
    186 使用指令[[BR]]
    187 
    188 ''root@ocean:~$ mysql -u root -p''[[BR]]
    189 
     184''root@ocean2:~$ /etc/init.d/mysql restart''[[BR]]
     185
     186'''/* @Master1 */''' [[BR]]
     187'''步驟四 :''' 我們將鎖定在 Master1 上的 exampledb 資料庫, 且查詢 Master1 的主機狀態資訊而將 exampledb 給 dump(傾倒)出來 (因為我們待會要將傾倒出來的 exampledb 匯入到 Master2去,這樣兩各nodes的資料庫都將會有相同的資料), 最後將 exampledb 資料庫給解鎖(unlock)以便後續使用[[BR]]
     188
     189使用指令[[BR]]
     190
     191''root@ocean1:~$ mysql -u root -p''[[BR]]
     192
     193''mysql>''[[BR]]
    190194''USE exampledb;[[BR]]FLUSH TABLES WITH READ LOCK;[[BR]]SHOW MASTER STATUS;[[BR]]''
    191195
     
    204208}}}
    205209
    206 
    207 
    208 
    209 
     210說明:[[BR]]
     211這時暫時還請勿離開 MySQL命令列(MySQL shell). 這時如果下exit指令而離開的話資料庫鎖定(database lock)將會被移除,因為我們必須先做完 MySQL dump(資料庫傾倒). 由於我們還有一各MySQL shell在開啟狀態,所以我們得藉由開啟第二各指令對話窗(command line window)來做 MySQL dump 而產生snapshot.sql並且透過 SCP(Secure Copy) 來傳送至Master2.[[BR]]
     212
     213'''/* @Master1 */''' [[BR]]
     214'''步驟五 :''' 在透過第二各指令對話窗(command line window)將資料庫給傾倒出來並SCP過去到遠端的 Master2的 /tmp資料夾暫存後便可關閉視窗二. 亦可離開並關閉第一視窗的 MySQL shell 藉以將資料庫解鎖
     215
     216使用指令[[BR]]
     217
     218''root@ocean1:~$ cd /tmp''[[BR]]
     219
     220''root@ocean1:~$ mysqldump -u root -pyourrootsqlpassword --opt exampledb > snapshot.sql''[[BR]]
     221
     222''root@ocean1:~$ scp snapshot.sql root@192.168.0.101:/tmp'' (Master2 IP: 192.168.0.101) [[BR]]
     223
     224'''步驟六 :''' 可以離開並關閉第一各指令對話窗(command line window)的 MySQL shell 藉以將資料庫解鎖
     225
     226使用指令[[BR]]
     227
     228''mysql> UNLOCK TABLES;''[[BR]]
     229
     230''mysql> quit;''[[BR]]
     231
     232'''/* @Master2 */''' [[BR]]
     233'''步驟七 :''' 我們可以匯入由 Master1 所 SQL dump 出來的 snapshot.sql 到 Master2 的 MySQL 資料庫
     234
     235使用指令[[BR]]
     236
     237''root@ocean2:~$ /usr/bin/mysqladmin --user=root --password=yourrootsqlpassword stop-slave''[[BR]]
     238
     239''root@ocean2:~$ cd /tmp''[[BR]]
     240
     241''root@ocean2:~$ mysql -u root -pyourrootsqlpassword exampledb < snapshot.sql''[[BR]]
     242
     243'''/* @Master2 */''' [[BR]]
     244'''步驟八 :''' 接著我們去查詢 Master2 的主機狀態並將他紀錄下來待會會用到
     245
     246使用指令[[BR]]
     247
     248''root@ocean2:~$ mysql -u root -p''[[BR]]
     249
     250''mysql> USE exampledb;''[[BR]]
     251
     252''mysql> FLUSH TABLES WITH READ LOCK;''[[BR]]
     253
     254''mysql> SHOW MASTER STATUS;''[[BR]]
     255
     256{{{
     257mysql> SHOW MASTER STATUS;
     258+------------------+----------+--------------+------------------+
     259| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
     260+------------------+----------+--------------+------------------+
     261| mysql-bin.000009 |      783 | exampledb    |                  |
     262+------------------+----------+--------------+------------------+
     2631 row in set (0.00 sec)
     264}}}
     265
     266'''/* @Master2 */''' [[BR]]
     267'''步驟九 :''' 然後我們解除該狀態表格的鎖定,並且透過以下指令來使得 Master2 成為 Master1的從屬(slave)之一
     268
     269使用指令[[BR]]
     270
     271''mysql> UNLOCK TABLES;''[[BR]]
     272
     273''mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.100', MASTER_USER='slave2_user', MASTER_PASSWORD='slave2_password', MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=98;''[[BR]]
     274
     275'''/* @Master2 */''' [[BR]]
     276'''步驟十 :''' 最後啟動該從屬(slave)關係
     277
     278使用指令[[BR]]
     279
     280''mysql> START SLAVE;''[[BR]]
     281
     282'''/* @Master2 */''' [[BR]]
     283'''步驟十一 :''' 檢查該從屬(slave)狀態
     284
     285使用指令[[BR]]
     286
     287''mysql> SHOW SLAVE STATUS;''[[BR]]
     288
     289
     290
     291
     292