当前位置:   article > 正文

通过proxysql实现MySQL主从读写分离_proxysql reader_shunned

proxysql reader_shunned
原文: https://www.yuque.com/wei01/wql35u/wbgk1a

自行部署MySQL主从环境

安装proxysql

  1. # 安装依赖包
  2. yum install gnutls perl-DBD-mysql -y
  3. rpm -ivh /tmp/proxysql-2.3.2-1-centos7.x86_64.rpm
  4. # 查看相应安装文件路径,有哪些文件
  5. root@mysqlhost01:/opt/software#rpm -ql proxysql
  6. /etc/logrotate.d/proxysql
  7. /etc/proxysql.cnf
  8. /etc/systemd/system/proxysql-initial.service
  9. /etc/systemd/system/proxysql.service
  10. /usr/bin/proxysql
  11. /usr/share/proxysql/tools/proxysql_galera_checker.sh
  12. /usr/share/proxysql/tools/proxysql_galera_writer.pl
  13. # 查看版本
  14. root@mysqlhost01:/opt/software#proxysql --version
  15. ProxySQL version 2.3.2-10-g8cd66cf, codename Truls
  16. # 如果是单独机器,可以安装MySQL客户端
  17. yum install Percona-XtraDB-Cluster-client-80
  18. # 查看状态
  19. root@mysqlhost01:/opt/software#systemctl start proxysql
  20. root@mysqlhost01:/opt/software#systemctl status proxysql
  21. ● proxysql.service - High Performance Advanced Proxy for MySQL
  22. Loaded: loaded (/etc/systemd/system/proxysql.service; enabled; vendor preset: disabled)
  23. Active: active (running) since Sat 2022-04-23 19:16:06 CST; 3s ago
  24. Process: 55697 ExecStart=/usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf $PROXYSQL_OPTS (code=exited, status=0/SUCCESS)
  25. Main PID: 55699 (proxysql)
  26. CGroup: /system.slice/proxysql.service
  27. ├─55699 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf
  28. └─55700 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf
  29. Apr 23 19:16:06 mysqlhost01 systemd[1]: Starting High Performance Advanced Proxy for MySQL...
  30. Apr 23 19:16:06 mysqlhost01 proxysql[55697]: 2022-04-23 19:16:06 [INFO] Using config file /etc/proxysql.cnf
  31. Apr 23 19:16:06 mysqlhost01 proxysql[55697]: 2022-04-23 19:16:06 [INFO] Current RLIMIT_NOFILE: 102400
  32. Apr 23 19:16:06 mysqlhost01 proxysql[55697]: 2022-04-23 19:16:06 [INFO] Using OpenSSL version: OpenSSL 1.1.1j 16 Feb 2021
  33. Apr 23 19:16:06 mysqlhost01 proxysql[55697]: 2022-04-23 19:16:06 [INFO] No SSL keys/certificates found in datadir (/var/lib/proxysql). Generating new keys/certificates.
  34. Apr 23 19:16:06 mysqlhost01 systemd[1]: Started High Performance Advanced Proxy for MySQL.
  35. # 查看使用的端口
  36. root@mysqlhost01:/opt/software#netstat -anlp | grep proxysql
  37. tcp 0 0 0.0.0.0:6032 0.0.0.0:* LISTEN 55700/proxysql
  38. tcp 0 0 0.0.0.0:6033 0.0.0.0:* LISTEN 55700/proxysql
  39. 6032 是 ProxySQL 的管理端口号,6033是对外服务的端口号
  40. ProxySQL 的用户名和密码都是默认的 admin
  41. 如果是MySQL数据库用户,则使用6033端口

