mysql GTID高可用与读写分离配置

mysqlfailover+proxysql做读写分离和高可用。现在生产环境中也是这一套。

前提:

mysqlfailover需要GTID复制。

proxysql需要:yum install -y perl-DBD-MySQL perl-DBI perl-Time-HiRes perl-IO-Socket-SSL

添加好程序账号(failover),故障转移账号(failover),proxysql心跳检测账号(monitor),这里图方便所以程序账号和故障转移账号用一个了。

规划:

master:192.168.88.126

slave:192.168.88.127

proxysql:192.168.88.198

部署好mysql服务。在proxysql机器上安装好mysql。

一、配置proxysql:

主要配置有4张表:mysql_servers,mysql_users,mysql_query_rules,mysql_replication_hostgroups

扫描二维码关注公众号,回复: 3133678 查看本文章

1:mysql_servers,这张表用来添加后端mysql实例的表。

insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values('1','192.168.88.126','3306',1,'Write Group');  这里设置1位写组
insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values('2','192.168.88.127','3306',1,'Read Group');  这里设置2为读组

2:mysql_users,这张表用来设置程序账号的,需要提前在两台mysql上添加。

insert into mysql_users(username,password,active,default_hostgroup,transaction_persistent) values('failover','123456',1,1,1);  这里图方便,MySQLfailover和proxysql用一个账号了,生产环境中不能这么做。

3:设置proxysql心跳检测账号:

set mysql-monitor_username='monitor'; set mysql-monitor_password='123456';

4:mysql_query_rules,路由表,用来配置分发的,也就是读写分离规则。

INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^SELECT.*FOR UPDATE$',1,1);  
INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^SELECT',2,1);

5:mysql_replication_hostgroups,用来配置高可用的,

mysql_replication_hostgroups中的每一行代表一对writer_hostgroup和reader_hostgroup。ProxySQL将监视read_only指定主机组中所有服务器的值,并根据值read_only将服务器分配给编写器或读取器主机组。字段注释可用于存储任意数据。

这些字段具有以下语义:

  • writer_hostgroup- 默认情况下将发送所有流量的主机组,read_only=0MySQL中的节点将分配给此主机组。
  • reader_hostgroup- 应该将读取流量的主机组发送到,应该定义查询规则或单独的只读用户将流量路由到此主机组,这些节点read_only=1将分配给该主机组。
  • comment - 可用于用户定义的任何目的的文本字段。可以是群集存储内容的描述,添加或禁用主机组的提醒,或某些检查器脚本处理的JSON。

以上是官方文档说明,意思是说用read_only变量来检测主机变化,从而实现高可用。表有3个字段,设置前面设置好的读写组的ID,插入到这张表里。

 insert into  mysql_replication_hostgroups (writer_hostgroup,reader_hostgroup,comment)values('1','2','高可用');

最后,要加载到runtime模块中使其生效,然后再保存到硬盘中。

mysql -uadmin -padmin -h 127.0.0.1 -P 6032 -e "load mysql servers to runtime;load mysql users to runtime;load mysql variables to runtime;LOAD MYSQL QUERY RULES TO RUNTIME;save mysql servers to disk;save mysql users to disk;save mysql variables to disk;SAVE MYSQL QUERY RULES TO DISK;"

二、MySQLfailover

具体的MySQLfailover工具可以在:

https://www.cnblogs.com/magmell/p/9570743.html

https://www.cnblogs.com/magmell/p/9257935.html  找到安装与运行方法。

三、启动服务,测试读写分离。

mysql -ufailover -p123456 -h127.0.0.1 -P6033    用程序账号连接。

mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from test;
+------+
| id |
+------+
| 111 |
| 111 |
| 3 |
| 3 |
| 3 |
| 3 |
+------+
6 rows in set (0.00 sec)

mysql> insert into test values(3);
Query OK, 1 row affected (0.01 sec)

