<返回更多

关于Linux下MySQL主备集群负载均衡之读写分离(MaxScale)

2022-10-15  网易号  互联网资讯看板
加入收藏

写在前面

 

 

「 只要足够开心,烦恼就追不上哦 ^_^ 」

一、为什么需要负载均衡?

需要负载均衡的理由:

 

 


 

从集群角度考虑,MySQL做主备集群复制如果只用作备份,有些浪费,和负载均衡结合使用一种相辅相成的作用。

所以MySQL的负载均衡架构通常和数据分片及复制紧密相关。我们今天要讲的读/写分离策略即属于负载均衡的一个实现。

有些负载均衡技术本身能够实现这一点,有些需要应用自己知道哪些节点是可读的或可写的。

客户端读写分离

常见的读写分离一种是通过客户端去区分读写,比如上面那个图,写在主库,读通过负载均衡到多个从库。

在应用层面粗粒度通过配置不同数据源分离读写实现,同时还需要考虑从库异步复制导致的脏数据问题,需要监控延迟复制来决策读写的分配。可以考虑在编码层次基于查询,版本,请求时间戳,会话等做一些读写策略,不能容忍脏数据的读可以放到写节点

从库的负载可以通过DNS负载、LVS+Keepalived、硬件负载均衡器F5、TCP代理(HAproxy,Nginx)、或者在应用中管理从库负载均衡。比如做简单的数据源池做线性负载等。

服务端读写分离

另一种是通过在服务端去区分,通过MySQL Proxy的方式实现。客户端的请求都到MySQL Proxy上,如果客户端要执行查询数据的请求,则交给从服务器来处理;如果客户端要对数据进行增、删、改的操作,则交给主服务器来处理;

MySQL Proxy相关的工具有很多,有自带的mysql-proxy插件,也有MyCat等中间件,今天和小伙伴分享通过MaxScale来实现的读写分离,不管使用那种方式,个人觉得如果对于脏数据零容忍的应用更多的应该在硬件资源上面考虑,并且大多数的读写分离解决方案都需要监控延迟复制来决策读写的分配。做的不好,总感觉有点不靠谱...

二、配置主从集群的读写分离


 

MariaDB MaxScale是MariaDB企业服务器、MariaDB ColumnStore和MariaDB Xpand的高级数据库代理,为它们提供企业高可用性、可伸缩性、安全和集成服务,同时抽象出底层数据库基础设施,以简化应用程序开发和数据库管理。

官方地址:https://mariadb.com/

读写分离工作原理

由 MaxScale面向客户端提供服务,收到SQL写请求时,交给master 服务器处理,收到SQL读请求时,交给slave服务器处理,这里我们已经搭建好一个主从结构的MySQL集群,关于集群搭建小伙伴可以看我之前的文章,有详细教程,所以这里只需要安装MaxScale,然后配置启动测试就OK


 

 

安装 MaxScale

 

可以参考官方文档:https://mariadb.com/docs/deploy/topologies/primary-replica/enterprise-server-10-6/install-mxs/

安装包下载: https://downloads.mariadb.com/files/MaxScale

需要的依赖包下载:https://rpmfind.NET/linux/rpm2html/search.php