proxysql 配置MySQL节点和用户

  1. # 连接proxysql
  2. root@192.168.51.21:/opt/software#mysql -uadmin -padmin -h 127.0.0.1 -P 6032
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 1
  6. Server version: 5.5.30 (ProxySQL Admin Module)
  7. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql> show databases;
  13. +-----+---------------+-------------------------------------+
  14. | seq | name | file |
  15. +-----+---------------+-------------------------------------+
  16. | 0 | main | |
  17. | 2 | disk | /var/lib/proxysql/proxysql.db |
  18. | 3 | stats | |
  19. | 4 | monitor | |
  20. | 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
  21. +-----+---------------+-------------------------------------+
  22. 5 rows in set (0.00 sec)
  23. mysql> show tables from main;
  24. +----------------------------------------------------+
  25. | tables |
  26. +----------------------------------------------------+
  27. | global_variables |
  28. | mysql_aws_aurora_hostgroups |
  29. | mysql_collations |
  30. | mysql_firewall_whitelist_rules |
  31. | mysql_firewall_whitelist_sqli_fingerprints |
  32. | mysql_firewall_whitelist_users |
  33. | mysql_galera_hostgroups |
  34. | mysql_group_replication_hostgroups |
  35. | mysql_query_rules |
  36. | mysql_query_rules_fast_routing |
  37. | mysql_replication_hostgroups |
  38. | mysql_servers |
  39. | mysql_users |
  40. | proxysql_servers |
  41. | restapi_routes |
  42. | runtime_checksums_values |
  43. | runtime_global_variables |
  44. | runtime_mysql_aws_aurora_hostgroups |
  45. | runtime_mysql_firewall_whitelist_rules |
  46. | runtime_mysql_firewall_whitelist_sqli_fingerprints |
  47. | runtime_mysql_firewall_whitelist_users |
  48. | runtime_mysql_galera_hostgroups |
  49. | runtime_mysql_group_replication_hostgroups |
  50. | runtime_mysql_query_rules |
  51. | runtime_mysql_query_rules_fast_routing |
  52. | runtime_mysql_replication_hostgroups |
  53. | runtime_mysql_servers |
  54. | runtime_mysql_users |
  55. | runtime_proxysql_servers |
  56. | runtime_restapi_routes |
  57. | runtime_scheduler |
  58. | scheduler |
  59. +----------------------------------------------------+
  60. mysql_servers: 后端可以连接 MySQL 服务器的列表
  61. mysql_users: 配置后端数据库的账号和监控的账号。
  62. mysql_query_rules: 指定 Query 路由到后端不同服务器的规则列表
  63. runtime_开头的是运行时的配置,这些是不能修改的。要修改ProxySQL的配置,需要修改了非runtime_表,修改后必须执行LOAD … TO RUNTIME才能加载到RUNTIME生效,执行save … to disk才能将配置持久化保存到磁盘
  64. -- 向ProxySQL中添加MySQL节点
  65. mysql@proxysql> insert into mysql_servers(hostgroup_id,hostname,port) values (10,'192.168.51.21',3306);
  66. mysql@proxysql> insert into mysql_servers(hostgroup_id,hostname,port) values (10,'192.168.51.22',3306);
  67. -- 应用配置
  68. mysql@proxysql> load mysql servers to runtime;
  69. mysql@proxysql> save mysql servers to disk;
  70. mysql> select * from mysql_servers\G
  71. *************************** 1. row ***************************
  72. hostgroup_id: 10
  73. hostname: 192.168.51.21
  74. port: 3306
  75. gtid_port: 0
  76. status: ONLINE
  77. weight: 1
  78. compression: 0
  79. max_connections: 1000
  80. max_replication_lag: 0
  81. use_ssl: 0
  82. max_latency_ms: 0
  83. comment:
  84. *************************** 2. row ***************************
  85. hostgroup_id: 10
  86. hostname: 192.168.51.22
  87. port: 3306
  88. gtid_port: 0
  89. status: ONLINE
  90. weight: 1
  91. compression: 0
  92. max_connections: 1000
  93. max_replication_lag: 0
  94. use_ssl: 0
  95. max_latency_ms: 0
  96. comment:
  97. 2 rows in set (0.00 sec)
  98. -- 添加节点之后,还需要监控后端节点。对于后端是主从复制的环境来说,这是必须的,因为ProxySQL需要通过每个节点的read_only值来自动调整它们是属于读组还是写组。
  99. #mysql -uroot -pBigdata@123
  100. CREATE USER 'proxysql'@'%' IDENTIFIED WITH mysql_native_password by 'ProxySQLPa55';
  101. GRANT replication client ON *.* TO 'proxysql'@'%';
  102. -- proxysql配置
  103. mysql@proxysql> UPDATE global_variables SET variable_value='proxysql' WHERE variable_name='mysql-monitor_username';
  104. mysql@proxysql> UPDATE global_variables SET variable_value='ProxySQLPa55' WHERE variable_name='mysql-monitor_password';
  105. -- 将配置写入到磁盘
  106. mysql@proxysql> LOAD MYSQL VARIABLES TO RUNTIME;
  107. mysql@proxysql> SAVE MYSQL VARIABLES TO DISK;
  108. -- 检查监控日志,如果错误,则说明有问题(如果不指定的话 监控用户 默认是monitor用户)
  109. mysql@proxysql> SELECT hostname,port,from_unixtime(time_start_us/1000/1000),connect_success_time_us,connect_error FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 6;
  110. +---------------+------+----------------------------------------+-------------------------+------------------------------------------------------------------------+
  111. | hostname | port | from_unixtime(time_start_us/1000/1000) | connect_success_time_us | connect_error |
  112. +---------------+------+----------------------------------------+-------------------------+------------------------------------------------------------------------+
  113. | 192.168.51.116| 3306 | 2022-04-25 08:43:56 | 2161 | NULL |
  114. | 192.168.51.116| 3306 | 2022-04-25 08:42:56 | 1437 | NULL |
  115. | 192.168.51.116| 3306 | 2022-04-25 08:41:56 | 1237 | NULL |
  116. | 192.168.51.116| 3306 | 2022-04-25 08:40:56 | 1114 | NULL |
  117. | 192.168.51.116| 3306 | 2022-04-25 08:40:04 | 0 | Access denied for user 'monitor'@'10.205.16.116' (using password: YES) |
  118. | 192.168.51.116| 3306 | 2022-04-25 08:39:04 | 0 | Access denied for user 'monitor'@'10.205.16.116' (using password: YES) |
  119. +---------------+------+----------------------------------------+-------------------------+------------------------------------------------------------------------+
  120. mysql@proxysql> SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 6;
  121. +--------------+------+------------------+----------------------+------------+
  122. | hostname | port | time_start_us | ping_success_time_us | ping_error |
  123. +--------------+------+------------------+----------------------+------------+
  124. | 192.168.51.21 | 3306 | 1650713803457556 | 61 | NULL |
  125. | 192.168.51.22 | 3306 | 1650713803298807 | 150 | NULL |
  126. | 192.168.51.22 | 3306 | 1650713793451431 | 217 | NULL |
  127. | 192.168.51.21 | 3306 | 1650713793298661 | 113 | NULL |
  128. | 192.168.51.22 | 3306 | 1650713783412590 | 227 | NULL |
  129. | 192.168.51.21 | 3306 | 1650713783298560 | 143 | NULL |
  130. +--------------+------+------------------+----------------------+------------+
  131. -- 插入组类定义
  132. mysql@proxysql> show create table mysql_replication_hostgroups \G
  133. *************************** 1. row ***************************
  134. table: mysql_replication_hostgroups
  135. Create Table: CREATE TABLE mysql_replication_hostgroups (
  136. writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY,
  137. reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND reader_hostgroup>=0),
  138. check_type VARCHAR CHECK (LOWER(check_type) IN ('read_only','innodb_read_only','super_read_only','read_only|innodb_read_only','read_only&innodb_read_only')) NOT NULL DEFAULT 'read_only',
  139. comment VARCHAR NOT NULL DEFAULT '', UNIQUE (reader_hostgroup))
  140. -- 第一个是写组ID,读组ID,校验类型(上面几种任意一种),注释(自定义)
  141. insert into mysql_replication_hostgroups values(10,20,'read_only','mysql2122');
  142. -- 生效配置
  143. load mysql servers to runtime;
  144. save mysql servers to disk;
  145. -- 一加载,Monitor模块就会开始监控后端的read_only值,当监控到read_only值后,就会按照read_only的值将某些节点自动移动到读/写组
  146. mysql> select * from mysql_servers\G
  147. *************************** 1. row ***************************
  148. hostgroup_id: 10
  149. hostname: 192.168.51.21
  150. port: 3306
  151. gtid_port: 0
  152. status: ONLINE
  153. weight: 1
  154. compression: 0
  155. max_connections: 1000
  156. max_replication_lag: 0
  157. use_ssl: 0
  158. max_latency_ms: 0
  159. comment:
  160. *************************** 2. row ***************************
  161. hostgroup_id: 20 --检测到51.22节点 read_only=ON时,会变成20
  162. hostname: 192.168.51.22
  163. port: 3306
  164. gtid_port: 0
  165. status: ONLINE
  166. weight: 1
  167. compression: 0
  168. max_connections: 1000
  169. max_replication_lag: 0
  170. use_ssl: 0
  171. max_latency_ms: 0
  172. comment:
  173. -- readonly 监控
  174. mysql> select * from mysql_server_read_only_log order by time_start_us desc limit 6;
  175. +--------------+------+------------------+-----------------+-----------+-------+
  176. | hostname | port | time_start_us | success_time_us | read_only | error |
  177. +--------------+------+------------------+-----------------+-----------+-------+
  178. | 192.168.51.22 | 3306 | 1650766001921374 | 240 | 1 | NULL |
  179. | 192.168.51.21 | 3306 | 1650766001903998 | 230 | 0 | NULL |
  180. | 192.168.51.22 | 3306 | 1650766000433229 | 294 | 1 | NULL |
  181. | 192.168.51.21 | 3306 | 1650766000403933 | 156 | 0 | NULL |
  182. | 192.168.51.22 | 3306 | 1650765998927168 | 302 | 1 | NULL |
  183. | 192.168.51.21 | 3306 | 1650765998903824 | 229 | 0 | NULL |
  184. +--------------+------+------------------+-----------------+-----------+-------+
  185. -- 设置最大lag记录(建议不设置,会影响读写路由规则)
  186. -- 如果延迟超过1秒,如果设置路由规则,则读失败,例如:
  187. -- mysql> select @@server_id;
  188. -- ERROR 9001 (HY000): Max connect timeout reached while reaching hostgroup 20 after 10000ms
  189. -- mysql> UPDATE mysql_servers SET max_replication_lag = 1 WHERE hostgroup_id=20;
  190. -- mysql> LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;
  191. -- 只要超过lag阈值就会记录
  192. -- 也会记录到runtime_mysql_servers
  193. mysql> SELECT * FROM runtime_mysql_servers\G
  194. *************************** 1. row ***************************
  195. hostgroup_id: 10
  196. hostname: 192.168.51.21
  197. port: 3306
  198. gtid_port: 0
  199. status: ONLINE
  200. weight: 1
  201. compression: 0
  202. max_connections: 1000
  203. max_replication_lag: 0
  204. use_ssl: 0
  205. max_latency_ms: 0
  206. comment:
  207. *************************** 2. row ***************************
  208. hostgroup_id: 20
  209. hostname: 192.168.51.22
  210. port: 3306
  211. gtid_port: 0
  212. status: SHUNNED -- 状态异常
  213. weight: 1
  214. compression: 0
  215. max_connections: 1000
  216. max_replication_lag: 1
  217. use_ssl: 0
  218. max_latency_ms: 0
  219. comment:
  220. 2 rows in set (0.00 sec)
  221. -- 记录到mysql_server_replication_lag_log (默认采集周期 基于变量mysql-monitor_replication_lag_interval)
  222. mysql> select * from mysql_server_replication_lag_log limit 6;
  223. +--------------+------+------------------+-----------------+----------+---------------------------------------------------------------------------------------------------------+
  224. | hostname | port | time_start_us | success_time_us | repl_lag | error |
  225. +--------------+------+------------------+-----------------+----------+---------------------------------------------------------------------------------------------------------+
  226. | 192.168.51.22 | 3306 | 1650767114031586 | 3023 | 1 | NULL |
  227. | 192.168.51.22 | 3306 | 1650767124031670 | 0 | NULL | Access denied; you need (at least one of) the SUPER, REPLICATION CLIENT privilege(s) for this operation |
  228. | 192.168.51.22 | 3306 | 1650767134031768 | 2854 | 1 | NULL |
  229. | 192.168.51.22 | 3306 | 1650767144031900 | 2950 | 1 | NULL |
  230. | 192.168.51.22 | 3306 | 1650767154031913 | 2577 | 1 | NULL |
  231. | 192.168.51.22 | 3306 | 1650767164032111 | 3236 | 0 | NULL |
  232. +--------------+------+------------------+-----------------+----------+---------------------------------------------------------------------------------------------------------+
  233. 6 rows in set (0.00 sec)
  234. -- 每次修改proxysql配置,都需要执行load 和 save操作 否则不生效