mysql> insert into test values(3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values(3);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test;
+------+
| id |
+------+
| 111 |
| 111 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
+------+
9 rows in set (0.00 sec)

mysql> \q

查看读写分离是否成功:mysql -uadmin -padmin -h 127.0.0.1 -P 6032

mysql> select * from stats_mysql_query_digest;
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| 1 | test | failover | 0x6FA973F1E0331C0E | insert into test values(?) | 3 | 1536640645 | 1536640646 | 13013 | 1391 | 9671 |
| 2 | test | failover | 0x38DF1D37B3136F42 | select * from test | 2 | 1536640640 | 1536640649 | 1625 | 582 | 1043 |
| 1 | test | failover | 0x99531AEFF718C501 | show tables | 1 | 1536640630 | 1536640630 | 368 | 368 | 368 |
| 1 | test | failover | 0x02033E45904D3DF0 | show databases | 1 | 1536640630 | 1536640630 | 930 | 930 | 930 |
| 1 | information_schema | failover | 0x620B328FE9D6D71A | SELECT DATABASE() | 1 | 1536640630 | 1536640630 | 750 | 750 | 750 |
| 1 | information_schema | failover | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 1 | 1536640627 | 1536640627 | 0 | 0 | 0 |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
6 rows in set (0.01 sec)

观察hostgroup字段,可以看到insert语句发送到了写组上,select语句发送到了读组上。

现在来测试高可用:

KILL掉master的mysql进程:

[root@node1 ~]# ps -ef|grep mysql
root 17224 1 0 11:09 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/mysql.pid
mysql 18126 17224 0 11:09 pts/0 00:00:05 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/mysqld.log --open-files-limit=28192 --pid-file=/usr/local/mysql/data/mysql.pid --socket=/usr/local/mysql/mysql.sock --port=3306
root 18358 1509 0 13:24 pts/0 00:00:00 grep mysql
[root@node1 ~]# kill -9 17224
[root@node1 ~]# kill -9 18126
[root@node1 ~]# ps -ef|grep mysql
root 18360 1509 0 13:25 pts/0 00:00:00 grep mysql

MySQLfailover输出:

Q-quit R-refresh H-health G-GTID Lists U-UUIDs
Failed to reconnect to the master after 3 attemps.
Failover starting in 'auto' mode...
# Candidate slave 192.168.88.127:3306 will become the new master.
# Checking slaves status (before failover).
# Preparing candidate for failover.
# Creating replication user if it does not exist.
# Stopping slaves.
# Performing STOP on all slaves.
# Switching slaves to new master.
# Disconnecting new master as slave.
# Starting slaves.
# Performing START on all slaves.
# Checking slaves for errors.
# Failover complete.
# Discovering slaves for master at 192.168.88.127:3306

Failover console will restart in 5 seconds.

MySQL Replication Failover Utility
Failover Mode = auto Next Interval = Tue Sep 11 15:09:05 2018

Master Information
------------------
Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB
mysql-bin.000001 4327

GTID Executed Set
0ba3df86-b570-11e8-940f-000c29715801:1-3 [...]

Replication Health Status
+-----------------+-------+---------+--------+------------+---------+
| host | port | role | state | gtid_mode | health |
+-----------------+-------+---------+--------+------------+---------+
| 192.168.88.127 | 3306 | MASTER | UP | ON | OK |
+-----------------+-------+---------+--------+------------+---------+

已经成功转移到192.168.88.127上并提升为master。

查看proxysql是否成功转移:

mysql> select * from mysql_servers;
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| 2 | 192.168.88.126 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Write Group |
| 2 | 192.168.88.127 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Read Group |
| 1 | 192.168.88.127 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Read Group |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
3 rows in set (0.00 sec)

可以看到旧master和旧slave的hostgroup_id 已经改变。来测试语句负载:

[root@nodetest ~]# mysql -ufailover -p123456 -h127.0.0.1 -P6033
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.30 (ProxySQL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

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> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from test;
+------+
| id |
+------+
| 111 |
| 111 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
| 4 |
| 45 |
| 695 |
| 6676 |
| 6676 |
+------+
14 rows in set (0.00 sec)

mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| node2 |
+------------+
1 row in set (0.01 sec)

mysql> insert into test values(43536);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test;
+-------+
| id |
+-------+
| 111 |
| 111 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
| 3 |
| 4 |
| 45 |
| 695 |
| 6676 |
| 6676 |
| 43536 |
+-------+
15 rows in set (0.00 sec)

mysql> \q
Bye

查看proxysql:

[root@nodetest ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

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> select * from stats_mysql_query_digest_reset;
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname | username | digest | digest_text | count_star | first_seen | last_seen | sum_time | min_time | max_time |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| 1 | test | failover | 0x6FA973F1E0331C0E | insert into test values(?) | 1 | 1536649818 | 1536649818 | 11699 | 11699 | 11699 |
| 2 | test | failover | 0x82A12D4C4E7B0A28 | select @@hostname | 1 | 1536649811 | 1536649811 | 495 | 495 | 495 |
| 2 | test | failover | 0x38DF1D37B3136F42 | select * from test | 2 | 1536649806 | 1536649820 | 1253 | 547 | 706 |
| 1 | test | failover | 0x02033E45904D3DF0 | show databases | 1 | 1536649802 | 1536649802 | 674 | 674 | 674 |
| 2 | information_schema | failover | 0x620B328FE9D6D71A | SELECT DATABASE() | 1 | 1536649802 | 1536649802 | 880 | 880 | 880 |
| 1 | test | failover | 0x99531AEFF718C501 | show tables | 1 | 1536649802 | 1536649802 | 590 | 590 | 590 |
| 1 | information_schema | failover | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 1 | 1536649800 | 1536649800 | 0 | 0 | 0 |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
7 rows in set (0.00 sec)

现在的读写组都是旧slave机,所以可以看到插入和查询语句都已经负载到了192.168.88.127这台机器上,证明高可用成功。

猜你喜欢

转载自www.cnblogs.com/magmell/p/9621596.html