proxysql 主从复制读写分离配置过程记录

1、环境信息

软件GitHub地址: https://github.com/sysown/proxysql/
软件官网:https://proxysql.com/
系统版本:
[[email protected] proxysql]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
主从环境dockers
+---------------+------+-----------+
| hostname      | port | status    |
+---------------+------+-----------+
| 192.168.56.11 | 3306 | master    |
| 192.168.56.11 | 3307 | slave     |
+---------------+------+-----------+

2、proxysql 安装和配置

cat <<EOF | tee /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL YUM repository
baseurl=https://repo.proxysql.com/ProxySQL/proxysql-2.0.x/centos/\$releasever
gpgcheck=1
gpgkey=https://repo.proxysql.com/ProxySQL/repo_pub_key
EOF

yum install proxysql OR yum install proxysql-version
/etc/init.d/proxysql start
export MYSQL_PS1="\\[email protected]\\h [\\d] \\r:\\m:\\s>>>"

3、docker 启动容器

[[email protected] proxysql]# docker run -p 3306:3306 --name mysqlmaster -e MYSQL_ROOT_PASSWORD=123456 -d docker.io/centos/mysql-57-centos7
5dd187415052bc46d8daa8b8045f1337c2e1fe4f139d5e6ef6a29be1e408547d
[[email protected] proxysql]# docker run -p 3307:3306 --name mysqlslave -e MYSQL_ROOT_PASSWORD=123456 -d docker.io/centos/mysql-57-centos7
1cfc67f4144b026bae1539be5abe313756c5595b8cf7be5223f80e1a7782f311
[[email protected] proxysql]# docker ps -a
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS              PORTS                    NAMES
1cfc67f4144b        docker.io/centos/mysql-57-centos7   "container-entrypo..."   9 seconds ago       Up 8 seconds        0.0.0.0:3307->3306/tcp   mysqlslave
5dd187415052        docker.io/centos/mysql-57-centos7   "container-entrypo..."   20 seconds ago      Up 18 seconds       0.0.0.0:3306->3306/tcp   mysqlmaster

4、增加主从必要配置

[[email protected] proxysql]# docker exec -it --user root 5 bash
bash-4.2# vi /etc/my.cnf.d/rep.cnf
"/etc/opt/rh/rh-mysql57/my.cnf.d/rep.cnf" [New] 5L, 48C written
bash-4.2# cat /etc/my.cnf.d/rep.cnf   

[mysqld]
server-id=1
log-bin
binlog-format=row
bash-4.2# exit
exit
[[email protected] proxysql]# docker exec -it --user root 1 bash
bash-4.2# vi /etc/my.cnf.d/rep.cnf
"/etc/opt/rh/rh-mysql57/my.cnf.d/rep.cnf" [New] 5L, 49C written
bash-4.2# cat /etc/my.cnf.d/rep.cnf 

[mysqld]
server-id=11
log-bin
binlog-format=row
bash-4.2# exit
exit
[[email protected] proxysql]# systemctl restart docker
[[email protected] proxysql]# mysql -h127.0.0.1 -p123456
ERROR 2003 (HY000): Can not connect to MySQL server on ‘127.0.0.1‘ (111 "Connection refused")
[[email protected] proxysql]# docker ps -a
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS                      PORTS               NAMES
1cfc67f4144b        docker.io/centos/mysql-57-centos7   "container-entrypo..."   4 minutes ago       Exited (0) 44 seconds ago                       mysqlslave
5dd187415052        docker.io/centos/mysql-57-centos7   "container-entrypo..."   4 minutes ago       Exited (0) 44 seconds ago                       mysqlmaster
[[email protected] proxysql]# docker start 5
5
[[email protected] proxysql]# docker start 1
1
[[email protected] proxysql]# docker ps -a
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS              PORTS                    NAMES
1cfc67f4144b        docker.io/centos/mysql-57-centos7   "container-entrypo..."   4 minutes ago       Up 4 seconds        0.0.0.0:3307->3306/tcp   mysqlslave
5dd187415052        docker.io/centos/mysql-57-centos7   "container-entrypo..."   4 minutes ago       Up 8 seconds        0.0.0.0:3306->3306/tcp   mysqlmaster