配置MySQL用户

  1. -- MySQL创建数据库,用户并授权
  2. mysql> create database test;
  3. mysql> CREATE USER 'test'@'%' IDENTIFIED BY 'test';
  4. mysql> GRANT ALL ON test.* TO 'test'@'%';
  5. -- 后端是MySQL8.0的话 需要修改环境变量
  6. -- 不然连接时,会报query_cache_size的错误
  7. update global_variables set variable_value="8.0.4 (ProxySQL)" where variable_name='mysql-server_version';
  8. load mysql variables to run;save mysql variables to disk;
  9. -- caching_sha2_password密码支持,否则只能用mysql_native_password
  10. update global_variables set variable_value='false' where variable_name='admin-hash_passwords';
  11. load admin variables to runtime;
  12. save admin variables to disk;
  13. -- 这样的话 MySQL密码在proxysql是明文存放的
  14. -- 插入MySQL的用户和密码default_hostgroup必须与mysql_servers的hostgroup_id一致
  15. mysql@proxysql> INSERT INTO mysql_users (username,password,default_hostgroup) VALUES ('test','test','10');
  16. Query OK, 1 row affected (0.00 sec)
  17. -- 存储到磁盘
  18. mysql@proxysql> LOAD MYSQL USERS TO RUNTIME;
  19. mysql@proxysql> SAVE MYSQL USERS TO DISK;
  20. mysql@proxysql> SAVE MYSQL USERS FROM RUNTIME; -- 加密密码
  21. mysql@proxysql> select * from mysql_users\G
  22. *************************** 1. row ***************************
  23. username: test
  24. password: *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 -- 加密密码
  25. active: 1 -- 只有active=1的用户才是有效的用户
  26. use_ssl: 0
  27. default_hostgroup: 10
  28. default_schema: NULL
  29. schema_locked: 0
  30. transaction_persistent: 1
  31. fast_forward: 0
  32. backend: 1
  33. frontend: 1
  34. max_connections: 10000
  35. attributes:
  36. comment:
  37. -- 测试连接
  38. root@mysqlclient:/usr/local/share#mysql -utest -ptest -h192.168.51.21 -P6033
  39. mysql: [Warning] Using a password on the command line interface can be insecure.
  40. Welcome to the MySQL monitor. Commands end with ; or \g.
  41. Your MySQL connection id is 1025
  42. Server version: 5.5.30 (ProxySQL)
  43. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  44. Oracle is a registered trademark of Oracle Corporation and/or its
  45. affiliates. Other names may be trademarks of their respective
  46. owners.
  47. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  48. mysql> show databases;
  49. +--------------------+
  50. | Database |
  51. +--------------------+
  52. | information_schema |
  53. | test |
  54. +--------------------+
  55. 2 rows in set (0.01 sec)

