Build Mysql-proxy to achieve master-slave synchronous read-write separation

Proxy server Wg61 192.168.0.180 (Mysql-proxy)
master server Wg62 192.168.0.142 (responsible for writing data)
slave server Wg63 192.168.0.156 (responsible for read-only data)

Experimental idea:

  1. Download Mysql-proxy and install lua language on proxy server Wg61
  2. Wg61 installs proxy and adds environment variable parameters of /etc/profile
  3. Modify proxy configuration file parameters to test read-write separation
  4. Build master-slave servers, create test database tables and authorize user access
  5. Start Mysql-proxy to test read-write separation
  6. The steps to test the failure of the slave server and the master server
    are as follows:
    1. Install the lua language on the Wg61 server, Mysql-proxy needs the lua language to call
    [root@Wg61 ~]# yum-y insall lua
    2. Download the Mysql-proxy installation package Go to Wg61 and extract it to /usr/local/
    [root@Wg61 ~]# wget http://dev.mysql.com/get/Downloads/MySQL-Proxy/mysql-proxy-0.8.5-linux-el6-x86 -64bit.tar.gz--no-check-certificate
    [root@Wg61 ~]# tar-xf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/
    [ root@Wg61 ~]# cd/usr/local/
    [root@Wg61 local]# ls
    bin games lib libexec sbin src
    etc include lib64 mysql-proxy-0.8.5-linux-el6-x86-64bit share
    [root@Wg61 local ]#mv mysql-proxy-0.8.5-linux-el6-x86-64bit/ mysql-proxy
    3. Modify environment variable parameters
    [root@Wg61 local]#vim /etc/profile
    Finally, add exportPATH=/usr/local/mysql-proxy/bin/:/usr/local/mysql/bin:$PATH
    [root@Wg61 local]# source!$
    source /etc/profile to take effect for the command
    4. Modify Mysql- Proxy configuration file parameters, test read-write separation
    [root@Wg61 local]#vim /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
    40 min_idle_connections = 1, in lines 41 and 42 will Change the minimum number of connections to 1
    41 max_idle_connections = 1,
    5. Create a test file on the Wg62 master server and authorize user1 to access
    mysql>show databases;
    +----------------- ---+
    |Database |
    +--------------------+
    |information_schema |
    |mysql |
    |test |
    +----------- ---------+
    3rows in set (0.00 sec)

mysql>create database HK;
QueryOK, 1 row affected (0.00 sec)

mysql>use HK;
Databasechanged
mysql>create table city(id int);
QueryOK, 0 rows affected (0.02 sec)

mysql>insert into city values(123);
QueryOK, 1 row affected (0.00 sec)

mysql>select * from city ;
+------+
|id |
+------+
| 123 |
+------+
1row in set (0.00 sec)

mysql>grant all on HK.* to user1@'%' identified by '123456';

6. Wg63 creates a test file from the server, and authorizes user user1 to access
mysql> show databases;
+--------------------+
| Database |
+--- -----------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)

mysql> create database HK;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| HK |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)

mysql> create table city(id int);
ERROR 1046 (3D000): No database selected
mysql> use HK;
Database changed
mysql> create table city(id int);
Query OK, 0 rows affected (0.06 sec)

mysql> insert into city values(456);
Query OK, 1 row affected (0.00 sec)

mysql> select * from city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.00 sec)

mysql> grant allon HK.* to user1@'%' identified by'123456';

7、 Wg1服务器上启动Mysql-proxy服务
[root@Wg61 local]#mysql-proxy
--proxy-read-only-backend-addresses=192.168.0.156:3306
--proxy-backend-addresses=192.168.0.142:3306
--proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua&
[1] 2044
[root@Wg61 local]#2018-01-04 05:25:31: (critical) plugin proxy 0.8.5 started

Open another window to check whether the service starts successfully:
[root@Wg61 ~]# lsof -i :4040
COMMAND PIDUSER FD TYPE DEVICE SIZE/OFF NODE NAME
mysql-pro 2044root 9u IPv4 12940 0t0 TCP *:yo-main (LISTEN)
parameter Description:
--proxy-read-only-backend-addresses=192.168.0.111:3306 # Define the backend read-only server
--proxy-backend-addresses=192.168.0.112:3306 # Define the backend mysql main server address, specify mysql Write the port of the main server
--proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua& #Specify the lua script, here, the rw-splitting script is used , for read-write separation
When there are multiple read-only servers, you can write multiple following parameters:
--proxy-read-only-backend-addresses=192.168.0.111:3306 # Define backend read-only server
--proxy- read-only-backend-addresses=192.168.0.112:3306 # Define the backend read-only server
#--proxy-address=192.168.0.110:3307 Specify the listening port of mysql proxy, the default is: 4040

8. Test read and write function
(1) Test write operation: you can view the Wg62 data information, you can also write data, but you can't see the Wg63 data
mysql>select user();
+----------- ----------+
|user() |
+---------------------+
|[email protected] |
+--- ------------------+
1row in set (0.00 sec)

mysql>show databases;
+--------------------+
|Database |
+--------------------+
| information_schema|
|HK |
|test |
+--------------------+
3rows in set (0.00 sec)

mysql>use HK;
Readingtable information for completion of table and column names
Youcan turn off this feature to get a quicker startup with -A

