Part1:寫在最前mysql
在以前的博文中有說到MaxScale,做爲中間件,配合MHA使用或者主從使用可實現讀寫分離和負載均衡,今天簡單介紹下MaxScale做爲Binlog Server來減小主從延遲的問題;MySQL的主從架構中,鏈式拓撲的架構比較容易出現主從延遲的問題。本文着重介紹MaxScale做爲Binlog Server是如何下降主從延遲的。sql
MaxScale配合MHA請移步至:後端
http://suifu.blog.51cto.com/9167728/1869520 centos
Part2:本文環境bash
HE1:192.168.1.248 slave網絡
HE3:192.168.1.250 master架構
HE4:192.168.1.251 maxscale負載均衡
Part1:安裝maxscaleide
[root@HE4 ~]# yum -y install maxscale-2.0.1-2.centos.6.x86_64.rpmui
[root@HE4 ~]# mkdir -p /data/binlog
[root@HE4 ~]# useradd maxscale
[root@HE4 ~]# chown -R maxscale. /data/binlog
[root@HE4 ~]# cat /etc/maxscale.cnf
[maxscale] threads=1 ##根據CPU核數設置 [Replication] type=service router=binlogrouter user=mysync passwd=MANAGER # 使用主庫上的repl複製帳號 # 權限: # GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl'; router_options=server_id=1251,heartbeat=30,binlogdir=/data/binlog,transaction_safety=1,mariadb10-compatibility=1,send_slave_heartbeat=1 # server_id設置maxscale的,記得不能與主和從庫重複,要惟一 # heartbeat=30秒,意思爲當maxscale在30秒內沒有接收到主庫推送的binlog日誌,發送心跳檢查 # binlogdir設置接收binlog的存放路徑,目錄屬性chown -R maxscale.maxscale /data/binlog # transaction_safety=1此參數用於啓用binlog日誌中的不完整事務檢測。 當MariaDB MaxScale啓動時,若是當前binlog文件已損壞或找到不完整的事務,則可能會出現錯誤消息。 在正常工做期間,binlog事件不會分配到從庫,直到事務已經提交。 默認值爲off,設置transaction_safety = on以啓用不徹底事務檢測。 # send_slave_heartbeat=1開啓心跳檢查 [Replication Listener] type=listener service=Replication protocol=MySQLClient port=5308 # 後端的從庫CHANGE MASTER TO這個端口,默認5308 [CLI] type=service router=cli [CLI Listener] type=listener service=CLI protocol=maxscaled port=6603
Part2:啓動Maxscale
[root@HE4 ~]# /etc/init.d/maxscale start
Starting MaxScale: maxscale (pid 16680) is running... [ OK ]
[root@HE4 ~]# /etc/init.d/maxscale status
Checking MaxScale status: MaxScale (pid 16680) is running.[ OK ]
Part3:從庫配置
[root@HE1 ~]# mysql -umysync -pMANAGER -h192.168.1.251 -P5308 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 3196 Server version: 10.0.0 2.0.1-maxscale Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.250',MASTER_USER='mysync',MASTER_PASSWORD='MANAGER',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=20; ERROR 1234 (42000): Can not set MASTER_LOG_POS to 20: Permitted binlog pos is 4. Specified master_log_file=mysql-bin.000005 MySQL [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.250',MASTER_USER='mysync',MASTER_PASSWORD='MANAGER',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=4; MySQL [(none)]> start slave; Query OK, 0 rows affected (0.00 sec)
這裏能夠看出,Maxscale binlog server只能從位置4開始配置
配置好後,在/data/binlog下生成的binlog文件
[root@HE4 ~]# cd /data/binlog/
[root@HE4 binlog]# ls
cache master.ini mysql-bin.000003
Part4:主庫配置
[root@HE3 ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 7 Server version: 10.1.16-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000005 | 652 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) MariaDB [(none)]> grant replication client,replication slave on *.* to 'mysync'@'192.168.1.%' identified by 'MANAGER'; MariaDB [(none)]>flush privileges;
Part5:主從配置
從庫指向binlogserver [root@HE1 ~]# mysql -uroot -pMANAGER Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 10.1.16-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.251',MASTER_USER='mysync',MASTER_PASSWORD='MANAGER',MASTER_PORT=5308,MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=652; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.251 Master_User: mysync Master_Port: 5308 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 652 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 537 Relay_Master_Log_File: mysql-bin.000005 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: 652 Relay_Log_Space: 835 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 1250 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative 1 row in set (0.00 sec)
——總結——
生產環境中,大多采用的是一主多從架構,例如星狀拓撲和鏈式拓撲,星狀拓撲在從庫過多的狀況下,會增長主庫的io壓力,而鏈式拓撲雖然緩解了主庫的網絡IO壓力,但其缺點是:二級Slave獲得最新的數據,須要再通過一層的複製纔到達,期間的延遲比一主多從架構要大。而採用maxscale binlog server則避免了這類問題。因爲筆者的水平有限,編寫時間也很倉促,文中不免會出現一些錯誤或者不許確的地方,不妥之處懇請讀者批評指正。