5、配置主从复制用户

[[email protected] proxysql]# mysql -h127.0.0.1 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:01:13>>>show master logs;
+-------------------------+-----------+
| Log_name                | File_size |
+-------------------------+-----------+
| 5dd187415052-bin.000001 |      1035 |
| 5dd187415052-bin.000002 |       154 |
+-------------------------+-----------+
2 rows in set (0.00 sec)

[email protected] [(none)] 02:01:20>>>reset master ;
Query OK, 0 rows affected (0.32 sec)

[email protected] [(none)] 02:01:29>>> GRANT REPLICATION SLAVE ON *.* TO ‘repluser‘@‘%‘ IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.28 sec)

[email protected] [(none)] 02:03:06>>>flush privileges;
Query OK, 0 rows affected (0.04 sec)

[email protected] [(none)] 02:03:13>>>exit
Bye

6、配置主从复制

[[email protected] proxysql]# mysql -h127.0.0.1 -p123456 -P3307
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:03:17>>> CHANGE MASTER TO MASTER_HOST=‘192.168.56.11‘, MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘123456‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘5dd187415052-bin.000001‘,MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.34 sec)

[email protected] [(none)] 02:03:21>>>start slave;
Query OK, 0 rows affected (0.30 sec)

[email protected] [(none)] 02:03:35>>>show salve status\G
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘salve status‘ at line 1
[email protected] [(none)] 02:03:42>>>show slave status\G
*************************** 1. row ***************************
            Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.56.11
                Master_User: repluser
                Master_Port: 3306
                Connect_Retry: 60
            Master_Log_File: 5dd187415052-bin.000001
        Read_Master_Log_Pos: 585
            Relay_Log_File: 1cfc67f4144b-relay-bin.000002
                Relay_Log_Pos: 758
        Relay_Master_Log_File: 5dd187415052-bin.000001
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            Replicate_Do_DB:
        Replicate_Ignore_DB:
        Replicate_Do_Table:
    Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                Last_Errno: 0
                Last_Error:
                Skip_Counter: 0
        Exec_Master_Log_Pos: 585
            Relay_Log_Space: 972
            Until_Condition: None
            Until_Log_File:
                Until_Log_Pos: 0
        Master_SSL_Allowed: No
        Master_SSL_CA_File:
        Master_SSL_CA_Path:
            Master_SSL_Cert:
            Master_SSL_Cipher:
            Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
            Last_SQL_Errno: 0
            Last_SQL_Error:
Replicate_Ignore_Server_Ids:
            Master_Server_Id: 1
                Master_UUID: be5e882c-a920-11e9-9acb-0242ac110002
            Master_Info_File: /var/lib/mysql/data/master.info
                    SQL_Delay: 0
        SQL_Remaining_Delay: NULL
    Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
        Master_Retry_Count: 86400
                Master_Bind:
    Last_IO_Error_Timestamp:
    Last_SQL_Error_Timestamp:
            Master_SSL_Crl:
        Master_SSL_Crlpath:
        Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
        Replicate_Rewrite_DB:
                Channel_Name:
        Master_TLS_Version:
1 row in set (0.00 sec)

[email protected] [(none)] 02:03:50>>>exit
Bye

7、创建proxysql_test库验证主从同步情况

[[email protected] proxysql]# mysql -h127.0.0.1 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:04:01>>>create database proxysql_test;
Query OK, 1 row affected (0.28 sec)

[email protected] [(none)] 02:04:15>>>exit
Bye
[[email protected] proxysql]# mysql -h127.0.0.1 -p123456 -P3307
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:04:24>>>show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| proxysql_test      |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

[email protected] [(none)] 02:04:28>>>exit
Bye

8、启动proxysql检查目前配置情况,因为我没有配置,都为空

[[email protected] proxysql]# /etc/init.d/proxysql start
Starting ProxySQL: 2019-07-18 14:24:37 [INFO] Using config file /etc/proxysql.cnf
2019-07-18 14:24:37 [INFO] SSL keys/certificates found in datadir (/var/lib/proxysql): loading them.
DONE!
[[email protected] proxysql]# mysql -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:25:09>>>select * from mysql_server_read_only_log;
Empty set (0.00 sec)