依赖安装┌──[root@vms152.liruilongs.github.io]-[~]└─$yum -y install libatomic软件包 libatomic-4.8.5-44.el7.x86_64 已安装并且是最新版本无须任何处理┌──[root@vms152.liruilongs.github.io]-[~]└─$wget https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/trousers-0.3.14-2.el7.x86_64.rpm--2022-09-29 20:13:22-- https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/trousers-0.3.14-2.el7.x86_64.rpm┌──[root@vms152.liruilongs.github.io]-[~]└─$rpm -ivh trousers-0.3.14-2.el7.x86_64.rpm准备中... ################################# [100%]正在升级/安装...1:trousers-0.3.14-2.el7 ################################# [100%]┌──[root@vms152.liruilongs.github.io]-[~]└─$┌──[root@vms152.liruilongs.github.io]-[~]└─$wget https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/nettle-2.7.1-8.el7.x86_64.rpm--2022-09-29 20:14:52-- https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/nettle-2.7.1-8.el7.x86_64.rpm┌──[root@vms152.liruilongs.github.io]-[~]└─$rpm -ivh nettle-2.7.1-8.el7.x86_64.rpm准备中... ################################# [100%]正在升级/安装...1:nettle-2.7.1-8.el7 ################################# [100%]┌──[root@vms152.liruilongs.github.io]-[~]└─$┌──[root@vms152.liruilongs.github.io]-[~]└─$wget https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/gnutls-3.3.29-9.el7_6.x86_64.rpm--2022-09-29 20:15:50-- https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/gnutls-3.3.29-9.el7_6.x86_64.rpm┌──[root@vms152.liruilongs.github.io]-[~]└─$rpm -ivh gnutls-3.3.29-9.el7_6.x86_64.rpm准备中... ################################# [100%]正在升级/安装...1:gnutls-3.3.29-9.el7_6 ################################# [100%]┌──[root@vms152.liruilongs.github.io]-[~]└─$

安装完依赖包安装 maxscale

安装 MaxScale┌──[root@vms152.liruilongs.github.io]-[~]└─$rpm -ivh maxscale-2.5.21-1.rhel.7.x86_64.rpm警告:maxscale-2.5.21-1.rhel.7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID e3c94f49: NOKEY准备中... ################################# [100%]正在升级/安装...1:maxscale-2.5.21-1.rhel.7 ################################# [100%]Created symlink from /etc/systemd/system/multi-user.target.wants/maxscale.service to /usr/lib/systemd/system/maxscale.service.┌──[root@vms152.liruilongs.github.io]-[~]└─$配置 MaxScale┌──[root@vms152.liruilongs.github.io]-[~]└─$cp /etc/maxscale.cnf /etc/maxscale.cnf.bak┌──[root@vms152.liruilongs.github.io]-[~]└─$vim /etc/maxscale.cnf

备份配置文件,然后修改

┌──[root@vms152.liruilongs.github.io]-[~]└─$cat /etc/maxscale.cnf# MaxScale documentation:# https://mariadb.com/kb/en/mariadb-maxscale-25/# Global parameters# Complete list of configuration options:# https://mariadb.com/kb/en/mariadb-maxscale-25-mariadb-maxscale-configuration-guide/[maxscale]# 定义线程的个数,一个线程对应一个CPU核心数,线程数量要小于等于CPU核心数threads=auto# Server definitions# Set the address of the server to the network# address of a MariaDB server.# 指定要代理的数据库服务器信息[server1]type=serveraddress=192.168.26.153 #需要数据库服务器IPport=3306protocol=MariaDBBackend[server2]type=serveraddress=192.168.26.154port=3306protocol=MariaDBBackend# Monitor for the servers# This will keep MaxScale aware of the state of the servers.# MariaDB Monitor documentation:# https://mariadb.com/kb/en/maxscale-25-monitors/# 定义要监控的数据库服务器[MariaDB-Monitor]type=monitormodule=mariadbmonservers=server1,server2#指定监控用户maxscalemon,用于登录后端服务器,检查服务器的运行状态和主从状态user=maxscalemonpassword=liruilongmonitor_interval=10000# Service definitions# Service Definition for a read-only service and# a read/write splitting service.# ReadConnRoute documentation:# https://mariadb.com/kb/en/mariadb-maxscale-25-readconnroute/#定义只读服务器配置,需要注释掉,我们配置的是读写分离#[Read-Only-Service]#type=service#router=readconnroute#servers=server1#user=myuser#password=mypwd#router_options=slave# ReadWriteSplit documentation:# https://mariadb.com/kb/en/mariadb-maxscale-25-readwritesplit/#定义读写分离服务器配置[Read-Write-Service]type=servicerouter=readwritesplitservers=server1,server2 ##指定做读写分离服务的数据库服务器user=maxscaleroutepassword=liruilong##客户端通过用户名和密码向数据库发送请求,先访问maxscale代理服务器,再由代理服务器将请求转发##给后端数据库服务器;maxscale代理服务器是通过路由用户连接后端服务器,检测客户端的用户名和密码在##后端数据库中是否存在# Listener definitions for the services# These listeners represent the ports the# services will listen on.#[Read-Only-Listener]#type=listener#service=Read-Only-Service#protocol=MariaDBClient#port=4008# 定义读写分离服务使用的端口号[Read-Write-Listener]type=listenerservice=Read-Write-Serviceprotocol=MariaDBClientport=4006┌──[root@vms152.liruilongs.github.io]-[~]└─$创建监控用户和路由用户

