环境:
proxysql :192.168.65.2
master :192.168.65.3
slave :192.168.65.4
mysql版本:5.7.26
下载Proxysql
https://proxysql.com/
安装
[root@test2 ~]# yum install -y proxysql-2.0.8-1-centos67.x86_64.rpm
启动
[root@test2 ~]# service proxysql start
Starting ProxySQL: 2019-07-03 17:25:12 [INFO] Using config file /etc/proxysql.cnf
2019-07-03 17:25:12 [INFO] No SSL keys/certificates found in datadir (/var/lib/proxysql). Generating new keys/certificates.
DONE!
检查状态
[root@test2 ~]# service proxysql status
ProxySQL is running (7464).
检查端口
[root@test2 ~]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:6032 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:6033 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::3306 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
6032 和 6033 就是 ProxySQL 的默认端口,6032 为管理端口,6033 为客户端口
登入管理端口(默认管理用户admin,密码admin,管理端口6032,客户端口6033)
[root@test2 ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
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 1
Server version: 5.5.30 (ProxySQL Admin Module)
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.
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 |
+-----+---------------+-------------------------------------+
5 rows in set (0.01 sec)
配置proxysql
a.添加服务器信息
insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.65.3',3306);
insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.65.4',3306);
mysql> select * from mysql_servers;
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10 | 192.168.65.3 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 10 | 192.168.65.4 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
保存修改
mysql> load mysql servers to runtime;
Query OK, 0 rows affected (0.01 sec)
mysql> save mysql servers to disk;
Query OK, 0 rows affected (0.03 sec)
b.master上添加监控账号
create user monitor@'%' identified by '123';
grant all on *.* to monitor@'%';
c.proxysql 管理端口配置监控账号信息
set mysql-monitor_username='monitor';
set mysql-monitor_password='123';
保存修改
load mysql variables to runtime;
save mysql variables to disk;
d.检查监控状态
连接状态
mysql> select * from mysql_server_connect_log;
+--------------+------+------------------+-------------------------+----------------------------------------------------------------+
| hostname | port | time_start_us | connect_success_time_us | connect_error |
+--------------+------+------------------+-------------------------+----------------------------------------------------------------+
| 192.168.65.4 | 3306 | 1562146513024875 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.3 | 3306 | 1562146513842424 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.4 | 3306 | 1562146573025888 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.3 | 3306 | 1562146574001548 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.4 | 3306 | 1562146633026875 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.3 | 3306 | 1562146633872452 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.3 | 3306 | 1562147094371541 | 2289 | NULL |
| 192.168.65.4 | 3306 | 1562147095536307 | 2564 | NULL |
+--------------+------+------------------+-------------------------+----------------------------------------------------------------+
看最后如果出现 connect_error = NULL 则表示正常。
ping状态
mysql> select * from mysql_server_ping_log;
+--------------+------+------------------+----------------------+----------------------------------------------------------------+
| hostname | port | time_start_us | ping_success_time_us | ping_error |
+--------------+------+------------------+----------------------+----------------------------------------------------------------+
| 192.168.65.4 | 3306 | 1562146593041343 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.3 | 3306 | 1562146593231401 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.4 | 3306 | 1562146603041844 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.3 | 3306 | 1562146603142607 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.4 | 3306 | 1562146613042174 | 0 | Access denied for user 'monitor'@'test2' (using password: YES) |
| 192.168.65.4 | 3306 | 1562147174354918 | 850 | NULL |
| 192.168.65.3 | 3306 | 1562147174490797 | 820 | NULL |
| 192.168.65.3 | 3306 | 1562147184355293 | 718 | NULL |
| 192.168.65.4 | 3306 | 1562147184510190 | 773 | NULL |
+--------------+------+------------------+----------------------+----------------------------------------------------------------+
同样,看最后如出现NULL则表示正常
e.配置proxysql组信息
mysql> insert into mysql_replication_hostgroups(writer_hostgroup,reader_hostgroup,comment) values(10,20,'test');
Query OK, 1 row affected (0.00 sec)
mysql> load mysql servers to runtime;
Query OK, 0 rows affected (0.01 sec)
mysql> save mysql servers to disk;
Query OK, 0 rows affected (0.04 sec)
如果配置成功,则mysql_servers中hostgroup_id 会根据 master和slave 中的read_only的状态修改mysql_servers 中服务器的分组。
mysql> select * from mysql_servers;
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10 | 192.168.65.3 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 20 | 192.168.65.4 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
最开始我们添加服务器信息的时候 hostgroup_id设置的都是10,现在 组配置信息添加后则变为 10,20,说明配置成功,如果没有变化,则检查是否配置正确或slave没有设置成read_only。
f.master上创建使用sql的账号
create user sqluser@'%' identified by '123';
grant all on *.* to sqluser@'%';
g.proxysql管理端口配置 sql使用账号的信息
insert into mysql_users(username,password,default_hostgroup) values('sqluser','123',10);
load mysql users to runtime;
save mysql users to disk;
mysql> mysql> select * from mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
| sqluser | 123 | 1 | 0 | 10 | NULL | 0 | 1 | 0 | 1 | 1 | 10000 | |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
h.测试,proxysql 客户端口(端口:6033)
[root@test2 ~]# mysql -usqluser -p123 -h127.0.0.1 -P6033
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 2 |
+-------------+
1 row in set (0.01 sec)
mysql> create database ll;
Query OK, 1 row affected (0.01 sec)
查询和ddl 都可以正常进行
i.proxysql管理端口配置 路由信息
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) VALUES(1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);
mysql> load mysql query rules to runtime;
Query OK, 0 rows affected (0.00 sec)
mysql> save mysql query rules to disk;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from mysql_query_rules\G
*************************** 1. row ***************************
rule_id: 1
active: 1
username: NULL
schemaname: NULL
flagIN: 0
client_addr: NULL
proxy_addr: NULL
proxy_port: NULL
digest: NULL
match_digest: ^SELECT.*FOR UPDATE$
match_pattern: NULL
negate_match_pattern: 0
re_modifiers: CASELESS
flagOUT: NULL
replace_pattern: NULL
destination_hostgroup: 10
cache_ttl: NULL
cache_empty_result: NULL
cache_timeout: NULL
reconnect: NULL
timeout: NULL
retries: NULL
delay: NULL
next_query_flagIN: NULL
mirror_flagOUT: NULL
mirror_hostgroup: NULL
error_msg: NULL
OK_msg: NULL
sticky_conn: NULL
multiplex: NULL
gtid_from_hostgroup: NULL
log: NULL
apply: 1
comment: NULL
*************************** 2. row ***************************
rule_id: 2
active: 1
username: NULL
schemaname: NULL
flagIN: 0
client_addr: NULL
proxy_addr: NULL
proxy_port: NULL
digest: NULL
match_digest: ^SELECT
match_pattern: NULL
negate_match_pattern: 0
re_modifiers: CASELESS
flagOUT: NULL
replace_pattern: NULL
destination_hostgroup: 20
cache_ttl: NULL
cache_empty_result: NULL
cache_timeout: NULL
reconnect: NULL
timeout: NULL
retries: NULL
delay: NULL
next_query_flagIN: NULL
mirror_flagOUT: NULL
mirror_hostgroup: NULL
error_msg: NULL
OK_msg: NULL
sticky_conn: NULL
multiplex: NULL
gtid_from_hostgroup: NULL
log: NULL
apply: 1
comment: NULL
2 rows in set (0.00 sec)
测试
测试,proxysql 客户端口(端口:6033)
[root@test2 ~]# mysql -usqluser -p123 -h127.0.0.1 -P6033
mysql> select * from ty.b;
+------+-------+------+
| id | bname | s |
+------+-------+------+
| 1 | ae | aa |
| 2 | cc | cc |
| 3 | ee | eec |
| 4 | ppe | ppc |
| 5 | yy | yy |
+------+-------+------+
5 rows in set (0.02 sec)
mysql> create database ooo;
Query OK, 1 row affected (0.01 sec)
进管理端口查看
mysql> select hostgroup,schemaname,username,digest_text from stats_mysql_query_digest;
+-----------+--------------------+----------+----------------------------------+
| hostgroup | schemaname | username | digest_text |
+-----------+--------------------+----------+----------------------------------+
| 10 | information_schema | sqluser | create database ooo |
| 10 | information_schema | sqluser | select * from ty.a |
| 10 | information_schema | sqluser | create database ll |
| 20 | information_schema | sqluser | select * from ty.b |
| 10 | information_schema | sqluser | select @@server_id |
| 10 | information_schema | sqluser | select @@version_comment limit ? |
+-----------+--------------------+----------+----------------------------------+
select * from ty.b 对应的组是20,也就是192.168.65.4 (slave)
create database ooo 对应的组是10,也就是192.168.65.3(master)
到此就实现了简单的读写分离
启用Web统计功能
([email protected]:6032) [main]> select * from global_variables where variable_name like 'admin-web%'; +-------------------+----------------+ | variable_name | variable_value | +-------------------+----------------+ | admin-web_enabled | false | | admin-web_port | 6080 | +-------------------+----------------+ 启用变量admin-web_enabled ([email protected]:6032) [main]> set admin-web_enabled='true'; Query OK, 1 row affected (0.00 sec) ([email protected]:6032) [main]> load admin variables to runtime; Query OK, 0 rows affected (0.00 sec) ([email protected]:6032) [main]> save admin variables to disk; Query OK, 31 rows affected (0.01 sec) ([email protected]:6032) [main]> select * from global_variables where variable_name like 'admin-web%'; +-------------------+----------------+ | variable_name | variable_value | +-------------------+----------------+ | admin-web_enabled | true | | admin-web_port | 6080 | +-------------------+----------------+ 查看登录Web界面的用户名、密码和端口 ([email protected]:6032) [main]> select * from global_variables where variable_name like 'admin-web%' or variable_name LIKE 'admin-stats%'; +-----------------------------------+----------------+ | variable_name | variable_value | +-----------------------------------+----------------+ | admin-stats_credentials | stats:stats | | admin-stats_mysql_connections | 60 | | admin-stats_mysql_connection_pool | 60 | | admin-stats_mysql_query_cache | 60 | | admin-stats_system_cpu | 60 | | admin-stats_system_memory | 60 | | admin-web_enabled | true | | admin-web_port | 6080 | +-----------------------------------+----------------+ 8 rows in set (0.01 sec)