[email protected] [(none)] 02:26:20>>>select * from mysql_server_replication_lag_log;
Empty set (0.00 sec)

[email protected] [(none)] 02:26:40>>>SELECT * FROM mysql_servers;
Empty set (0.00 sec)

[email protected] [(none)] 02:28:11>>> SELECT * FROM mysql_replication_hostgroups;
Empty set (0.00 sec)

[email protected] [(none)] 02:28:46>>>SELECT * FROM mysql_users;
Empty set (0.00 sec)

[email protected] [(none)] 02:29:16>>>SELECT * FROM mysql_query_rules;
Empty set (0.00 sec)

9、新增server数据

[email protected] [(none)] 02:29:35>>> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘192.168.56.11‘,3306);
Query OK, 1 row affected (0.00 sec)

[email protected] [(none)] 02:30:44>>>  INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘192.168.56.11‘,3307);
Query OK, 1 row affected (0.00 sec)

[email protected] [(none)] 02:31:22>>>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.56.11 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 10           | 192.168.56.11 | 3307 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+---------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)

[email protected] [(none)] 02:31:31>>>exit
Bye

10、创建监控用户并配置proxysql

[[email protected] proxysql]# mysql -h127.0.0.1 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:33:05>>>  GRANT REPLICATION SLAVE ON *.* TO ‘proxysqlmon‘@‘%‘ IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.28 sec)

[email protected] [(none)] 02:33:14>>>flush privileges;
Query OK, 0 rows affected (0.02 sec)

[email protected] [(none)] 02:33:23>>>exit
Bye
[[email protected] proxysql]# mysql -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:34:00>>>SET mysql-monitor_username=‘proxysqlmon‘;
Query OK, 1 row affected (0.00 sec)

[email protected] [(none)] 02:34:12>>>SET mysql-monitor_password=‘123456‘;
Query OK, 1 row affected (0.00 sec)

[email protected] [(none)] 02:34:20>>>LOAD MYSQL VARIABLES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

[email protected] [(none)] 02:34:35>>>SAVE MYSQL VARIABLES TO DISK;
Query OK, 116 rows affected (0.28 sec)

[email protected] [(none)] 02:34:49>>>select * from mysql_server_connect_log;
+---------------+------+------------------+-------------------------+------------------------------------------------------------------------+
| hostname      | port | time_start_us    | connect_success_time_us | connect_error                                                          |
+---------------+------+------------------+-------------------------+------------------------------------------------------------------------+
| 192.168.56.11 | 3307 | 1563431498030552 | 0                       | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3306 | 1563431498668916 | 0                       | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3306 | 1563431558031708 | 0                       | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3307 | 1563431559067995 | 0                       | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3306 | 1563431618031624 | 0                       | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3307 | 1563431618808593 | 0                       | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3307 | 1563431676331614 | 2304                    | NULL                                                                   |
| 192.168.56.11 | 3306 | 1563431677521700 | 2621                    | NULL                                                                   |
+---------------+------+------------------+-------------------------+------------------------------------------------------------------------+
8 rows in set (0.00 sec)

