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)