创建监控用户maxscalemon,用于登录后端服务器,检查服务器的状态

grant replication slave,replication client on *.* to maxscalemon@"%" identified by "liruilong";

 

MariaDB [(none)]> grant replication slave,replication client on *.* to maxscalemon@"%" identified by "liruilong";Query OK, 0 rows affected (0.00 sec)

 

创建路由用户maxscalerouter,检测客户端的用户名和密码在后端数据库中是否存在

 

MariaDB [(none)]> grant select on mysql.* to maxscaleroute@"%" identified by "liruilong";Query OK, 0 rows affected (0.00 sec)

 

在mysql库的user表中,查看监控用户和路由用户

MariaDB [(none)]> select user,host from mysql.user;| user | host || maxscalemon | % || maxscaleroute | % || repluser | % || root | 127.0.0.1 || root | ::1 || root | localhost || root | vms153.liruilongs.github.io |7 rows in set (0.00 sec)MariaDB [(none)]>

在154从库的mysql库的user表中,查看同步过去监控用户和路由用户

┌──[root@vms154.liruilongs.github.io]-[~]└─$mysql -uroot -pliruilong -e'select user,host from mysql.user;'| user | host || maxscalemon | % || maxscaleroute | % || tom | % || root | 127.0.0.1 || root | ::1 || root | localhost || root | vms154.liruilongs.github.io |┌──[root@vms154.liruilongs.github.io]-[~]└─$

启动MaxScale服务

┌──[root@vms152.liruilongs.github.io]-[~]└─$maxscale -f /etc/maxscale.cnf -U maxscale┌──[root@vms152.liruilongs.github.io]-[~]└─$netstat -ntulp | grep maxscaletcp 0 0 127.0.0.1:8989 0.0.0.0:* LISTEN 1169/maxscaletcp6 0 0 :::4006 :::* LISTEN 1169/maxscale┌──[root@vms152.liruilongs.github.io]-[~]└─$ps -C maxscalePID TTY TIME CMD1169 ? 00:00:00 maxscale

查看日志,配置文件有个报错,解决下

┌──[root@vms152.liruilongs.github.io]-[~]└─$tail -n 5 /var/log/maxscale/maxscale.log2022-09-29 22:38:24 error : Monitor timed out when connecting to server server2[192.168.26.26.154:3306] : 'Unknown MySQL server host '192.168.26.26.154' (-2)'2022-09-29 22:38:24 notice : [mariadbmon] Selecting new master server.2022-09-29 22:38:24 warning: [mariadbmon] No running master candidates detected and no master currently set. Accepting a non-running server as master.2022-09-29 22:38:24 notice : [mariadbmon] Setting 'server1' as master.2022-09-29 22:39:15 warning: [mariadbmon] The current master server 'server1' is no longer valid because it has been down over 5 (failcount) monitor updates and it does not have any running slaves, but there is no valid alternative to swap to.┌──[root@vms152.liruilongs.github.io]-[~]└─$kill -9 1169┌──[root@vms152.liruilongs.github.io]-[~]└─$vim /etc/maxscale.cnf┌──[root@vms152.liruilongs.github.io]-[~]└─$maxscale -f /etc/maxscale.cnf -U maxscale┌──[root@vms152.liruilongs.github.io]-[~]└─$测试 MaxScale检查全局配置

使用maxctrl show maxscale命令查看全局maxscale配置。

