proxysql 2.6部署代理MGR集群读写分离

2024-03-06 22:20

本文主要是介绍proxysql 2.6部署代理MGR集群读写分离,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官方文档

https://proxysql.com/documentation/ProxySQL-Configuration/

下载安装proxysql

https://github.com/sysown/proxysql/releases/download/v2.6.0/proxysql-2.6.0-1-centos7.x86_64.rpmyum -y localinstall proxysql-2.6.0-1-centos7.x86_64.rpm
# 软链接数据目录
rm -rf /var/lib/proxysql/
mkdir -p /data/proxysql
ln -s /data/proxysql/ /var/lib/
chown -R proxysql.proxysql /var/lib/proxysql/
chown -R proxysql.proxysql /var/lib/proxysql# 启动proxysql
systemctl start proxysql
systemctl status proxysql
systemctl enable proxysql

防火墙放行

firewall-cmd --permanent --zone=public --add-port={6032..6033}/tcp && firewall-cmd --reload

下载安装mysql客户端

tar -zxvf mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar.gz
rpm -ivh mysql-community-client-8.0.19-1.el7.x86_64.rpm --nodeps --force

连接proxy管理接口配置

mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '# 确认mysql_servers、mysql_replication_hostgroups、mysql_query_rules三张表的配置是否为空
SELECT * FROM mysql_servers;
SELECT * from mysql_replication_hostgroups;
SELECT * from mysql_query_rules;
proxysql添加后端mysql
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.200.31',3308);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.200.32',3308);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.200.33',3308);load mysql servers to runtime;
save mysql servers to disk;

MGR集群主节点添加监控账号

create user 'monitor'@'192.168.200.%' identified with mysql_native_password by 'UBXWlwDw4X93s%#GtXyx';GRANT SELECT on sys.* to 'monitor'@'192.168.200.%';
GRANT SELECT on performance_schema.* to 'monitor'@'192.168.200.%';

proxysql添加设置监控账号

UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username';
UPDATE global_variables SET variable_value='UBXWlwDw4X93s%#GtXyx' WHERE variable_name='mysql-monitor_password';load mysql variables to runtime;
save mysql variables to disk;

MGR集群主节点添加监控视图

