相关准备
Mysql 主从
192.168.11.120 33001 master
192.168.11.120 33002 slave
192.168.11.120 33004 slave
192.168.11.120 MaxScale
安装 MaxScale
参考:https://mariadb.com/kb/en/mariadb-package-repository-setup-and-usage/
apt-get update
apt-get install maxscale
MaxScale 创建用户
创建监控用户
##########################################################################
mysql> create user maxscale@'%' identified by "123456";
mysql> grant replication slave, replication client on *.* to maxscale@'%';
创建路由用户
mysql> create user scaleroute@'%' identified by "123456";
mysql> grant select on mysql.* to scaleroute@'%';
#########################################################################
CREATE USER maxscale@'%' IDENTIFIED BY "123456";
GRANT replication slave, replication client ON *.* TO maxscale@'%';
GRANT SELECT ON mysql.* TO maxscale@'%';
GRANT ALL ON maxscale_schema.* TO maxscale@'%';
GRANT SHOW DATABASES ON *.* TO maxscale@'%';
在创建一个程序使用的 mysql 用户
Master 读写
slave 只读
grant select on *.* to "mysql_user"@"IP" identified by "Gwr0cfca43WoAF"
建议限定创建限定 IP 的连接用户 ; 如不想限定 IP 改为 % 即可
MaxScale 配置文件
# MaxScale documentation:
# https://mariadb.com/kb/en/mariadb-maxscale-24/
# Global parameters
#
# Complete list of configuration options:
# https://mariadb.com/kb/en/mariadb-maxscale-24-mariadb-maxscale-configuration-guide/
cat /etc/maxscale.cnf
- [maxscale]
- threads=auto
- maxlog=1 #将日志写入到maxscale的日志文件中
- #log_to_shm=0 #不将日志写入到共享缓存中,开启debug模式时可打开加快速度
- log_warning=1 #记录告警信息
- log_notice=1 #记录notice
- log_info=1 #记录info
- log_debug=0 #不打开debug模式
- # Server definitions
- # Set the address of the server to the network
- # address of a MariaDB server.
- #
- [server1]
- type=server
- address=192.168.11.120
- port=33001
- protocol=MariaDBBackend
- [server2]
- type=server
- address=192.168.11.120
- port=33002
- protocol=MariaDBBackend
- [server3]
- type=server
- address=192.168.11.120
- port=33004
- protocol=MariaDBBackend
- # Monitor for the servers
- #
- # This will keep MaxScale aware of the state of the servers.
- # MariaDB Monitor documentation:
- # https://mariadb.com/kb/en/mariadb-maxscale-24-mariadb-monitor/
- [MariaDB-Monitor]
- type=monitor
- module=mariadbmon
- servers=server1,server2,server3
- user=maxscale
- password=123456
- monitor_interval=2000
- # 检查复制延迟的情况
- detect_replication_lag=true
- # 当所有的slave都不可用时,select查询请求会转发到master。
- detect_stale_master=true
- # Service definitions
- #
- # Service Definition for a read-only service and
- # a read/write splitting service.
- #
- # ReadConnRoute documentation:
- # https://mariadb.com/kb/en/mariadb-maxscale-24-readconnroute/
- [Read-Only-Service]
- type=service
- router=readconnroute
- servers=server1,server2,server3
- user=maxscale
- password=123456
- router_options=slave
- # ReadWriteSplit documentation:
- # https://mariadb.com/kb/en/mariadb-maxscale-24-readwritesplit/
- [Read-Write-Service]
- type=service
- router=readwritesplit
- servers=server1,server2,server3
- user=maxscale
- password=123456
- ##所有的slave提供select查询服务
- max_slave_connections=100%
- ### slave超时5秒,就把请求转发到其他slave
- max_slave_replication_lag=5
- ###动态参数可以走全部的数据库 [all|master] 如果设置为master的话前台看到的中文是乱码,一般建议设置为all
- #use_sql_variables_in=all
- #weightby=serversize #权重设置
- ##enable_root_user=1 #允许root用户登录执行
- #auth_all_servers=true #
- #log_auth_warnings=true #身份验证失败和警告的日志记录,记录那些试图连接到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=listener
- service=Read-Write-Service
- protocol=MariaDBClient
- port=4006
- [MaxAdmin-Service]
- type=service
- router=cli
- [MaxAdmin-Listener]
- type=listener
- service=MaxAdmin-Service
- protocol=maxscaled
- socket=default
service maxscale start
报错 根据提示查找问题
正常通过 netstat -nltp | grep 4006 就可以看到已启动
[root@local-debain:~]# maxctrl
maxctrl list servers
Server | Address | Port | Connections | State | GTID |
---|---|---|---|---|---|
server1 | 192.168.11.120 | 33001 | 0 | Master, Running | 0-1000-732 |
server2 | 192.168.11.120 | 33002 | 0 | Slave, Running | |
server3 | 192.168.11.120 | 33004 | 0 | Slave, Running | 0-1000-732 |
OR
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 192.168.11.120 | 33001 | 0 | Master, Running
server2 | 192.168.11.120 | 33002 | 0 | Slave, Running
server3 | 192.168.11.120 | 33004 | 0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------
Server 0x560389fc8ab0 (server1)
Server: 192.168.11.120
Status: Master, Running
Protocol: MariaDBBackend
Port: 33001
Server Version: 10.4.12-MariaDB-1:10.4.12+maria~stretch-log
Node Id: 1000
Master Id: -1
Last event: server_up
Triggered at: Thu, 14 May 2020 02:52:52 GMT
Server Parameters:
address 192.168.11.120
extra_port 0
persistmaxtime 0
persistpoolmax 0
port 33001
protocol MariaDBBackend
proxy_protocol false
rank primary
ssl false
ssl_cert_verify_depth 9
ssl_verify_peer_certificate false
ssl_version MAX
type server
Number of connections: 2
Current no. of conns: 0
Current no. of operations: 0
Number of routed packets: 1498
Adaptive avg. select time: 1.91765ms
Server 0x560389fc74a0 (server2)
Server: 192.168.11.120
Status: Slave, Running
Protocol: MariaDBBackend
Port: 33002
Server Version: 5.7.29-log
Node Id: 1023
Master Id: 1000
Last event: server_up
Triggered at: Thu, 14 May 2020 02:52:52 GMT
Slave delay: 0
Server Parameters:
address 192.168.11.120
extra_port 0
persistmaxtime 0
persistpoolmax 0
port 33002
protocol MariaDBBackend
proxy_protocol false
rank primary
ssl false
ssl_cert_verify_depth 9
ssl_verify_peer_certificate false
ssl_version MAX
type server
Number of connections: 2
Current no. of conns: 0
Current no. of operations: 0
Number of routed packets: 23
Adaptive avg. select time: 367.72us
Server 0x560389fc5ea0 (server3)
Server: 192.168.11.120
Status: Slave, Running
Protocol: MariaDBBackend
Port: 33004
Server Version: 10.4.12-MariaDB-1:10.4.12+maria~stretch-log
Node Id: 1024
Master Id: 1000
Last event: server_up
Triggered at: Thu, 14 May 2020 02:52:52 GMT
Slave delay: 0
Server Parameters:
address 192.168.11.120
extra_port 0
persistmaxtime 0
persistpoolmax 0
port 33004
protocol MariaDBBackend
proxy_protocol false
rank primary
ssl false
ssl_cert_verify_depth 9
ssl_verify_peer_certificate false
ssl_version MAX
type server
Number of connections: 2
Current no. of conns: 0
Current no. of operations: 0
Number of routed packets: 3
Adaptive avg. select time: not available
使用mysql账号(上述中用于程序的账号)连接到maxscale的4006端口上,例如:
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
+--------------------+
2 rows in set (0.00 sec)
参考:
http://www.ttlsa.com/mysql/maxscale-right-read-write-split/
https://blog.51cto.com/lee90/1945504
https://longsheng.org/post/15113.html
https://mariadb.com/kb/en/mariadb-maxscale-23-maxadmin-admin-interface/
https://mariadb.com/docs/deploy/maxscale-debian9-mxs23/
https://mariadb.com/docs/reference/mxs2.4/maxctrl/
没有评论