1. 程式人生 > 實用技巧 >Mysql主從配置和跳過事務

Mysql主從配置和跳過事務

Mysql主從配置和跳過事務

一、介紹:

大型網站中資料層還是原來那種傳統的資料架構,或者只是淡淡靠一臺伺服器來扛,如此多的資料庫連線操作,資料必然會崩潰,資料丟失的話,可想而知後果不堪設想。所以我們想到很多解決方法·:一方面採用優秀的程式碼框架,進行程式碼的優化,採用優秀的資料快取技術如:redis,如果資金豐厚的話,必然會想到架設伺服器群,來分擔主資料庫的壓力。然後重點到了今天介紹的:利用MySQL主從配置,實現讀寫分離,減輕資料庫壓力。這種方式,在如今很多網站裡都有使用,也不是什麼新鮮事情,今天總結一下,方便大家學習參考一下。

原理:主伺服器(Master)負責網站NonQuery

操作,從伺服器負責Query操作,使用者可以根據網站功能模特性塊固定訪問Slave伺服器,或者自己寫個池或佇列,自由為請求分配從伺服器連線。主從伺服器利用MySQL的二進位制日誌檔案,實現資料同步。二進位制日誌由主伺服器產生,從伺服器響應獲取同步資料庫。

拓撲圖:

f73c83a77fcd7496fafb9cf0a86829a4.png

Master---cml5:192.168.5.103

Slave---cml2:192.168.5.102

Slave---cml6:192.168.5.106

二、配置:

1、首先備份主(master)主機資料庫(全備),然後倒入到從(slave)主機上:

[[email protected]~]#mysqldump-uroot-predhat--single-transaction-R--triggers-E--master-data--flush-logs--all-databases>cml.sql
mysqldump:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.
[
[email protected]
mydata]#scpcml.sqlcml2:/data/ cml.sql100%16171.6KB/s00:00 [[email protected]data]#mysql-uroot-predhat<cml.sql mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.

2、編輯master主機的my.cnf配置檔案:

[[email protected]mydata]#cat/etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/mydata
socket=/usr/local/mysql/mysql.sock
log_bin=/usr/local/mysql/mydata/mysql-bin
server-id=1

##備註:server-id 伺服器唯一標識,log_bin 啟動MySQL二進位制日誌

3、主庫建立同步使用者:

mysql>grantallon*.*to'rsync'@'%'identifiedby'redhat';
QueryOK,0rowsaffected,1warning(0.00sec)

mysql>flushprivileges;
QueryOK,0rowsaffected(0.00sec)

4、檢視主庫上資料節點:

83d4694eb09433d85ab536f0d52d5758.png

5、修改slave上的my.cnf配置檔案:

[[email protected]mydata]#cat/etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/mydata
socket=/usr/local/mysql/mysql.sock
log_bin=/usr/local/mysql/mydata/mysql-bin
server-id=2
relay_log=relay-log

6、slave褲上面同步:

mysql>CHANGEMASTERTOMASTER_HOST='192.168.5.103',MASTER_USER='rsync',MASTER_PASSWORD='redhat',MASTER_LOG_FILE='mysql-bin.000011',MASTER_LOG_POS=587;
QueryOK,0rowsaffected,2warnings(0.00sec)
mysql>startslave;
QueryOK,0rowsaffected(0.01sec)

7、檢視狀態兩個執行緒必須都是YES:

mysql> show slave status\G;

*************************** 1. row***************************

Slave_IO_State: Connecting tomaster

Master_Host: 192.168.5.103

Master_User: rsync

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000011

Read_Master_Log_Pos: 587

Relay_Log_File: relay-log.000001

Relay_Log_Pos: 4

Relay_Master_Log_File: mysql-bin.000011

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

8、連線一臺空的DB,測試資料是否同步:

server-id=3
relay_log=relay-log

###測試:

mysql>CHANGEMASTERTOMASTER_HOST='192.168.5.103',MASTER_USER='rsync',MASTER_PASSWORD='redhat',MASTER_LOG_FILE='mysql-bin.000011',MASTER_LOG_POS=587;
QueryOK,0rowsaffected,2warnings(0.01sec)
mysql>startslave;

##因為之前已經建立了一張表,所以寫入肯定會出錯:
mysql> show slave status\G;

*************************** 1. row***************************

Slave_IO_State: Connecting tomaster

Master_Host: 192.168.5.103

Master_User: rsync

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000011

Read_Master_Log_Pos: 587

Relay_Log_File: relay-log.000001

Relay_Log_Pos: 4

Relay_Master_Log_File: mysql-bin.000011

Slave_IO_Running: Yes

Slave_SQL_Running: no
##線上可以直接跳過這個錯誤,但是這個事務的資料久找不到了:

mysql>stopslave;
QueryOK,0rowsaffected(0.00sec)
mysql>SETGLOBALSQL_SLAVE_SKIP_COUNTER=1;##跳過一個事務,多個就改變數字。
QueryOK,0rowsaffected(0.00sec)
mysql>startslave;
QueryOK,0rowsaffected(0.00sec)

##重設定slave:

mysql>stopslave;
QueryOK,0rowsaffected(0.00sec)

mysql>resetslave;
QueryOK,0rowsaffected(0.00sec)


轉載於:https://blog.51cto.com/legehappy/1981409