# addition_to_sys.sql MGR创建监控视图sql文件
https://lefred.be/content/mysql-group-replication-native-support-in-proxysql/
cat > addition_to_sys.sql << EOF
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
BEGINDECLARE 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 DOSET 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 THENSET 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;ELSESET 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
BEGINRETURN (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
BEGINRETURN (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 ;
EOF
cat > addition_to_sys2.sql << EOF
USE sys;DELIMITER $$DROP FUNCTION gr_member_in_primary_partition$$DROP VIEW gr_member_routing_candidate_status$$CREATE FUNCTION gr_member_in_primary_partition()
RETURNS VARCHAR(3)
DETERMINISTIC
BEGINRETURN (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 rgms USING(member_id) WHERE rgms.MEMBER_ID=@@SERVER_UUID);
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 rgms 
where rgms.MEMBER_ID=(select gv.VARIABLE_VALUE from `performance_schema`.global_variables gv where gv.VARIABLE_NAME='server_uuid');$$DELIMITER ;
EOF

加载视图sql

source /root/addition_to_sys.sql;

查看视图监控状态

select * from gr_member_routing_candidate_status;

如果报错

ERROR 1242 (21000): Subquery returns more than 1 row
source /root/addition_to_sys2.sql;

proxysql设置读写组

# 查看读写复制组表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(10,20,30,40,1,1,0,0);load mysql servers to runtime;
save mysql servers to disk;

MGR集群状态查询

mysql> select * from sys.gr_member_routing_candidate_status;
+------------------+-----------+---------------------+----------------------+
| viable_candidate | read_only | transactions_behind | transactions_to_cert |
+------------------+-----------+---------------------+----------------------+
| YES              | NO        |                   0 |                    0 |
+------------------+-----------+---------------------+----------------------+
1 row in set (0.04 sec)

proxysql状态查询

Admin> select hostgroup_id, hostname, status  from runtime_mysql_servers;
+--------------+----------------+--------+
| hostgroup_id | hostname       | status |
+--------------+----------------+--------+
| 10           | 192.168.200.32 | ONLINE |
| 20           | 192.168.200.31 | ONLINE |
| 20           | 192.168.200.33 | ONLINE |
+--------------+----------------+--------+

MGR主节点创建业务账号

create user 'gadmin'@'192.168.200.%' identified with mysql_native_password by 'UBXWlwDw4X93s%#GtXyx';GRANT all privileges on *.* to 'gadmin'@'192.168.200.%';

proxysql添加业务账号

INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('gadmin','UBXWlwDw4X93s%#GtXyx',10);LOAD MYSQL USERS TO RUNTIME;
SAVE MYSQL USERS TO DISK;SELECT * FROM mysql_users;

proxysql添加读写分离规则

INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (10,1,'gadmin','^SELECT.*FOR UPDATE$',10,1);INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (20,1,'gadmin','^SELECT',30,1);load mysql query rules to runtime;
save mysql query rules to disk;# 查看规则
SELECT match_digest,destination_hostgroup FROM mysql_query_rules WHERE active=1 AND username='gadmin' ORDER BY rule_id;# 查看路由状态
SELECT hostgroup hg, sum_time, count_star, digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC;

测试读写分离

mysql -ugadmin -p'UBXWlwDw4X93s%#GtXyx' -h192.168.200.30 -P6033 -e 'create database proxysql_mgr_test;'mysql -ugadmin -p'UBXWlwDw4X93s%#GtXyx' -h192.168.200.30 -P6033 -e 'use proxysql_mgr_test;select * from tb01;'mysql -ugadmin -p'UBXWlwDw4X93s%#GtXyx' -h192.168.200.30 -P6033 -e 'show databases;'mysql -ugadmin -p'UBXWlwDw4X93s%#GtXyx' -h192.168.200.30 -P6033 -e 'drop database proxysql_mgr_test;'

这篇关于proxysql 2.6部署代理MGR集群读写分离的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/781544

相关文章

闲置电脑也能活出第二春?鲁大师AiNAS让你动动手指就能轻松部署

对于大多数人而言,在这个“数据爆炸”的时代或多或少都遇到过存储告急的情况,这使得“存储焦虑”不再是个别现象,而将会是随着软件的不断臃肿而越来越普遍的情况。从不少手机厂商都开始将存储上限提升至1TB可以见得,我们似乎正处在互联网信息飞速增长的阶段,对于存储的需求也将会不断扩大。对于苹果用户而言,这一问题愈发严峻,毕竟512GB和1TB版本的iPhone可不是人人都消费得起的,因此成熟的外置存储方案开

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

HDFS—集群扩容及缩容

白名单:表示在白名单的主机IP地址可以,用来存储数据。 配置白名单步骤如下: 1)在NameNode节点的/opt/module/hadoop-3.1.4/etc/hadoop目录下分别创建whitelist 和blacklist文件 (1)创建白名单 [lytfly@hadoop102 hadoop]$ vim whitelist 在whitelist中添加如下主机名称,假如集群正常工作的节

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

10. 文件的读写

10.1 文本文件 操作文件三大类: ofstream:写操作ifstream:读操作fstream:读写操作 打开方式解释ios::in为了读文件而打开文件ios::out为了写文件而打开文件,如果当前文件存在则清空当前文件在写入ios::app追加方式写文件ios::trunc如果文件存在先删除,在创建ios::ate打开文件之后令读写位置移至文件尾端ios::binary二进制方式

高效+灵活,万博智云全球发布AWS无代理跨云容灾方案!

摘要 近日,万博智云推出了基于AWS的无代理跨云容灾解决方案,并与拉丁美洲,中东,亚洲的合作伙伴面向全球开展了联合发布。这一方案以AWS应用环境为基础,将HyperBDR平台的高效、灵活和成本效益优势与无代理功能相结合,为全球企业带来实现了更便捷、经济的数据保护。 一、全球联合发布 9月2日,万博智云CEO Michael Wong在线上平台发布AWS无代理跨云容灾解决方案的阐述视频,介绍了

阿里开源语音识别SenseVoiceWindows环境部署

SenseVoice介绍 SenseVoice 专注于高精度多语言语音识别、情感辨识和音频事件检测多语言识别: 采用超过 40 万小时数据训练,支持超过 50 种语言,识别效果上优于 Whisper 模型。富文本识别:具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。高效推

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

搭建Kafka+zookeeper集群调度

前言 硬件环境 172.18.0.5        kafkazk1        Kafka+zookeeper                Kafka Broker集群 172.18.0.6        kafkazk2        Kafka+zookeeper                Kafka Broker集群 172.18.0.7        kafkazk3