赞
踩
原文: https://www.yuque.com/wei01/wql35u/wbgk1a
-
- # 安装依赖包
- yum install gnutls perl-DBD-mysql -y
-
- rpm -ivh /tmp/proxysql-2.3.2-1-centos7.x86_64.rpm
-
- # 查看相应安装文件路径,有哪些文件
- root@mysqlhost01:/opt/software#rpm -ql proxysql
- /etc/logrotate.d/proxysql
- /etc/proxysql.cnf
- /etc/systemd/system/proxysql-initial.service
- /etc/systemd/system/proxysql.service
- /usr/bin/proxysql
- /usr/share/proxysql/tools/proxysql_galera_checker.sh
- /usr/share/proxysql/tools/proxysql_galera_writer.pl
-
- # 查看版本
- root@mysqlhost01:/opt/software#proxysql --version
- ProxySQL version 2.3.2-10-g8cd66cf, codename Truls
-
- # 如果是单独机器,可以安装MySQL客户端
- yum install Percona-XtraDB-Cluster-client-80
-
- # 查看状态
- root@mysqlhost01:/opt/software#systemctl start proxysql
- root@mysqlhost01:/opt/software#systemctl status proxysql
- ● proxysql.service - High Performance Advanced Proxy for MySQL
- Loaded: loaded (/etc/systemd/system/proxysql.service; enabled; vendor preset: disabled)
- Active: active (running) since Sat 2022-04-23 19:16:06 CST; 3s ago
- Process: 55697 ExecStart=/usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf $PROXYSQL_OPTS (code=exited, status=0/SUCCESS)
- Main PID: 55699 (proxysql)
- CGroup: /system.slice/proxysql.service
- ├─55699 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf
- └─55700 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf
-
- Apr 23 19:16:06 mysqlhost01 systemd[1]: Starting High Performance Advanced Proxy for MySQL...
- Apr 23 19:16:06 mysqlhost01 proxysql[55697]: 2022-04-23 19:16:06 [INFO] Using config file /etc/proxysql.cnf
- Apr 23 19:16:06 mysqlhost01 proxysql[55697]: 2022-04-23 19:16:06 [INFO] Current RLIMIT_NOFILE: 102400
- 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
- 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.
- Apr 23 19:16:06 mysqlhost01 systemd[1]: Started High Performance Advanced Proxy for MySQL.
-
- # 查看使用的端口
- root@mysqlhost01:/opt/software#netstat -anlp | grep proxysql
- tcp 0 0 0.0.0.0:6032 0.0.0.0:* LISTEN 55700/proxysql
- tcp 0 0 0.0.0.0:6033 0.0.0.0:* LISTEN 55700/proxysql
-
- 6032 是 ProxySQL 的管理端口号,6033是对外服务的端口号
-
- ProxySQL 的用户名和密码都是默认的 admin
- 如果是MySQL数据库用户,则使用6033端口
- # 连接proxysql
- root@192.168.51.21:/opt/software#mysql -uadmin -padmin -h 127.0.0.1 -P 6032
- mysql: [Warning] Using a password on the command line interface can be insecure.
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 1
- Server version: 5.5.30 (ProxySQL Admin Module)
-
- Copyright (c) 2000, 2021, Oracle and/or its affiliates.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- mysql> show databases;
- +-----+---------------+-------------------------------------+
- | seq | name | file |
- +-----+---------------+-------------------------------------+
- | 0 | main | |
- | 2 | disk | /var/lib/proxysql/proxysql.db |
- | 3 | stats | |
- | 4 | monitor | |
- | 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
- +-----+---------------+-------------------------------------+
- 5 rows in set (0.00 sec)
-
- mysql> show tables from main;
- +----------------------------------------------------+
- | tables |
- +----------------------------------------------------+
- | global_variables |
- | mysql_aws_aurora_hostgroups |
- | mysql_collations |
- | mysql_firewall_whitelist_rules |
- | mysql_firewall_whitelist_sqli_fingerprints |
- | mysql_firewall_whitelist_users |
- | mysql_galera_hostgroups |
- | mysql_group_replication_hostgroups |
- | mysql_query_rules |
- | mysql_query_rules_fast_routing |
- | mysql_replication_hostgroups |
- | mysql_servers |
- | mysql_users |
- | proxysql_servers |
- | restapi_routes |
- | runtime_checksums_values |
- | runtime_global_variables |
- | runtime_mysql_aws_aurora_hostgroups |
- | runtime_mysql_firewall_whitelist_rules |
- | runtime_mysql_firewall_whitelist_sqli_fingerprints |
- | runtime_mysql_firewall_whitelist_users |
- | runtime_mysql_galera_hostgroups |
- | runtime_mysql_group_replication_hostgroups |
- | runtime_mysql_query_rules |
- | runtime_mysql_query_rules_fast_routing |
- | runtime_mysql_replication_hostgroups |
- | runtime_mysql_servers |
- | runtime_mysql_users |
- | runtime_proxysql_servers |
- | runtime_restapi_routes |
- | runtime_scheduler |
- | scheduler |
- +----------------------------------------------------+
-
-
- mysql_servers: 后端可以连接 MySQL 服务器的列表
-
- mysql_users: 配置后端数据库的账号和监控的账号。
-
- mysql_query_rules: 指定 Query 路由到后端不同服务器的规则列表
-
- runtime_开头的是运行时的配置,这些是不能修改的。要修改ProxySQL的配置,需要修改了非runtime_表,修改后必须执行LOAD … TO RUNTIME才能加载到RUNTIME生效,执行save … to disk才能将配置持久化保存到磁盘
-
- -- 向ProxySQL中添加MySQL节点
- mysql@proxysql> insert into mysql_servers(hostgroup_id,hostname,port) values (10,'192.168.51.21',3306);
- mysql@proxysql> insert into mysql_servers(hostgroup_id,hostname,port) values (10,'192.168.51.22',3306);
- -- 应用配置
- mysql@proxysql> load mysql servers to runtime;
- mysql@proxysql> save mysql servers to disk;
-
- mysql> select * from mysql_servers\G
- *************************** 1. row ***************************
- hostgroup_id: 10
- hostname: 192.168.51.21
- port: 3306
- gtid_port: 0
- status: ONLINE
- weight: 1
- compression: 0
- max_connections: 1000
- max_replication_lag: 0
- use_ssl: 0
- max_latency_ms: 0
- comment:
- *************************** 2. row ***************************
- hostgroup_id: 10
- hostname: 192.168.51.22
- port: 3306
- gtid_port: 0
- status: ONLINE
- weight: 1
- compression: 0
- max_connections: 1000
- max_replication_lag: 0
- use_ssl: 0
- max_latency_ms: 0
- comment:
- 2 rows in set (0.00 sec)
-
- -- 添加节点之后,还需要监控后端节点。对于后端是主从复制的环境来说,这是必须的,因为ProxySQL需要通过每个节点的read_only值来自动调整它们是属于读组还是写组。
-
- #mysql -uroot -pBigdata@123
- CREATE USER 'proxysql'@'%' IDENTIFIED WITH mysql_native_password by 'ProxySQLPa55';
- GRANT replication client ON *.* TO 'proxysql'@'%';
-
- -- proxysql配置
- mysql@proxysql> UPDATE global_variables SET variable_value='proxysql' WHERE variable_name='mysql-monitor_username';
- mysql@proxysql> UPDATE global_variables SET variable_value='ProxySQLPa55' WHERE variable_name='mysql-monitor_password';
-
- -- 将配置写入到磁盘
- mysql@proxysql> LOAD MYSQL VARIABLES TO RUNTIME;
- mysql@proxysql> SAVE MYSQL VARIABLES TO DISK;
-
- -- 检查监控日志,如果错误,则说明有问题(如果不指定的话 监控用户 默认是monitor用户)
- 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;
- +---------------+------+----------------------------------------+-------------------------+------------------------------------------------------------------------+
- | hostname | port | from_unixtime(time_start_us/1000/1000) | connect_success_time_us | connect_error |
- +---------------+------+----------------------------------------+-------------------------+------------------------------------------------------------------------+
- | 192.168.51.116| 3306 | 2022-04-25 08:43:56 | 2161 | NULL |
- | 192.168.51.116| 3306 | 2022-04-25 08:42:56 | 1437 | NULL |
- | 192.168.51.116| 3306 | 2022-04-25 08:41:56 | 1237 | NULL |
- | 192.168.51.116| 3306 | 2022-04-25 08:40:56 | 1114 | NULL |
- | 192.168.51.116| 3306 | 2022-04-25 08:40:04 | 0 | Access denied for user 'monitor'@'10.205.16.116' (using password: YES) |
- | 192.168.51.116| 3306 | 2022-04-25 08:39:04 | 0 | Access denied for user 'monitor'@'10.205.16.116' (using password: YES) |
- +---------------+------+----------------------------------------+-------------------------+------------------------------------------------------------------------+
-
- mysql@proxysql> SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 6;
- +--------------+------+------------------+----------------------+------------+
- | hostname | port | time_start_us | ping_success_time_us | ping_error |
- +--------------+------+------------------+----------------------+------------+
- | 192.168.51.21 | 3306 | 1650713803457556 | 61 | NULL |
- | 192.168.51.22 | 3306 | 1650713803298807 | 150 | NULL |
- | 192.168.51.22 | 3306 | 1650713793451431 | 217 | NULL |
- | 192.168.51.21 | 3306 | 1650713793298661 | 113 | NULL |
- | 192.168.51.22 | 3306 | 1650713783412590 | 227 | NULL |
- | 192.168.51.21 | 3306 | 1650713783298560 | 143 | NULL |
- +--------------+------+------------------+----------------------+------------+
-
- -- 插入组类定义
-
- mysql@proxysql> 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),
- 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',
- comment VARCHAR NOT NULL DEFAULT '', UNIQUE (reader_hostgroup))
-
- -- 第一个是写组ID,读组ID,校验类型(上面几种任意一种),注释(自定义)
- insert into mysql_replication_hostgroups values(10,20,'read_only','mysql2122');
-
- -- 生效配置
- load mysql servers to runtime;
- save mysql servers to disk;
-
- -- 一加载,Monitor模块就会开始监控后端的read_only值,当监控到read_only值后,就会按照read_only的值将某些节点自动移动到读/写组
-
- mysql> select * from mysql_servers\G
- *************************** 1. row ***************************
- hostgroup_id: 10
- hostname: 192.168.51.21
- port: 3306
- gtid_port: 0
- status: ONLINE
- weight: 1
- compression: 0
- max_connections: 1000
- max_replication_lag: 0
- use_ssl: 0
- max_latency_ms: 0
- comment:
- *************************** 2. row ***************************
- hostgroup_id: 20 --检测到51.22节点 read_only=ON时,会变成20
- hostname: 192.168.51.22
- port: 3306
- gtid_port: 0
- status: ONLINE
- weight: 1
- compression: 0
- max_connections: 1000
- max_replication_lag: 0
- use_ssl: 0
- max_latency_ms: 0
- comment:
-
- -- readonly 监控
- mysql> select * from mysql_server_read_only_log order by time_start_us desc limit 6;
- +--------------+------+------------------+-----------------+-----------+-------+
- | hostname | port | time_start_us | success_time_us | read_only | error |
- +--------------+------+------------------+-----------------+-----------+-------+
- | 192.168.51.22 | 3306 | 1650766001921374 | 240 | 1 | NULL |
- | 192.168.51.21 | 3306 | 1650766001903998 | 230 | 0 | NULL |
- | 192.168.51.22 | 3306 | 1650766000433229 | 294 | 1 | NULL |
- | 192.168.51.21 | 3306 | 1650766000403933 | 156 | 0 | NULL |
- | 192.168.51.22 | 3306 | 1650765998927168 | 302 | 1 | NULL |
- | 192.168.51.21 | 3306 | 1650765998903824 | 229 | 0 | NULL |
- +--------------+------+------------------+-----------------+-----------+-------+
-
- -- 设置最大lag记录(建议不设置,会影响读写路由规则)
- -- 如果延迟超过1秒,如果设置路由规则,则读失败,例如:
- -- mysql> select @@server_id;
- -- ERROR 9001 (HY000): Max connect timeout reached while reaching hostgroup 20 after 10000ms
-
- -- mysql> UPDATE mysql_servers SET max_replication_lag = 1 WHERE hostgroup_id=20;
-
- -- mysql> LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;
-
- -- 只要超过lag阈值就会记录
- -- 也会记录到runtime_mysql_servers
- mysql> SELECT * FROM runtime_mysql_servers\G
- *************************** 1. row ***************************
- hostgroup_id: 10
- hostname: 192.168.51.21
- port: 3306
- gtid_port: 0
- status: ONLINE
- weight: 1
- compression: 0
- max_connections: 1000
- max_replication_lag: 0
- use_ssl: 0
- max_latency_ms: 0
- comment:
- *************************** 2. row ***************************
- hostgroup_id: 20
- hostname: 192.168.51.22
- port: 3306
- gtid_port: 0
- status: SHUNNED -- 状态异常
- weight: 1
- compression: 0
- max_connections: 1000
- max_replication_lag: 1
- use_ssl: 0
- max_latency_ms: 0
- comment:
- 2 rows in set (0.00 sec)
-
- -- 记录到mysql_server_replication_lag_log (默认采集周期 基于变量mysql-monitor_replication_lag_interval)
- mysql> select * from mysql_server_replication_lag_log limit 6;
- +--------------+------+------------------+-----------------+----------+---------------------------------------------------------------------------------------------------------+
- | hostname | port | time_start_us | success_time_us | repl_lag | error |
- +--------------+------+------------------+-----------------+----------+---------------------------------------------------------------------------------------------------------+
- | 192.168.51.22 | 3306 | 1650767114031586 | 3023 | 1 | NULL |
- | 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 |
- | 192.168.51.22 | 3306 | 1650767134031768 | 2854 | 1 | NULL |
- | 192.168.51.22 | 3306 | 1650767144031900 | 2950 | 1 | NULL |
- | 192.168.51.22 | 3306 | 1650767154031913 | 2577 | 1 | NULL |
- | 192.168.51.22 | 3306 | 1650767164032111 | 3236 | 0 | NULL |
- +--------------+------+------------------+-----------------+----------+---------------------------------------------------------------------------------------------------------+
- 6 rows in set (0.00 sec)
-
- -- 每次修改proxysql配置,都需要执行load 和 save操作 否则不生效
- -- MySQL创建数据库,用户并授权
- mysql> create database test;
- mysql> CREATE USER 'test'@'%' IDENTIFIED BY 'test';
- mysql> GRANT ALL ON test.* TO 'test'@'%';
-
- -- 后端是MySQL8.0的话 需要修改环境变量
- -- 不然连接时,会报query_cache_size的错误
- update global_variables set variable_value="8.0.4 (ProxySQL)" where variable_name='mysql-server_version';
- load mysql variables to run;save mysql variables to disk;
-
-
- -- caching_sha2_password密码支持,否则只能用mysql_native_password
- update global_variables set variable_value='false' where variable_name='admin-hash_passwords';
-
- load admin variables to runtime;
- save admin variables to disk;
-
- -- 这样的话 MySQL密码在proxysql是明文存放的
-
-
- -- 插入MySQL的用户和密码default_hostgroup必须与mysql_servers的hostgroup_id一致
- mysql@proxysql> INSERT INTO mysql_users (username,password,default_hostgroup) VALUES ('test','test','10');
- Query OK, 1 row affected (0.00 sec)
-
- -- 存储到磁盘
- mysql@proxysql> LOAD MYSQL USERS TO RUNTIME;
- mysql@proxysql> SAVE MYSQL USERS TO DISK;
-
- mysql@proxysql> SAVE MYSQL USERS FROM RUNTIME; -- 加密密码
-
- mysql@proxysql> select * from mysql_users\G
- *************************** 1. row ***************************
- username: test
- password: *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 -- 加密密码
- active: 1 -- 只有active=1的用户才是有效的用户
- use_ssl: 0
- default_hostgroup: 10
- default_schema: NULL
- schema_locked: 0
- transaction_persistent: 1
- fast_forward: 0
- backend: 1
- frontend: 1
- max_connections: 10000
- attributes:
- comment:
-
- -- 测试连接
- root@mysqlclient:/usr/local/share#mysql -utest -ptest -h192.168.51.21 -P6033
- mysql: [Warning] Using a password on the command line interface can be insecure.
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 1025
- Server version: 5.5.30 (ProxySQL)
-
- Copyright (c) 2000, 2021, Oracle and/or its affiliates.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | test |
- +--------------------+
- 2 rows in set (0.01 sec)
-
- mysql@proxysql> 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);
- mysql@proxysql> load mysql query rules to runtime;
- mysql@proxysql> save mysql query rules to disk;
-
- 查看sql执行日志
- mysql> select * FROM stats_mysql_query_digest where digest_text like 'delete%' order by first_seen desc limit 1\G
- *************************** 1. row ***************************
- hostgroup: 10
- schemaname: information_schema
- username: test
- client_address:
- digest: 0xC66101CC27808EB4
- digest_text: delete from test.sbtest1 limit ?
- count_star: 1
- first_seen: 1650769517
- last_seen: 1650769517
- sum_time: 1871104
- min_time: 1871104
- max_time: 1871104
- sum_rows_affected: 200000
- sum_rows_sent: 0
- 1 row in set (0.00 sec)
-
- 如果从库异常,或者延迟超过设定的值,设置了读写规则就会失败,建议lag 设置为0
- mysql> select @@server_id;
- ERROR 9001 (HY000): Max connect timeout reached while reaching hostgroup 20 after 10001ms
- mysql> begin;select @@server_id;
- Query OK, 0 rows affected (0.00 sec)
-
- 但是如果读库挂了,不会自动切到写库去查询
-
- mysql> select @@server_id;
- ERROR 2013 (HY000): Lost connection to MySQL server during query
-
- +-------------+
- | @@server_id |
- +-------------+
- | 5121 |
- +-------------+
- 1 row in set (0.00 sec)
-
- 查看proxysql 日志
- root@zp-prd-oracle-51-21:/opt/software#tail -10 /var/lib/proxysql/proxysql.log
- 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)
- 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)
- 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)
- 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
- 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
- 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
- 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
- 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
- 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
- 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"}]
-
- -- 增加写库的读群组,并增加从库的权重
-
- update mysql_servers set weight=10000000 where hostgroup_id=20 and hostname='192.168.51.22';
-
- insert into mysql_servers(hostgroup_id,hostname,port) values (20,'192.168.51.21',3306);
-
- load mysql servers to runtime;
- save mysql servers to disk;
- -- 查看现有节点
- mysql> select * from global_variables where variable_name in ('admin-admin_credentials', 'admin-cluster_password', 'mysql-monitor_password', 'admin-cluster_username', 'mysql-monitor_username');
- +-------------------------+----------------+
- | variable_name | variable_value |
- +-------------------------+----------------+
- | admin-admin_credentials | admin:admin |
- | admin-cluster_password | |
- | admin-cluster_username | |
- | mysql-monitor_password | ProxySQLPa55 |
- | mysql-monitor_username | proxysql |
- +-------------------------+----------------+
-
- -- 依次在各个proxySQL节点修改管理员密码,集群用户密码
- update global_variables set variable_value='admin:newadmin;cluster:cluster' where variable_name='admin-admin_credentials';
- update global_variables set variable_value='cluster' where variable_name='admin-cluster_username';
- update global_variables set variable_value='cluster' where variable_name='admin-cluster_password';
-
- -- 下面是默认配置,不需要修改(集群同步配置)
- update global_variables set variable_value=1000 where variable_name='admin-cluster_check_interval_ms';
- update global_variables set variable_value=10 where variable_name='admin-cluster_check_status_frequency';
- update global_variables set variable_value='true' where variable_name='admin-cluster_mysql_query_rules_save_to_disk';
- update global_variables set variable_value='true' where variable_name='admin-cluster_mysql_servers_save_to_disk';
- update global_variables set variable_value='true' where variable_name='admin-cluster_mysql_users_save_to_disk';
- update global_variables set variable_value='true' where variable_name='admin-cluster_proxysql_servers_save_to_disk';
- update global_variables set variable_value=3 where variable_name='admin-cluster_mysql_query_rules_diffs_before_sync';
- update global_variables set variable_value=3 where variable_name='admin-cluster_mysql_servers_diffs_before_sync';
- update global_variables set variable_value=3 where variable_name='admin-cluster_mysql_users_diffs_before_sync';
- update global_variables set variable_value=3 where variable_name='admin-cluster_proxysql_servers_diffs_before_sync';
-
- load admin variables to RUNTIME;
- save admin variables to disk;
-
-
- INSERT INTO proxysql_servers (hostname,port,weight,comment) VALUES ('192.168.51.21',6032,0,'p1');
- INSERT INTO proxysql_servers (hostname,port,weight,comment) VALUES ('192.168.51.22',6032,0,'p2');
-
- LOAD PROXYSQL SERVERS TO RUNTIME;
- SAVE PROXYSQL SERVERS TO DISK;
-
- -- 新节点验证
- select * from mysql_query_rules\G
- select * from mysql_servers;
- select * from mysql_users ;
- select * from proxysql_servers;
-
- -- 验证集群配置是否已经同步
-
- 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;
- +--------------+----------+------+-------------------+---------+---------------------+--------------------+------------+
- | hostname | Uptime_s | port | name | version | epoch | checksum | diff_check |
- +--------------+----------+------+-------------------+---------+---------------------+--------------------+------------+
- | 192.168.51.22 | 1899 | 6032 | admin_variables | 2 | 2022-04-24 09:30:27 | 0x8759E3B35A6FD155 | 0 |
- | 192.168.51.21 | 80602 | 6032 | admin_variables | 2 | 2022-04-24 09:24:44 | 0x8759E3B35A6FD155 | 0 |
- | 192.168.51.22 | 1899 | 6032 | mysql_query_rules | 2 | 2022-04-24 09:30:35 | 0x8F7C9C6F2E6449F5 | 0 |
- | 192.168.51.21 | 80602 | 6032 | mysql_query_rules | 2 | 2022-04-24 02:40:54 | 0x8F7C9C6F2E6449F5 | 0 |
- | 192.168.51.21 | 80602 | 6032 | mysql_servers | 16 | 2022-04-24 09:32:48 | 0x159BEACEDABA56A2 | 0 |
- | 192.168.51.22 | 1899 | 6032 | mysql_servers | 3 | 2022-04-24 09:32:48 | 0x159BEACEDABA56A2 | 0 |
- | 192.168.51.21 | 80602 | 6032 | mysql_users | 6 | 2022-04-24 02:23:55 | 0x57AFBBC015E19DB5 | 0 |
- | 192.168.51.22 | 1899 | 6032 | mysql_users | 2 | 2022-04-24 09:30:35 | 0x57AFBBC015E19DB5 | 0 |
- | 192.168.51.22 | 1899 | 6032 | mysql_variables | 2 | 2022-04-24 09:30:35 | 0x299483C6D90F1055 | 0 |
- | 192.168.51.21 | 80602 | 6032 | mysql_variables | 2 | 2022-04-23 11:33:33 | 0x299483C6D90F1055 | 0 |
- | 192.168.51.22 | 1899 | 6032 | proxysql_servers | 2 | 2022-04-24 09:30:32 | 0x514523051E515E87 | 0 |
- | 192.168.51.21 | 80602 | 6032 | proxysql_servers | 2 | 2022-04-24 09:25:00 | 0x514523051E515E87 | 0 |
- +--------------+----------+------+-------------------+---------+---------------------+--------------------+------------+
-
- 相同配置的变量 version大的变量值会把自己数据同步到version小的节点,如果version一样会复制最高的epoch值
-
- 因为集群间,所有节点都是相互监控的。所以当配置发生变动时,它们可以立即发现。当其他节点的配置发生变动时,本节点会先去检查一次它自身的配置,因为有可能remote instance 和local instance 同时发生配置变动。如果不同:
-
- 如果它们自身的 version = 1,就去找集群内 version >1,并且 epoch 最高的节点,并立即同步。
- 如果version >1, 该节点开始统计和其他节点间的differ 数。
- 当 differ 大于 clustername_diffs_before_sync , 并且clusternamediffs_before_sync > 0, 就去找集群内 version >1, 并且epoch 最高的节点,并立即同步。
- 同步过程如下:
-
- 健康检查语句会执行一系列的SELECT 语句,例如 Select list_of_columns FROM runtime_module.
- SELECT hostgroup_id, hostname, port, status, weight, compression, max_connections, max_replication_lag, use_ssl, max_latency_ms, comment FROM runtime_mysql_servers;
- SELECT writer_hostgroup, reader_hostgroup, comment FROM runtime_mysql_replication_hostgroups;
- 删除本地配置。例如:
- DELETE FROM mysql_servers;
- DELETE FROM mysql_replication_hostgroups;
- 将新的配置insert 到本地表
- 内部提交LOAD module_name TO RUNTIME。 对应的version 会增加,并且生成一个新的 checksum
- 如果clusternamesave_to_disk =true,还会内部执行 SAVE module_name TO DISK。
- CREATE USER 'test'@'%' IDENTIFIED BY 'test';
- GRANT ALL ON test.* TO 'test'@'%';
-
- -- 当MySQL创建好用户,proxysql插入记录
- INSERT INTO mysql_users (username,password,default_hostgroup) VALUES ('test','test','10');
-
- -- 如果是只读用户,将组设置为20
- INSERT INTO mysql_users (username,password,default_hostgroup) VALUES ('test','test','20');
-
- -- proxysql生效记录
- LOAD MYSQL USERS TO RUNTIME;
- SAVE MYSQL USERS TO DISK;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。