Changes between Version 2 and Version 3 of MySQL_Replication


Ignore:
Timestamp:
Oct 7, 2008, 4:46:25 PM (16 years ago)
Author:
rider
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • MySQL_Replication

    v2 v3  
    4444
    4545'''/* @Master1 */''' [[BR]]
    46 
    4746'''步驟六 :''' 設定 root@master1.nchc.org.tw的MySQL使用者密碼
    4847
     
    6059
    6160'''/* @Master2 */''' [[BR]]
    62 
    6361'''步驟八 :''' 設定 root@master2.nchc.org.tw的MySQL使用者密碼,與設定一組同步使用者 slave1_user 給 Master1 能存取 Master2 的MySQL 資料庫的權限(重複步驟六&七: 設定 Master1 MySQL的方法)
    6462
     
    7573=== 注意事項 ===
    7674
    77 
    78 
    79 
    80 
    81 
    82 
    83 
    84 
     75我們假設我們已經在 Master1 建夠好一個 exampledb 並且存有資料表與相關資料內容. 接著我們要做的就是設定 exampledb 的副本到 Master2 , 接著也可將 Master2 的 exampledb 複製喔份回去Master1. 在設定資料庫複製 (MySQL Replication) 的同時我們先在 Master2 上建一個空的資料庫:exampledb. [[BR]]
     76
     77'''/* @Master2 */''' [[BR]]
     78使用指令[[BR]]
     79
     80''root@ocean:~$ mysql -u root -p''[[BR]] -> 登入MySQL
     81
     82''CREATE DATABASE exampledb;[[BR]]quit;[[BR]]''
     83
     84=== Part2: 設定資料庫複製(Replication) ===
     85
     86首先我們要透過編輯 /etc/mysql/my.cnf 來完成 master-master replication 的設定, 而 master-master replication 最重要的兩個設定選項就是 auto_increment_increment & auto_increment_offset [[BR]]
     87
     88auto_increment_increment: 控制連續性增量的AUTO_INCREMENT值. [[BR]]
     89auto_increment_offset: 決定AUTO_INCREMENT欄位的起始值.  [[BR]]
     90
     91假設我們有 N 個 MySQL nodes (本範例是兩個nodes,所以 N=2),因此 "auto_increment_increment" 在每各node的值都是 2 , 而每各node都必須給 "value for auto_increment_offset" 一各不同的值(1, 2, ..., N). 而接下來我們便要開始設定本範例中的兩各 MySQL nodes. [[BR]]
     92
     93'''/* @Master1 */''' [[BR]]
     94'''步驟一 :''' 編輯 /etc/mysql/my.cnf -> 從[mysqld]開始新增以下的選項,並且將有衝突的相關選項給註釋掉.
     95
     96使用指令[[BR]]
     97
     98''root@ocean:~$ vim /etc/mysql/my.cnf''[[BR]]
     99
     100{{{
     101[...]
     102[mysqld]
     103#
     104# * Basic Settings
     105#
     106
     107#
     108# * IMPORTANT
     109#   If you make changes to these settings and your system uses apparmor, you may
     110#   also need to also adjust /etc/apparmor.d/usr.sbin.mysqld.
     111#
     112server-id = 1
     113replicate-same-server-id = 0
     114auto-increment-increment = 2
     115auto-increment-offset = 1
     116
     117master-host = 192.168.0.101
     118master-user = slave1_user
     119master-password = slave1_password
     120master-connect-retry = 60
     121replicate-do-db = exampledb
     122
     123log-bin = /var/log/mysql/mysql-bin.log
     124binlog-do-db = exampledb
     125
     126relay-log = /var/lib/mysql/slave-relay.log
     127relay-log-index = /var/lib/mysql/slave-relay-log.index
     128
     129expire_logs_days        = 10
     130max_binlog_size         = 500M
     131[...]
     132}}}
     133
     134'''步驟二 :''' 重新啟動 MySQL
     135使用指令[[BR]]
     136
     137''root@ocean:~$ /etc/init.d/mysql restart''[[BR]]
     138
     139
     140'''/* @Master2 */''' [[BR]]
     141'''步驟三 :''' 對 Master2 重複相同的設定步驟(步驟一 ~ 步驟二)-> 編輯 /etc/mysql/my.cnf -> 從[mysqld]開始新增以下的選項,並且將有衝突的相關選項給註釋掉.再重新啟動MySQL
     142
     143使用指令[[BR]]
     144
     145''root@ocean:~$ vim /etc/mysql/my.cnf''[[BR]]
     146
     147{{{
     148[...]
     149[mysqld]
     150#
     151# * Basic Settings
     152#
     153
     154#
     155# * IMPORTANT
     156#   If you make changes to these settings and your system uses apparmor, you may
     157#   also need to also adjust /etc/apparmor.d/usr.sbin.mysqld.
     158#
     159server-id = 2
     160replicate-same-server-id = 0
     161auto-increment-increment = 2
     162auto-increment-offset = 2
     163
     164master-host = 192.168.0.100
     165master-user = slave2_user
     166master-password = slave2_password
     167master-connect-retry = 60
     168replicate-do-db = exampledb
     169
     170log-bin= /var/log/mysql/mysql-bin.log
     171binlog-do-db = exampledb
     172
     173relay-log = /var/lib/mysql/slave-relay.log
     174relay-log-index = /var/lib/mysql/slave-relay-log.index
     175
     176expire_logs_days        = 10
     177max_binlog_size         = 500M
     178[...]
     179}}}
     180
     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
     190''USE exampledb;[[BR]]FLUSH TABLES WITH READ LOCK;[[BR]]SHOW MASTER STATUS;[[BR]]''
     191
     192接著應該會顯示如下的資訊 (請將他記下來之後會有用) [[BR]]
     193
     194{{{
     195mysql> SHOW MASTER STATUS;
     196+------------------+----------+--------------+------------------+
     197| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
     198+------------------+----------+--------------+------------------+
     199| mysql-bin.000009 |       98 | exampledb    |                  |
     200+------------------+----------+--------------+------------------+
     2011 row in set (0.00 sec)
     202
     203mysql>
     204}}}
     205
     206
     207
     208
     209