[email protected] [(none)] 02:34:59>>>select * from mysql_server_ping_log;
+---------------+------+------------------+----------------------+------------------------------------------------------------------------+
| hostname      | port | time_start_us    | ping_success_time_us | ping_error                                                             |
+---------------+------+------------------+----------------------+------------------------------------------------------------------------+
| 192.168.56.11 | 3306 | 1563431448313821 | 0                    | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3306 | 1563431458086145 | 0                    | Access denied for user  ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3306 | 1563431648134014 | 0                    | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3307 | 1563431648333984 | 0                    | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3306 | 1563431658135211 | 0                    | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3307 | 1563431658286566 | 0                    | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3306 | 1563431668157058 | 0                    | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3307 | 1563431668264603 | 0                    | Access denied for user ‘monitor‘@‘192.168.56.11‘ (using password: YES) |
| 192.168.56.11 | 3307 | 1563431676386597 | 627                  | NULL                                                                   |
| 192.168.56.11 | 3306 | 1563431676506906 | 554                  | NULL                                                                   |
| 192.168.56.11 | 3306 | 1563431686387739 | 670                  | NULL                                                                   |
| 192.168.56.11 | 3307 | 1563431686558685 | 868                  | NULL                                                                   |
| 192.168.56.11 | 3306 | 1563431696387964 | 609                  | NULL                                                                   |
| 192.168.56.11 | 3307 | 1563431696495978 | 173                  | NULL                                                                   |
| 192.168.56.11 | 3307 | 1563431706388009 | 623                  | NULL                                                                   |
| 192.168.56.11 | 3306 | 1563431706559451 | 331                  | NULL                                                                   |
+---------------+------+------------------+----------------------+------------------------------------------------------------------------+
53 rows in set (0.00 sec)

11、配置读写分离组,proxysql会按照规则自动修改server的hostgroup_id

[email protected] [(none)] 02:35:15>>>show create table mysql_replication_hostgroups\G
*************************** 1. row ***************************
    table: mysql_replication_hostgroups
Create Table: CREATE TABLE mysql_replication_hostgroups (
    writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY,
    reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND reader_hostgroup>=0),
    check_type VARCHAR CHECK (LOWER(check_type) IN (‘read_only‘,‘innodb_read_only‘,‘super_read_only‘)) NOT NULL DEFAULT ‘read_only‘,
    comment VARCHAR NOT NULL DEFAULT ‘‘, UNIQUE (reader_hostgroup))
1 row in set (0.00 sec)

[email protected] [(none)] 02:36:16>>>INSERT INTO mysql_replication_hostgroups VALUES(10,20,"read_only","test replication with read and write separation");
Query OK, 1 row affected (0.00 sec)

[email protected] [(none)] 02:39:39>>>SELECT * FROM mysql_replication_hostgroups;
+------------------+------------------+------------+-------------------------------------------------+
| writer_hostgroup | reader_hostgroup | check_type | comment                                         |
+------------------+------------------+------------+-------------------------------------------------+
| 10               | 20               | read_only  | test replication with read and write separation |
+------------------+------------------+------------+-------------------------------------------------+
1 row in set (0.00 sec)

[email protected] [(none)] 02:39:59>>>LOAD MYSQL SERVERS TO RUNTIME;
Query OK, 0 rows affected (0.01 sec)

[email protected] [(none)] 02:40:12>>>SAVE MYSQL SERVERS TO DISK;
Query OK, 0 rows affected (0.05 sec)

[email protected] [(none)] 02:40:25>>>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.56.11 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 10           | 192.168.56.11 | 3307 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+---------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)

[email protected] [(none)] 02:40:34>>>exit
Bye
[[email protected] proxysql]# mysql -h127.0.0.1 -p123456 -P3307
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:40:40>>>set global read_only=1;
Query OK, 0 rows affected (0.00 sec)

[email protected] [(none)] 02:40:54>>>exit
Bye
[[email protected] proxysql]# mysql -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:41:05>>>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.56.11 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 192.168.56.11 | 3307 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+---------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)

12、配置proxysql 中用于客户端访问的用户

[email protected] [(none)] 02:41:09>>>exit
Bye
[[email protected] proxysql]# mysql -h127.0.0.1 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:43:43>>> GRANT ALL ON *.* TO ‘rootuser‘@‘%‘ IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)

[email protected] [(none)] 02:43:50>>>flush privileges;
Query OK, 0 rows affected (0.27 sec)

[email protected] [(none)] 02:44:03>>>exit
Bye
[[email protected] proxysql]# mysql -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:44:17>>> INSERT INTO mysql_users(username,password,default_hostgroup) VALUES (‘rootuser‘,‘123456‘,10);
Query OK, 1 row affected (0.00 sec)

[email protected] [(none)] 02:44:25>>>SELECT * FROM mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
| rootuser | 123456   | 1      | 0       | 10                | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           |         |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+---------+
1 row in set (0.00 sec)

