How To Setup ProxySQL(MySql Proxy) Server ? Part -1

0
60
views
Setup MysqlProxy Server

Read with Slave and Write with Master

In today’s world, most of the infra is cloud-based now and these cloud services are reliable nowadays. if we have a look at  RDS in AWS there are reader and writer and AWS RDS decide where to send read/write request. In an ideal scenario, read request should get server via MySql slave and write request with MySQL master.

But Most of the organization still used self-hosted MySQL servers setups, thus makes ideal situation bit tough so, in this article, we will learn about MySQL Proxy.

What is MySQL Proxy?

Using MySQL Proxy an application  may communicate between one or more MySQL servers and one or more MySQL clients over the network

Why Mysql Proxy?

MySQLProxy is a middle layer sitting between a MySQL database and an application. The application connects to a proxy, which then forwards connections into the database and it works application layer.
How To Setup MySql Proxy Server?
So let’s start getting our hands dirty

Our Scenario:

Host OS: Ubuntu 18.04
RAM: 2GB memory
Proxy server: 172.31.2.24
Master IP: 172.31.2.196
Slave IP:-172.31.2.162

Lets Start

Step 1: Add MySQL Proxy Repository

To install MySQL proxy we need to set up a repository. follow below command to install repo on the Proxy server host.

[[email protected] ~]$ apt-get install -y lsb-release
[[email protected] ~]$ wget -O - 'https://repo.proxysql.com/ProxySQL/repo_pub_key' | apt-key add -
echo deb https://repo.proxysql.com/ProxySQL/proxysql-2.0.x/$(lsb_release -sc)/ ./ 
| tee /etc/apt/sources.list.d/proxysql.list
Step 2: Install MySQL

Run below command on the Proxy server host

[[email protected] ~]$ apt-get update
[[email protected] ~]$ apt-get install proxysql

Note: By default, ProxySQL binds with two Ports 6032 and 6033.

MySQL 6032 is the admin port and 6033 is the one that accepts incoming connections from clients.

Step 3: Start ProxySQL

Now run this simple command to start and enable ProxySQL

[[email protected] ~]$ systemctl start proxysql
[[email protected] ~]$ systemctl enable proxysql

Alternatively, we can do this via the Admin interface as well.

[[email protected] ~]$ mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '
Admin> proxysql start
Admin> proxysql stop
Step 4: Configuring ProxySQL via the admin interface

Before entering to the admin interface of ProxySQL, create one application user with all privileges required to your application and one monitoring user at every MySQL DB server.

On Mysql Master Host

[[email protected] ~]$ mysql -u root -ppassword
mysql> CREATE USER 'sysbench'@'%' IDENTIFIED BY 'sysbench';
mysql> GRANT ALL PRIVILEGES on *.* TO 'sysbench'@'%'; 
mysql> CREATE USER  'monitor'@'%' IDENTIFIED BY 'monitor';
mysql> GRANT USAGE,REPLICATION CLIENT on *.* TO 'monitor'@'%';
mysql> FLUSH PRIVILEGES;

On the Proxy server host

After installation and  making service up and creating application user, we need to configure ProxySQL

In this setup, we are used using need 2 host groups
hostgroup 0: For the master [Used for Write queries]
hostgroup 1: For the slaves [Used for read Queries]

[[email protected] ~]$ mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin>'
Admin>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)

Add Master for read and write requests.

Admin>INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (0,'172.31.2.196',3306);
Admin>INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'172.31.2.196',3306);

Add slave for read requests

Admin>INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'172.31.2.162',3306);

Proxysql 2.0 comes with native monitoring for Aurora, by checking the variable “Innodb_read_only”, to determine the reader/Writer for the individual nodes, This info of check has to be added to the table “mysql_replication_hostgroups” in ProxySQL

Admin>insert into mysql_replication_hostgroups values (0,1,'read_only','STG');
Admin>LOAD MYSQL SERVERS TO RUNTIME; 
Admin>SAVE MYSQL SERVERS TO DISK;

Note: When we load MYSQL SERVERS, Our writer host also gets configured in reader host group automatically by ProxySQL to handle all those queries which are redirected to reader host group in case no slaves are online.

this is optional to decrease the weightage assigned to master servers inside mysql_server table for reader host group so that our most of read queries will go on the server which has a higher weight.

Admin>UPDATE mysql_servers SET weight=200 WHERE hostgroup_id=1 AND hostname='172.31.2.196';
Admin>UPDATE mysql_servers SET weight=1000 WHERE hostgroup_id=1 AND hostname='172.31.2.162';
Configure Monitor User

monitor user will continuously check the status of the backend in a specified interval.
sysbench is the user, created for the application.

Admin> UPDATE global_variables SET variable_value='monitor' WHERE variable_name='monitor';
Query OK, 0 rows affected (0.00 sec)

Admin> LOAD MYSQL VARIABLES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

Admin> SAVE MYSQL VARIABLES TO DISK;
Query OK, 116 rows affected (0.03 sec)

INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('sysbench','sysbench',1);

Confirm user details and we will save the configuration to disk:

Admin> SELECT username,password,active,default_hostgroup,default_schema,max_connections,max_connections FROM mysql_users;
+----------+----------+--------+-------------------+----------------+-----------------+-----------------+
| username | password | active | default_hostgroup | default_schema | max_connections | max_connections |
+----------+----------+--------+-------------------+----------------+-----------------+-----------------+
| sysbench | sysbench | 1      | 1                 | NULL           | 10000           | 10000           |
+----------+----------+--------+-------------------+----------------+-----------------+-----------------+
1 row in set (0.00 sec)

Admin>  LOAD MYSQL USERS TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
Admin> SAVE MYSQL USERS TO DISK;
Query OK, 0 rows affected (0.04 sec)
Admin> SAVE MYSQL SERVERS TO DISK;
Query OK, 0 rows affected (0.30 sec)
Step 5: Configure Query Rule

After setting up we need to define which host group should get select query and which should get write. so to do this we need to run below commands.

To send all SELECT queries on the slave ( based on Regex ).

Admin> INSERT INTO mysql_query_rules (active, match_digest, destination_hostgroup, apply) VALUES (1, '^SELECT.*', 1, 0);
Query OK, 1 row affected (0.00 sec)

To send  read and write queries on master  (based on Regex)

Admin> INSERT INTO mysql_query_rules (active, match_digest, destination_hostgroup, apply) VALUES (1, '^SELECT.*FOR UPDATE', 0, 1); Query OK, 1 row affected (0.00 sec)

and write an update on Disk with below commands

Adminl> LOAD MYSQL QUERY RULES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

Admin> SAVE MYSQL QUERY RULES TO DISK;
Query OK, 0 rows affected (0.08 sec)

and this all you need to configure ProxySQL and now you just have to tell your application to communicate with ProxySQL host

I hope you like this article and I would love to answer your question. in the next article, we will explore ProxySql monitoring, configuration status, and its tests.