龙空技术网

技术分享 | MaxScale 实现 MySQL读写分离

爱可生 482

前言:

目前朋友们对“dog059magnet”大概比较关注,咱们都想要知道一些“dog059magnet”的相关内容。那么小编在网上汇集了一些对于“dog059magnet””的相关资讯,希望姐妹们能喜欢,朋友们一起来了解一下吧!

作者:李鹏博

爱可生 DBA 团队成员,主要负责 MySQL 故障处理和 SQL 审核优化。对技术执着,为客户负责。

本文来源:原创投稿

*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。

MaxScale 是由 MariaDB 官方出品的一款开源数据库中间件,其插件是插拔式的,而且可以定制化开发属于自己的插件,使用非常的灵活自由,目前官方提供了例如监控、高可用、读写分离、防火墙等插件。其中高可用和监控插件相互配合可以实现 MariaDB 的 Failover 、Switchover 、autoRejoin 功能,并在故障转移时可以自动进行数据补偿,不过遗憾的是由于 MySQL 的 GTID 构成方式和 MariaDB 的差异性,目前 MySQL 无法使用其高可用功能。不过可以使用其读写分离功能。

提到数据库的读写分离,其中需要解决的问题就是分别在主从实例上进行读写操作时如何保证在从实例读取的数据的正确性,一般我们有如下几种做法,比如:延迟读取,就是在读取前等待一段时间;转发需要数据正确性较高的查询到主实例;借助 MySQL 本身的半同步复制保证主从数据的一致性,并转发查询到无延迟或延迟较小的从实例上。第一种做法会人为的拉大查询的返回时间;第二种则配置及维护起来较为困难;第三种则看起来"针不戳"的样子。而 MaxScale 的实现方式就是第三种,通过指定读取时能够容忍的最大延迟时间,当从实例延迟时间超过该时间后,读操作就不会被路由到该节点,如果实在没有可用从节点,读操作就会被路由到主节点。而且 MaxScale 还支持因果读取,通过配置 causal_reads=local 和 causal_reads_timeout 参数来实现,效果就是在从实例进行查询时,如果实例延迟较大,会等待 causal_reads_timeout 超时,默认10s,超时后就将查询路由到主节点。当然,也并不是说这种实现方式就是最完美的,考虑一种场景,如果所有的从实例都延迟较高,在进行查询时没有可用从实例,这时主实例就要承担所有的读写压力,这时候负载会不会将主实例压死也是一个需要考虑的问题。所以没有最完美的方案,只有最适合自己的。接下来让我们瞅瞅如何配置 MaxScale 实现 MySQL 数据库的读写分离。

部署拓扑

主机名 IP 角色 node4 10.186.63.88 Maxscale node1 10.186.61.191 MySQL Master node2 10.186.61.192 MySQL Slave node3 10.186.63.64 MySQL Slave

部署后端 MySQL 一主两从半同步复制,部署步骤略,状态如下:

## 一主两从mysql> show slave hosts;+-----------+---------------+------+-----------+--------------------------------------+| Server_id | Host          | Port | Master_id | Slave_UUID                           |+-----------+---------------+------+-----------+--------------------------------------+| 737716692 | 10.186.61.192 | 3306 | 622227692 | d121bf0f-1922-11ed-86d9-02000aba3dc0 || 534997148 | 10.186.63.64  | 3306 | 622227692 | bb3d53a9-1940-11ed-a059-02000aba3f40 |+-----------+---------------+------+-----------+--------------------------------------+2 rows in set (0.00 sec)## 半同步复制mysql> show global status like 'Rpl_semi_sync_master_clients';+------------------------------+-------+| Variable_name                | Value |+------------------------------+-------+| Rpl_semi_sync_master_clients | 2     |+------------------------------+-------+1 row in set (0.00 sec)

创建 MaxScale 用户并授权

mysql> CREATE USER 'maxscale'@'%' IDENTIFIED BY '123';Query OK, 0 rows affected (0.00 sec)mysql> grant select on mysql.* to maxscale@'%';Query OK, 0 rows affected (0.01 sec)mysql> GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%';Query OK, 0 rows affected (0.00 sec)

创建监控用户并授权

mysql> CREATE USER 'monitor'@'%' IDENTIFIED BY '123';Query OK, 0 rows affected (0.01 sec)mysql> GRANT REPLICATION CLIENT on *.* to 'monitor'@'%';Query OK, 0 rows affected (0.00 sec)

安装 MaxScale

## 安装依赖包yum -y install libcurl libaio openssl gnutls libatomic## 下载MaxScale安装包wget  添加MaxScale运行用户groupadd maxscaleuseradd -g maxscale maxscale## 解压安装包并授权mkdir /data/maxscaletar xf maxscale-6.1.4.rhel.7.tar.gz -C /data/maxscale --strip-components=1chown maxscale.maxscale /data/maxscale/ -R## 配置环境变量echo "export PATH=$PATH:/data/maxscale/bin/" >> /etc/profilesource /etc/profile# maxscale --versionMaxScale 6.1.4