[email protected] [(none)] 02:45:27>>>LOAD MYSQL USERS  TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

[email protected] [(none)] 02:45:54>>>SAVE MYSQL USERS TO DISK;
Query OK, 0 rows affected (0.03 sec)

[email protected] [(none)] 02:45:58>>>exit
Bye

13、配置读写分离路由规则,配置路由前都是用用户的默认规则

[[email protected] proxysql]#  mysql -urootuser -p123456 -h192.168.56.11  -P6033 -e "SELECT @@server_id;"
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
[[email protected] proxysql]#  mysql -urootuser -p123456 -h192.168.56.11 -P6033 -e "BEGIN;SELECT @@server_id;commit;"
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
[[email protected] proxysql]# mysql -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[email protected] [(none)] 02:48:27>>>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);
Query OK, 2 rows affected (0.00 sec)

[email protected] [(none)] 02:48:32>>>SELECT * FROM mysql_query_rules;
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+----------------------+---------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+---------+
| rule_id | active | username | schemaname | flagIN | client_addr | proxy_addr | proxy_port | digest | match_digest         | match_pattern | negate_match_pattern | re_modifiers | flagOUT | replace_pattern | destination_hostgroup | cache_ttl | cache_empty_result | cache_timeout | reconnect | timeout | retries | delay | next_query_flagIN | mirror_flagOUT | mirror_hostgroup | error_msg | OK_msg | sticky_conn | multiplex | gtid_from_hostgroup | log | apply | comment |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+----------------------+---------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+---------+
| 1       | 1      | NULL     | NULL       | 0      | NULL        | NULL       | NULL       | NULL   | ^SELECT.*FOR UPDATE$ | NULL          | 0                    | CASELESS     | NULL    | NULL            | 10                    | NULL      | NULL               | NULL          | NULL      | NULL    | NULL    | NULL  | NULL              | NULL           | NULL             | NULL      | NULL   | NULL        | NULL      | NULL                | NULL | 1     | NULL    |
| 2       | 1      | NULL     | NULL       | 0      | NULL        | NULL       | NULL       | NULL   | ^SELECT              | NULL          | 0                    | CASELESS     | NULL    | NULL            | 20                    | NULL      | NULL               | NULL          | NULL      | NULL    | NULL    | NULL  | NULL              | NULL           | NULL             | NULL      | NULL   | NULL        | NULL      | NULL                | NULL | 1     | NULL    |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+----------------------+---------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+---------+
2 rows in set (0.00 sec)

[email protected] [(none)] 02:48:50>>>LOAD MYSQL QUERY RULES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

[email protected] [(none)] 02:49:07>>>SAVE MYSQL QUERY RULES TO DISK;
Query OK, 0 rows affected (0.31 sec)

[email protected] [(none)] 02:49:18>>>exit
Bye
[[email protected] proxysql]#  mysql -urootuser -p123456 -h192.168.56.11 -P6033 -e "BEGIN;SELECT @@server_id;commit;"
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
[[email protected] proxysql]#  mysql -urootuser -p123456 -h192.168.56.11  -P6033 -e "SELECT @@server_id;"
+-------------+
| @@server_id |
+-------------+
|          11 |
+-------------+

原文地址:https://blog.51cto.com/860143/2421400

时间: 2024-10-27 13:57:39

proxysql 主从复制读写分离配置过程记录的相关文章

MySQL Group Replication mgr 单主 proxysql 读写分离配置过程

1.前期准备,mgr安装见上一篇文章 2.创建用户和导入脚本 GRANT ALL ON *.* TO 'rootuser'@'%' IDENTIFIED BY '123456'; /mgr/mysql/bin/mysql -h127.0.0.1 -P24802 <a.sql [[email protected] ~]# cat a.sql USE sys; DELIMITER $$ CREATE FUNCTION IFZERO(a INT, b INT) RETURNS INT DETERMIN

mysql主从复制读写分离之——proxysql应用