配置读写分离

  1. mysql@proxysql> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)
  2. VALUES (1,1,'^SELECT.*FOR UPDATE$',10,1),
  3. (2,1,'^SELECT',20,1);
  4. mysql@proxysql> load mysql query rules to runtime;
  5. mysql@proxysql> save mysql query rules to disk;
  6. 查看sql执行日志
  7. mysql> select * FROM stats_mysql_query_digest where digest_text like 'delete%' order by first_seen desc limit 1\G
  8. *************************** 1. row ***************************
  9. hostgroup: 10
  10. schemaname: information_schema
  11. username: test
  12. client_address:
  13. digest: 0xC66101CC27808EB4
  14. digest_text: delete from test.sbtest1 limit ?
  15. count_star: 1
  16. first_seen: 1650769517
  17. last_seen: 1650769517
  18. sum_time: 1871104
  19. min_time: 1871104
  20. max_time: 1871104
  21. sum_rows_affected: 200000
  22. sum_rows_sent: 0
  23. 1 row in set (0.00 sec)
  24. 如果从库异常,或者延迟超过设定的值,设置了读写规则就会失败,建议lag 设置为0
  25. mysql> select @@server_id;
  26. ERROR 9001 (HY000): Max connect timeout reached while reaching hostgroup 20 after 10001ms
  27. mysql> begin;select @@server_id;
  28. Query OK, 0 rows affected (0.00 sec)
  29. 但是如果读库挂了,不会自动切到写库去查询
  30. mysql> select @@server_id;
  31. ERROR 2013 (HY000): Lost connection to MySQL server during query
  32. +-------------+
  33. | @@server_id |
  34. +-------------+
  35. | 5121 |
  36. +-------------+
  37. 1 row in set (0.00 sec)
  38. 查看proxysql 日志
  39. root@zp-prd-oracle-51-21:/opt/software#tail -10 /var/lib/proxysql/proxysql.log
  40. 2022-04-24 10:53:04 MySQL_Monitor.cpp:2287:monitor_replication_lag_thread(): [ERROR] Replication lag on server 192.168.51.22:3306 is NULL, using the value 60 (mysql-monitor_slave_lag_when_null)
  41. 2022-04-24 10:53:14 MySQL_Monitor.cpp:2287:monitor_replication_lag_thread(): [ERROR] Replication lag on server 192.168.51.22:3306 is NULL, using the value 60 (mysql-monitor_slave_lag_when_null)
  42. 2022-04-24 10:53:24 MySQL_Monitor.cpp:2287:monitor_replication_lag_thread(): [ERROR] Replication lag on server 192.168.51.22:3306 is NULL, using the value 60 (mysql-monitor_slave_lag_when_null)
  43. 2022-04-24 10:54:34 MySQL_HostGroups_Manager.cpp:2888:get_random_MySrvC(): [ERROR] Hostgroup 20 has no servers available! Checking servers shunned for more than 1 second
  44. 2022-04-24 10:54:36 MySQL_HostGroups_Manager.cpp:2888:get_random_MySrvC(): [ERROR] Hostgroup 20 has no servers available! Checking servers shunned for more than 1 second
  45. 2022-04-24 10:54:38 MySQL_HostGroups_Manager.cpp:2888:get_random_MySrvC(): [ERROR] Hostgroup 20 has no servers available! Checking servers shunned for more than 1 second
  46. 2022-04-24 10:54:40 MySQL_HostGroups_Manager.cpp:2888:get_random_MySrvC(): [ERROR] Hostgroup 20 has no servers available! Checking servers shunned for more than 1 second
  47. 2022-04-24 10:54:42 MySQL_HostGroups_Manager.cpp:2888:get_random_MySrvC(): [ERROR] Hostgroup 20 has no servers available! Checking servers shunned for more than 1 second
  48. 2022-04-24 10:54:44 MySQL_HostGroups_Manager.cpp:2888:get_random_MySrvC(): [ERROR] Hostgroup 20 has no servers available! Checking servers shunned for more than 1 second
  49. 2022-04-24 10:54:44 MySQL_Session.cpp:2594:handler_again___status_CONNECTING_SERVER(): [ERROR] Max connect timeout reached while reaching hostgroup 20 after 10000ms . HG status: [{"Bytes_recv":"27","Bytes_sent":"54","ConnERR":"0","ConnFree":"0","ConnOK":"1","ConnUsed":"0","Latency_us":"637","MaxConnUsed":"1","Queries":"3","Queries_GTID_sync":"0","hostgroup":"20","srv_host":"192.168.51.22","srv_port":"3306","status":"SHUNNED_REPLICATION_LAG"}]
  50. -- 增加写库的读群组,并增加从库的权重
  51. update mysql_servers set weight=10000000 where hostgroup_id=20 and hostname='192.168.51.22';
  52. insert into mysql_servers(hostgroup_id,hostname,port) values (20,'192.168.51.21',3306);
  53. load mysql servers to runtime;
  54. save mysql servers to disk;

