EXSI-5 內建立 VM二台
DB Server ( Debian6.0.3 )
需求 :
mysql replaction
master ns1.vtest.tv IP: 192.168.11.11
slave ns2.vtest.tv IP: 192.168.11.12
DB名稱 mytest
設定之前
1. 設定Master對Slave複製的動作之前,要先確認Mysql版本之間允許複製的相容性條件,參閱這個版本之間相容性的說明 http://dev.mysql.com/doc/refman/5.0/en/replication-compatibility.html。
A. Master DB 操作部分 ( 192.168.11.11)
Master要做的流程大概如下:1. 設定 一個同步的帳號(如果是用root,可以忽略此步驟)
2. 設定my.cnf
3. 將資料庫變成唯讀(read only)
4. 將mytest資料匯出(mysqldump或tar)
5. 解除資料唯讀狀態
6. 重新啟動mysql讓剛設定的my.cnf 生效
7. 將匯出的資料拷貝(scp)至slave
0. apt-get update 更新套件 list 並 安裝 MYSQL套件
apt-get update ; apt-get install libdbd-mysql-perl libmysqlclient16 mysql-client mysql-client-5.1 mysql-common mysql-server-5.1
mysql-server-core-5.1
1. 設定msql 可以開放本機之外的機器連線
vim /etc/mysql/my.cnf 內設定改成下面
關閉才能本機使用設定
#skip-external-locking
#bind-address = 127.0.0.1
設定log 放的地方
log_slow_queries = /var/log/mysql/mysql-slow.log
# Here you can see queries with especially long duration
log_slow_queries = /var/log/mysql/mysql-slow.log
重新啟動 mysql 看3306 port 是否有正常啟動
/etc/init.d/mysql restart
2. 設定一個同步的帳號repl,密碼為slavepass,讓repl具有REPLICATION的權限,相關指令如下:
先建立"mytest" DB
mysql> create database `mytest` ;
Query OK, 1 row affected (0.00 sec)
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytest |
+--------------------+
3 rows in set (0.00 sec)
2. 設定一個同步的帳號repl,密碼為slavepass,讓repl具有REPLICATION的權限,相關指令如下:
mysql> create user 'repl'@'%' identified by 'replpasswd' ;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'repl'@'%' ;
Query OK, 0 rows affected (0.00 sec)
3. Master主機必須產生binlog檔案,並且指定只有同步 mytest 這個資料庫,/etc/my.cnf 的修改如下:
[mysqld]
server-id = 1 #此id不可以重覆
log-bin = mysql-bin #指定產生binlog檔案的開頭檔名
binlog-do-db = mytest #只針對mytest db 產生binlog
4.複製Master上的資料,查詢目前寫到那
在Replication完成前,Master跟Slave的資料必須一致,故在匯出資料前先Lock成唯讀
mysql > FLUSH TABLES WITH READ LOCK;
mysql> show master status ;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 494 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
關閉Slave Mysql(如果Mysql沒有shutdown的話)。
# mysqladmin -u root –p shutdown
5. 將mytest資料庫整個匯出,此方式可以使用mysqldump或者是tar的方式把檔案壓縮起來
#tar -cvf mytest.tar.gz mytest.tar.gz #將tar起來的壓縮檔複製到Slave Mysql上資料存放的資料夾中,資料庫有innoDB時最好用這各備份。
#mysqldump -u root -p mysql > mysql.sql #將mysql的資料匯出成mysql.sql
6. 將資料匯出後,解除資料唯讀狀態
mysql>UNLOCK TABLES;
7. 重新啟動mysql,讓剛設定的my.cnf生效;重新啟動後,在Master主機上的add、update、delete等動作都會記錄在binlog檔案中
$/etc/init.d/mysql restart
8. 最後將mytest.sql拷貝(scp、rysnc )至slave主機
MySQL Replication Slave設定
Slave要做的流程大概如下:
1. 修改my.cnf
2. 匯入mytest資料庫的資料
3. 設定Master主機的相關訊息
4. 重新啟動mysql
0. apt-get update 更新套件 list 並 安裝 MYSQL套件及my.conf基本設定如上MASTER
1. 設定my.cnf相關訊息
[mysqld]
server-id = 2 #此id不可以重覆
log-slave-updates #告訴slave讀取binlog,啟動slave的重要選項之一
log-bin = mysql-bin #指定產生binlog檔案的開頭檔名
binlog_format = mixed #設定binlog的儲存格式,(maxed為預設值)
relay-log = host_name-relay-bin #記錄著binlog處理的過程,可以執行【FLUSH LOGS】讓mysql自動刪除較舊的檔案
replicate-do-db = mytest #限制slave只同步mytest資料庫的資料
master-connect-retry = 60 #當slave無法連線至master時,間隔60秒嘗試連線(預設為60秒)
2. 將mytest的資料匯入,匯入之前請先關閉 slave_mysql
#mysql -u root -p mytest < mytest.sql
或將資料給解壓縮覆蓋至/etc/mysql
# tar -xvf mytest.tar.gz
這樣子的動作是在於Master和Slave之間要同步的資料庫,在啟動Replication之前可以一致。
3. 設定Master主機的相關訊息,讓Slave知道Master的位置
請在mysql下執行底下指令
mysql>CHANGE MASTER TO
MASTER_HOST=’192.168.11.11′,
MASTER_USER=’repl’,
MASTER_PASSWORD=’slavepass’;
ps. 以往的方式是直接在my.cnf中設定master的相關訊息;但是,這樣的方式已經改了,必須使用在 mysql下執行【CHANGE MASTER TO】的指令
4. 重新啟動mysql
$/etc/init.d/mysql restart
設定完MySQL的M/S架構後,再來就是檢查及測試的動作了
5. 要如何確定mysql slave是否正常運作?(用mysql commone & phpmyadmin)
登入phpMyAdmin後,在【狀態】的分頁中往下拉,會看到一個Slave status的狀態表
如果底下2行都出現Yes的話表示slave是在執行中
Slave_IO_Running Yes
Slave_SQL_Running Yes
mysql 指令 :
觀察目前Master的狀態
mysql> show master status ;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000008 | 193 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql > show slave status\G;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.11.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 193
Relay_Log_File: host_name-relay-bin.000115
Relay_Log_Pos: 338
Relay_Master_Log_File: mysql-bin.000012
Slave_IO_Running: Yes -->有正常抄寫
Slave_SQL_Running: Yes -->有正常抄寫
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 193
Relay_Log_Space: 642
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
ERROR:
No query specified
在master 建立新的db來測試看slave是否有即時建立即成功
由於目前是新建立的db所以資料不用寫入slave 資料即可即時抄寫。
如果是有一堆資料的舊db 請使用mysql 指令設定Slaver機制
6. 設定這些參數到Slave Mysql運作
mysql> CHANGE MASTER TO
MASTER_HOST=’192.168.11.11′,
MASTER_USER=’repl’,
MASTER_PASSWORD=’replpasswd’,
MASTER_LOG_FILE=’mysql-bin.000003 ′,
MASTER_LOG_POS=494;
7. 登入phpmadmin 修改 slave 的 localhost ip 並鎖定Slave Mysql的帳號
鎖定Slave Mysql的所有帳號也包含root,只有select的權限,這樣子才能確保覆寫機制的同步性。
要如何關閉、啟動slave?
mysql>slave stop; #關閉slave服務
mysql>slave start; #啟動slave服務
注意事項:
A. 整個過程不論Master Mysql是否有shutwown,務必確保Master Mysql不會有資料的異動。
B. Slave Mysql的所有帳號一定要只剩select權限。
C. 有使用 INNODB 資料庫最好用tar 來備份,否則資料會備份不完全。
D. 後續補上防火牆設定規則
沒有留言:
張貼留言