ubuntu16.04 用ProxySQL实现mysql读写分离

说之前还是要说一下proxySQL我们主要用来做MySQL主从服务器的读写分离,MySQL主从服务器搭建好是第一步,没有搭建好的同学请看这篇文章:地址
或者复制查询 : https://blog.csdn.net/qq_39455116/article/details/91603317

1. 参考官方文档,用Ubuntu16.04 安装proxySQL

官网:地址

  1. 安装
wget https://github.com/sysown/proxysql/releases/download/v2.0.1/proxysql_2.0.1-ubuntu16_amd64.deb
dpkg -i proxysql_2.0.1-ubuntu16_amd64.deb
  1. 查看版本
proxysql --version
  1. 启动关闭等
//开
service proxysql start
//关
service proxysql stop
//重启
service proxysql restart
//查看状态
service proxysql status

2. 进入ProxySQL管理界面

  1. mysql8.04 之前版本进入
mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt = ' Admin> ' 
  1. MySQL8.04之后的版本进入,我是MySQL8.0.16所以用后者进入
mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt = ' Admin> '  --default-auth=mysql_native_password
  1. 进入之后查看数据库show databases;默认会有5个
=show databases;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | disk          | /var/lib/proxysql/proxysql.db       |
| 3   | stats         |                                     |
| 4   | monitor       |                                     |
| 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.00 sec)

3 、到此为止ProxySQL搭建完成,下面对其MySQL主从进行配置读写分离

3.1 登录主MySQL服务器 mysql-master

添加账号并授权

create user   'proxysql'@'%' IDENTIFIED BY 'proxysql';

create user   'monitor'@'%' IDENTIFIED BY 'monitor';
GRANT ALL ON *.* TO 'proxysql'@'%' IDENTIFIED BY 'proxysql';
GRANT SELECT ON *.* TO 'monitor'@'%' IDENTIFIED BY 'monitor';

要先创建两个数据库账号用于后续配置,其中proxysql用于操作数据库monitor用于监控

2.2 登录查看proxySQL的具体信息

用步骤二的方法进入proxySQL

  1. 这是数据库信息 show databases;
mysql> show databases;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | disk          | /var/lib/proxysql/proxysql.db       |
| 3   | stats         |                                     |
| 4   | monitor       |                                     |
| 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
| 6   | myhgm         |                                     |
+-----+---------------+-------------------------------------+
6 rows in set (0.00 sec)
main:默认数据库,存放用户验证、路由规则等信息。我们要做的配置都是针对这个库的
disk:持久化到硬盘的配置
stats:proxysql运行抓取的统计信息,如各命令的执行次数、查询执行时间等
monitor:monitor模块收集的信息,db的健康情况、各种检查等
  1. 查看表show tables;
show tables;
+--------------------------------------------+
| tables                                     |
+--------------------------------------------+
| global_variables                           |
| mysql_collations                           |
| mysql_galera_hostgroups                    |
| mysql_group_replication_hostgroups         |
| mysql_query_rules                          |
| mysql_query_rules_fast_routing             |
| mysql_replication_hostgroups               |
| mysql_servers                              |
| mysql_users                                |
| proxysql_servers                           |
| runtime_checksums_values                   |
| runtime_global_variables                   |
| runtime_mysql_galera_hostgroups            |
| runtime_mysql_group_replication_hostgroups |
| runtime_mysql_query_rules                  |
| runtime_mysql_query_rules_fast_routing     |
| runtime_mysql_replication_hostgroups       |
| runtime_mysql_servers                      |
| runtime_mysql_users                        |
| runtime_proxysql_servers                   |
| runtime_scheduler                          |
| scheduler                                  |
+--------------------------------------------+
22 rows in set (0.00 sec)

4.2 正式配置读写分离

我们一共有2个节点(一个master1个slave),要进行读写分离,这里我们将master设为写节点, slave设为读节点

hostgroup_id:一个角色一个id,该表的主键是hostgroup_id+hostname+port
hostname:db实例IP
port:db实例端口
weight:权重,如果有多个相同角色的实例,会优先选择权重高的
status:状态
    -ONLINE 正常
    -SHUNNED 临时被剔除
    -OFFLINE_SOFT 软离线状态,不再接受新的连接,已建立的连接会等待
    -OFFLINE_HARD 离线,不接收新连接, 已建立的连接也会强制断开(宕机或者网络不可用)
max_connections:最大连接数
max_replication_lag:允许的最大延迟
  1. 配置写(主)服务器,端口是:10000
insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(1000,'127.0.0.1',10000,1,1000,10,'write group');
  1. 配置从(读)服务器,端口是:10001
insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(2000,'127.0.0.1',10001,1,1000,10,'read group');
  1. 查看
select * from mysql_servers;

select * from mysql_servers;
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| hostgroup_id | hostname  | port  | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment     |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| 1000         | 127.0.0.1 | 10000 | 0         | ONLINE | 1      | 0           | 1000            | 10                  | 0       | 0              | write group |
| 2000         | 127.0.0.1 | 10001 | 0         | ONLINE | 1      | 0           | 1000            | 10                  | 0       | 0              | read group  |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
2 rows in set (0.00 sec)
  1. 在主服务器节点,刚才的hostgroup_id为1000 的添加服务账号
INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('proxysql','proxysql',1000);


select * from mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
| proxysql | proxysql | 1      | 0       | 1000              | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           |         |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
1 row in set (0.00 sec)
  1. 设置监控账号
set mysql-monitor_username='monitor';
Query OK, 1 row affected (0.00 sec)

set mysql-monitor_password='monitor';
Query OK, 1 row affected (0.00 sec

4.3添加主从读写分离规则

insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1000,1);
Query OK, 1 row affected (0.00 sec)

insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',2000,1);
Query OK, 1 row affected (0.00 sec)

select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_digest         | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| 1       | 1      | ^SELECT.*FOR UPDATE$ | 1000                  | 1     |
| 2       | 1      | ^SELECT              | 2000                  | 1     |
+---------+--------+----------------------+-----------------------+-------+
2 rows in set (0.00 sec)

4.4添加主从读写分离规则

load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)

load mysql servers to runtime;
Query OK, 0 rows affected (0.01 sec)

load mysql query rules to runtime;
Query OK, 0 rows affected (0.00 sec)

load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)

load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)

4.5 将配置保存到硬盘

save mysql users to disk;
Query OK, 0 rows affected (0.01 sec)

save mysql servers to disk;
Query OK, 0 rows affected (0.04 sec)

save mysql query rules to disk;
Query OK, 0 rows affected (0.02 sec)

save mysql variables to disk;
Query OK, 109 rows affected (0.01 sec)

save admin variables to disk;
Query OK, 32 rows affected (0.00 sec)


到此读写分离的相关配置就大功告成了!之后我们的应用可以通过配置中配置proxysql用户连接proxysql服务操作我们的mysql集群

6032为管理和监控端口,6033为对外提供数据库服务的端口,admin/amin为默认的管理监控的账号密码,可以在proxysql.conf文件的admin_variables找到。
刚才我们创建了一个账号和密码都是proxysql的账号,我们现在登录

# MySQL8.0.4之前的
mysql -uproxysql -pproxysql -h 127.0.0.1 -P 6032

# MySQL8.0.4之后的
mysql -uproxysql -pproxysql -h 127.0.0.1 -P 6033 --default-auth=mysql_native_password

我的是之后的,所以用第二个登录:

root@yan:~# mysql -uproxysql -pproxysql -h 127.0.0.1 -P 6033 --default-auth=mysql_native_password
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL)

Copyright (c) 2000, 2019, 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.

4.6 但是创建的用户proxy SQL查询报错,如下:

可能是因为MySQL8.0的加密方式导致的,所以还是劝大家用MySQL5.7版本,不会有这么多问题

mysql> show databases;
ERROR 1129 (HY000): Host '172.18.0.1' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
mysql> 

9. 更改MySQL8.0默认的加密方式,不然客户端连接不上

通过本地去连接远程的mysql时报错,原因时mysql8.0的加密方法变了。

mysql8.0默认采用caching_sha2_password的加密方式

第三方客户端基本都不支持这种加密方式,只有自带的命令行支持

所以需要修改加密方式。

首先进入mysql命令行

[root@localhost ~]# mysql -u root -p
    Enter password:

第一步:修改加密方式

ALTER USER 'proxysql'@'%' IDENTIFIED BY 'proxysql' PASSWORD EXPIRE NEVER;

第二步:重新修改密码

ALTER USER 'proxysql'@'%' IDENTIFIED WITH mysql_native_password BY 'proxysql'; 

第三步:刷新(不做可能无法生效)

    FLUSH PRIVILEGES;

之后可以成功连接。

GRANT ALL PRIVILEGES ON . TO ‘proxysql’@’%’ IDENTIFIED BY ‘proxysql’;

猜你喜欢

转载自blog.csdn.net/qq_39455116/article/details/91892968
今日推荐