proxysql集群

  1. -- 查看现有节点
  2. mysql> select * from global_variables where variable_name in ('admin-admin_credentials', 'admin-cluster_password', 'mysql-monitor_password', 'admin-cluster_username', 'mysql-monitor_username');
  3. +-------------------------+----------------+
  4. | variable_name | variable_value |
  5. +-------------------------+----------------+
  6. | admin-admin_credentials | admin:admin |
  7. | admin-cluster_password | |
  8. | admin-cluster_username | |
  9. | mysql-monitor_password | ProxySQLPa55 |
  10. | mysql-monitor_username | proxysql |
  11. +-------------------------+----------------+
  12. -- 依次在各个proxySQL节点修改管理员密码,集群用户密码
  13. update global_variables set variable_value='admin:newadmin;cluster:cluster' where variable_name='admin-admin_credentials';
  14. update global_variables set variable_value='cluster' where variable_name='admin-cluster_username';
  15. update global_variables set variable_value='cluster' where variable_name='admin-cluster_password';
  16. -- 下面是默认配置,不需要修改(集群同步配置)
  17. update global_variables set variable_value=1000 where variable_name='admin-cluster_check_interval_ms';
  18. update global_variables set variable_value=10 where variable_name='admin-cluster_check_status_frequency';
  19. update global_variables set variable_value='true' where variable_name='admin-cluster_mysql_query_rules_save_to_disk';
  20. update global_variables set variable_value='true' where variable_name='admin-cluster_mysql_servers_save_to_disk';
  21. update global_variables set variable_value='true' where variable_name='admin-cluster_mysql_users_save_to_disk';
  22. update global_variables set variable_value='true' where variable_name='admin-cluster_proxysql_servers_save_to_disk';
  23. update global_variables set variable_value=3 where variable_name='admin-cluster_mysql_query_rules_diffs_before_sync';
  24. update global_variables set variable_value=3 where variable_name='admin-cluster_mysql_servers_diffs_before_sync';
  25. update global_variables set variable_value=3 where variable_name='admin-cluster_mysql_users_diffs_before_sync';
  26. update global_variables set variable_value=3 where variable_name='admin-cluster_proxysql_servers_diffs_before_sync';
  27. load admin variables to RUNTIME;
  28. save admin variables to disk;
  29. INSERT INTO proxysql_servers (hostname,port,weight,comment) VALUES ('192.168.51.21',6032,0,'p1');
  30. INSERT INTO proxysql_servers (hostname,port,weight,comment) VALUES ('192.168.51.22',6032,0,'p2');
  31. LOAD PROXYSQL SERVERS TO RUNTIME;
  32. SAVE PROXYSQL SERVERS TO DISK;
  33. -- 新节点验证
  34. select * from mysql_query_rules\G
  35. select * from mysql_servers;
  36. select * from mysql_users ;
  37. select * from proxysql_servers;
  38. -- 验证集群配置是否已经同步
  39. SELECT stats_proxysql_servers_checksums.hostname, stats_proxysql_servers_metrics.Uptime_s, stats_proxysql_servers_checksums.port, stats_proxysql_servers_checksums.name, stats_proxysql_servers_checksums.version, FROM_UNIXTIME(stats_proxysql_servers_checksums.epoch) epoch, stats_proxysql_servers_checksums.checksum, stats_proxysql_servers_checksums.diff_check FROM stats_proxysql_servers_metrics JOIN stats_proxysql_servers_checksums ON stats_proxysql_servers_checksums.hostname = stats_proxysql_servers_metrics.hostname WHERE stats_proxysql_servers_metrics.Uptime_s > 0 ORDER BY name,version desc;
  40. +--------------+----------+------+-------------------+---------+---------------------+--------------------+------------+
  41. | hostname | Uptime_s | port | name | version | epoch | checksum | diff_check |
  42. +--------------+----------+------+-------------------+---------+---------------------+--------------------+------------+
  43. | 192.168.51.22 | 1899 | 6032 | admin_variables | 2 | 2022-04-24 09:30:27 | 0x8759E3B35A6FD155 | 0 |
  44. | 192.168.51.21 | 80602 | 6032 | admin_variables | 2 | 2022-04-24 09:24:44 | 0x8759E3B35A6FD155 | 0 |
  45. | 192.168.51.22 | 1899 | 6032 | mysql_query_rules | 2 | 2022-04-24 09:30:35 | 0x8F7C9C6F2E6449F5 | 0 |
  46. | 192.168.51.21 | 80602 | 6032 | mysql_query_rules | 2 | 2022-04-24 02:40:54 | 0x8F7C9C6F2E6449F5 | 0 |
  47. | 192.168.51.21 | 80602 | 6032 | mysql_servers | 16 | 2022-04-24 09:32:48 | 0x159BEACEDABA56A2 | 0 |
  48. | 192.168.51.22 | 1899 | 6032 | mysql_servers | 3 | 2022-04-24 09:32:48 | 0x159BEACEDABA56A2 | 0 |
  49. | 192.168.51.21 | 80602 | 6032 | mysql_users | 6 | 2022-04-24 02:23:55 | 0x57AFBBC015E19DB5 | 0 |
  50. | 192.168.51.22 | 1899 | 6032 | mysql_users | 2 | 2022-04-24 09:30:35 | 0x57AFBBC015E19DB5 | 0 |
  51. | 192.168.51.22 | 1899 | 6032 | mysql_variables | 2 | 2022-04-24 09:30:35 | 0x299483C6D90F1055 | 0 |
  52. | 192.168.51.21 | 80602 | 6032 | mysql_variables | 2 | 2022-04-23 11:33:33 | 0x299483C6D90F1055 | 0 |
  53. | 192.168.51.22 | 1899 | 6032 | proxysql_servers | 2 | 2022-04-24 09:30:32 | 0x514523051E515E87 | 0 |
  54. | 192.168.51.21 | 80602 | 6032 | proxysql_servers | 2 | 2022-04-24 09:25:00 | 0x514523051E515E87 | 0 |
  55. +--------------+----------+------+-------------------+---------+---------------------+--------------------+------------+
  56. 相同配置的变量 version大的变量值会把自己数据同步到version小的节点,如果version一样会复制最高的epoch值

