Changes between Version 3 and Version 4 of MySQL_Replication
- Timestamp:
- Oct 7, 2008, 5:51:08 PM (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
MySQL_Replication
v3 v4 14 14 ''rider@ocean:~$ sudo apt-get install mysql-server-5.0 mysql-client-5.0''[[BR]] 15 15 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]] 20 20 {{{ 21 21 # Instead of skip-networking the default is now to listen only on … … 27 27 28 28 使用指令[[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]] 31 31 32 32 '''步驟四 :''' 檢查MySQL所開啟的連接埠 33 33 34 34 使用指令[[BR]] 35 ''root@ocean :~$ netstat -tap | grep mysql''[[BR]]35 ''root@ocean1:~$ netstat -tap | grep mysql''[[BR]] 36 36 {{{ 37 37 tcp 0 0 *:mysql *:* LISTEN 21678/mysqld … … 41 41 42 42 使用指令[[BR]] 43 ''root@ocean :~$ mysqladmin -u root password yourrootsqlpassword''[[BR]]43 ''root@ocean1:~$ mysqladmin -u root password yourrootsqlpassword''[[BR]] 44 44 45 45 '''/* @Master1 */''' [[BR]] … … 47 47 48 48 使用指令[[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]] 50 50 51 51 '''步驟七 :''' 設定一組同步使用者 slave2_user 給 Master2 能存取 Master1 的MySQL 資料庫的權限 52 52 53 53 使用指令[[BR]] 54 ''root@ocean :~$ mysql -u root -p''[[BR]] -> 登入MySQL54 ''root@ocean1:~$ mysql -u root -p''[[BR]] -> 登入MySQL 55 55 56 56 ''Enter password: rootmysqlpasswd''[[BR]] -> 輸入root 的 MySQL 登入密碼 57 57 58 ''mysql>''[[BR]] 58 59 ''GRANT REPLICATION SLAVE ON *.* TO 'slave2_user'@'%' IDENTIFIED BY 'slave2_password';[[BR]]FLUSH PRIVILEGES;[[BR]]quit;[[BR]]'' -> 設定使用者: slave2_user 的同步權限並給予密碼 59 60 … … 63 64 使用指令[[BR]] 64 65 65 ''root@ocean :~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword''[[BR]]66 67 ''root@ocean :~$ mysql -u root -p''[[BR]] -> 登入MySQL66 ''root@ocean2:~$ mysqladmin -h master1.nchc.org.tw -u root password yourrootsqlpassword''[[BR]] 67 68 ''root@ocean2:~$ mysql -u root -p''[[BR]] -> 登入MySQL 68 69 69 70 ''Enter password: rootmysqlpasswd''[[BR]] -> 輸入root 的 MySQL 登入密碼 70 71 72 ''mysql>''[[BR]] 71 73 ''GRANT REPLICATION SLAVE ON *.* TO 'slave1_user'@'%' IDENTIFIED BY 'slave1_password';[[BR]]FLUSH PRIVILEGES;[[BR]]quit;[[BR]]'' -> 設定使用者: slave1_user 的同步權限並給予密碼 72 74 … … 78 80 使用指令[[BR]] 79 81 80 ''root@ocean:~$ mysql -u root -p''[[BR]] -> 登入MySQL 81 82 ''root@ocean2:~$ mysql -u root -p''[[BR]] -> 登入MySQL 83 84 ''mysql>''[[BR]] 82 85 ''CREATE DATABASE exampledb;[[BR]]quit;[[BR]]'' 83 86 … … 92 95 93 96 '''/* @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]] 99 102 100 103 {{{ … … 135 138 使用指令[[BR]] 136 139 137 ''root@ocean :~$ /etc/init.d/mysql restart''[[BR]]140 ''root@ocean1:~$ /etc/init.d/mysql restart''[[BR]] 138 141 139 142 … … 143 146 使用指令[[BR]] 144 147 145 ''root@ocean :~$ vim /etc/mysql/my.cnf''[[BR]]148 ''root@ocean2:~$ vim /etc/mysql/my.cnf''[[BR]] 146 149 147 150 {{{ … … 179 182 }}} 180 183 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]] 190 194 ''USE exampledb;[[BR]]FLUSH TABLES WITH READ LOCK;[[BR]]SHOW MASTER STATUS;[[BR]]'' 191 195 … … 204 208 }}} 205 209 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 {{{ 257 mysql> SHOW MASTER STATUS; 258 +------------------+----------+--------------+------------------+ 259 | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | 260 +------------------+----------+--------------+------------------+ 261 | mysql-bin.000009 | 783 | exampledb | | 262 +------------------+----------+--------------+------------------+ 263 1 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