一.说明ProxySQL是一个开源的MySQL代理服务器,这意味着它充当MySQL服务器和访问其数据库的应用程序之间的中介.ProxySQL可以通过在多个数据库服务器池之间分配流量来提高性能,并且如果一个或多个数据库服务器发生故障,还可以通过自动故障切换到备用数据库来提高可用性. 系统环境:master1:ubuntu16.04 mysql5.6 192.168.1.10 3307 master2:ubuntu16.04 mysql5.6 192.168.1.20 3307slave1: ubu

MySQL5.6 Replication主从复制(读写分离) 配置完整版

MySQL5.6主从复制(读写分离)教程 1.MySQL5.6开始主从复制有两种方式: 基于日志(binlog): 基于GTID(全局事务标示符). 需要注意的是:GTID方式不支持临时表!所以如果你的业务系统要用到临时表的话就不要考虑这种方式了,至少目前最新版本MySQL5.6.12的GTID复制还是不支持临时表的. 所以本教程主要是告诉大家如何通过日志(binlog)方式做主从复制! 2.MySQL官方提供的MySQL Replication教程: http://dev.mysql.com/

MHA+ProxySQL实现读写分离高可用

最近在研究ProxySQL,觉得还挺不错的,所以就简单的折腾了一下,ProxySQL目前也是Percona在推荐的一个读写分离的中间件.关于详细的介绍可以参考官方文档.https://github.com/sysown/proxysql/wiki 本文主要介绍的是MHA+ProxySQL读写分离以及高可用,ProxySQL的细节请参考文档,目前已经有人写发非常详细了,文章最后会给出链接.当然和Group Replication,PXC搭配那是更完美的.关于MHA的配置参考我前面的文章,本文就不再

mysql数据业务垂直+水平分割+主从复制读写分离

友情提示:本人第一次写技术博客,会继续完善,尽量做到图文并茂,通俗易懂,如果有什么写的不好的地方,还请大家多多提意见,您的意见将是我宝贵的资源.如果有兴趣的话,还可以一起讨论相关技术哦,亲!一定要注意软件版本哦! 联系方式 QQ:794884160 指导老师:双星  冯德勇老师  曾勇老师 一.拓扑图: 垂直+水平分割+主从复制+读写分离完整原理图: 仅说明原理,详细拓扑及参数参考本次实验拓扑图 本次试验拓扑图:(上图左侧部分) 二.实验描述: 根据业务原型先进行数据库垂直切割,然后用户数据根据

【实战】Amoeba 代理 MySQL 主从复制 + 读写分离 【提供源码包】

目录简介: 1· Amoeba 的介绍2· MySQL 主从复制原理3· MySQL 读写分离原理4· 实战案例5· 总结归纳 Amoeba 的介绍 1)Amoeba 是什么: 1·Amoeba 的中文名是:变形虫.它是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy.它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行.基于此可以实现负载均衡.读写分离.高可用性等需求. 2·Amoeba相当于一个SQL请求的路由器,目的是为负载均衡.读

Mysql一主多从和读写分离配置简记

Mysql一主多从和读写分离配置简记 标签: mysql数据库服务器class数据库servermanager 2012-05-30 16:44 14981人阅读 评论(1) 收藏 举报  分类: 数据库 版权声明:本文为博主原创文章,未经博主允许不得转载. 近期开发的系统中使用MySql作为数据库,由于数据涉及到Money,所以不得不慎重.同时,用户对最大访问量也提出了要求.为了避免Mysql成为性能瓶颈并具备很好的容错能力,特此实现主从热备和读写分离.在此简做纪要,以备日后所用! 一.配置主

MySQL主从同步、读写分离配置步骤、问题解决笔记

根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记:       现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用.       为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master)           192.168.0.2 (Slave) MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i6

mysql主从复制读写分离-Altas

mysql主从复制读写分离 本文读写分离使用的软件是Altas,altas是奇虎360公司开发的开源数据库代理软件.它是基于mysql-proxy开发而成的 它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行.基于此可以实现负载均衡.读写分离.高可用性等需求. mysql读写分离原理: 数据库层在高并发的情况下,i/o会产生瓶颈.而实际上用户读的请求要远远大于写的请求. 使用代理服务作为数据库前端,将不同的请求根据规则分配到不同的后端数据上面去,比如将写的请求分