Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'.
mysql> GRANT ALL ON *.* TO 'proxysql'@'192.168.1.%' IDENTIFIED BY 'pwproxysql'; 5.1.2 登陆proxysql管理端 [root@ProxySQL ~]# yum install mysql -y # 安装mysql客户端命令;依赖:mysql-libs [root@ProxySQL ~]# export MYSQL_PS1="(/u@/h:/p) [/d]> " [root@ProxySQL ~]# mysql -uadmin -padmin -h227.0.0.1 -P6032 # 默认的用户名密码都是 admin。 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, 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.
(admin@127.0.0.1:6032) [(none)]> 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) 库说明:
(admin@127.0.0.1:6032) [(none)]> load mysql servers to runtime; (admin@127.0.0.1:6032) [(none)]> save mysql servers to disk; 在proxysql主机的mysql_users表中添加刚才创建的账号,proxysql客户端需要使用这个账号来访问数据库。 default_hostgroup默认组设置为写组,也就是1; 当读写分离的路由规则不符合时,会访问默认组的数据库;
(admin@127.0.0.1:6032) [(none)]> load mysql users to runtime; (admin@127.0.0.1:6032) [(none)]> save mysql users to disk; 5.2 添加健康监测的账号 5.2.1 mysql端添加proxysql只能查的账号 首先在后端master节点上创建一个用于监控的用户名(只需在master上创建即可,因为会复制到slave上),这个用户名只需具有USAGE权限即可。如果还需要监控复制结构中slave是否严重延迟于master(先混个眼熟:这个俗语叫做"拖后腿",术语叫做"replication lag"),则还需具备replication client权限。这里直接赋予这个权限。
mysql> GRANT replication client ON *.* TO 'monitor'@'192.168.1.%' IDENTIFIED BY 'monitor'; 5.2.2 proxysql端修改变量设置健康检测的账号 (admin@127.0.0.1:6032) [(none)]> set mysql-monitor_username='monitor'; Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username'; Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_password'; Query OK, 1 row affected (0.00 sec) 修改后,加载到RUNTIME,并保存到disk。
(admin@127.0.0.1:6032) [(none)]> load mysql variables to runtime; (admin@127.0.0.1:6032) [(none)]> save mysql variables to disk; 5.3 添加读写分离的路由规则: 将select语句全部路由至hostgroup_id=2的组(也就是读组) 但是select * from tb for update这样的语句是修改数据的,所以需要单独定义,将它路由至hostgroup_id=1的组(也就是写组) 其他没有被规则匹配到的组将会被路由至用户默认的组(mysql_users表中的default_hostgroup) (admin@127.0.0.1:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1,1); Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> 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$ | 1 | 1 | | 2 | 1 | ^SELECT | 2 | 1 | +---------+--------+----------------------+-----------------------+-------+ 2 rows in set (0.00 sec) 5.4 将刚才我们修改的数据加载至RUNTIME中(参考ProxySQL的多层配置结构): 5.4.1 load进runtime,使配置生效 (admin@127.0.0.1:6032) [(none)]> load mysql query rules to runtime; (admin@127.0.0.1:6032) [(none)]> load admin variables to runtime; 5.4.2 save到磁盘(/var/lib/proxysql/proxysql.db)中,永久保存配置 (admin@127.0.0.1:6032) [(none)]> save mysql query rules to disk; (admin@127.0.0.1:6032) [(none)]> save admin variables to disk; 6 测试读写分离 6.1 连接proxysql客户端: 登录用户是刚才我们在mysql_user表中创建的用户,端口为6033
[root@centos7 ~]#mysql -uproxysql -ppwproxysql -h227.0.0.1 -P6033 Welcome to the MariaDB monitor. Commands end with ; or /g. Your MySQL connection id is 4 Server version: 5.5.30 (ProxySQL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)