本文章配套视频 | https://www.ixigua.com/7086085500540289572?id=7087546160079962660 |
本专栏全部文章 | https://blog.csdn.net/tonghu_note/category_11755726.html |
总目录 | https://blog.csdn.net/tonghu_note/article/details/124333034 |
来我的dou音 aa10246666, 看配套视频
MGR实现了服务端高可用,ProxySQL实现了集群的客户端高可用,二者结合来用才是完美的。
Primary 节点(node1) | mysql 8.0.28 | 10.211.55.9 |
Secondary1 节点(node2) | mysql 8.0.28 | 10.211.55.4 |
Secondary2 节点(node3) | mysql 8.0.28 | 10.211.55.6 |
ProxySQL 节点(node4) | 2.2.0 | 10.211.55.7 |
监控脚本 addition_to_sys_8.0.28.sql 需要先导入Primary节点,因为ProxySQL依赖这个监控框架,下面是下载地址
点霹《童虎学习笔记》mysql8.0.28mgr监控视图gr_member_routing_ca更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/tonghu_note/85108757如果想了解如何监控MGR可参考下面这篇文档《童虎学习笔记》2分钟学会如何监控MySQL MGR集群_童虎学习笔记的博客-CSDN博客童虎学习笔记的博客_CSDN博客-领域博主https://blog.csdn.net/tonghu_note来我的西瓜视频,看免费配套视频https://www.ixigua.com/home/2058760810138187来我的dou音 aa10246666, 看配套视频一、实战环境Primary 节点(node1)mysql 8.0.2810.211.55.9Secondary1 节点(node2)mysql 8.0.2810....https://blog.csdn.net/tonghu_note/article/details/124088561
我的环境是mac m1芯片,所以采用arm的包来安装
wget https://github.com/sysown/proxysql/releases/download/v2.2.0/proxysql_2.2.0-debian10_arm64.deb
具体要根据自已服务器的情况来决定下载哪个包 ,官方安装包下载地址如下:Download and Install ProxySQL - ProxySQLhttps://proxysql.com/documentation/installing-proxysql/
dpkg -i proxysql_2.2.0-debian10_arm64.deb
/etc/proxysql.cnf
create user proxysql_user@'%' identified with mysql_native_password by 'proxysql_pwd';
grant select on sys.* to proxysql_user@'%';
create user app_user@'%' identified with mysql_native_password by 'app_pwd';
grant all on d1.* to app_user@'%';
systemctl start proxysql
先在本地连进ProxySQL服务
mysql -uadmin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '
由于默认的admin管理员帐号只能本地连接,所以我们新建一个管理员用户root以便可以远程连接ProxySQL ,其中1234是root帐号对应的密码
set admin-admin_credentials='admin:admin;root:1234';
load admin variables to runtime;
save admin variables to disk;
可以通过以下命令查看参数配置情况
select * from global_variables;
set mysql-monitor_username='proxysql_user';
set mysql-monitor_password='proxysql_pwd';
load mysql variables to runtime;
save mysql variables to disk;
INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('app_user','app_pwd',1);
load mysql users to runtime;
save mysql users to disk;select * from mysql_users;
select * from runtime_mysql_users;
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(1, 2, 3, 4, 1, 1, 0, 100);
load mysql servers to runtime;
save mysql servers to disk;select * from mysql_group_replication_hostgroups;
select * from runtime_mysql_group_replication_hostgroups;
接下来对每一个字段进行介绍:
writer_hostgroup | 默认情况下会将所有流量发送到这个组。具有read_only=0的节点也将分配到这个组 |
backup_writer_hostgroup | 用于多写模式,如果集群有多个写节点(read_only=0)且超过了max_writers规定数量,则会把多出来的写节点放到备用写组里面 |
reader_hostgroup | 读取的流量应该发送到该组,只读节点(read_only=1)会被分配到该组 |
offline_hostgroup | 当ProxySQL监视到某个节点不正常时,会被放入该组 |
active | 是否启用主机组,当启用时,ProxySQL将监视主机在各族之间移动 |
max_writers | 用于多写模式, 最大写节点的数量,超过该值的节点应该被放入backup_write_hostgroup |
writer_is_also_reader | 一个写节点是否也做读节点。主模型的MGR时,必须设置writer_is_also_reader=1。如果该值为2,则backup_writer_hostgroup的节点做读写点,但是writer_hostgroup不会做读节点 |
max_transactions_behind | 类似主从延迟流量停用功能。设置一个节点落后的事务数量,达到这个数量后,节点状态被设置为 shunned ,被完全处理完后,再变更为正常状态。 |
insert into mysql_servers(hostgroup_id,hostname,port) values
(1, '10.211.55.9' ,3306),
(1, '10.211.55.4' ,3306),
(1, '10.211.55.6' ,3306);
load mysql servers to runtime;
save mysql servers to disk;select * from mysql_servers;
select * from runtime_mysql_servers;
写走1号组,读走3号组
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)VALUES
(1,1,'^SELECT.*FOR UPDATE$',1,1),
(2,1,'^SELECT',3,1);load mysql query rules to runtime;
save mysql query rules to disk;select * from mysql_query_rules \G
select * from runtime_mysql_query_rules \G
mysql -uapp_user -p'app_pwd' -h 10.211.55.7 -P6033
发起一些sql
use d1
create table t11(id int primary key);
insert into t11 select 1;
insert into t11 select 2;
select * from t11;
select * from t11;
select * from t11;
select * from t11;
select * from t11;
select * from t11;
select * from t11;
。。。
SELECT hostgroup hg, sum_time, count_star, digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC;
结果如下,可以看到读写请求已经分散到不同步服务器组1和3了
+----+----------+------------+--------------------------------------+
| hg | sum_time | count_star | digest_text |
+----+----------+------------+--------------------------------------+
| 1 | 33409 | 1 | create table t11(id int) |
| 1 | 29276 | 3 | insert into t11 select ? |
| 1 | 26688 | 1 | create table t11(id int primary key) |
| 1 | 20873 | 1 | drop table t11 |
| 3 | 9430 | 7 | select * from t11 |
| 1 | 8535 | 1 | show databases |
| 3 | 5819 | 1 | SELECT DATABASE() |
| 3 | 5503 | 1 | SELECT DATABASE() |
| 1 | 4757 | 1 | show databases |
| 1 | 2622 | 1 | show tables |
| 3 | 1643 | 1 | SELECT * FROM `t2` WHERE ?=? |
| 1 | 0 | 1 | select @@version_comment limit ? |
+----+----------+------------+--------------------------------------+
12 rows in set (0.00 sec)
root@node1:~# mysqladmin -uroot -proot shutdown
可以看到node1已经补踢出集群,node2现在为主节点了
root@node3:~# mysql -uroot -proot
mysql> select member_host, member_state, member_role from performance_schema.replication_group_members;
+------------------------+------------------------+------------------------+
| MEMBER_HOST | MEMBER_STATE | MEMBER_ROLE |
+------------------------+------------------------+------------------------+
| node2 | ONLINE | PRIMARY |
| node3 | ONLINE | SECONDARY |
+------------------------+--------------+-------------+
2 rows in set (0.00 sec)
通过程序端口6033确认读写是正常的
root@node3:~# mysql -uapp_user -p'app_pwd' -h 10.211.55.7 -P6033
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| d1 |
| information_schema |
+--------------------+
2 rows in set (0.02 sec)mysql> use d1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> show tables;
+--------------+
| Tables_in_d1 |
+--------------+
| t11 |
| t2 |
+--------------+
2 rows in set (0.01 sec)mysql> select * from t11;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
2 rows in set (0.00 sec)mysql> insert into t11 select 3;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0mysql> insert into t11 select 4;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
通过管理端口6032确认成员节点状态,可以看到node1节点状态是下线状态,同时他也被移到了4号组(即故障组)里面
root@node3:~# mysql -uroot -p1234 -h 10.211.55.7 -P6032
mysql> select hostgroup_id, hostname, status from runtime_mysql_servers;
+------------------+----------------+---------+
| hostgroup_id | hostname | status |
+-----------------+-----------------+---------+
| 1 | 10.211.55.4 | ONLINE |
| 4 | 10.211.55.9 | SHUNNED |
| 3 | 10.211.55.6 | ONLINE |
+-----------------+-----------------+---------+
3 rows in set (0.01 sec)mysql>
将故障节点加回MGR集群
root@node1:~# mysqld_safe --user=mysql &
[1] 103812root@node1:~# mysql -uroot -proot
mysql> start group_replication;
Query OK, 0 rows affected (1.95 sec)mysql> select member_host, member_state, member_role from performance_schema.replication_group_members;
+-----------------------+-------------------------+------------------------+
| MEMBER_HOST | MEMBER_STATE | MEMBER_ROLE |
+-----------------------+-------------------------+------------------------+
| node1 | ONLINE | SECONDARY |
| node2 | ONLINE | PRIMARY |
| node3 | ONLINE | SECONDARY |
+-----------------------+-------------------------+-------------------------+
3 rows in set (0.00 sec)
在node1上通过管理端口6032确认成员节点状态,可以看到node1节点状态是在线状态,同时他也被移到了3号组(即读组)里面
root@node1:~# mysql -uroot -p1234 -h 10.211.55.7 -P6032
mysql> select hostgroup_id, hostname, status from runtime_mysql_servers;
+------------------+----------------+---------+
| hostgroup_id | hostname | status |
+-----------------+-----------------+---------+
| 1 | 10.211.55.4 | ONLINE |
| 3 | 10.211.55.9 | ONLINE |
| 3 | 10.211.55.6 | ONLINE |
+-----------------+-----------------+---------+
3 rows in set (0.01 sec)mysql>
root@node4:/var/lib/proxysql# systemctl stop proxysql.service
root@node4:/var/lib/proxysql# cd /var/lib/proxysql
root@node4:/var/lib/proxysql# rm -rf *
root@node4:/var/lib/proxysql# systemctl start proxysql.service