proxysql 集群监控原理

  1. 因为集群间,所有节点都是相互监控的。所以当配置发生变动时,它们可以立即发现。当其他节点的配置发生变动时,本节点会先去检查一次它自身的配置,因为有可能remote instance 和local instance 同时发生配置变动。如果不同:
  2. 如果它们自身的 version = 1,就去找集群内 version >1,并且 epoch 最高的节点,并立即同步。
  3. 如果version >1, 该节点开始统计和其他节点间的differ 数。
  4. 当 differ 大于 clustername_diffs_before_sync , 并且clusternamediffs_before_sync > 0, 就去找集群内 version >1, 并且epoch 最高的节点,并立即同步。
  5. 同步过程如下:
  6. 健康检查语句会执行一系列的SELECT 语句,例如 Select list_of_columns FROM runtime_module.
  7. SELECT hostgroup_id, hostname, port, status, weight, compression, max_connections, max_replication_lag, use_ssl, max_latency_ms, comment FROM runtime_mysql_servers;
  8. SELECT writer_hostgroup, reader_hostgroup, comment FROM runtime_mysql_replication_hostgroups;
  9. 删除本地配置。例如:
  10. DELETE FROM mysql_servers;
  11. DELETE FROM mysql_replication_hostgroups;
  12. 将新的配置insert 到本地表
  13. 内部提交LOAD module_name TO RUNTIME。 对应的version 会增加,并且生成一个新的 checksum
  14. 如果clusternamesave_to_disk =true,还会内部执行 SAVE module_name TO DISK。

proxysql配置MySQL用户

  1. CREATE USER 'test'@'%' IDENTIFIED BY 'test';
  2. GRANT ALL ON test.* TO 'test'@'%';
  3. -- 当MySQL创建好用户,proxysql插入记录
  4. INSERT INTO mysql_users (username,password,default_hostgroup) VALUES ('test','test','10');
  5. -- 如果是只读用户,将组设置为20
  6. INSERT INTO mysql_users (username,password,default_hostgroup) VALUES ('test','test','20');
  7. -- proxysql生效记录
  8. LOAD MYSQL USERS TO RUNTIME;
  9. SAVE MYSQL USERS TO DISK;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/230577
推荐阅读
相关标签
  

闽ICP备14008679号