GitHub:
- Percona Xtradb Cluster https://github.com/percona/percona-xtradb-cluster
- Proxysql https://github.com/sysown/proxysql
- proxsysql-admin-tool https://github.com/percona/proxysql-admin-tool
- percona dockers https://github.com/percona/percona-docker
參考:
https://github.com/sysown/proxysql/wiki/ProxySQL-Configuration
http://seanlook.com/2017/04/10/mysql-proxysql-install-config/
延伸閱讀
https://www.percona.com/doc/percona-xtradb-cluster/LATEST/howtos/proxysql.html
官方文件的 load balancing,但是透過 proxysql-admin-tool 來操作
ProxySQL之性能測試對比 http://seanlook.com/2017/04/20/mysql-proxysql-performance-test/
Percona監控工具初探 https://www.hi-linux.com/posts/52766.html
ProxySQL 安裝配置詳解及讀寫分離、負載均衡 https://dwj999.github.io/ProxySQL-%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE%E8%AF%A6%E8%A7%A3%E5%8F%8A%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E3%80%81%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1.html
proxysql.cnf 只會安裝完後第一次啟動時被用到,啟動後就會把 cnf 的資訊讀到 /var/lib/proxysql/proxysql.db 裡,之後透過 mysql client 的修改也都是寫到該 db 裡。
而 proxysql-admin-tool 的使用時機則是在第一次啟動後,協助建立相關的設定(如使用者設定、singlewrite/readwrite 模式)
proxysql-admin-tool 使用前,需要先將 proxysql 啟動,可以使用預設的 proxysql.cnf,但建議把 admin credential 改掉後再啟動,啟動完畢後,修改 proxysql-admin.cnf。
可以不需要使用 proxysql-admin-tool,只透過 proxysql.cnf 也是可以用的。
在 local build,使用到以下檔案
- pxc-57 docker https://github.com/percona/percona-docker/tree/master/pxc-57
- proxysql docker https://github.com/percona/percona-docker/tree/master/proxysql
- 可以設定 add_to_cluster.sh,調整需要的參數
writer_hostgroup = 1
reader_hostgroupd = 2
先建立 etcd
先在 local 建立 pxc 搭配剛剛建立的 etcd
建立 proxysql,並掛上 pxc
調整 proxysql
read/write 分離
參考 https://github.com/sysown/proxysql/wiki/ProxySQL-Configuration#mysql-replication-hostgroups
若要設定 read/write 分離,則想要只有 read 的機器都需要開啟
read_only1 2set global read_only = 1; SHOW GLOBAL VARIABLES LIKE 'read_only';並在
mysql_replication_hostgroupstable 設定相關資訊1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Admin> SHOW CREATE TABLE mysql_replication_hostgroups\G *************************** 1. row *************************** table: mysql_replication_hostgroups Create Table: CREATE TABLE mysql_replication_hostgroups ( writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY, reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND reader_hostgroup>0), comment VARCHAR, UNIQUE (reader_hostgroup)) 1 row in set (0.00 sec) Admin> INSERT INTO mysql_replication_hostgroups (reader_hostgroup, writer_hostgroup, comment) VALUES (2,1,'cluster'); Query OK, 1 row affected (0.00 sec) # 確認 Admin> select * from mysql_replication_hostgroups;透過觀察 query log,再新增 mysql rules
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24# query log Admin> SELECT hostgroup hg, sum_time, count_star, digest, digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC; Admin> INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (10,1,'go','^INSERT',1,1); # 或是使用 digest 來新增 Admin> INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (10,1,'go','0x86CFF744679FA77D',1,1); # 0x86CFF744679FA77D = insert ... # on update 要用 writer Admin> INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (12,1,'go','^SELECT .* ON UPDATE',1,1); # select 要用 reader Admin> INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (11,1,'go','^SELECT id, oid FROM identities WHERE oid=\? ORDER BY ID desc LIMIT \?$',2,1); Admin> INSERT INTO mysql_query_rules (rule_id,active,match_digest,destination_hostgroup,apply) VALUES (1,1,'^SELECT.*FOR UPDATE$',10,1), (2,1,'^SELECT',20,1); # 確認 Admin> SELECT match_digest,match_pattern,destination_hostgroup FROM mysql_query_rules WHERE active=1 ORDER BY rule_id; # load to runtime and save to disk Admin> LOAD MYSQL QUERY RULES TO RUNTIME; SAVE MYSQL QUERY RULES TO DISK;壓測後可以透過以下指令確認是否有分流
1 2 3 4 5# 先 reset counter Admin> SELECT * FROM stats_mysql_query_digest_reset LIMIT 1; # 再觀察 summary Admin> SELECT hostgroup hg, SUM(sum_time), SUM(count_star) FROM stats_mysql_query_digest GROUP BY hostgroup;如果沒有,確認 mysql_users 裡的 transaction_persistent 是不是被設定 1 了,若被設定成 1,記得改成 0。
load balancer
1 2 3 4 5 6# XXXX mysql_replication_hostgroups SHOW CREATE TABLE mysql_replication_hostgroups\G SHOW CREATE TABLE s\G INSERT INTO mysql_replication_hostgroups VALUES (1,2,'cluster1');1 2 3 4Admin> INSERT INTO mysql_group_replication_hostgroups (writer_hostgroup, backup_writer_hostgroup, reader_hostgroup, offline_hostgroup, active, max_writers, writer_is_also_reader, max_transactions_behind) VALUES (1, 4, 2, 3, 1, 3, 1, 100); # 確認 Admin> select * from mysql_group_replication_hostgroups;activeset to1enables ProxySQL’s monitoring of these host groups.max_writersdefines how many nodes can act as writers. We used3here because In a multi-primary configuration, all nodes can be treated equal, so here we used3(the total number of nodes).writer_is_also_readerset to1instructs ProxySQL to treat writers as readers as well.max_transactions_behindsets the maximum number of delayed transactions before a node is classified as offline.
再分別加入或是修改
hostgroup_id為2(writer_hostgroup),同上面的設定1 2 3 4Admin> INSERT INTO mysql_servers(hostgroup_id, hostname, port) VALUES (2, 'pxc-{1,2,3}', 3306); # 確認 Admin> select * from mysql_servers;最後把所有的設定寫入 runtime 及 disk (sqlitedb)
1 2 3Admin> LOAD MYSQL QUERY RULES TO RUNTIME; SAVE MYSQL QUERY RULES TO DISK; Admin> LOAD MYSQL VARIABLES TO RUNTIME; SAVE MYSQL VARIABLES TO DISK; Admin> LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;再檢查一次 runtime 的 mysql_server
1Admin> SELECT hostgroup_id, hostname, status FROM runtime_mysql_servers;query cache
1 2 3Admin> INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,cache_ttl,apply) VALUES(1,'^SELECT id, oid FROM identities',2,2000,1); Admin> LOAD MYSQL QUERY RULES TO RUNTIME; SAVE MYSQL QUERY RULES TO DISK;
列出 query log,並使用總和時間排序
| |
Adding Galera Support
https://www.percona.com/doc/percona-xtradb-cluster/LATEST/howtos/proxysql.html
| |
| Argument | Name | Required | Description |
|---|---|---|---|
arg1 | HOSTGROUP WRITERS | YES | The ID of the hostgroup with nodes that will server writes. |
arg2 | HOSTGROUP READERS | NO | The ID of the hostgroup with nodes that will server reads. |
arg3 | NUMBER WRITERS | NO | Maximum number of the node from the writer hostgroup that can be marked ONLINE. If set to 0, all nodes can be marked ONLINE. |
arg4 | WRITERS ARE READERS | NO | If set to 1 (default), ONLINE nodes in the writer hostgroup will prefer not to be ONLINE in the reader hostgroup. |
arg5 | LOG FILE | NO | File where node state checks and changes are logged to (verbose). |
| |
新增 proxysql -> mysql 的使用者
| |
PMM (Percona Monitoring and Management)
ref:
安裝 server (docker):
| |
安裝 client:
| |
設定 client:
| |
接著就可以連上 PMM 看各種數據。
note: 想知道 container 的 ip 可以透過此指令來找到
| |
Grafana dashboard
https://github.com/percona/grafana-dashboards/blob/master/dashboards/ProxySQL_Overview.json
Mysql status
除非有連線,不然 status 不會自動改
舉例:將 proxysql 開起來,pxc 沒有開,此時 runtime_mysql_servers 的 status 都還是 ONLINE
但透過 mysql client 試著連進去,會 timeout,此時 runtime_mysql_servers 的 status 就會是 SHUNNED
centos 開機順序
舊版的會吃 /etc/rc.local,但其實是相容模式,背後還是透過 systemd 處理
若要在 rc.local 執行前啟動 docker 的話,則要在編輯 service 檔案
https://fedoramagazine.org/systemd-unit-dependencies-and-order/
| |
??? 在 After 加入 docker.service,這樣就會在開機啟動時,執行 rc.local 前啟動 docker.service
| |
default proxysql.cnf
| |
Cheatsheet
| |