Verifying Read/Write Splitting and Load Balancing of MySQL Router
- Configure the MySQL Router file.
- Open the file.
vim /etc/mysqlrouter/mysqlrouter.conf
- Replace the file content with the following:
[DEFAULT] logging_folder = /var/log/mysqlrouter runtime_folder = /var/run/mysqlrouter config_folder = /etc/mysqlrouter [logger] level = INFO # If no plugin is configured which starts a service, keepalive # will make sure MySQL Router will not immediately exit. It is # safe to remove once Router is configured. [keepalive] interval = 60 # The following options can be used in the policy part of the route distinguisher. [routing:basic_failover] # Router address bind_address = 192.168.53.22 # Router port bind_port = 7001 routing_strategy = first-available # Target server destinations = 192.168.53.22:3306,192.168.53.22:3307 connect_timeout = 2 [routing:load_balance] # Router address bind_address = 192.168.53.22 # Router port bind_port = 7002 routing_strategy = round-robin # Target server destinations = 192.168.53.22:3306,192.168.53.22:3307,192.168.53.22:3308 connect_timeout = 1
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Open the file.
- Start MySQL Router.
- Query the process ID of MySQL Router.
ps -ef|grep mysqlrouter

- Stop the MySQL Router process.
kill -9 72890
- Start MySQL Router.
1mysqlrouter &
- Check whether the startup is successful.
1 2
netstat -an|grep 7001 netstat -an|grep 7002

If the command output is the same as that shown in the figure, MySQL Router is started.
- Query the process ID of MySQL Router.
- Perform a query test.
- Open the file.
vim /home/read_7001.sh
- Replace the file content with the following:
1 2 3 4 5
#!/bin/bash for i in {1..1000} do /usr/local/mysql/bin/mysql -uroot -p123456 -P7001 -h192.168.53.22 -e "select @@report_host" done
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Execute the read request script.
1sh /home/read_7001.sh
As shown in the figure above, all read requests sent to port 7001 are routed to the first primary database (192.168.53.22:3306).
- Open the file.
vim /home/read_7002.sh
- Replace the file content with the following:
1 2 3 4 5
#!/bin/bash for i in {1..1000} do /usr/local/mysql/bin/mysql -uroot -p123456 -P7002 -h192.168.53.22 -e "select @@report_host" done
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Execute the read request script.
1sh /home/read_7002.sh
As shown in the figure above, all read requests sent to port 7002 are routed to the three MySQL servers in polling mode to achieve read load balancing.
- Open the file.
- Perform a write test.
- Open the file.
vim /home/write_7001.sh
- Replace the file content with the following:
1 2 3 4 5
#!/bin/bash for i in {1..2000} do /usr/local/mysql/bin/mysql -uroot -p123456 -P7001 -h192.168.53.22 -Dsysbench -e "insert into test1(id,report_hostname) values($i,@@report_host)" done
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Execute the write request script.
1sh /home/write_7001.sh - Confirm the test data.
1/usr/local/mysql/bin/mysql -uroot -p123456 -P7001 -h192.168.53.22 -Dsysbench -e "select * from test1"

As shown in the figure above, all write requests sent to port 7001 are routed to the first primary database (192.168.53.22:3306).
It can be learned from the foregoing exercises that read/write splitting can be implemented as long as write requests are sent to port 7001 and read requests are sent to port 7002.
- Open the file.
- Establish the primary-primary replication scheme.
Initialize the two databases by following instructions in Initializing the MySQL Database. Primary database 1 is 192.168.53.22:3309, and primary database 2 is 192.168.53.22:3310.
- Add the following configurations to the parameter configuration files of the two databases:
1 2
auto_increment_increment=2 auto_increment_offset=1
Primary database 2:
1 2
auto_increment_increment=2 auto_increment_offset=2
auto_increment_increment and auto_increment_offset are generally used in primary-primary synchronization to stagger self-increment and prevent key value conflicts.
Refer to Semisynchronous Replication and Parallel Replication in the MySQL Primary-Secondary Replication Deployment Guide to establish the primary-secondary replication relationship and then switch the role.
- Configure the MySQL Router file.
- Open the file.
vim /etc/mysqlrouter/mysqlrouter.conf
- Replace the file content with the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[DEFAULT] logging_folder = /var/log/mysqlrouter runtime_folder = /var/run/mysqlrouter config_folder = /etc/mysqlrouter [logger] level = INFO # If no plugin is configured which starts a service, keepalive # will make sure MySQL Router will not immediately exit. It is # safe to remove once Router is configured. [keepalive] interval = 60 [routing:load_balance] # Router address bind_address = 192.168.53.22 # Router port bind_port = 7001 routing_strategy = round-robin # Target server destinations = 192.168.53.22:3309,192.168.53.22:3310 connect_timeout = 1
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Open the file.
- Create a test database and a test table.
- Connect to primary database 1.
/usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysqlm1.sock
- Create a test database and table.
1 2 3 4 5 6 7
create database sysbench; use sysbench; CREATE TABLE `test1` ( `id` bigint(20) NOT NULL, `report_hostname` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- Confirm that data is synchronized to primary database 2.
1 2 3 4
/usr/local/mysql/bin/mysql -uroot -p123456 -S /data/mysql/run/mysqlm2.sock show databases; use sysbench; show tables;

- Connect to primary database 1.
- Start MySQL Router.
- Query the process ID of MySQL Router.
ps -ef|grep mysqlrouter

- Stop the MySQL Router process.
kill -9 72890
- Start MySQL Router.
mysqlrouter &
- Check whether the startup is successful.
1netstat -an|grep 7001

If the command output is the same as that shown in the figure, MySQL Router is started.
- Query the process ID of MySQL Router.
- Perform a write test.
1sh /home/write_7001.sh - Confirm the test data.
1/usr/local/mysql/bin/mysql -uroot -p123456 -P7001 -h192.168.53.22 -Dsysbench -e "select * from test1"

According to the preceding exercises, to achieve read/write load balancing, you need to configure primary-primary replication. Set the routing strategy to round-robin so that read and write requests can be handled in polling mode.
- Add the following configurations to the parameter configuration files of the two databases: