1. cat << EOF | tee /etc/yum.repos.d/proxysql.repo
    2. [proxysql_repo]
    3. name= ProxySQL YUM repository
    4. baseurl=https:/$repo.proxysql.com/ProxySQL/proxysql-2.2.x/centos/$releasever
    5. gpgcheck=1
    6. gpgkey=https:/$repo.proxysql.com/ProxySQL/repo_pub_key
    7. EOF
    1. yum install proxysql
    2. 或者
    3. yum install proxysql-2.2.0
    1. systemctl start proxysql
    1. mysql -uadmin -padmin -h127.0.0.1 -P6032 --prompt='Admin> '
    1. select * from mysql_servers;
    2. #第一组
    3. insert into
    4. mysql_servers(hostgroup_id,max_connections,comment,hostname,port)
    5. values(1000,3000,'MGR01-01','192.168.2.3',3306);
    6. insert into
    7. mysql_servers(hostgroup_id,max_connections,comment,hostname,port)
    8. values(1000,3000,'MGR01-02','192.168.2.4',3306);
    9. insert into
    10. mysql_servers(hostgroup_id,max_connections,comment,hostname,port)
    11. values(1000,3000,'MGR01-03','192.168.2.5',3306);
    12. load mysql servers to runtime;
    13. save mysql servers to disk;
    14. select * from runtime_mysql_servers;
    1. ## MGR主节点创建系统视图
    2. USE sys;
    3. DELIMITER $$
    4. CREATE FUNCTION IFZERO ( a INT, b INT ) RETURNS INT DETERMINISTIC RETURN
    5. IF
    6. ( a = 0, b, a ) $$DELIMITER $$
    7. CREATE FUNCTION LOCATE2 ( needle TEXT ( 10000 ), haystack TEXT ( 10000 ), OFFSET INT ) RETURNS INT DETERMINISTIC RETURN IFZERO ( LOCATE( needle, haystack, OFFSET ), LENGTH( haystack ) + 1 ) $$DELIMITER $$
    8. CREATE FUNCTION GTID_NORMALIZE (
    9. g TEXT ( 10000 )) RETURNS TEXT ( 10000 ) DETERMINISTIC RETURN GTID_SUBTRACT ( g, '' ) $$DELIMITER $$
    10. CREATE FUNCTION GTID_COUNT (
    11. gtid_set TEXT ( 10000 )) RETURNS INT DETERMINISTIC BEGIN
    12. DECLARE
    13. result BIGINT DEFAULT 0;
    14. DECLARE
    15. colon_pos INT;
    16. DECLARE
    17. next_dash_pos INT;
    18. DECLARE
    19. next_colon_pos INT;
    20. DECLARE
    21. next_comma_pos INT;
    22. SET gtid_set = GTID_NORMALIZE ( gtid_set );
    23. SET colon_pos = LOCATE2 ( ':', gtid_set, 1 );
    24. WHILE
    25. colon_pos != LENGTH( gtid_set ) + 1 DO
    26. SET next_dash_pos = LOCATE2 ( '-', gtid_set, colon_pos + 1 );
    27. SET next_colon_pos = LOCATE2 ( ':', gtid_set, colon_pos + 1 );
    28. SET next_comma_pos = LOCATE2 ( ',', gtid_set, colon_pos + 1 );
    29. IF
    30. next_dash_pos < next_colon_pos
    31. AND next_dash_pos < next_comma_pos THEN
    32. SET result = result + SUBSTR(
    33. gtid_set,
    34. next_dash_pos + 1,
    35. LEAST( next_colon_pos, next_comma_pos ) - ( next_dash_pos + 1 )) - SUBSTR(
    36. gtid_set,
    37. colon_pos + 1,
    38. next_dash_pos - ( colon_pos + 1 )) + 1;
    39. ELSE
    40. SET result = result + 1;
    41. END IF;
    42. SET colon_pos = next_colon_pos;
    43. END WHILE;
    44. RETURN result;
    45. END $$DELIMITER $$
    46. CREATE FUNCTION gr_applier_queue_length () RETURNS INT DETERMINISTIC BEGIN
    47. RETURN (
    48. SELECT
    49. sys.gtid_count (
    50. GTID_SUBTRACT ( ( SELECT Received_transaction_set FROM PERFORMANCE_SCHEMA.replication_connection_status WHERE Channel_name = 'group_replication_applier' ), ( SELECT @@GLOBAL.GTID_EXECUTED ) )));
    51. END $$DELIMITER $$
    52. CREATE FUNCTION gr_member_in_primary_partition () RETURNS VARCHAR ( 3 ) DETERMINISTIC BEGIN
    53. RETURN (
    54. SELECT
    55. IF
    56. (
    57. MEMBER_STATE = 'ONLINE'
    58. AND (( SELECT COUNT(*) FROM PERFORMANCE_SCHEMA.replication_group_members WHERE MEMBER_STATE != 'ONLINE' ) >= (( SELECT COUNT(*) FROM PERFORMANCE_SCHEMA.replication_group_members )/ 2 ) = 0 ),
    59. 'YES',
    60. 'NO'
    61. )
    62. FROM
    63. PERFORMANCE_SCHEMA.replication_group_members
    64. JOIN PERFORMANCE_SCHEMA.replication_group_member_stats USING ( member_id ));
    65. END $$
    66. DELIMITER $$
    67. CREATE VIEW gr_member_routing_candidate_status AS SELECT
    68. sys.gr_member_in_primary_partition () AS viable_candidate,
    69. IF
    70. ( ( 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,
    71. sys.gr_applier_queue_length () AS transactions_behind,
    72. Count_Transactions_in_queue AS 'transactions_to_cert'
    73. FROM
    74. PERFORMANCE_SCHEMA.replication_group_member_stats;
    75. $$
    76. DELIMITER;
    77. #查看
    78. select * from sys.gr_member_routing_candidate_status;
    79. #监控用户
    80. create user proxysql_monitor@'%' identified by '123456';
    81. grant select on sys.* to proxysql_monitor@'%';
    1. ## MGR主节点创建系统视图
    2. USE sys;
    3. DELIMITER $$
    4. CREATE FUNCTION IFZERO ( a INT, b INT )
    5. RETURNS INT
    6. DETERMINISTIC
    7. RETURN IF( a = 0, b, a )$$
    8. CREATE FUNCTION LOCATE2 ( needle TEXT ( 10000 ), haystack TEXT ( 10000 ), OFFSET INT )RETURNS INT
    9. DETERMINISTIC
    10. RETURN IFZERO( LOCATE( needle, haystack, OFFSET ), LENGTH( haystack ) + 1 )$$
    11. CREATE FUNCTION GTID_NORMALIZE (g TEXT ( 10000 ))
    12. RETURNS TEXT ( 10000 )
    13. DETERMINISTIC
    14. RETURN GTID_SUBTRACT ( g, '' ) $$
    15. CREATE FUNCTION GTID_COUNT(gtid_set TEXT ( 10000 ))
    16. RETURNS INT
    17. DETERMINISTIC
    18. BEGIN
    19. DECLARE
    20. result BIGINT DEFAULT 0;
    21. DECLARE
    22. colon_pos INT;
    23. DECLARE
    24. next_dash_pos INT;
    25. DECLARE
    26. next_colon_pos INT;
    27. DECLARE
    28. next_comma_pos INT;
    29. SET gtid_set = GTID_NORMALIZE ( gtid_set );
    30. SET colon_pos = LOCATE2 ( ':', gtid_set, 1 );
    31. WHILE
    32. colon_pos != LENGTH( gtid_set ) + 1 DO
    33. SET next_dash_pos = LOCATE2 ( '-', gtid_set, colon_pos + 1 );
    34. SET next_colon_pos = LOCATE2 ( ':', gtid_set, colon_pos + 1 );
    35. SET next_comma_pos = LOCATE2 ( ',', gtid_set, colon_pos + 1 );
    36. IF
    37. next_dash_pos < next_colon_pos
    38. AND next_dash_pos < next_comma_pos THEN
    39. SET result = result + SUBSTR(
    40. gtid_set,
    41. next_dash_pos + 1,
    42. LEAST( next_colon_pos, next_comma_pos ) - ( next_dash_pos + 1 )) - SUBSTR(
    43. gtid_set,
    44. colon_pos + 1,
    45. next_dash_pos - ( colon_pos + 1 )) + 1;
    46. ELSE
    47. SET result = result + 1;
    48. END IF;
    49. SET colon_pos = next_colon_pos;
    50. END WHILE;
    51. RETURN result;
    52. END$$
    53. CREATE FUNCTION gr_applier_queue_length ()
    54. RETURNS INT
    55. DETERMINISTIC
    56. BEGIN
    57. RETURN (
    58. SELECT
    59. sys.gtid_count (
    60. GTID_SUBTRACT ( ( SELECT Received_transaction_set FROM PERFORMANCE_SCHEMA.replication_connection_status WHERE Channel_name = 'group_replication_applier' ), ( SELECT @@GLOBAL.GTID_EXECUTED ) )));
    61. END$$
    62. CREATE FUNCTION gr_member_in_primary_partition ()
    63. RETURNS VARCHAR ( 3 )
    64. DETERMINISTIC
    65. BEGIN
    66. RETURN (
    67. SELECT
    68. IF
    69. (
    70. MEMBER_STATE = 'ONLINE'
    71. AND (( SELECT COUNT(*) FROM PERFORMANCE_SCHEMA.replication_group_members WHERE MEMBER_STATE != 'ONLINE' ) >= (( SELECT COUNT(*) FROM PERFORMANCE_SCHEMA.replication_group_members )/ 2 ) = 0 ),
    72. 'YES',
    73. 'NO'
    74. )
    75. FROM
    76. PERFORMANCE_SCHEMA.replication_group_members
    77. JOIN PERFORMANCE_SCHEMA.replication_group_member_stats USING ( member_id ));
    78. END$$
    79. CREATE VIEW gr_member_routing_candidate_status AS SELECT
    80. sys.gr_member_in_primary_partition () AS viable_candidate,
    81. IF
    82. ( ( 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,
    83. sys.gr_applier_queue_length () AS transactions_behind,
    84. Count_Transactions_in_queue AS 'transactions_to_cert'
    85. FROM
    86. PERFORMANCE_SCHEMA.replication_group_member_stats;$$
    87. DELIMITER ;
    1. set mysql-monitor_username='proxysql_monitor';
    2. set mysql-monitor_password='123456';
    3. load mysql servers to runtime;
    4. save mysql servers to disk;
    5. select * from global_variables where variable_name like 'mysql-monitor%';
    6. SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us
    7. DESC LIMIT 10;
    8. #mysql_server_connect_log
    9. SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us
    10. DESC LIMIT 6;
    11. # 控制监控时间间隔
    12. UPDATE global_variables SET variable_value='2000' WHERE variable_name
    13. IN ('mysql-monitor_connect_interval','mysql-monitor_ping_interval','mysql-monitor_read_only_interval');
    14. #配置一个mgr组
    15. insert into mysql_group_replication_hostgroups(
    16. writer_hostgroup,
    17. backup_writer_hostgroup,
    18. reader_hostgroup,
    19. offline_hostgroup,
    20. active,
    21. max_writers,
    22. writer_is_also_reader,
    23. max_transactions_behind,
    24. comment)
    25. values(1000,1001,1002,1003,1,1,0,10,'mgr01');
    26. load mysql servers to runtime;
    27. save mysql servers to disk;
    28. ## 查看结果
    29. select * from mysql_group_replication_hostgroups\G
    30. select * from mysql_group_replication_hostgroups;
    1. grant all on *.* to mgr01_root@'%' identified by '123456';
    1. delete from mysql_users;
    2. insert into
    3. mysql_users(username,password,default_hostgroup,transaction_persistent
    4. ) values('mgr01_root','123456',1000,1);
    5. load mysql users to runtime;
    6. save mysql users to disk;
    7. select username,password,active,default_hostgroup,transaction_persistent,fast_forward,frontend,comment from mysql_users;
    8. #查看已执行的语句
    9. select hostgroup,digest_text from stats_mysql_query_digest ORDER BY
    10. sum_time DESC limit 10;
    1. INSERT INTO mysql_query_rules
    2. (rule_id,active,username,match_digest,destination_hostgroup,apply)
    3. VALUES (100,1,'mgr01_root','^SELECT.*FOR UPDATE$',1000,1);
    4. INSERT INTO mysql_query_rules
    5. (rule_id,active,username,match_digest,destination_hostgroup,apply)
    6. VALUES (101,1,'mgr01_root','^SELECT',1002,1);
    7. load mysql query rules to runtime;
    8. save mysql query rules to disk;
    9. select * from runtime_mysql_query_rules\G;
    10. #语句路由
    11. select hostgroup,digest_text from stats_mysql_query_digest;