┌──[root@vms152.liruilongs.github.io]-[~]└─$maxctrl show maxscale┌──────────────┬───────────────────────────────────────────────────────┐│ Version │ 2.5.21 │├──────────────┼───────────────────────────────────────────────────────┤│ Commit │ eb659891d7b507958f3c5f100d1ebe5f0f68afaf │├──────────────┼───────────────────────────────────────────────────────┤│ Started At │ Sun, 09 Oct 2022 14:50:14 GMT │├──────────────┼───────────────────────────────────────────────────────┤│ Activated At │ Sun, 09 Oct 2022 14:50:14 GMT │├──────────────┼───────────────────────────────────────────────────────┤│ Uptime │ 43 │├──────────────┼───────────────────────────────────────────────────────┤│ Parameters │ { ││ │ "admin_auth": true, ││ │ "admin_enabled": true, ││ │ "admin_gui": true, ││ │ "admin_host": "127.0.0.1", ││ │ "admin_log_auth_failures": true, ││ │ "writeq_low_water": 8192 ││ │ } │└──────────────┴───────────────────────────────────────────────────────┘┌──[root@vms152.liruilongs.github.io]-[~]└─$检查服务器配置

使用maxctrl list servers和maxctrl show server命令查看配置的服务器对象。

获取服务器对象的完整列表:

 

┌──[root@vms152.liruilongs.github.io]-[~]└─$maxctrl list servers┌─────────┬────────────────┬──────┬─────────────┬─────────────────┬──────┐│ Server │ Address │ Port │ Connections │ State │ GTID │├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┤│ server1 │ 192.168.26.153 │ 3306 │ 0 │ Master, Running │ │├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┤│ server2 │ 192.168.26.154 │ 3306 │ 0 │ Slave, Running │ │└─────────┴────────────────┴──────┴─────────────┴─────────────────┴──────┘┌──[root@vms152.liruilongs.github.io]-[~]└─$

 

对于每一个服务器对象,查看配置:

┌──[root@vms152.liruilongs.github.io]-[~]└─$maxctrl show server server1┌─────────────────────┬───────────────────────────────────────────┐│ Server │ server1 │├─────────────────────┼───────────────────────────────────────────┤│ Address │ 192.168.26.153 │├─────────────────────┼───────────────────────────────────────────┤│ Port │ 3306 │├─────────────────────┼───────────────────────────────────────────┤│ State │ Master, Running │├─────────────────────┼───────────────────────────────────────────┤│ Version │ 5.5.68-MariaDB │├─────────────────────┼───────────────────────────────────────────┤│ Last Event │ master_up │├─────────────────────┼───────────────────────────────────────────┤│ Triggered At │ Sun, 09 Oct 2022 14:50:14 GMT │├─────────────────────┼───────────────────────────────────────────┤│ Services │ Read-Write-Service │├─────────────────────┼───────────────────────────────────────────┤│ Monitors │ MariaDB-Monitor │├─────────────────────┼───────────────────────────────────────────┤│ Master ID │ -1 │├─────────────────────┼───────────────────────────────────────────┤│ Node ID │ 153 │├─────────────────────┼───────────────────────────────────────────┤│ Slave Server IDs │ │├─────────────────────┼───────────────────────────────────────────┤│ Current Connections │ 0 │├─────────────────────┼───────────────────────────────────────────┤│ Total Connections │ 0 │├─────────────────────┼───────────────────────────────────────────┤│ Max Connections │ 0 │├─────────────────────┼───────────────────────────────────────────┤│ Statistics │ { ││ │ "active_operations": 0, ││ │ "total_connections": 0 ││ │ } │├─────────────────────┼───────────────────────────────────────────┤│ Parameters │ { ││ │ "address": "192.168.26.153", ││ │ "ssl_version": "MAX" ││ │ } │└─────────────────────┴───────────────────────────────────────────┘┌──[root@vms152.liruilongs.github.io]-[~]└─$maxctrl show server server2检查监控配置

使用maxctrl list monitors和maxctrl show monitor命令查看已配置的监视器。

获取监控器的完整列表:

┌──[root@vms152.liruilongs.github.io]-[~]└─$maxctrl list monitors┌─────────────────┬─────────┬──────────────────┐│ Monitor │ State │ Servers │├─────────────────┼─────────┼──────────────────┤│ MariaDB-Monitor │ Running │ server1, server2 │└─────────────────┴─────────┴──────────────────┘

