1、Maxscale簡介前端
MaxScale 是 MariaDB 開發的一個數據庫智能代理服務,容許根據數據庫 SQL 語句將請求轉向目標一個到多個服務器,可設定各類複雜程度的轉向規則。MaxScale 設計用於透明的提供數據庫的負載均衡和高可用性,同時提供高度可伸縮和靈活的架構,支持不一樣的協議和路由決策。MaxScale 使用 C 語言開發,利用 Linux 下的異步 I/O 功能。使用 epoll 做爲事件驅動框架。
Maxscale有兩種方式實現讀/寫分離。一種是基於connect的,相似於Haproxy,不解析SQL語句,能夠經過PHP Yii框架或Java Mybatis框架實現。在此方式中,用Maxscale作多臺slave的負載均衡,而且支持主從同步延遲檢測功能。 java
另外一種是基於statement的,要解析SQL語句。在這種方式裏,前端程序不須要修改,經過Maxscale對SQL語句進行解析,把讀/寫請求自動路由到後端數據庫節點上,從而實現讀/寫分離。商業軟件OneProxy中間件也是基於statement方式實現讀/寫分離的。這種方式的好處是不修改程序代碼,減小了複雜度,可平滑遷移,無感知;缺點是解析SQL勢必會增長CPU的性能損耗,性能沒有基於connect的方式好。mysql
2、實驗環境算法
本文利用一主兩從的複製架構介紹Maxscale讀寫分離的實現,環境以下:sql
角色 | 主機名 | IP地址 | 操做系統 | 軟件版本 | 備註 |
Maxscale | maxscale.ueb.cn | 172.16.4.101 | CentOS release 6.8 (Final) x86_64 | maxscale-2.1.4 | 讀寫分離中間件 |
MasterDB | masterdb.ueb.cn | 172.16.4.212 | CentOS release 6.8 (Final) x86_64 | MariaDB-10.1.24 | 寫入操做 |
SlaveDB01 | slavedb01.ueb.cn | 172.16.4.214 | CentOS release 6.8 (Final) x86_64 | MariaDB-10.1.24 | 讀取操做 |
SlaveDB02 | slavedb02.ueb.cn | 172.16.4.215 | CentOS release 6.8 (Final) x86_64 | MariaDB-10.1.24 | 讀取操做 |
3、主從配置數據庫
略過。參見《MySQL5.6:基於GTID的主從複製》vim
4、下載安裝後端
官方下載地址:https://mariadb.com/downloads/mariadb-tx/maxscale 可根據操做系統類型,選擇合適的版本。緩存
1 [root@maxscale local]# wget https://downloads.mariadb.com/MaxScale/2.1.4/rhel/6/x86_64/maxscale-2.1.4-1.rhel.6.x86_64.rpm 2 [root@maxscale ~]# rpm -ivh maxscale-2.1.4-1.rhel.6.x86_64.rpm 3 warning: maxscale-2.1.4-1.rhel.6.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 8167ee24: NOKEY 4 Preparing... ########################################### [100%] 5 1:maxscale ########################################### [100%] 6 [root@maxscale ~]#
5、配置MaxScale實現讀寫分離服務器
5.1 建立加密文件
1 [root@maxscale local]# maxkeys /var/lib/maxscale/
5.2 建立加密密碼
1 [root@maxscale local]# maxpasswd /var/lib/maxscale/ 2wsx#EDC 2 C100888B653922DAEBBB26540D504250 3 [root@maxscale local]#
5.3 修改文件描述符65535以達到tcp的最大鏈接數
1 [root@maxscale local]# vim /etc/security/limits.conf 2 * soft nofile 65535 3 * hard nofile 65535 4 * soft nproc 65535 5 * hard nproc 65535 6 [root@maxscale local]# vim /etc/sysctl.conf 7 fs.file-max=65535 8 net.ipv4.tcp_fin_timeout = 30 9 net.ipv4.tcp_tw_reuse = 1 10 net.ipv4.tcp_tw_recycle = 1 11 net.ipv4.ip_local_port_range = 1024 65000 12 net.ipv4.tcp_max_syn_backlog = 65536 13 net.ipv4.tcp_max_tw_buckets = 20000 14 net.ipv4.route.gc_timeout = 100 15 net.ipv4.tcp_syn_retries = 1 16 net.ipv4.tcp_synack_retries = 1 17 net.core.somaxconn = 65535 18 net.core.netdev_max_backlog = 262144 19 net.ipv4.tcp_timestamps = 0 20 net.ipv4.tcp_max_orphans = 262144 21 [root@maxscale local]# 22 [root@maxscale local]# sysctl -p 23 net.ipv4.ip_forward = 0 24 net.ipv4.conf.default.rp_filter = 1 25 net.ipv4.conf.default.accept_source_route = 0 26 kernel.sysrq = 0 27 kernel.core_uses_pid = 1 28 net.ipv4.tcp_syncookies = 1 29 kernel.msgmnb = 65536 30 kernel.msgmax = 65536 31 kernel.shmmax = 68719476736 32 kernel.shmall = 4294967296 33 fs.file-max = 65535 34 net.ipv4.tcp_fin_timeout = 30 35 net.ipv4.tcp_tw_reuse = 1 36 net.ipv4.tcp_tw_recycle = 1 37 net.ipv4.ip_local_port_range = 1024 65000 38 net.ipv4.tcp_max_syn_backlog = 65536 39 net.ipv4.tcp_max_tw_buckets = 20000 40 net.ipv4.route.gc_timeout = 100 41 net.ipv4.tcp_syn_retries = 1 42 net.ipv4.tcp_synack_retries = 1 43 net.core.somaxconn = 65535 44 net.core.netdev_max_backlog = 262144 45 net.ipv4.tcp_timestamps = 0 46 net.ipv4.tcp_max_orphans = 262144 47 [root@maxscale local]#
5.4 配置Maxscale服務,配置文件以下:
1 [root@maxscale local]# cat /etc/maxscale.cnf 2 [maxscale] 3 threads=4 4 auth_connect_timeout=10 5 auth_read_timeout =10 6 auth_write_timeout=10 7 ms_timestamp=1 8 skip_permission_checks =false 9 syslog=1 10 maxlog=1 11 log_notice=1 12 log_throttling=10,1000,10000 13 14 # Server definitions 15 [server1] 16 type=server 17 address=172.16.4.212 18 port=3306 19 protocol=MySQLBackend 20 21 [server2] 22 type=server 23 address=172.16.4.214 24 port=3306 25 protocol=MySQLBackend 26 27 [server3] 28 type=server 29 address=172.16.4.215 30 port=3306 31 protocol=MySQLBackend 32 33 34 [MySQL Monitor] 35 type=monitor 36 module=mysqlmon 37 servers=server1,server2,server3 38 user=monitor 39 passwd=C100888B653922DAEBBB26540D504250 40 monitor_interval=10000 41 detect_stale_master=1 42 detect_replication_lag=1 43 44 [Read-Only Service] 45 type=service 46 router=readconnroute 47 servers=server2,server3 48 user=super 49 passwd=C100888B653922DAEBBB26540D504250 50 router_options=slave 51 52 [Read-Write Service] 53 type=service 54 router=readwritesplit 55 servers=server1,server2,server3 56 user=super 57 passwd=C100888B653922DAEBBB26540D504250 58 max_slave_replication_lag=5 59 enable_root_user=1 60 version_string=5.5.37-MariaDB-RWsplit 61 connection_timeout=300 62 max_connections=2000 63 log_auth_warnings=1 64 auth_all_servers=1 65 strip_db_esc=1 66 retry_on_failure=1 67 max_slave_connections=100% 68 69 [MaxAdmin Service] 70 type=service 71 router=cli 72 73 [Read-Only Listener] 74 type=listener 75 service=Read-Only Service 76 protocol=MySQLClient 77 port=4008 78 79 [Read-Write Listener] 80 type=listener 81 service=Read-Write Service 82 protocol=MySQLClient 83 port=4006 84 85 [MaxAdmin Listener] 86 type=listener 87 service=MaxAdmin Service 88 protocol=maxscaled 89 socket=default
5.5 啓動MaxScale服務:
1 [root@maxscale maxscale]# /etc/init.d/maxscale start 2 Starting MaxScale: maxscale (pid 3489) is running... [ OK ] 3 [root@maxscale maxscale]# netstat -tnlp 4 Active Internet connections (only servers) 5 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 6 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1871/sshd 7 tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2035/sshd 8 tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 2891/sshd 9 tcp 0 0 :::4006 :::* LISTEN 3489/maxscale 10 tcp 0 0 :::4008 :::* LISTEN 3489/maxscale 11 tcp 0 0 :::22 :::* LISTEN 1871/sshd 12 tcp 0 0 ::1:6010 :::* LISTEN 2035/sshd 13 tcp 0 0 ::1:6011 :::* LISTEN 2891/sshd 14 [root@maxscale maxscale]#
能夠看到讀寫分離端口4006和只讀端口4008已經啓動。
6、測試驗證讀寫分離
6.1 基於connect方式的測試
6.1.1 從庫進行Load Balance負載均衡測試
測試以前,在slavedb01(172.16.4.214)、slavedb02(172.16.4.215)上打開General Log,命令:set global general_log=1;
經過客戶端鏈接Maxscale 4008端口,命令以下:
1 [root@maxscale maxscale]# mysql -usuper -p -h 172.16.4.101 -P4008 2 Enter password: 3 Welcome to the MariaDB monitor. Commands end with ; or \g. 4 Your MariaDB connection id is 3493 5 Server version: 5.5.37-MariaDB-RWsplit MariaDB Server 6 7 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. 8 9 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 10 11 MariaDB [(none)]> select user,host,password from mysql.user; 12 +---------+------------------+-------------------------------------------+ 13 | user | host | password | 14 +---------+------------------+-------------------------------------------+ 15 | root | localhost | | 16 | root | slavedb01.ueb.cn | | 17 | root | 127.0.0.1 | | 18 | root | ::1 | | 19 | | localhost | | 20 | | slavedb01.ueb.cn | | 21 | super | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 22 | monitor | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 23 +---------+------------------+-------------------------------------------+ 24 8 rows in set (0.00 sec) 25 26 MySQL [(none)]>
在slavedb01和slavedb02上查看General Log,得知把請求輪訓轉發給了slavedb01和slavedb02
1 [root@slavedb01 mysqllogs]# tail -f general.log 2 .......................省略N行............................ 3 170725 14:29:32 9 Query select user,host,password from mysql.user 4 170725 14:29:40 5 Query SELECT @@server_id, @@read_only 5 ...............省略N行.........................
6.1.2 模擬slave延遲
在slavedb01(172.16.4.214)上執行flush tables with read lock全局讀鎖
1 MariaDB [(none)]> show slave status\G; 2 MariaDB [(none)]> show slave status\G; 3 *************************** 1. row *************************** 4 Slave_IO_State: Waiting for master to send event 5 Master_Host: 172.16.4.212 6 Master_User: rpl_user 7 Master_Port: 3306 8 Connect_Retry: 60 9 Master_Log_File: mysql-bin.000003 10 Read_Master_Log_Pos: 321335 11 Relay_Log_File: slavedb01-relay-bin.000010 12 Relay_Log_Pos: 300281 13 Relay_Master_Log_File: mysql-bin.000003 14 Slave_IO_Running: Yes 15 Slave_SQL_Running: Yes 16 Replicate_Do_DB: 17 Replicate_Ignore_DB: 18 Replicate_Do_Table: 19 Replicate_Ignore_Table: 20 Replicate_Wild_Do_Table: 21 Replicate_Wild_Ignore_Table: 22 Last_Errno: 0 23 Last_Error: 24 Skip_Counter: 0 25 Exec_Master_Log_Pos: 320871 26 Relay_Log_Space: 301047 27 Until_Condition: None 28 Until_Log_File: 29 Until_Log_Pos: 0 30 Master_SSL_Allowed: No 31 Master_SSL_CA_File: 32 Master_SSL_CA_Path: 33 Master_SSL_Cert: 34 Master_SSL_Cipher: 35 Master_SSL_Key: 36 Seconds_Behind_Master: 12 37 Master_SSL_Verify_Server_Cert: No 38 Last_IO_Errno: 0 39 Last_IO_Error: 40 Last_SQL_Errno: 0 41 Last_SQL_Error: 42 Replicate_Ignore_Server_Ids: 43 Master_Server_Id: 212 44 Master_SSL_Crl: 45 Master_SSL_Crlpath: 46 Using_Gtid: No 47 Gtid_IO_Pos: 48 Replicate_Do_Domain_Ids: 49 Replicate_Ignore_Domain_Ids: 50 Parallel_Mode: conservative 51 1 row in set (0.00 sec) 52 53 ERROR: No query specified 54 55 MariaDB [(none)]>
經過查看複製狀態能夠得知已經延遲12秒。那麼如今經過客戶端鏈接Maxscale 4008端口
1 [root@maxscale ~]# mysql -usuper -p -h 172.16.4.101 -P4008 2 Enter password: 3 Welcome to the MariaDB monitor. Commands end with ; or \g. 4 Your MySQL connection id is 3493 5 Server version: 10.0.0 2.1.4-maxscale MariaDB Server 6 7 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. 8 9 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 10 11 MySQL [(none)]> select user,host,password from mysql.user; 12 +---------+------------------+-------------------------------------------+ 13 | user | host | password | 14 +---------+------------------+-------------------------------------------+ 15 | root | localhost | | 16 | root | slavedb01.ueb.cn | | 17 | root | 127.0.0.1 | | 18 | root | ::1 | | 19 | | localhost | | 20 | | slavedb01.ueb.cn | | 21 | super | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 22 | monitor | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 23 +---------+------------------+-------------------------------------------+ 24 8 rows in set (0.00 sec) 25 26 MySQL [(none)]>
在slavedb01(172.16.4.214)上查看General Log,能夠得知複製延遲超過5秒的客戶端仍舊會轉發到slavedb01上。基於connect方式在maxscale2.1.4版本里,沒法實現延遲檢測功能。
1 [root@slavedb01 mysqllogs]# tail -f general.log 2 ......................省略N行....................... 3 170725 14:45:30 9 Query select user,host,password from mysql.user 4 170725 14:45:36 5 Query SELECT @@server_id, @@read_only 5 5 Query SHOW ALL SLAVES STATUS 6 5 Query SELECT master_timestamp FROM maxscale_schema.replication_heartbeat WHERE maxscale_id = 0 AND master_server_id = 212 7 ....................省略N行........................
6.1.3 模擬1臺slave故障
在slavedb01(172.16.4.214)上執行stop slave io_thread或 stop slave sql_thread,模擬slavedb01複製故障
1 MariaDB [(none)]> stop slave io_thread; 2 Query OK, 0 rows affected (0.05 sec) 3 MariaDB [(none)]>
經過客戶端鏈接maxscale 4008端口,並執行查詢操做:
1 [root@maxscale ~]# mysql -usuper -p -h 172.16.4.101 -P4008 2 Enter password: 3 Welcome to the MariaDB monitor. Commands end with ; or \g. 4 Your MySQL connection id is 3493 5 Server version: 10.0.0 2.1.4-maxscale MariaDB Server 6 7 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. 8 9 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 10 11 MySQL [(none)]> select user,host,password from mysql.user; 12 +---------+------------------+-------------------------------------------+ 13 | user | host | password | 14 +---------+------------------+-------------------------------------------+ 15 | root | localhost | | 16 | root | slavedb01.ueb.cn | | 17 | root | 127.0.0.1 | | 18 | root | ::1 | | 19 | | localhost | | 20 | | slavedb01.ueb.cn | | 21 | super | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 22 | monitor | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 23 +---------+------------------+-------------------------------------------+ 24 8 rows in set (0.00 sec) 25 26 MySQL [(none)]>
分別在slavedb01和slavedb02上查看General Log,能夠得知此次查詢操做被轉發到slavedb02(172.16.4.215)上
1 [root@slavedb02 mysqllogs]# tail -f general.log 2 170725 14:56:59 23 Connect super@172.16.4.101 as anonymous on 3 23 Query select user,host,password from mysql.user 4 170725 14:57:00 17 Query SELECT @@server_id, @@read_only 5 17 Query SHOW ALL SLAVES STATUS 6 17 Query SELECT master_timestamp FROM maxscale_schema.replication_heartbeat WHERE maxscale_id = 0 AND master_server_id = 212 7 7 Query BEGIN 8 .....................省略N行.....................
6.1.4 模擬所有的slave故障
在slavedb02(172.16.4.214)和slavedb02(172.16.4.215)上執行stop slave;
1 MariaDB [(none)]> stop salve;
經過客戶端鏈接maxscale的4008端口,並執行查詢操做
1 [root@maxscale ~]# mysql -usuper -p -h 172.16.4.101 -P4008 2 Enter password: 3 Welcome to the MariaDB monitor. Commands end with ; or \g. 4 Your MySQL connection id is 4534 5 Server version: 10.0.0 2.1.4-maxscale MariaDB Server 6 7 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. 8 9 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 10 11 MySQL [(none)]> select user,host,password from mysql.user; 12 +----------+-----------------+-------------------------------------------+ 13 | user | host | password | 14 +----------+-----------------+-------------------------------------------+ 15 | root | localhost | | 16 | root | masterdb.ueb.cn | | 17 | root | 127.0.0.1 | | 18 | root | ::1 | | 19 | | localhost | | 20 | | masterdb.ueb.cn | | 21 | rpl_user | 172.16.4.214 | *C674C5817CE4A2679CCFE1F603339AE0BEB5FD0A | 22 | rpl_user | 172.16.4.215 | *C674C5817CE4A2679CCFE1F603339AE0BEB5FD0A | 23 | super | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 24 | monitor | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 25 +----------+-----------------+-------------------------------------------+ 26 10 rows in set (0.00 sec) 27 28 MySQL [(none)]>
在masterdb(172.16.4.212)上查看General Log,能夠得知全部的請求將只轉發給masterdb(172.16.4.212),slave上並不會有請求轉發,實現了平滑故障轉移。
1 [root@masterdb ~]# tail -f /data/mysqllogs/general.log 2 ...................省略N行................... 3 170725 15:15:22 32 Query SELECT @@server_id, @@read_only 4 32 Query SHOW ALL SLAVES STATUS 5 170725 15:15:30 37 Query select user,host,password from mysql.user 6 170725 15:15:32 32 Query SELECT @@server_id, @@read_only 7 32 Query SHOW ALL SLAVES STATUS 8 170725 15:15:42 32 Query SELECT @@server_id, @@read_only 9 32 Query SHOW ALL SLAVES STATUS 10 .......................省略N行......................
6.2 基於statement方式(SQL解析)的測試
測試以前,在masterdb(172.16.4.212)、slavedb01(172.16.4.214)、slavedb02(172.16.4.215)上打開General Log,命令:set global general_log=1;
6.2.1 讀寫分離測試
經過客戶端鏈接Maxscale 4006端口
1 [root@maxscale ~]# mysql -usuper -p -h 172.16.4.101 -P4006 2 Enter password: 3 Welcome to the MariaDB monitor. Commands end with ; or \g. 4 Your MariaDB connection id is 4634 5 Server version: 5.5.37-MariaDB-RWsplit MariaDB Server 6 7 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. 8 9 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 10 11 MariaDB [(none)]>
執行如下寫操做:
1 MariaDB [(none)]> CREATE DATABASE ueb_cmdb; 2 ERROR 2006 (HY000): MySQL server has gone away 3 No connection. Trying to reconnect... 4 Connection id: 4634 5 Current database: *** NONE *** 6 7 ` varchar(32) DEFAULT NULL, 8 `cpu_info` varchar(32) NOT NULL, 9 `mem_info` varchar(32) NOT NULL, 10 `disk_info` varchar(32) NOT NULL, 11 `status` int(11) NOT NULL, 12 `add_time` date NOT NULL, 13 PRIMARY KEY (`id`) 14 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;Query OK, 1 row affected (0.06 sec) 15 16 MariaDB [(none)]> USE ueb_cmdb; 17 Database changed 18 MariaDB [ueb_cmdb]> CREATE TABLE `cmdb_hostsinfo` ( 19 -> `id` int(11) NOT NULL AUTO_INCREMENT, 20 -> `host_id` int(11) NOT NULL, 21 -> `host_name` varchar(64) NOT NULL, 22 -> `ip_addr` varchar(64) NOT NULL, 23 -> `brand_name` varchar(10) NOT NULL, 24 -> `type` varchar(32) DEFAULT NULL, 25 -> `cpu_info` varchar(32) NOT NULL, 26 -> `mem_info` varchar(32) NOT NULL, 27 -> `disk_info` varchar(32) NOT NULL, 28 -> `status` int(11) NOT NULL, 29 -> `add_time` date NOT NULL, 30 -> PRIMARY KEY (`id`) 31 -> ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4; 32 Query OK, 0 rows affected (0.32 sec) 33 34 MariaDB [ueb_cmdb]> INSERT INTO `cmdb_hostsinfo` VALUES ('2', '2', 'slavedb02.ueboms.com', '172.16.2.236', 'lenovo', 'x3650 m5', 'Xeon(R) CPU E5-2620 v2', '128g', '1024g', '2', '2017-04-11'); 35 Query OK, 1 row affected (0.06 sec) 36 MariaDB [ueb_cmdb]>
經過General Log得知,MaxScale會把create、insert語句的請求轉發到masterdb上
1 [root@masterdb ~]# tail -f /data/mysqllogs/general.log 2 ...............省略N行........................ 3 170725 17:00:51 46 Connect super@172.16.4.101 as anonymous on 4 46 Query CREATE DATABASE ueb_cmdb 5 46 Init DB ueb_cmdb 6 170725 17:00:55 40 Query SELECT @@server_id, @@read_only 7 40 Query SHOW ALL SLAVES STATUS 8 40 Query SELECT table_name FROM information_schema.tables WHERE table_schema = 'maxscale_schema' AND table_name = 'replication_heartbeat' 9 40 Query DELETE FROM maxscale_schema.replication_heartbeat WHERE master_timestamp < 1500800448 10 40 Query UPDATE maxscale_schema.replication_heartbeat SET master_timestamp = 1500973248 WHERE master_server_id = 212 AND maxscale_id = 0 11 170725 17:00:59 46 Query CREATE TABLE `cmdb_hostsinfo` ( 12 `id` int(11) NOT NULL AUTO_INCREMENT, 13 `host_id` int(11) NOT NULL, 14 `host_name` varchar(64) NOT NULL, 15 `ip_addr` varchar(64) NOT NULL, 16 `brand_name` varchar(10) NOT NULL, 17 `type` varchar(32) DEFAULT NULL, 18 `cpu_info` varchar(32) NOT NULL, 19 `mem_info` varchar(32) NOT NULL, 20 `disk_info` varchar(32) NOT NULL, 21 `status` int(11) NOT NULL, 22 `add_time` date NOT NULL, 23 PRIMARY KEY (`id`) 24 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 25 26 170725 17:29:04 52 Query INSERT INTO `cmdb_hostsinfo`('host_id','host_name','ip_addr','brand_name','type','cpu_info','mem_info','disk_info','status','add_time') values(2,'bbs.ueb.cn','172.16.4.201','lenovo','server','Intel Xeon CPU E5-2640v42.40GHz','DDR316008G','1000G',1,'2017-7-25') 27 .................省略N行............................
執行查詢語句:select host_id,host_name,ip_addr,brand_name,cpu_info,mem_info from cmdb_hostsinfo;
1 MariaDB [ueb_cmdb]> select host_id,host_name,ip_addr,brand_name,cpu_info,mem_info from cmdb_hostsinfo; 2 +---------+----------------------+--------------+------------+------------------------+----------+ 3 | host_id | host_name | ip_addr | brand_name | cpu_info | mem_info | 4 +---------+----------------------+--------------+------------+------------------------+----------+ 5 | 2 | slavedb02.ueboms.com | 172.16.2.236 | lenovo | Xeon(R) CPU E5-2620 v2 | 128g | 6 +---------+----------------------+--------------+------------+------------------------+----------+ 7 1 row in set (0.00 sec) 8 9 MariaDB [ueb_cmdb]>
經過General Log得知,MaxScale會把select語句的請求轉發到slavedb01(172.16.4.214)上
1 [root@slavedb01 mysqllogs]# tail -f general.log 2 ..............省略N行....................... 3 170725 17:55:42 44 Query select host_id,host_name,ip_addr,brand_name,cpu_info,mem_info from cmdb_hostsinfo 4 170725 17:55:51 27 Query SELECT @@server_id, @@read_only 5 27 Query SHOW ALL SLAVES STATUS 6 27 Query SELECT master_timestamp FROM maxscale_schema.replication_heartbeat WHERE maxscale_id = 0 AND master_server_id = 212 7 26 Query BEGIN 8 26 Query COMMIT 9 ...................省略N行...................
5.2.2 模擬slave延遲
在slavedb01(172.16.4.214)上執行鎖表語句 flush tables with read lock;
1 MariaDB [(none)]> flush tables with read lock; 2 Query OK, 0 rows affected (0.00 sec) 3 4 MariaDB [(none)]> show slave status\G; 5 *************************** 1. row *************************** 6 Slave_IO_State: Waiting for master to send event 7 Master_Host: 172.16.4.212 8 Master_User: rpl_user 9 Master_Port: 3306 10 Connect_Retry: 60 11 Master_Log_File: mysql-bin.000003 12 Read_Master_Log_Pos: 628831 13 Relay_Log_File: slavedb01-relay-bin.000012 14 Relay_Log_Pos: 273929 15 Relay_Master_Log_File: mysql-bin.000003 16 Slave_IO_Running: Yes 17 Slave_SQL_Running: Yes 18 Replicate_Do_DB: 19 Replicate_Ignore_DB: 20 Replicate_Do_Table: 21 Replicate_Ignore_Table: 22 Replicate_Wild_Do_Table: 23 Replicate_Wild_Ignore_Table: 24 Last_Errno: 0 25 Last_Error: 26 Skip_Counter: 0 27 Exec_Master_Log_Pos: 628599 28 Relay_Log_Space: 291919 29 Until_Condition: None 30 Until_Log_File: 31 Until_Log_Pos: 0 32 Master_SSL_Allowed: No 33 Master_SSL_CA_File: 34 Master_SSL_CA_Path: 35 Master_SSL_Cert: 36 Master_SSL_Cipher: 37 Master_SSL_Key: 38 Seconds_Behind_Master: 6 39 Master_SSL_Verify_Server_Cert: No 40 Last_IO_Errno: 0 41 Last_IO_Error: 42 Last_SQL_Errno: 0 43 Last_SQL_Error: 44 Replicate_Ignore_Server_Ids: 45 Master_Server_Id: 212 46 Master_SSL_Crl: 47 Master_SSL_Crlpath: 48 Using_Gtid: No 49 Gtid_IO_Pos: 50 Replicate_Do_Domain_Ids: 51 Replicate_Ignore_Domain_Ids: 52 Parallel_Mode: conservative 53 1 row in set (0.00 sec) 54 55 ERROR: No query specified 56 57 MariaDB [(none)]>
經過客戶端鏈接maxscale 4006端口,執行查詢語句 select user,host,password from mysql.user;
1 [root@maxscale ~]# mysql -usuper -p -h 172.16.4.101 -P4006 2 Enter password: 3 Welcome to the MariaDB monitor. Commands end with ; or \g. 4 Your MariaDB connection id is 4634 5 Server version: 5.5.37-MariaDB-RWsplit MariaDB Server 6 7 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. 8 9 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 10 11 MariaDB [(none)]> select user,host,password from mysql.user; 12 +---------+------------------+-------------------------------------------+ 13 | user | host | password | 14 +---------+------------------+-------------------------------------------+ 15 | root | localhost | | 16 | root | slavedb02.ueb.cn | | 17 | root | 127.0.0.1 | | 18 | root | ::1 | | 19 | | localhost | | 20 | | slavedb02.ueb.cn | | 21 | super | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 22 | monitor | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 23 +---------+------------------+-------------------------------------------+ 24 8 rows in set (0.00 sec) 25 26 MariaDB [(none)]>
經過查看General Log能夠得知,select請求被轉發到沒有延遲的slavedb02(172.16.4.215)上
1 [root@slavedb02 scripts]# tail -f /data/mysqllogs/general.log 2 ....................省略N行................... 3 70725 18:41:00 38 Query COMMIT 4 170725 18:41:04 56 Query select user,host,password from mysql.user 5 170725 18:41:09 39 Query SELECT @@server_id, @@read_only 6 39 Query SHOW ALL SLAVES STATUS 7 170725 18:41:10 38 Query BEGIN 8 39 Query SELECT master_timestamp FROM maxscale_schema.replication_heartbeat WHERE maxscale_id = 0 AND master_server_id = 212 9 38 Query COMMIT 10 ....................省略N行......................
5.2.3 模擬一臺slave故障
在slavedb01(172.16.4.214)上執行 stop slave;模擬slave故障
1 MariaDB [(none)]> stop slave; 2 Query OK, 0 rows affected (0.04 sec) 3 4 MariaDB [(none)]>
經過客戶端鏈接到MaxScale 4006端口上執行 select user,host,password from mysql.user;
1 [root@maxscale ~]# mysql -usuper -p -h 172.16.4.101 -P4006 2 Enter password: 3 Welcome to the MariaDB monitor. Commands end with ; or \g. 4 Your MariaDB connection id is 4634 5 Server version: 5.5.37-MariaDB-RWsplit MariaDB Server 6 7 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. 8 9 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 10 11 MariaDB [(none)]>select user,host,password from mysql.user; 12 ERROR 2006 (HY000): MySQL server has gone away 13 No connection. Trying to reconnect... 14 Connection id: 4641 15 Current database: *** NONE *** 16 17 +---------+------------------+-------------------------------------------+ 18 | user | host | password | 19 +---------+------------------+-------------------------------------------+ 20 | root | localhost | | 21 | root | slavedb02.ueb.cn | | 22 | root | 127.0.0.1 | | 23 | root | ::1 | | 24 | | localhost | | 25 | | slavedb02.ueb.cn | | 26 | super | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 27 | monitor | 172.16.4.101 | *A13D037357AEF8C9BAC95CB6BC39B4862A606D4B | 28 +---------+------------------+-------------------------------------------+ 29 8 rows in set (0.00 sec) 30 31 MariaDB [(none)]>
經過查看General Log能夠得知,select查詢請求再也不被轉發到slavedb01上,二是被轉發到slavedb02(172.16.4.215)上
1 [root@slavedb02 scripts]# tail -f /data/mysqllogs/general.log 2 ................省略N行........................ 3 170727 10:06:20 57 Connect super@172.16.4.101 as anonymous on 4 57 Query select user,host,password from mysql.user 5 170727 10:06:22 39 Query SELECT @@server_id, @@read_only 6 39 Query SHOW ALL SLAVES STATUS 7 39 Query SELECT master_timestamp FROM maxscale_schema.replication_heartbeat WHERE maxscale_id = 0 AND master_server_id = 212 8 38 Query BEGIN 9 38 Query COMMIT 10 ................省略N..........................
6.2.4 模擬所有slave故障
在slavedb01(172.16.4.214)和slavedb02(172.16.4.215)上執行stop slave;模擬所有slave故障
1 MariaDB [(none)]> stop slave; 2 Query OK, 0 rows affected (0.02 sec) 3 4 MariaDB [(none)]>
經過查看General Log能夠得知,select查詢請求再也不被轉發到任何slave上,而是被轉發到masterdb(172.16.4.212)上
1 [root@masterdb ~]# tail -f /data/mysqllogs/general.log 2 .................省略N行...................... 3 170727 10:16:48 60 Connect super@172.16.4.101 as anonymous on 4 60 Query select user,host,password from mysql.user 5 170727 10:16:57 57 Query SELECT @@server_id, @@read_only 6 .................省略N行...................
7、PHP/JAVA程序接入注意事項:
在如下狀況下,select查詢將會在master上執行:
附、配置文件參數詳解
MaxScale配置文件內容一共分爲五個部分,每一個部分使用[]標記,典型的ini文件格式:
[maxscale]
在名爲[MaxScale]的部分中,全局設置容許對MariaDB MaxScale進行總體調優的各類參數。
[Service]
Service表示MariaDB MaxScale爲客戶提供的數據庫服務。通常來講,服務由一組後端數據庫服務器和一種路由算法組成,它決定了MariaDB MaxScale如何決定發送語句或路由到這些後端服務器的鏈接。
[Server]
Server段用於定義能夠提供服務的後端數據庫服務器。server能夠是MariaDB MaxScale中的一個或多個服務的成員。服務器由服務器名標識,服務器名是配置文件中的部分名稱。服務器有一個服務器的類型參數,加上地址端口和協議參數。
[Listener]
偵聽器定義了用於偵聽服務鏈接的端口和協議對。服務可能有多個與之相關的偵聽器,以支持多個協議或多個端口。與配置的其餘元素同樣,section名稱是偵聽器名稱,能夠自由選擇。類型參數用於標識做爲偵聽器定義的部分。
[Monitor]
Monitor模塊被MariaDB MaxScale用於內部監控後臺數據庫的狀態,以便爲這些服務器設置服務器標誌。而後,路由器模塊使用這些標誌來肯定特定的服務器是否適合用於特定查詢分類的路由鏈接。這些監視器運行在MariaDB MaxScale的單獨線程中,不影響MariaDB MaxScale的路由性能。
下面就對各個模塊進行參數的解釋:
全局模塊[maxscale]
1 [maxscale] 2 threads=1 3 auth_connect_timeout=10 4 auth_read_timeout =10 5 auth_write_timeout=10 6 ms_timestamp=<0|1> #啓用或禁用日誌文件中的高精度時間戳。這樣能夠爲全部的日誌文件時間戳增長毫秒的精度。 7 skip_permission_checks =<true|false> #跳過服務和監視用戶權限檢查。當你知道權限是能夠的,你想加快啓動進程時,這是頗有用的。這個參數須要一個布爾值,默認狀況下是禁用的。建議不要禁用權限檢查,這樣當maxscale啓動時就會檢測到任何缺失的特權。若是您正在經歷一個緩慢啓動的MaxScale,由於在檢查權限時大量的鏈接超時,禁用權限檢查能夠加快啓動進程。 8 syslog=<0|1> #啓用或禁用對syslog的消息記錄。 9 maxlog=<0|1> #容許禁用向MariaDB MaxScale的日誌文件記錄消息。默認的日誌記錄是啓用的。 10 log_to_shm=<0|1> #啓用或禁用maxscale.log日誌文件的共享內存。若是啓用,那麼將在/dev/ shm下建立實際的日誌文件,並在MaxScale日誌目錄中建立一個指向該文件的符號連接。若是啓用log_info和/或log_debug,日誌記錄到共享內存多是合適的,由於日誌記錄到一個常規文件可能會致使性能降低,由於記錄的數據量是這樣的。可是,因爲共享內存是一種稀缺資源,因此對共享內存的日誌記錄應該只是臨時的,而不是按期的。因爲MariaDB MaxScale能夠記錄到文件和syslog,所以提供最大靈活性的方法是啓用syslog和log_to_shm,以及禁用maxlog。這樣,消息一般會登陸到syslog,可是若是有什麼要調查的話,則能夠從maxadmin啓用log_info和maxlog,在這種狀況下,信息消息將被記錄到maxscale。共享內存中的日誌文件。 11 log_warning=<0|1> #啓用或禁用syslog優先級警告的日誌記錄。默認狀況下啓用此優先級的消息。 12 log_notice=<0|1> #啓用或禁用syslog優先級通知的日誌記錄。這個優先級的消息提供了關於MariaDB MaxScale功能的信息,默認狀況下是啓用的。 13 log_info=<0|1> #啓用或禁用syslog優先級信息的日誌記錄。這些消息提供了關於MariaDB MaxScale內部工做的詳細信息,不該該因爲它們的頻率而啓用,除非有特定的緣由。例如,從這些信息中能夠明顯看出。爲何一個特定的查詢被路由到主服務器而不是一個奴隸。這些信息在默認狀況下是禁用的。 14 log_augmentation=<0|1> #啓用或禁用消息的加強。若是啓用了這個操做,那麼每一個已登陸的消息將附加到日誌記錄的函數的名稱。這主要用於開發目的,所以在默認狀況下是禁用的。 15 log_throttling=X,Y,Z #若是持續存在錯誤的緣由,可能會一次又一次地記錄一個特定的錯誤(或警告)。爲了防止日誌被洪水淹沒,能夠指定一個特定的錯誤可能在一個時間段內記錄多少次,在這個錯誤的日誌記錄被抑制一段時間以前。默認值是十、1000、10000,這意味着若是一樣的錯誤在1秒內被記錄10次,那麼在接下來的10秒中,記錄錯誤的日誌就會被抑制。要禁用log throttling,添加一個帶有空值的條目:log_throttling= 16 logdir=path #設置日誌文件存儲的目錄。該文件夾須要由運行MariaDB MaxScale的用戶可讀和寫。 17 datadir=path #設置MariaDB MaxScale使用的數據文件存儲的目錄。模塊能夠寫入這個目錄,例如binlogrouter使用這個文件夾做爲存儲二進制日誌的默認位置。 18 libdir=/home/user/lib64/ #設置MariaDB MaxScale尋找模塊的目錄。庫目錄是MariaDB MaxScale在搜索模塊時使用的惟一目錄。若是您有MariaDB MaxScale的自定義模塊,請確保將它們放在這個文件夾中。 19 cachedir=/tmp/maxscale_cache/ #配置MariaDB MaxScale用於存儲緩存數據的目錄。緩存數據的一個示例是從後端服務器獲取的身份驗證數據。若是鏈接到後端服務器是不可能的,那麼MariaDB MaxScale存儲這些數據。 20 piddir=/tmp/maxscale_cache/ #爲MariaDB MaxScale配置PID文件的目錄。這個文件包含運行的MariaDB MaxScale進程的進程ID。 21 execdir=/usr/local/bin/ #配置可執行文件所在的目錄。啓動的全部內部流程將使用此目錄查找可執行文件。 22 connector_plugindir=/usr/lib/plugin/ #MariaDB connector-c插件目錄的位置。在MaxScale中使用的MariaDB connector - c可使用此目錄加載身份驗證插件。插件的版本必須與MaxScale所構建的鏈接器版本兼容。 23 persistdir=/var/lib/maxscale/maxscale.cnf.d/ #配置保存持久配置的目錄。當經過MaxAdmin建立新服務器時,它將存儲在這個目錄中。不要使用或修改該目錄的內容,使用/etc/maxscale.cnf.d /代替。 24 module_configdir=/var/lib/maxscale/ #配置存儲模塊配置的目錄。路徑參數解析相對於這個目錄。該目錄應該用於存儲特定於模塊的配置,例如dbfwfilter規則文件。任何非絕對路徑的配置參數都將被解釋爲相對路徑。相對路徑使用模塊配置目錄做爲工做目錄。例如,配置參數file= my_file.txt將被解釋爲/etc/maxscale.cnf.d/my_file.txt,而file=/home/user/my_file.txt將被解釋爲/home/user/my_file.txt。
Server模塊[Server]
1 [server1] 2 type=server 3 address=IP 4 port=3306 5 protocol=MySQLBackend
Service模塊[Service]
1 [Read-Write Service] 2 type=service 3 router=readwritesplit | readconnroute #服務的路由器參數定義了路由器模塊的名稱,該模塊將用於實現MariaDB MaxScale客戶機和後端數據庫之間的路由算法。另外,路由器也能夠經過一個逗號分隔的選項列表來控制路由算法的行爲。控制路由選擇的兩個參數是router和router_options。路由器選項是特定於特定路由器的,並被用來修改路由器的行爲。讀取的鏈接路由器能夠經過master、slave或synced的選項,例如配置一個服務來使用此路由器,並限制服務器對處於從屬狀態的服務器的選擇。 4 router_options=slave | master,slave 5 servers=server1 #服務定義中的servers參數提供了包含服務的後端服務器的逗號分隔列表。服務器名是使用服務器類型參數的塊的名稱部分使用的名稱(見下文)。 6 user=myuser 7 passwd=mypwd 8 filters=counter | QLA 9 enable_root_user=[0|1] #此參數控制root鏈接到MariaDB MaxScale的能力,並經過MariaDB MaxScale將其轉到後端服務器。默認值爲0,禁用root鏈接到MariaDB MaxScale。 10 localhost_match_wildcard_host=[0|1] #該參數使「127.0.0.1」(localhost)與「%」通配符匹配,用於MySQL協議驗證。默認值爲0,所以爲了驗證與MariaDB MaxScale正在運行的同一機器的鏈接,MySQL用戶表中須要一個顯式user@localhost條目。 11 version_string=5.5.37-MariaDB-RWsplit #這個參數設置了一個自定義的版本字符串,它是由MariaDB MaxScale向客戶端發送的。 12 connection_timeout=300 #connection_timeout參數用於將會話與已經閒置過久的MariaDB MaxScale斷開鏈接。默認狀況下,會話超時是禁用的。要啓用它們,請在服務的配置部分中以秒爲單位定義超時。 13 max_connections=100 #最大同時鏈接MaxScale應該容許該服務。若是參數爲零或省略,則沒有限制。在到達極限後,任何試圖創建更多鏈接的嘗試都會致使返回「太多的鏈接」錯誤。 14 log_auth_warnings=[0|1] #啓用或禁用認證失敗和警告的日誌記錄。該參數採用布爾值。MariaDB MaxScale一般會抑制關於失敗身份驗證的警告消息。啓用此選項將把這些消息記錄到消息日誌中,並詳細介紹誰嘗試鏈接到MariaDB MaxScale和從何處鏈接。 15 auth_all_servers=[0|1] #此參數控制在從後端服務器加載用戶時僅使用單個服務器或全部服務器。這須要一個布爾值,當啓用時,將在全部服務器上建立全部用戶和授予的聯合。 16 strip_db_esc=[0|1] #當從後端服務器加載用戶時,strip_db_esc參數從數據庫名稱中刪除字符。該參數採用布爾值,當啓用時,將從數據庫名稱中刪除全部反斜槓(\)字符。自MaxScale 2.0.1以來,該參數的默認值爲true。在之前的版本中,默認值爲false。一些可視化數據庫管理工具會自動地避開一些字符,這可能會致使當MariaDB MaxScale試圖對用戶進行身份驗證時發生衝突。 17 retry_on_failure=[0|1] # retry_on_failure參數控制MariaDB MaxScale是否嘗試從新啓動失敗的服務並接受布爾值。此功能默認啓用,以防止因爲網絡中斷致使服務啓動失敗而致使永久禁用服務。若是在啓動MariaDB MaxScale時不能啓動服務,禁用從新啓動失敗的服務將使它們永久禁用。 18 max_slave_connections=100%