cat << EOF | tee /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL YUM repository
baseurl=https:/$repo.proxysql.com/ProxySQL/proxysql-2.2.x/centos/$releasever
gpgcheck=1
gpgkey=https:/$repo.proxysql.com/ProxySQL/repo_pub_key
EOF
yum install proxysql
或者
yum install proxysql-2.2.0
systemctl start proxysql
mysql -uadmin -padmin -h127.0.0.1 -P6032 --prompt='Admin> '
select * from mysql_servers;
#第一组
insert into
mysql_servers(hostgroup_id,max_connections,comment,hostname,port)
values(1000,3000,'MGR01-01','192.168.2.3',3306);
insert into
mysql_servers(hostgroup_id,max_connections,comment,hostname,port)
values(1000,3000,'MGR01-02','192.168.2.4',3306);
insert into
mysql_servers(hostgroup_id,max_connections,comment,hostname,port)
values(1000,3000,'MGR01-03','192.168.2.5',3306);
load mysql servers to runtime;
save mysql servers to disk;
select * from runtime_mysql_servers;
## MGR主节点创建系统视图
USE sys;
DELIMITER $$
CREATE FUNCTION IFZERO ( a INT, b INT ) RETURNS INT DETERMINISTIC RETURN
IF
( a = 0, b, a ) $$DELIMITER $$
CREATE FUNCTION LOCATE2 ( needle TEXT ( 10000 ), haystack TEXT ( 10000 ), OFFSET INT ) RETURNS INT DETERMINISTIC RETURN IFZERO ( LOCATE( needle, haystack, OFFSET ), LENGTH( haystack ) + 1 ) $$DELIMITER $$
CREATE FUNCTION GTID_NORMALIZE (
g TEXT ( 10000 )) RETURNS TEXT ( 10000 ) DETERMINISTIC RETURN GTID_SUBTRACT ( g, '' ) $$DELIMITER $$
CREATE FUNCTION GTID_COUNT (
gtid_set TEXT ( 10000 )) RETURNS INT DETERMINISTIC BEGIN
DECLARE
result BIGINT DEFAULT 0;
DECLARE
colon_pos INT;
DECLARE
next_dash_pos INT;
DECLARE
next_colon_pos INT;
DECLARE
next_comma_pos INT;
SET gtid_set = GTID_NORMALIZE ( gtid_set );
SET colon_pos = LOCATE2 ( ':', gtid_set, 1 );
WHILE
colon_pos != LENGTH( gtid_set ) + 1 DO
SET next_dash_pos = LOCATE2 ( '-', gtid_set, colon_pos + 1 );
SET next_colon_pos = LOCATE2 ( ':', gtid_set, colon_pos + 1 );
SET next_comma_pos = LOCATE2 ( ',', gtid_set, colon_pos + 1 );
IF
next_dash_pos < next_colon_pos
AND next_dash_pos < next_comma_pos THEN
SET result = result + SUBSTR(
gtid_set,
next_dash_pos + 1,
LEAST( next_colon_pos, next_comma_pos ) - ( next_dash_pos + 1 )) - SUBSTR(
gtid_set,
colon_pos + 1,
next_dash_pos - ( colon_pos + 1 )) + 1;
ELSE
SET result = result + 1;
END IF;
SET colon_pos = next_colon_pos;
END WHILE;
RETURN result;
END $$DELIMITER $$
CREATE FUNCTION gr_applier_queue_length () RETURNS INT DETERMINISTIC BEGIN
RETURN (
SELECT
sys.gtid_count (
GTID_SUBTRACT ( ( SELECT Received_transaction_set FROM PERFORMANCE_SCHEMA.replication_connection_status WHERE Channel_name = 'group_replication_applier' ), ( SELECT @@GLOBAL.GTID_EXECUTED ) )));
END $$DELIMITER $$
CREATE FUNCTION gr_member_in_primary_partition () RETURNS VARCHAR ( 3 ) DETERMINISTIC BEGIN
RETURN (
SELECT
IF
(
MEMBER_STATE = 'ONLINE'
AND (( SELECT COUNT(*) FROM PERFORMANCE_SCHEMA.replication_group_members WHERE MEMBER_STATE != 'ONLINE' ) >= (( SELECT COUNT(*) FROM PERFORMANCE_SCHEMA.replication_group_members )/ 2 ) = 0 ),
'YES',
'NO'
)
FROM
PERFORMANCE_SCHEMA.replication_group_members
JOIN PERFORMANCE_SCHEMA.replication_group_member_stats USING ( member_id ));
END $$
DELIMITER $$
CREATE VIEW gr_member_routing_candidate_status AS SELECT
sys.gr_member_in_primary_partition () AS viable_candidate,
IF
( ( SELECT ( SELECT GROUP_CONCAT( variable_value ) FROM PERFORMANCE_SCHEMA.global_variables WHERE variable_name IN ( 'read_only', 'super_read_only' )) != 'OFF,OFF' ), 'YES', 'NO' ) AS read_only,
sys.gr_applier_queue_length () AS transactions_behind,
Count_Transactions_in_queue AS 'transactions_to_cert'
FROM
PERFORMANCE_SCHEMA.replication_group_member_stats;
$$
DELIMITER;
#查看
select * from sys.gr_member_routing_candidate_status;
#监控用户
create user proxysql_monitor@'%' identified by '123456';
grant select on sys.* to proxysql_monitor@'%';
## MGR主节点创建系统视图
USE sys;
DELIMITER $$
CREATE FUNCTION IFZERO ( a INT, b INT )
RETURNS INT
DETERMINISTIC
RETURN IF( a = 0, b, a )$$
CREATE FUNCTION LOCATE2 ( needle TEXT ( 10000 ), haystack TEXT ( 10000 ), OFFSET INT )RETURNS INT
DETERMINISTIC
RETURN IFZERO( LOCATE( needle, haystack, OFFSET ), LENGTH( haystack ) + 1 )$$
CREATE FUNCTION GTID_NORMALIZE (g TEXT ( 10000 ))
RETURNS TEXT ( 10000 )
DETERMINISTIC
RETURN GTID_SUBTRACT ( g, '' ) $$
CREATE FUNCTION GTID_COUNT(gtid_set TEXT ( 10000 ))
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE
result BIGINT DEFAULT 0;
DECLARE
colon_pos INT;
DECLARE
next_dash_pos INT;
DECLARE
next_colon_pos INT;
DECLARE
next_comma_pos INT;
SET gtid_set = GTID_NORMALIZE ( gtid_set );
SET colon_pos = LOCATE2 ( ':', gtid_set, 1 );
WHILE
colon_pos != LENGTH( gtid_set ) + 1 DO
SET next_dash_pos = LOCATE2 ( '-', gtid_set, colon_pos + 1 );
SET next_colon_pos = LOCATE2 ( ':', gtid_set, colon_pos + 1 );
SET next_comma_pos = LOCATE2 ( ',', gtid_set, colon_pos + 1 );
IF
next_dash_pos < next_colon_pos
AND next_dash_pos < next_comma_pos THEN
SET result = result + SUBSTR(
gtid_set,
next_dash_pos + 1,
LEAST( next_colon_pos, next_comma_pos ) - ( next_dash_pos + 1 )) - SUBSTR(
gtid_set,
colon_pos + 1,
next_dash_pos - ( colon_pos + 1 )) + 1;
ELSE
SET result = result + 1;
END IF;
SET colon_pos = next_colon_pos;
END WHILE;
RETURN result;
END$$
CREATE FUNCTION gr_applier_queue_length ()
RETURNS INT
DETERMINISTIC
BEGIN
RETURN (
SELECT
sys.gtid_count (
GTID_SUBTRACT ( ( SELECT Received_transaction_set FROM PERFORMANCE_SCHEMA.replication_connection_status WHERE Channel_name = 'group_replication_applier' ), ( SELECT @@GLOBAL.GTID_EXECUTED ) )));
END$$
CREATE FUNCTION gr_member_in_primary_partition ()
RETURNS VARCHAR ( 3 )
DETERMINISTIC
BEGIN
RETURN (
SELECT
IF
(
MEMBER_STATE = 'ONLINE'
AND (( SELECT COUNT(*) FROM PERFORMANCE_SCHEMA.replication_group_members WHERE MEMBER_STATE != 'ONLINE' ) >= (( SELECT COUNT(*) FROM PERFORMANCE_SCHEMA.replication_group_members )/ 2 ) = 0 ),
'YES',
'NO'
)
FROM
PERFORMANCE_SCHEMA.replication_group_members
JOIN PERFORMANCE_SCHEMA.replication_group_member_stats USING ( member_id ));
END$$
CREATE VIEW gr_member_routing_candidate_status AS SELECT
sys.gr_member_in_primary_partition () AS viable_candidate,
IF
( ( SELECT ( SELECT GROUP_CONCAT( variable_value ) FROM PERFORMANCE_SCHEMA.global_variables WHERE variable_name IN ( 'read_only', 'super_read_only' )) != 'OFF,OFF' ), 'YES', 'NO' ) AS read_only,
sys.gr_applier_queue_length () AS transactions_behind,
Count_Transactions_in_queue AS 'transactions_to_cert'
FROM
PERFORMANCE_SCHEMA.replication_group_member_stats;$$
DELIMITER ;
set mysql-monitor_username='proxysql_monitor';
set mysql-monitor_password='123456';
load mysql servers to runtime;
save mysql servers to disk;
select * from global_variables where variable_name like 'mysql-monitor%';
SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us
DESC LIMIT 10;
#mysql_server_connect_log
SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us
DESC LIMIT 6;
# 控制监控时间间隔
UPDATE global_variables SET variable_value='2000' WHERE variable_name
IN ('mysql-monitor_connect_interval','mysql-monitor_ping_interval','mysql-monitor_read_only_interval');
#配置一个mgr组
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,
comment)
values(1000,1001,1002,1003,1,1,0,10,'mgr01');
load mysql servers to runtime;
save mysql servers to disk;
## 查看结果
select * from mysql_group_replication_hostgroups\G
select * from mysql_group_replication_hostgroups;
grant all on *.* to mgr01_root@'%' identified by '123456';
delete from mysql_users;
insert into
mysql_users(username,password,default_hostgroup,transaction_persistent
) values('mgr01_root','123456',1000,1);
load mysql users to runtime;
save mysql users to disk;
select username,password,active,default_hostgroup,transaction_persistent,fast_forward,frontend,comment from mysql_users;
#查看已执行的语句
select hostgroup,digest_text from stats_mysql_query_digest ORDER BY
sum_time DESC limit 10;
INSERT INTO mysql_query_rules
(rule_id,active,username,match_digest,destination_hostgroup,apply)
VALUES (100,1,'mgr01_root','^SELECT.*FOR UPDATE$',1000,1);
INSERT INTO mysql_query_rules
(rule_id,active,username,match_digest,destination_hostgroup,apply)
VALUES (101,1,'mgr01_root','^SELECT',1002,1);
load mysql query rules to runtime;
save mysql query rules to disk;
select * from runtime_mysql_query_rules\G;
#语句路由
select hostgroup,digest_text from stats_mysql_query_digest;