对于每个监控,查看监控配置:

┌──[root@vms152.liruilongs.github.io]-[~]└─$maxctrl show monitor MariaDB-Monitor┌─────────────────────┬──────────────────────────────────────────────────────┐│ Monitor │ MariaDB-Monitor │├─────────────────────┼──────────────────────────────────────────────────────┤│ Module │ mariadbmon │├─────────────────────┼──────────────────────────────────────────────────────┤│ State │ Running │├─────────────────────┼──────────────────────────────────────────────────────┤│ Servers │ server1 ││ │ server2 │├─────────────────────┼──────────────────────────────────────────────────────┤│ Parameters │ { ││ │ "assume_unique_hostnames": true, ││ │ "slave_conditions": "none", ││ │ "switchover_on_low_disk_space": false, ││ │ "switchover_timeout": 90, ││ │ "user": "maxscalemon", ││ │ "verify_master_failure": true ││ │ } │├─────────────────────┼──────────────────────────────────────────────────────┤│ Monitor Diagnostics │ { ││ │ "master": "server1", ││ │ "master_gtid_domain_id": null, ││ │ "primary": null, ││ │ "server_info": [ ││ │ { ││ │ "gtid_binlog_pos": null, ││ │ "last_sql_error": "", ││ │ "master_host": "192.168.26.153", ││ │ "master_port": 3306, ││ │ "master_server_id": 153, ││ │ "seconds_behind_master": 0, ││ │ "slave_io_running": "Yes", ││ │ "slave_sql_running": "Yes" ││ │ } ││ │ ] ││ │ } ││ │ ], ││ │ "state": "Idle" ││ │ } │└─────────────────────┴──────────────────────────────────────────────────────┘┌──[root@vms152.liruilongs.github.io]-[~]└─$检查服务配置

使用maxctrl list services和maxctrl show service命令查看已配置的路由服务。

获取路由服务的完整列表:

┌──[root@vms152.liruilongs.github.io]-[~]└─$maxctrl list services┌────────────────────┬────────────────┬─────────────┬───────────────────┬──────────────────┐│ Service │ Router │ Connections │ Total Connections │ Targets │├────────────────────┼────────────────┼─────────────┼───────────────────┼──────────────────┤│ Read-Write-Service │ readwritesplit │ 0 │ 0 │ server1, server2 │└────────────────────┴────────────────┴─────────────┴───────────────────┴──────────────────┘

查看详细信息

┌──[root@vms152.liruilongs.github.io]-[~]└─$maxctrl show services┌─────────────────────┬─────────────────────────────────────────────────────────────┐│ Service │ Read-Write-Service │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Router │ readwritesplit │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ State │ Started │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Started At │ Sun Oct 9 22:50:17 2022 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Current Connections │ 0 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Total Connections │ 0 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Max Connections │ 0 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Cluster │ │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Servers │ server1 ││ │ server2 │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Services │ │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Filters │ │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Parameters │ { ││ │ "auth_all_servers": false, ││ │ "causal_reads": "false", ││ │ "causal_reads_timeout": 10000, ││ │ "transaction_replay_max_size": "1073741824", ││ │ "transaction_replay_retry_on_deadlock": false, ││ │ "use_sql_variables_in": "all", ││ │ "user": "maxscaleroute", ││ │ "version_string": null ││ │ } │├─────────────────────┼─────────────────────────────────────────────────────────────┤│ Router Diagnostics │ { ││ │ "queries": 0, ││ │ "server_query_statistics": [] ││ │ } │└─────────────────────┴─────────────────────────────────────────────────────────────┘┌──[root@vms152.liruilongs.github.io]-[~]└─$检查读写分离

创建一个普通用户用于测试,并且授权数据库liruilong_db

┌──[root@vms153.liruilongs.github.io]-[~]└─$mysql -uroot -pliruilongWelcome to the MariaDB monitor. Commands end with ; or g.Your MariaDB connection id is 50Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.MariaDB [(none)]> grant all on liruilong_db.* to liruilong@"%" identified by "liruilong";Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> select user,host from mysql.user;| user | host || liruilong | % || maxscalemon | % || maxscaleroute | % || repluser | % || root | 127.0.0.1 || root | ::1 || root | localhost || root | vms153.liruilongs.github.io |8 rows in set (0.00 sec)MariaDB [(none)]>

