MYSQL REPLICATION(MASTER SLAVE負載平衡)
站長們都知道,DB是SERVER最大的罩門,而DB最重要的就是I/O和備援。
其實這也不算新技術了,透過MySQL內健的功能REPLICATION,即可達到單向同步。
MySQL M/S架構的原理大致如下:
1. 新增(Add)、刪除(delete)、修改(update)的動作必須移至Master
2. Master會針對這些動作的語法自動產生binlog 檔案
3. Master會將binlog檔案同步(Replication)至所有的Slave
4. Slave會執行binlog檔案中的SQL語法
開始實做!
由於歷史包袱,我MySQL 是4.0版本
Master:192.168.10.2
Slave:192.168.10.3
只有同步(Replication) 資料庫mytest
Master及Slave最好是同個版本的MySQL
MySQL Replication Master設定
Master要做的流程大概如下:
1. 設定 一個同步的帳號(如果是用root,可以忽略此步驟)
2. 設定my.cnf
3. 重新啟動mysql讓剛設定的my.cnf 生效
下面針對Master流程的部份,做個詳細說明:
1. 設定一個同步的帳號repl,密碼為slavepass,讓repl具有REPLICATION的權限,
這邊直接進入phpMyAdmin設定即可,記得加入Salve的IP位置
2. Master主機必須產生binlog檔案,並且指定只有同步 mytest 這個資料庫,/etc/my.cnf 的修改如下:
[mysqld]
server-id = 1 #此id不可以重覆
log-bin = mysql-bin #指定產生binlog檔案的開頭檔名
binlog-do-db = mytest #只針對mytest產生binlog
這樣就設定完畢了!
MySQL Replication Slave設定
Slave要做的流程大概如下:
1. 修改my.cnf
2. 設定Master主機的相關訊息
3. 把Master的DB整個COPY過來,直接用rsync比較快!
3. 重新啟動mysql
下面針對Slave流程的部份,做個詳細說明:
1. 設定my.cnf相關訊息
[mysqld]
server-id = 2 #此id不可以重覆
log-bin = mysql-bin #指定產生binlog檔案的開頭檔名
replicate-do-db = mytest #限制slave只同步mytest資料庫的資料
master-connect-retry = 60 #當slave無法連線至master時,間隔60秒嘗試連線(預設為60秒)
master-host = 192.168.10.2
master-user = repl
master-password = slavepass
master-port = 3306
4. 重新啟動mysql
設定完MySQL的M/S架構後,再來就是檢查及測試的動作了
要如何確定mysql slave是否正常運作?
直接進入phpMyadmin看狀態
Slave_IO_Running Yes
Slave_SQL_Running Yes
當然您也可以在mysql下執行【show slave status;】但是,顯示出來的格式有點亂,看不清楚是否正在執行
要如何關閉、啟動slave?
mysql>slave stop; #關閉slave服務
mysql>slave start; #啟動slave服務
測試資料是否有同步?
在master上面更新mytest資料庫裡面的資料後,再至slave查看是否有更新,就可以確認了
MySQL 多台機器多重 Replication 設定方式
想要作到 A -> B -> C, 只需要於 B 的 [mysqld] 設定下述即可:
log-bin=mysql-bin
log-slave-updates
範例
[mysqld]
server-id = 1
log-bin=mysql-bin
log-slave-updates
===================================================================================
Webmasters are aware , DB is SERVER biggest Achilles Heel , but the most important thing is DB I / O and redundancy.
In fact, this is not a new technology , and through the MySQL functions within the health REPLICATION, you can achieve a one-way synchronization.
Principle MySQL M / S structure is as follows:
1 Add (Add), delete (delete), modify (update) the action must move to Master
2. Master binlog file will be automatically generated for the syntax of these actions
3. Master will binlog file synchronization (Replication) to all of the Slave
4. Slave binlog file will execute the SQL syntax
Real started doing !
Because of the historical burden , I MySQL is version 4.0
Master: 192.168.10.2
Slave: 192.168.10.3
Only Synchronization (Replication) database mytest
Master and Slave preferably the same version of MySQL
MySQL Replication Master Set
Master process to do something like this :
1 Set a synchronization account ( if you are using the root, you can ignore this step )
2 Set my.cnf
3 Restart mysql my.cnf let ‘s just set in force
For part of the Master following the process, make a detailed description :
1 Set an account repl synchronized password slavepass, let repl have REPLICATION permissions
Here you can set directly into phpMyAdmin , remember to join the IP position Salve
. 2 Master master must generate binlog file , and specify only sync mytest this database , / etc / my.cnf is modified as follows :
[mysqld]
server-id = 1 # This id can not repeat
log-bin = mysql-bin # binlog file generated at the beginning of the specified file name
binlog-do-db = mytest # mytest generated only for binlog
This sets finished !
MySQL Replication Slave settings
Slave process to do something like this :
1 Modify my.cnf
2 Set Master host of related information
3 The Master of DB over the entire COPY directly with rsync faster !
3 Restart mysql
For part of the Slave following the process, to be described in detail :
1 Set my.cnf related information
[mysqld]
server-id = 2 # This id can not repeat
log-bin = mysql-bin # binlog file generated at the beginning of the specified file name
replicate-do-db = mytest # restrict slave sync only mytest database information
When master-connect-retry = 60 # When the slave can not connect to the master, spaced 60 seconds trying to connect ( default is 60 seconds )
master-host = 192.168.10.2
master-user = repl
master-password = slavepass
master-port = 3306
4 Restart mysql
After setting the MySQL M / S structure , again is the action of the inspection and testing
How to determine mysql slave is working ?
PhpMyadmin look directly into the state
Slave_IO_Running Yes
Slave_SQL_Running Yes
Of course, you can also perform [ show slave status; } In mysql , however, show up in the format a bit messy , not clear whether it is executed
How do I enable or disable the slave?
mysql> slave stop; # Close slave Service
mysql> slave start; # start slave service
Are there test data synchronization ?
After updating the data inside the database mytest master above slave again to check for updates , you can confirm