proxysql是一款高性能的mysql中間件,能夠實現讀寫分離,查詢重寫,防火牆,數據庫監控等功能。是目前percona主推的一款數據庫中間件產品。html
實驗環境:
兩臺服務器配置mysql GTID主從複製,一臺爲master,一臺爲slave。master負責寫入數據,slave只負責讀取數據。主從複製配置方法能夠參考我以前的博客https://www.cnblogs.com/stacks/p/7171648.html
配置方法mysql
proxysql啓動登陸
proxysql監聽在6032和6033端口,6032爲管理端口,6033爲數據庫鏈接端口
/etc/init.d/proxysql start便可啓動proxysql,經過mysql客戶端鏈接管理端口便可登陸到proxysql進行管理工做:mysql -uadmin -padmin -h127.0.0.1 -P6032
登陸以後操做方法和mysql是同樣的。git
grant usage on *.* to 'monitor'@'%' identified by 'monitor'; #建立monitor用戶 grant all on *.* to 'sbuser'@'%' identified by 'sbuser' #添加數據操做用戶 flush privileges #刷權限
proxysql執行以下語句進行服務端和用戶的添加:github
INSERT INTO MySQL_servers(hostgroup_id, hostname, port) VALUES (0,'192.168.11.184',3306); #hostgroupid適用於區分讀寫分離服務器組的,能夠把全部寫庫加入到一個組id,全部讀庫加入到另外一個組id INSERT INTO MySQL_servers(hostgroup_id, hostname, port) VALUES (1,'192.168.11.185',3306); INSERT INTO MySQL_users(username,password,default_hostgroup) VALUES ('sbuser','sbuser',0); UPDATE global_variables SET variable_value='monitor' WHERE variable_name='MySQL-monitor_username'; UPDATE global_variables SET variable_value='monitor' WHERE variable_name='MySQL-monitor_password'; load mysql servers to runtime; #加載memory剛纔的配置到runtime生效 save mysql servers to disk; #持久化配置到sqlite存儲中
mysql_servers表用於存儲proxysql後端服務器,mysql_users表用於存儲後端服務器數據庫帳號和密碼
global_variables表存儲各類全局配置信息;正則表達式
執行mysql -usbuser -psbuser -h127.0.0.1 -P6033
能夠登陸到後端服務器sql
show slave hosts;
查看是否已經有slave庫的存在;insert into mysql_query_rules (active,match_pattern,destination_hostgroup,apply) VALUES(1,'^SELECT.*FOR UPDATE$',0,1); INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^SELECT',1,1); LOAD MYSQL QUERY RULES TO RUNTIME; SAVE MYSQL QUERY RULES TO DISK;
mysql_query_rules表適用於配置讀寫路由規則的表,全部的配置都是經過正則表達式的模式去進行匹配的
actime:表示此規則是不是激活狀態,1表示激活
match_pattern:表示匹配的sql語句規則,以正則表達式方式進行匹配,select .* for update表示匹配全部數據修改操做語句
destination_hostgroup:分配到那個組;
apply:正則匹配上此規則以後是否還能夠應用其餘的rules,若是須要應用其餘規則,則須要修改此配置參數;數據庫
SELECT 1 FROM stats_mysql_query_digest_reset LIMIT 1;
語句用於清空query統計信息;mysql> select hostgroup hg,sum_time,count_star,digest_text from stats_mysql_query_digest order by digest_text; #從stats_mysql_query_digest表查詢query統計信息 +----+----------+------------+------------------------------------------+ | hg | sum_time | count_star | digest_text | +----+----------+------------+------------------------------------------+ | 0 | 2338 | 1 | desc sbtest.sbtest1 | | 0 | 4469 | 1 | insert into sbtest1 values (?,?,?,?) | | 1 | 10019135 | 4 | select count(*) from sbtest.sbtest1 | | 0 | 3617 | 1 | update sbtest.sbtest1 set c=? where id=? | +----+----------+------------+------------------------------------------+ 4 rows in set (0.00 sec)
能夠看到已經實現了讀寫分離了後端
mysql> SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10; #查詢後端庫鏈接信息; +----------------+------+------------------+-------------------------+---------------+ | hostname | port | time_start_us | connect_success_time_us | connect_error | +----------------+------+------------------+-------------------------+---------------+ | 192.168.11.185 | 3306 | 1530675542539279 | 3844 | NULL | | 192.168.11.184 | 3306 | 1530675542523231 | 867 | NULL | | 192.168.11.185 | 3306 | 1530675482538252 | 3026 | NULL | | 192.168.11.184 | 3306 | 1530675482522256 | 646 | NULL | | 192.168.11.185 | 3306 | 1530675422538393 | 3043 | NULL | | 192.168.11.184 | 3306 | 1530675422522091 | 2244 | NULL | | 192.168.11.185 | 3306 | 1530675362535883 | 1984 | NULL | | 192.168.11.184 | 3306 | 1530675362520011 | 828 | NULL | | 192.168.11.185 | 3306 | 1530675302563722 | 4890 | NULL | | 192.168.11.184 | 3306 | 1530675302519919 | 945 | NULL | +----------------+------+------------------+-------------------------+---------------+ 10 rows in set (0.00 sec) mysql> SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 10; #查詢後端庫存活信息 +----------------+------+------------------+----------------------+------------+ | hostname | port | time_start_us | ping_success_time_us | ping_error | +----------------+------+------------------+----------------------+------------+ | 192.168.11.185 | 3306 | 1530675592871849 | 695 | NULL | | 192.168.11.184 | 3306 | 1530675592869012 | 176 | NULL | | 192.168.11.185 | 3306 | 1530675582871431 | 683 | NULL | | 192.168.11.184 | 3306 | 1530675582868776 | 178 | NULL | | 192.168.11.185 | 3306 | 1530675572871008 | 854 | NULL | | 192.168.11.184 | 3306 | 1530675572868341 | 175 | NULL | | 192.168.11.185 | 3306 | 1530675562870802 | 732 | NULL | | 192.168.11.184 | 3306 | 1530675562868067 | 183 | NULL | | 192.168.11.185 | 3306 | 1530675552870456 | 784 | NULL | | 192.168.11.184 | 3306 | 1530675552867687 | 187 | NULL | +----------------+------+------------------+----------------------+------------+ 10 rows in set (0.00 sec)