登录测试

┌──[root@vms153.liruilongs.github.io]-[~]└─$mysql -uliruilong -pliruilongWelcome to the MariaDB monitor. Commands end with ; or g.Your MariaDB connection id is 51Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.MariaDB [(none)]> show grants;| Grants for liruilong@% || GRANT USAGE ON *.* TO 'liruilong'@'%' IDENTIFIED BY PASSWORD '*73CA7DD1B0BD11DCA665AB9C635C2188533331B3' || GRANT ALL PRIVILEGES ON `liruilong_db`.* TO 'liruilong'@'%' |2 rows in set (0.00 sec)MariaDB [(none)]>

从库查看是否同步

┌──[root@vms154.liruilongs.github.io]-[~]└─$mysql -uroot -pliruilong -e'select user,host from mysql.user;'| user | host || liruilong | % || maxscalemon | % || maxscaleroute | % || tom | % || root | 127.0.0.1 || root | ::1 || root | localhost || root | vms154.liruilongs.github.io |┌──[root@vms154.liruilongs.github.io]-[~]└─$

代理机器上查看路由端口

┌──[root@vms152.liruilongs.github.io]-[~]└─$ maxctrl list listeners┌─────────────────────┬──────┬──────┬─────────┬────────────────────┐│ Name │ Port │ Host │ State │ Service │├─────────────────────┼──────┼──────┼─────────┼────────────────────┤│ Read-Write-Listener │ 4006 │ :: │ Running │ Read-Write-Service │└─────────────────────┴──────┴──────┴─────────┴────────────────────┘┌──[root@vms152.liruilongs.github.io]-[~]└─$

客户机连接MaxScale代理,查到登录用户测试库等信息

┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'show grants'| Grants for liruilong@% || GRANT USAGE ON *.* TO 'liruilong'@'%' IDENTIFIED BY PASSWORD '*73CA7DD1B0BD11DCA665AB9C635C2188533331B3' || GRANT ALL PRIVILEGES ON `liruilong_db`.* TO 'liruilong'@'%' |┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'show databases;'| Database || information_schema || liruilong_db || test |┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;show tables;'| Tables_in_liruilong_db || user |

客户机连接MaxScale代理,通过代理插入一条数据

┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;insert into user values(2,now());'┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 |┌──[root@vms155.liruilongs.github.io]-[~]└─$

可以正常读取,下面在从(读)库插入一条数据

┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.154 -uliruilong -pliruilong -e'use liruilong_db;insert into user values(154,now());'┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.154 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 || 154 | 2022-09-30 13:32:18 |

主库没有数据,但是通过代理读到了数据,说明读是通过从读取。

┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.153 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 |┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 || 154 | 2022-09-30 13:32:18 |

通过代理插入数据,主从库数据同时存在,说明写是在主库,然后同步给从库

┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.152 -P4006 -uliruilong -pliruilong -e'use liruilong_db;insert into user values(152,now());'┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.153 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 || 152 | 2022-09-30 13:34:26 |┌──[root@vms155.liruilongs.github.io]-[~]└─$mysql -h192.168.26.154 -uliruilong -pliruilong -e'use liruilong_db;select * from user;'| id | create_date || 1 | 2022-09-29 00:22:36 || 1 | 2022-09-29 11:08:38 || 1 | 2022-09-29 13:43:09 || 1 | 2022-09-29 13:51:33 || 1 | 2022-09-29 13:54:41 || 2 | 2022-09-30 13:29:59 || 154 | 2022-09-30 13:32:18 || 152 | 2022-09-30 13:34:26 |┌──[root@vms155.liruilongs.github.io]-[~]└─$博文参考

《高性能Mysql》第三版(High Performance MySQL,Third Edition)

https://mariadb.com/docs/deploy/topologies/primary-replica/enterprise-server-10-6/

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>