1、什么是MaxScale
MaxScale是MariaDB数据库的一个中间件,为MariaDB提供代理服务,主要可以实现读写分离和一定的负载均衡功能,其中读写分离可将读操作和写操作分离到不同的数据库服务器上,以提高系统的整体性能和扩展性,而正是读写分离功能,使得MaxScale具备已经的负载均衡特性。
2、搭建环境
3、搭建步骤
3.1、分别在两台OpenEuler上安装MariaDB数据库并分别完成初始化(两台服务器步骤相同)
(1)执行安装命令“yum install mariadb-server -y
”;
(2)执行启动MariaDB数据库命令“systemctl start mariadb
”;
(3)执行MariaDB初始化命令“mysql_secure_installation”,并按自身需求完成MariaDB初始化,初始化案例如下:
[root@localhost ~]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.Enter current password for root (enter for none): #输入事先设置的数据库root账户密码
OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.You already have a root password set, so you can safely answer 'n'.Change the root password? [Y/n] n #是否改变root用户密码?... skipping.By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.Remove anonymous users? [Y/n] n #是否删除匿名用户?... skipping.Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n #是否不允许root用户直接登录?... skipping.By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.Remove test database and access to it? [Y/n] n #是否删除测试数据库?... skipping.Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.Reload privilege tables now? [Y/n] y #是否重新加载权限信息?... Success!Cleaning up...All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.Thanks for using MariaDB!
(4)输入以下代码分别在两套MariaDB上创建监控用户“monitor”和路由用户“maxscale”,并授予monitor用户replication slave权限和super权限,授予maxscale所有权限:
# 路由账号
create user 'maxscale'@'%' identified by 'ymh123';
grant all on *.* to maxscale@'%';# 监控账号
create user 'monitor'@'%' identified by 'ymh123';
grant replication slave,super on *.* to 'monitor'@'%';
3.2、将两套MariaDB数据库设置为主从关系
3.2.1、配置MariaDB-Master
(1)在192.168.174.136服务器上输入命令“vim /etc/my.cnf
”,并在[mysqld]
字段下添加如下配置,并保存退出:
server-id=1
log-bin=mysql-bin
(2)输入命令“systemctl restart mariadb
”重启MariaDB数据库;
(3)输入命令“mysql -u root
”进入数据库,并输入命令“show master status;
”记录好“File”字段下的binlog文件的文件名和“Position”字段下的数值,如下:
3.2.2、配置MariaDB-Slave
(1)在192.168.174.136服务器上输入命令“vim /etc/my.cnf
”,并在[mysqld]
字段下添加如下配置,并保存退出(server-id要与Master不同,且不写入log-bin字段):
server-id=2
(2)输入命令“systemctl restart mariadb
”重启MariaDB数据库;
(3)输入命令“mysql -u root
”进入数据库,并输入命令“change master to master_host = '192.168.174.136', master_user = 'monitor', master_password = 'ymh123', master_log_file = 'mysql-bin.000006', master_log_pos = 594;
”最后输入“exit
”退出MariaDB数据库;
(4)输入命令“systemctl restart mariadb
”重启MariaDB数据库后,输入“mysql -u root
”进入数据库;
(5)输入命令“show slave status\G;
”如果以下两个字段都是yes,则主从搭建成功(当然,可在Master进行写操作,如果同步到slave,则主从没问题):
4、安装并配置MaxScale
(1)进入MariaDB官方网站,找到对应版本的MaxScale安装包,并下载;
(2)通过dpkg命令对安装包进行本地安装,需要注意的是,安装过程中可能会缺乏必要的依赖,如果出现缺依赖的情况,此时通过“apt install -f
”命令自动补充依赖;
(3)输入“vim /etc/maxscale.cnf
”命令,对MaxScale进行如下配置:
[maxscale]
threads=auto[server1]
type=server
address=192.168.174.136
port=3306[server2]
type=server
address=192.168.174.130
port=3306[MariaDB-Monitor]
type=monitor
module=mariadbmon
servers=server1,server2 #监控模块写入所有MariaDB节点
user=monitor
password=ymh123
monitor_interval=2s#本实验只涉及读写分离,不涉及只读,因此只读模块注释掉不配置
#[Read-Only-Service]
#type=service
#router=readconnroute
#servers=server1
#user=service_user
#password=service_pw
#router_options=slave[Read-Write-Service]
type=service
router=readwritesplit
servers=server1,server2
user=maxscale
password=ymh123#本实验只涉及读写分离,不涉及只读,因此只读模块注释掉不配置
#[Read-Only-Listener]
#type=listener
#service=Read-Only-Service
#protocol=mariadbprotocol
#port=4008[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=mariadbprotocol
port=4006
(4)输入命令“systemctl restart maxscale
”重启MaxScale;
(5)输入命令“maxctrl list servers
”,若出现下图结果,则MaxScale读写分离搭建成功;
(6)查看MaxScale读写分离模块的监听端口,为默认的4006端口
5、读写分离验证
在读写分离验证之前,我们需要明确MariaDB主从架构的一个特点,即数据的同步只能是Master节点向Slave节点单方向进行同步。因此如果在Slave节点进行写操作,则写入的数据不会同步到Master节点中。
基于上述特性,如果通过MaxScale转发出来的写操作,写入的数据在Master和 Slave两个节点同时存在,则该写操作是由MaxScale分配到Master节点,然后由Master节点主从同步到Slave,步骤如下:
(1)打开数据库客户端,连接到MaxScale节点的4006端口;
(2)进行写操作,并验证Master和Slave节点是否同时存在写入的数据;
(3)通过SQL语句“select @@hostname
”查看读操作访问的节点(需提前修改数据库服务器的主机名)
综上,读写分离配置成功。
6、注意事项
(1)创建用户时,监控用户monitor除赋予replication slave权限外,还要赋予super权限,否则MaxScale与MariaDB通过monitor用户建立监控进程时,会出现认证错误(Auth Error),这一点无论是在国内其他文章还是在官方英文文档中,都没有指明,如果出现认证错误,此时最佳的排查方式是查看路径为/var/log/maxscale/maxscale.log
的日志内容;
(2)进行写操作后,不要单纯使用“select @@hostname
”来判断写操作的节点,因为“select @@hostname”本身是读操作语句,只能判断自己,结果也一定是slave节点;
(3)OpenEuler系统本身暂未适配MaxScale,如果要在OpenEuler上安装MaxScale,最好的方式是使用容器版本的MaxScale;
(4)MaxScale不具备高可用功能,需要配合MariaDB的其他高可用方案使用。