Databasechanged
mysql>use HK;
Databasechanged
mysql>show tables;
+--------------+
|Tables_in_HK |
+--------------+
|city |
+--------------+
1row in set (0.00 sec)

mysql>select * from city;
+------+
|id |
+------+
| 123 |
+------+
1row in set (0.00 sec)

mysql>insert into city values(1313);
QueryOK, 1 row affected (0.00 sec)

mysql>select * from city;
+------+
|id |
+------+
| 123 |
| 1313 |
+------+
2rows in set (0.00 sec)
(2) The test read data
is logged in from the Wg2 simulated client and shows that there is no data from the server. You can view the corresponding data information
[root@Wg62 ~]# mysql-uuser1 -p123456 -P4040 -h192.168.0.180
[root@Wg62 ~]# mysql - uuser1 -p123456 -P4040-h192.168.0.180
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporationand/or its
affiliates. Other names may be trademarks of theirrespective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear thecurrent input statement.

mysql> select * from HK.city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.00 sec)

mysql> Ctrl-C -- exit!
Aborted
[root@Wg62 ~]# mysql -uuser1 -p123456 -P4040-h192.168.0.180
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporationand/or its
affiliates. Other names may be trademarks of theirrespective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear thecurrent input statement.

mysql> select * from HK.city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.01 sec)

mysql> select user();
+---------------------+
| user() |
+-------------- -------+
| [email protected] |
+---------------------+
1 row in set (0.00sec)
inserting data shows success , but the query found that no data was inserted
mysql> insert into HK.city values(4545);
Query OK, 1 row affected (0.01 sec)

mysql> select * from HK.city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.00 sec)

mysql> insert into HK.city values(232323);
Query OK, 1 row affected (0.00 sec)

mysql> select from HK.city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.00sec)
9. Configure MYsql Master-slave and read-write separation
(1) Synchronize the data information of Wg2 and Wg3 ​​servers
[root@Wg62 ~]#mysqldump -uroot -p -A > all.sql
[root@Wg62 ~]# scpall.sql 192.168.0.156 :/root/
[root@Wg63 ~]# mysql-uroot -p123456
mysql> source/root/all.sql
(2) Wg2 master server is configured as MASTER, and user2 is authorized as synchronization user
[root@Wg62 ~]# vim /etc/my.cnf
log-bin=mysql-binlog
binlog-do-db=HK
binlog_format=row
server-id=1
mysql> grant all on
.*to user2@'%' identified by '123456';
restart the database

(3) Wg3 slave server is configured as SLAVE
Method 1: [root@Wg63 ~]#vim /etc/my.cnf
master_host='192.168.0.142'
master_user='user2'
master_password='123456'
Method 2: Log in to the database
MySQL> changemaster to master_host='192.168.0.142', master_user='user2', master_password='123456';
Restart the database
10. Test whether the master-slave synchronization read-write separation is achieved through Mysql-proxy
(1) Check whether the master-slave synchronizes
mysql> select user();
+---------------------+
| user() |
+----------------- ----+
| [email protected] |
+---------------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| HK |
| test |
+--------------------+
3 rows in set (0.00 sec)

mysql> use HK;
Reading table information for completion of table andcolumn names
You can turn off this feature to get a quicker startupwith -A

Database changed
mysql> use HK;
Database changed
mysql> select * from city;
+--------+
| id |
+--------+
| 123 |
| 1313 |
| 4545 |
| 232323 |
+--------+
4 rows in set (0.00 sec)

mysql> insert into city values(789);
Query OK, 1 row affected (0.00 sec)

mysql> select * from city;
+--------+
| id |
+--------+
| 123 |
| 1313 |
| 4545 |
| 232323 |
| 789 |
+---- ----+
5 rows in set (0.00 sec)
log in to the master server to view, it shows that the insertion is successful,
log in to the slave server to view, it shows that the synchronization is successful
mysql> select user();
+------------- ---+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> select * from HK.city;
+--------+
| id |
+--------+
| 123 |
| 1313 |
| 4545 |
| 232323 |
| 789 |
+-- ------+
5 rows in set (0.00sec)
(2) Test that the slave server is down (you can write and view data)
mysql> insert into HK.city values(888);
Query OK, 1 row affected (0.01 sec)

mysql> select * from HK.city;
+--------+
| id |
+--------+
| 123 |
| 1313 |
| 4545 |
| 232323 |
| 789 |
| 0 |
| 888 |
+--------+

7 rows in set (0.00sec)

Summary: 1. When the slave database is stopped, the proxy query will be transferred to the master. When the slave is started, the proxy is still reading the master. When a new link comes in, it will read the slave data again. Sometimes you may need to restart mysql-proxy

  1. After recovery from the server, synchronize the data just inserted again
    (3) Test that the main server is down
    mysql> select * from HK.city
    -> ;
    +--------+
    | id |
    +----- ---+
    | 123 |
    | 1313 |
    | 4545 |
    | 232323 |
    | 789 |
    | 0 |
    | 888 |
    +-------+
    7 rows in set (0.00 sec)

mysql> use HK;
ERROR 2013 (HY000): Lost connection to MySQL serverduring query
mysql> insert into HK.city values(999);
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 6
Current database: NONE

ERROR 2013 (HY000): Lost connection to MySQL server during query
shows that data can only be read but cannot be written

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324828125&siteId=291194637