1. Prepare three servers (centos7.6)
maxscale | ip:192.168.1.58 | Read-write separation middleware |
mariadb(master) | ip:192.168.1.60 | Write operation |
mariadb(slave) | ip:192.168.1.63 | Read operation |
2.mariadb master-slave configuration
Remarks: yum installation of mariadb, mariadb-server will not go into details
2.1: Modify the mariadb configuration file in the two servers to support mysqlbinlog master-slave replication
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
2.2: Modify the server-id = value in my.cnf, modify the server-id of the two servers to different values
2.3: Start the master mariadb (192.168.1.60), log in for authorization, query the master status, record the mysql-bin.000007 file name and binlog start bit 2942, these two data will be used when receiving authorization from the server
grant replication slave on *.* to slave@'%' identified by '123456';
show master status;
2.4 Start from mariadb (192.168.1.63), log in to receive authorization, start slave, check status
change master to
master_user='slave',
master_password='123456',
master_host='192.168.1.60',
master_log_file='mysql-bin.000007',
master_log_pos=2942;
start slave;
#\G的作用是以key:value的形式一行一行显示
show slave status\G;
3. Configure maxscale read-write separation
3.1 Download and install maxscale and some of its dependencies at maxscale (192.168.1.58)
yum install libaio.x86_64 libaio-devel.x86_64 novacom-server.x86_64 libedit -y
wget https://downloads.mariadb.com/MaxScale/1.4.5/centos/7/x86_64/maxscale-1.4.5-1.centos.7.x86_64.rpm
rpm -ivh maxscale-1.4.5-1.centos.7.x86_64.rpm
3.2 Log in to the main mariadb (192.168.1.60) to create a monitoring user (maxmon) and routing user (maxrou)
grant replication slave ,replication client on *.* to maxmon@'%' identified by '123456';
grant all on *.* to maxrou@'%' identified by '123456';
3.3 Modify maxscale configuration file (192.168.1.58)
vim /etc/maxscale.cnf
[maxscale]
threads=1
# Server definitions
# Set the address of the server to the network
# address of a MySQL server.
#定义数据库服务器
[server1]
type=server
#主数据库地址master
address=192.168.1.60
port=3306
protocol=MySQLBackend
#定义数据库服务器
[server2]
type=server
#从数据库地址slave
address=192.168.1.63
port=3306
protocol=MySQLBackend
#定义要监视的服务器总共两台,监控的用户
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2
user=maxmon
passwd=123456
monitor_interval=10000
# Service definitions
# Service Definition for a read-only service and
# a read/write splitting service.
# 读负载均衡模块
# 读写分离模块可以实现读负载均衡,因此注释掉该模块
#[Read-Only Service]
#type=service
#router=readconnroute
#servers=server2
#user=maxrou
#passwd=123456
#router_options=slave
# 定义读写分离服务及路由用户
[Read-Write Service]
type=service
router=readwritesplit
# 主从数据库
servers=server1,server2
# 设置路由用户名和密码
user=maxrou
passwd=123456
max_slave_connections=100%
# 定义管理服务
[MaxAdmin Service]
type=service
router=cli
# 注释监听
#[Read-Only Listener]
#type=listener
#service=Read-Only Service
#protocol=MySQLClient
#port=4008
# 定义读写分离服务的端口4006
[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006
# 定义管理服务监听端口6603
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
port=6603
3.4 Start and view status (192.168.1.58)
systemctl start maxscale
systemctl status maxscale
netstat -antp
4. Test Verification
4.1 Log in to the MaxScale(192.168.1.58) manager to check the database connection status. The default user name and password are admin/mariadb
maxadmin -uadmin -pmariadb -P6603
list servers
4.2 Create a test user, log in to the main mariadb (192.168.1.60) to create a test user test
grant all on *.* to test@'%' identified by '123456';
4.3 Create a test database in the main database mariadb (192.168.1.60), and create a table wen, and then insert the value
create database test;
use test;
create table wen(id int);
insert into wen values(1),(2);
Find test in the slave database mariadb (192.168.1.63), and insert the value in the table wen
insert into wen values (9);
Note: The master-slave replication has been implemented at the beginning, the data in the master database (192.168.1.60) will be automatically synchronized to the slave database (192.168.1.63), and the new data from the slave database will not be synchronized to the master database
4.4 On the MaxScale server (192.168.1.58), use the Mysql client to connect to MaxScale
mysql -utest -p123456 -h'192.168.1.58' -P4006
Query all data in the test.wen table. The data is the same as the test.wen table in the secondary database, with one more record than the main database test.wen
Insert two pieces of data through the mysql client of the proxy server maxscale (192.168.1.58) and view the results
View the results through the main server (192.168.1.60) mysql client
View the results through the mysql client from the server (192.168.1.63)
Conclusion: The master server (192.168.1.60) is used for writing, because writing data to the slave server will not be synchronized to the master server; and the reading is read by the slave server (192.168.1.63).
As a result, master-slave replication is realized, and mysql read-write separation is realized.