对前面创建的数据库的 maxscale 用户和监控用户的密码加密

## 生成密钥# /data/maxscale/bin/maxkeys /data/maxscale/var/lib/maxscalePermissions of '/data/maxscale/var/lib/maxscale/.secrets' set to owner:read.Ownership of '/data/maxscale/var/lib/maxscale/.secrets' given to maxscale. ## 使用密钥对明文密码加密# /data/maxscale/bin/maxpasswd /data/maxscale/var/lib/maxscale/ 123F270B97B3D4D37BC619EB02304A02C1456F41B05CF2D7E5F37BA48CC7646C2E4

MaxScale 也支持 Web GUI ,如果要开启 Web GUI(本文档未开启),需要生成自签名证书

# mkdir /data/maxscale/ssl## 一直Enter即可# openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /data/maxscale/ssl/mariadb.key -out /data/maxscale/ssl/mariadb.crtGenerating a 2048 bit RSA private key..................+++..............................................+++writing new private key to '/data/maxscale/ssl/mariadb.key'-----You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [XX]:State or Province Name (full name) []:Locality Name (eg, city) [Default City]:Organization Name (eg, company) [Default Company Ltd]:Organizational Unit Name (eg, section) []:Common Name (eg, your name or your server's hostname) []:Email Address []:# chown maxscale.maxscale /data/maxscale/ssl/ -R

配置 MaxScale 配置文件

# vim /data/maxscale/etc/maxscale.cnf[maxscale]threads=auto # Server definitions[dbserv1]type=serveraddress=10.186.61.191port=3306protocol=MariaDBBackend #Not Modified [dbserv2]type=serveraddress=10.186.61.192port=3306protocol=MariaDBBackend #Not Modified [dbserv3]type=serveraddress=10.186.63.64port=3306protocol=MariaDBBackend #Not Modified # Monitor for the servers[Replication-Monitor]type=monitormodule=mariadbmonservers=dbserv1, dbserv2, dbserv3user=monitorpassword=F270B97B3D4D37BC619EB02304A02C1456F41B05CF2D7E5F37BA48CC7646C2E4monitor_interval=2000ms # Service definitions[Read-Write-Service]type=servicerouter=readwritesplitservers=dbserv1, dbserv2, dbserv3user=maxscalepassword=F270B97B3D4D37BC619EB02304A02C1456F41B05CF2D7E5F37BA48CC7646C2E4max_slave_connections=2master_accept_reads=falsemax_connections=0# Ensure read and write consistencymax_slave_replication_lag=1s#causal_reads=local#causal_reads_timeout=10 # Listener definitions for the services[Read-Write-Listener]type=listenerservice=Read-Write-Serviceprotocol=MariaDBClientport=33060

配置 MaxScale 启动服务

# vim /usr/lib/systemd/system/maxscale.service[Unit]Description=MariaDB MaxScale Database ProxyAfter=network.target [Service]Type=forkingRestart=on-abort PermissionsStartOnly=true # Use the default user and groupUser=maxscaleGroup=maxscale # Start MaxScaleExecStart=/data/maxscale/bin/maxscale --user=maxscale --basedir=/data/maxscale/ --config=/data/maxscale/etc/maxscale.cnfTimeoutStartSec=120LimitNOFILE=65535StartLimitBurst=0WatchdogSec=60sNotifyAccess=all # MaxScale should be restarted if it exits with 75 (BSD's EX_TEMPFAIL)RestartForceExitStatus=75 # Only relevant when MaxScale is linked with -fsanitize=addressEnvironment=ASAN_OPTIONS=abort_on_error=1 [Install]WantedBy=multi-user.target

启动 MaxScale

# systemctl daemon-reload# systemctl start maxscale

确认读写分离效果

# /data/mysql/base/5.7.25/bin/mysql -umaxscale -p -h 10.186.63.98 -P 33060Enter password: mysql> select @@hostname;+------------+| @@hostname |+------------+| node3      |+------------+1 row in set (0.00 sec)mysql> select @@hostname;+------------+| @@hostname |+------------+| node2      |+------------+1 row in set (0.00 sec)

检查 MaxScale 后端状态

# maxctrl list servers┌─────────┬───────────────┬──────┬─────────────┬─────────────────┬──────┐│ Server  │ Address       │ Port │ Connections │ State           │ GTID │├─────────┼───────────────┼──────┼─────────────┼─────────────────┼──────┤│ dbserv1 │ 10.186.61.191 │ 3306 │ 1           │ Master, Running │      │├─────────┼───────────────┼──────┼─────────────┼─────────────────┼──────┤│ dbserv2 │ 10.186.61.192 │ 3306 │ 1           │ Slave, Running  │      │├─────────┼───────────────┼──────┼─────────────┼─────────────────┼──────┤│ dbserv3 │ 10.186.63.64  │ 3306 │ 1           │ Slave, Running  │      │└─────────┴───────────────┴──────┴─────────────┴─────────────────┴──────┘

标签: #dog059magnet