内容简介:本文主要介绍MariaDB中间件与代理解决方案,包括中间件的基本概念、代理的基本概念、常见的中间件与代理等内容。通过实际案例讲解MaxScale、ProxySQL和HAProxy的配置与使用,帮助读者掌握MariaDB中间件与代理的技能。风哥教程参考MariaDB官方文档MaxScale等相关内容。
Part01-基础概念与理论知识
1.1 中间件的基本概念
中间件(Middleware)是位于应用程序和数据库之间的软件层,用于提供额外的功能和服务。MariaDB中间件的主要作用包括:
- 负载均衡:将请求分发到多个数据库节点
- 读写分离:将读请求和写请求分发到不同的节点
- 高可用性:当节点故障时自动切换到健康节点
- 连接池管理:管理数据库连接,提高性能
- 监控与管理:监控数据库节点的状态和性能
1.2 代理的基本概念
代理(Proxy)是一种网络服务,用于转发客户端请求到后端服务器。MariaDB代理的主要作用包括:
- 请求转发:将客户端请求转发到后端数据库服务器
- 负载均衡:将请求分发到多个后端服务器
- 高可用性:当后端服务器故障时自动切换到健康服务器
- 安全控制:提供访问控制和安全防护
1.3 常见的中间件与代理
MariaDB常见的中间件与代理包括:
- MaxScale:MariaDB官方提供的中间件,支持负载均衡、读写分离、高可用性等功能
- ProxySQL:开源的MySQL/MariaDB代理,支持负载均衡、读写分离、连接池等功能
- HAProxy:高性能的TCP/HTTP代理,支持负载均衡、高可用性等功能
- MySQL Router:MySQL官方提供的轻量级代理,支持高可用性和读写分离
更多视频教程www.fgedu.net.cn
Part02-生产环境规划与建议
2.1 中间件规划
中间件规划建议:
- 选择合适的中间件:根据业务需求和技术栈选择合适的中间件
- 部署架构:采用高可用架构,避免中间件成为单点故障
- 配置参数:根据业务需求和硬件配置调整中间件参数
- 监控方案:设置中间件监控,及时发现问题
- 故障演练:定期进行故障演练,提高故障处理能力
2.2 代理规划
代理规划建议:
- 选择合适的代理:根据业务需求和技术栈选择合适的代理
- 部署架构:采用高可用架构,避免代理成为单点故障
- 负载均衡策略:选择合适的负载均衡策略,如轮询、最少连接等
- 健康检查:配置健康检查,及时发现后端服务器故障
- 安全配置:配置访问控制和安全防护
2.3 性能优化建议
性能优化建议:
- 连接池优化:调整连接池大小,提高连接利用率
- 负载均衡策略:选择合适的负载均衡策略,提高性能
- 缓存配置:启用查询缓存,提高查询性能
- 网络优化:使用高速网络,减少网络延迟
- 硬件配置:使用高性能硬件,提高中间件和代理的性能
学习交流加群风哥微信: itpux-com
Part03-生产环境项目实施方案
3.1 MaxScale配置
更多学习教程公众号风哥教程itpux_com
# MaxScale配置
# 1. 安装MaxScale
# 在CentOS/RHEL上
yum install maxscale
# 2. 配置MaxScale
vi /etc/maxscale.cnf
[maxscale]
threads=auto
[server1]
type=server
address=192.168.1.100
port=3306
protocol=MariaDBBackend
[server2]
type=server
address=192.168.1.101
port=3306
protocol=MariaDBBackend
[server3]
type=server
address=192.168.1.102
port=3306
protocol=MariaDBBackend
[Galera Monitor]
type=monitor
module=galeramon
servers=server1,server2,server3
user=maxscale
password=maxscale_password
monitor_interval=1000
[Read-Write Split Router]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxscale
password=maxscale_password
max_slave_connections=100%
[MaxAdmin Service]
type=service
router=cli
[Read-Write Split Listener]
type=listener
service=Read-Write Split Router
protocol=MariaDBClient
port=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
# 3. 启动MaxScale
systemctl start maxscale
# 4. 检查MaxScale状态
maxadmin list servers
# 1. 安装MaxScale
# 在CentOS/RHEL上
yum install maxscale
# 2. 配置MaxScale
vi /etc/maxscale.cnf
[maxscale]
threads=auto
[server1]
type=server
address=192.168.1.100
port=3306
protocol=MariaDBBackend
[server2]
type=server
address=192.168.1.101
port=3306
protocol=MariaDBBackend
[server3]
type=server
address=192.168.1.102
port=3306
protocol=MariaDBBackend
[Galera Monitor]
type=monitor
module=galeramon
servers=server1,server2,server3
user=maxscale
password=maxscale_password
monitor_interval=1000
[Read-Write Split Router]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxscale
password=maxscale_password
max_slave_connections=100%
[MaxAdmin Service]
type=service
router=cli
[Read-Write Split Listener]
type=listener
service=Read-Write Split Router
protocol=MariaDBClient
port=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
# 3. 启动MaxScale
systemctl start maxscale
# 4. 检查MaxScale状态
maxadmin list servers
3.2 ProxySQL配置
# ProxySQL配置
# 1. 安装ProxySQL
# 在CentOS/RHEL上
yum install proxysql
# 2. 启动ProxySQL
systemctl start proxysql
# 3. 连接ProxySQL管理接口
mysql -u admin -padmin -h 127.0.0.1 -P 6032
# 4. 配置后端服务器
INSERT INTO mysql_servers(hostgroup_id, hostname, port, weight, max_connections) VALUES
(1, ‘192.168.1.100’, 3306, 1, 1000),
(1, ‘192.168.1.101’, 3306, 1, 1000),
(2, ‘192.168.1.100’, 3306, 1, 1000),
(2, ‘192.168.1.101’, 3306, 1, 1000);
# 5. 配置用户
INSERT INTO mysql_users(username, password, default_hostgroup, active, max_connections) VALUES
(‘appuser’, ‘apppassword’, 1, 1, 1000);
# 6. 配置查询规则
INSERT INTO mysql_query_rules(rule_id, active, match_pattern, destination_hostgroup, apply) VALUES
(1, 1, ‘^SELECT.*FOR UPDATE$’, 1, 1),
(2, 1, ‘^SELECT’, 2, 1);
# 7. 加载配置到运行时
LOAD MYSQL SERVERS TO RUNTIME;
LOAD MYSQL USERS TO RUNTIME;
LOAD MYSQL QUERY RULES TO RUNTIME;
# 8. 保存配置到磁盘
SAVE MYSQL SERVERS TO DISK;
SAVE MYSQL USERS TO DISK;
SAVE MYSQL QUERY RULES TO DISK;
# 9. 检查ProxySQL状态
mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e “SELECT * FROM mysql_servers;
”
# 1. 安装ProxySQL
# 在CentOS/RHEL上
yum install proxysql
# 2. 启动ProxySQL
systemctl start proxysql
# 3. 连接ProxySQL管理接口
mysql -u admin -padmin -h 127.0.0.1 -P 6032
# 4. 配置后端服务器
INSERT INTO mysql_servers(hostgroup_id, hostname, port, weight, max_connections) VALUES
(1, ‘192.168.1.100’, 3306, 1, 1000),
(1, ‘192.168.1.101’, 3306, 1, 1000),
(2, ‘192.168.1.100’, 3306, 1, 1000),
(2, ‘192.168.1.101’, 3306, 1, 1000);
# 5. 配置用户
INSERT INTO mysql_users(username, password, default_hostgroup, active, max_connections) VALUES
(‘appuser’, ‘apppassword’, 1, 1, 1000);
# 6. 配置查询规则
INSERT INTO mysql_query_rules(rule_id, active, match_pattern, destination_hostgroup, apply) VALUES
(1, 1, ‘^SELECT.*FOR UPDATE$’, 1, 1),
(2, 1, ‘^SELECT’, 2, 1);
# 7. 加载配置到运行时
LOAD MYSQL SERVERS TO RUNTIME;
LOAD MYSQL USERS TO RUNTIME;
LOAD MYSQL QUERY RULES TO RUNTIME;
# 8. 保存配置到磁盘
SAVE MYSQL SERVERS TO DISK;
SAVE MYSQL USERS TO DISK;
SAVE MYSQL QUERY RULES TO DISK;
# 9. 检查ProxySQL状态
mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e “SELECT * FROM mysql_servers;
”
3.3 HAProxy配置
# HAProxy配置
# 1. 安装HAProxy
# 在CentOS/RHEL上
yum install haproxy
# 2. 配置HAProxy
vi /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
option tcplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend mariadb
bind *:3306
default_backend mariadb_servers
backend mariadb_servers
balance roundrobin
server db1 192.168.1.100:3306 check
server db2 192.168.1.101:3306 check
server db3 192.168.1.102:3306 check
# 3. 启动HAProxy
systemctl start haproxy
# 4. 检查HAProxy状态
systemctl status haproxy
# 1. 安装HAProxy
# 在CentOS/RHEL上
yum install haproxy
# 2. 配置HAProxy
vi /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
option tcplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend mariadb
bind *:3306
default_backend mariadb_servers
backend mariadb_servers
balance roundrobin
server db1 192.168.1.100:3306 check
server db2 192.168.1.101:3306 check
server db3 192.168.1.102:3306 check
# 3. 启动HAProxy
systemctl start haproxy
# 4. 检查HAProxy状态
systemctl status haproxy
学习交流加群风哥QQ113257174
Part04-生产案例与实战讲解
4.1 MaxScale配置案例
场景描述:配置MaxScale实现MariaDB集群的负载均衡和读写分离。
# MaxScale配置
# 1. 安装MaxScale
[root@maxscale ~]# yum install maxscale
# 2. 配置MaxScale
[root@maxscale ~]# vi /etc/maxscale.cnf
[maxscale]
threads=auto
[server1]
type=server
address=192.168.1.100
port=3306
protocol=MariaDBBackend
[server2]
type=server
address=192.168.1.101
port=3306
protocol=MariaDBBackend
[server3]
type=server
address=192.168.1.102
port=3306
protocol=MariaDBBackend
[Galera Monitor]
type=monitor
module=galeramon
servers=server1,server2,server3
user=maxscale
password=maxscale_password
monitor_interval=1000
[Read-Write Split Router]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxscale
password=maxscale_password
max_slave_connections=100%
[MaxAdmin Service]
type=service
router=cli
[Read-Write Split Listener]
type=listener
service=Read-Write Split Router
protocol=MariaDBClient
port=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
# 3. 创建MaxScale用户
[root@db1 ~]# mysql -u root -p
MariaDB [(none)]> CREATE USER ‘maxscale’@’%’ IDENTIFIED BY ‘maxscale_password’;
MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ‘maxscale’@’%’;
MariaDB [(none)]> FLUSH PRIVILEGES;
# 4. 启动MaxScale
[root@maxscale ~]# systemctl start maxscale
# 5. 检查MaxScale状态
[root@maxscale ~]# maxadmin list servers
Servers.
——————-+—————–+——-+————-+——————–
Server | Address | Port | Connections | Status
——————-+—————–+——-+————-+——————–
server1 | 192.168.1.100 | 3306 | 0 | Master, Synced
server2 | 192.168.1.101 | 3306 | 0 | Slave, Synced
server3 | 192.168.1.102 | 3306 | 0 | Slave, Synced
——————-+—————–+——-+————-+——————–
# 1. 安装MaxScale
[root@maxscale ~]# yum install maxscale
# 2. 配置MaxScale
[root@maxscale ~]# vi /etc/maxscale.cnf
[maxscale]
threads=auto
[server1]
type=server
address=192.168.1.100
port=3306
protocol=MariaDBBackend
[server2]
type=server
address=192.168.1.101
port=3306
protocol=MariaDBBackend
[server3]
type=server
address=192.168.1.102
port=3306
protocol=MariaDBBackend
[Galera Monitor]
type=monitor
module=galeramon
servers=server1,server2,server3
user=maxscale
password=maxscale_password
monitor_interval=1000
[Read-Write Split Router]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxscale
password=maxscale_password
max_slave_connections=100%
[MaxAdmin Service]
type=service
router=cli
[Read-Write Split Listener]
type=listener
service=Read-Write Split Router
protocol=MariaDBClient
port=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
# 3. 创建MaxScale用户
[root@db1 ~]# mysql -u root -p
MariaDB [(none)]> CREATE USER ‘maxscale’@’%’ IDENTIFIED BY ‘maxscale_password’;
MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ‘maxscale’@’%’;
MariaDB [(none)]> FLUSH PRIVILEGES;
# 4. 启动MaxScale
[root@maxscale ~]# systemctl start maxscale
# 5. 检查MaxScale状态
[root@maxscale ~]# maxadmin list servers
Servers.
——————-+—————–+——-+————-+——————–
Server | Address | Port | Connections | Status
——————-+—————–+——-+————-+——————–
server1 | 192.168.1.100 | 3306 | 0 | Master, Synced
server2 | 192.168.1.101 | 3306 | 0 | Slave, Synced
server3 | 192.168.1.102 | 3306 | 0 | Slave, Synced
——————-+—————–+——-+————-+——————–
执行结果:
# MaxScale配置成功
[root@maxscale ~]# maxadmin list servers
Servers.
——————-+—————–+——-+————-+——————–
Server | Address | Port | Connections | Status
——————-+—————–+——-+————-+——————–
server1 | 192.168.1.100 | 3306 | 0 | Master, Synced
server2 | 192.168.1.101 | 3306 | 0 | Slave, Synced
server3 | 192.168.1.102 | 3306 | 0 | Slave, Synced
——————-+—————–+——-+————-+——————–
[root@maxscale ~]# maxadmin list servers
Servers.
——————-+—————–+——-+————-+——————–
Server | Address | Port | Connections | Status
——————-+—————–+——-+————-+——————–
server1 | 192.168.1.100 | 3306 | 0 | Master, Synced
server2 | 192.168.1.101 | 3306 | 0 | Slave, Synced
server3 | 192.168.1.102 | 3306 | 0 | Slave, Synced
——————-+—————–+——-+————-+——————–
4.2 ProxySQL配置案例
场景描述:配置ProxySQL实现MariaDB集群的负载均衡和读写分离。
# ProxySQL配置
# 1. 安装ProxySQL
[root@proxysql ~]# yum install proxysql
# 2. 启动ProxySQL
[root@proxysql ~]# systemctl start proxysql
# 3. 连接ProxySQL管理接口
[root@proxysql ~]# mysql -u admin -padmin -h 127.0.0.1 -P 6032
# 4. 配置后端服务器
MariaDB [(none)]> INSERT INTO mysql_servers(hostgroup_id, hostname, port, weight, max_connections) VALUES
-> (1, ‘192.168.1.100’, 3306, 1, 1000),
-> (1, ‘192.168.1.101’, 3306, 1, 1000),
-> (2, ‘192.168.1.100’, 3306, 1, 1000),
-> (2, ‘192.168.1.101’, 3306, 1, 1000);
# 5. 配置用户
MariaDB [(none)]> INSERT INTO mysql_users(username, password, default_hostgroup, active, max_connections) VALUES
-> (‘appuser’, ‘apppassword’, 1, 1, 1000);
# 6. 配置查询规则
MariaDB [(none)]> INSERT INTO mysql_query_rules(rule_id, active, match_pattern, destination_hostgroup, apply) VALUES
-> (1, 1, ‘^SELECT.*FOR UPDATE$’, 1, 1),
-> (2, 1, ‘^SELECT’, 2, 1);
# 7. 加载配置到运行时
MariaDB [(none)]> LOAD MYSQL SERVERS TO RUNTIME;
MariaDB [(none)]> LOAD MYSQL USERS TO RUNTIME;
MariaDB [(none)]> LOAD MYSQL QUERY RULES TO RUNTIME;
# 8. 保存配置到磁盘
MariaDB [(none)]> SAVE MYSQL SERVERS TO DISK;
MariaDB [(none)]> SAVE MYSQL USERS TO DISK;
MariaDB [(none)]> SAVE MYSQL QUERY RULES TO DISK;
# 9. 检查ProxySQL状态
[root@proxysql ~]# mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e “SELECT * FROM mysql_servers;
”
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
| 1 | 192.168.1.100 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 1 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 2 | 192.168.1.100 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 2 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
# 1. 安装ProxySQL
[root@proxysql ~]# yum install proxysql
# 2. 启动ProxySQL
[root@proxysql ~]# systemctl start proxysql
# 3. 连接ProxySQL管理接口
[root@proxysql ~]# mysql -u admin -padmin -h 127.0.0.1 -P 6032
# 4. 配置后端服务器
MariaDB [(none)]> INSERT INTO mysql_servers(hostgroup_id, hostname, port, weight, max_connections) VALUES
-> (1, ‘192.168.1.100’, 3306, 1, 1000),
-> (1, ‘192.168.1.101’, 3306, 1, 1000),
-> (2, ‘192.168.1.100’, 3306, 1, 1000),
-> (2, ‘192.168.1.101’, 3306, 1, 1000);
# 5. 配置用户
MariaDB [(none)]> INSERT INTO mysql_users(username, password, default_hostgroup, active, max_connections) VALUES
-> (‘appuser’, ‘apppassword’, 1, 1, 1000);
# 6. 配置查询规则
MariaDB [(none)]> INSERT INTO mysql_query_rules(rule_id, active, match_pattern, destination_hostgroup, apply) VALUES
-> (1, 1, ‘^SELECT.*FOR UPDATE$’, 1, 1),
-> (2, 1, ‘^SELECT’, 2, 1);
# 7. 加载配置到运行时
MariaDB [(none)]> LOAD MYSQL SERVERS TO RUNTIME;
MariaDB [(none)]> LOAD MYSQL USERS TO RUNTIME;
MariaDB [(none)]> LOAD MYSQL QUERY RULES TO RUNTIME;
# 8. 保存配置到磁盘
MariaDB [(none)]> SAVE MYSQL SERVERS TO DISK;
MariaDB [(none)]> SAVE MYSQL USERS TO DISK;
MariaDB [(none)]> SAVE MYSQL QUERY RULES TO DISK;
# 9. 检查ProxySQL状态
[root@proxysql ~]# mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e “SELECT * FROM mysql_servers;
”
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
| 1 | 192.168.1.100 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 1 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 2 | 192.168.1.100 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 2 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
执行结果:
# ProxySQL配置成功
[root@proxysql ~]# mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e “SELECT * FROM mysql_servers;
”
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
| 1 | 192.168.1.100 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 1 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 2 | 192.168.1.100 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 2 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
[root@proxysql ~]# mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e “SELECT * FROM mysql_servers;
”
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
| 1 | 192.168.1.100 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 1 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 2 | 192.168.1.100 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 2 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+————–+—————-+——+——–+——–+————-+—————–+———————+———+—————-+————————+
4.3 HAProxy配置案例
场景描述:配置HAProxy实现MariaDB集群的负载均衡。
# HAProxy配置
# 1. 安装HAProxy
[root@haproxy ~]# yum install haproxy
# 2. 配置HAProxy
[root@haproxy ~]# vi /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
option tcplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend mariadb
bind *:3306
default_backend mariadb_servers
backend mariadb_servers
balance roundrobin
server db1 192.168.1.100:3306 check
server db2 192.168.1.101:3306 check
server db3 192.168.1.102:3306 check
# 3. 启动HAProxy
[root@haproxy ~]# systemctl start haproxy
# 4. 检查HAProxy状态
[root@haproxy ~]# systemctl status haproxy
● haproxy.service – HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service;
enabled;
vendor preset: disabled)
Active: active (running) since Sun 2023-01-01 10:00:00 CST;
1h ago
Main PID: 12345 (haproxy)
Status: “haproxy is running [WARNING: config : ‘option forwardfor’ ignored for frontend ‘mariadb’ since it requires HTTP mode]”
CGroup: /system.slice/haproxy.service
└─12345 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
# 5. 测试连接
[root@client ~]# mysql -u appuser -papppassword -h 192.168.1.200 -P 3306
Welcome to the MariaDB monitor. Commands end with;
or \g.
Your MariaDB connection id is 123
Server version: 10.5.12-MariaDB MariaDB Server
Copyright (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)]>
# 1. 安装HAProxy
[root@haproxy ~]# yum install haproxy
# 2. 配置HAProxy
[root@haproxy ~]# vi /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
option tcplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend mariadb
bind *:3306
default_backend mariadb_servers
backend mariadb_servers
balance roundrobin
server db1 192.168.1.100:3306 check
server db2 192.168.1.101:3306 check
server db3 192.168.1.102:3306 check
# 3. 启动HAProxy
[root@haproxy ~]# systemctl start haproxy
# 4. 检查HAProxy状态
[root@haproxy ~]# systemctl status haproxy
● haproxy.service – HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service;
enabled;
vendor preset: disabled)
Active: active (running) since Sun 2023-01-01 10:00:00 CST;
1h ago
Main PID: 12345 (haproxy)
Status: “haproxy is running [WARNING: config : ‘option forwardfor’ ignored for frontend ‘mariadb’ since it requires HTTP mode]”
CGroup: /system.slice/haproxy.service
└─12345 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
# 5. 测试连接
[root@client ~]# mysql -u appuser -papppassword -h 192.168.1.200 -P 3306
Welcome to the MariaDB monitor. Commands end with;
or \g.
Your MariaDB connection id is 123
Server version: 10.5.12-MariaDB MariaDB Server
Copyright (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)]>
执行结果:
# HAProxy配置成功
[root@client ~]# mysql -u appuser -papppassword -h 192.168.1.200 -P 3306
Welcome to the MariaDB monitor. Commands end with;
or \g.
Your MariaDB connection id is 123
Server version: 10.5.12-MariaDB MariaDB Server
Copyright (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)]>
[root@client ~]# mysql -u appuser -papppassword -h 192.168.1.200 -P 3306
Welcome to the MariaDB monitor. Commands end with;
or \g.
Your MariaDB connection id is 123
Server version: 10.5.12-MariaDB MariaDB Server
Copyright (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)]>
风哥提示:安全开发是防止SQL注入的第一道防线
Part05-风哥经验总结与分享
5.1 中间件与代理最佳实践
风哥提示:在配置和管理MariaDB中间件与代理时,应遵循最佳实践,确保系统的稳定运行。
- 选择合适的中间件:根据业务需求和技术栈选择合适的中间件
- 部署高可用架构:避免中间件成为单点故障
- 配置合理的参数:根据业务需求和硬件配置调整参数
- 定期监控:定期检查中间件和代理的状态和性能
- 故障演练:定期进行故障演练,提高故障处理能力
5.2 性能优化技巧
- 连接池优化:调整连接池大小,提高连接利用率
- 负载均衡策略:选择合适的负载均衡策略,提高性能
- 缓存配置:启用查询缓存,提高查询性能
- 网络优化:使用高速网络,减少网络延迟
- 硬件配置:使用高性能硬件,提高中间件和代理的性能
5.3 常见问题与解决方案
- 连接失败:检查网络连接,确认中间件和后端服务器状态
- 性能下降:检查中间件配置,优化参数,检查后端服务器负载
- 故障切换失败:检查健康检查配置,确保后端服务器状态正确
- 配置错误:检查配置文件,确保配置正确
- 安全问题:配置访问控制,使用SSL加密连接
# 中间件与代理配置示例
— MaxScale配置
[maxscale]
threads=auto
[server1]
type=server
address=192.168.1.100
port=3306
protocol=MariaDBBackend
[Read-Write Split Router]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxscale
password=maxscale_password
— ProxySQL配置
INSERT INTO mysql_servers(hostgroup_id, hostname, port, weight, max_connections) VALUES
(1, ‘192.168.1.100’, 3306, 1, 1000),
(1, ‘192.168.1.101’, 3306, 1, 1000),
(2, ‘192.168.1.100’, 3306, 1, 1000),
(2, ‘192.168.1.101’, 3306, 1, 1000);
— HAProxy配置
backend mariadb_servers
balance roundrobin
server db1 192.168.1.100:3306 check
server db2 192.168.1.101:3306 check
server db3 192.168.1.102:3306 check
— MaxScale配置
[maxscale]
threads=auto
[server1]
type=server
address=192.168.1.100
port=3306
protocol=MariaDBBackend
[Read-Write Split Router]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxscale
password=maxscale_password
— ProxySQL配置
INSERT INTO mysql_servers(hostgroup_id, hostname, port, weight, max_connections) VALUES
(1, ‘192.168.1.100’, 3306, 1, 1000),
(1, ‘192.168.1.101’, 3306, 1, 1000),
(2, ‘192.168.1.100’, 3306, 1, 1000),
(2, ‘192.168.1.101’, 3306, 1, 1000);
— HAProxy配置
backend mariadb_servers
balance roundrobin
server db1 192.168.1.100:3306 check
server db2 192.168.1.101:3306 check
server db3 192.168.1.102:3306 check
通过以上措施,可以有效配置和管理MariaDB中间件与代理,提高系统的性能和可用性。
from MariaDB视频:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
