mysqlrouter使⽤配置
mysql router使⽤配置
参考资料:
⼀、架构图
介绍:
MySQL Router是处于应⽤client和dbserver之间的轻量级代理程序,它能检测,分析和转发查询到后端数据库实例,并把结果返回给client。是mysql-proxy的⼀个替代品。
Router实现读写分离,程序不是直接连接数据库IP,⽽是固定连接到mysql router。MySQL Router对前端应⽤是透明的。应⽤程序把MySQL Router当作是普通的mysql实例,把查询发给MySQL Router,⽽MySQL Router会把查询结果返回给前端的应⽤程序。
从数据库服务器故障,业务可以正常运⾏。由MySQL Router来进⾏⾃动下线不可⽤服务器。程序配置不需要任何修改。
主数据库故障,由MySQL Router来决定主从⾃动切换,业务可以正常访问。程序配置不需要做任何修改。
⼆、读写分离原理
MySQL Router接受前端应⽤程序请求后,根据不同的端⼝来区分读写,把连接读写端⼝的所有查询发往主库,把连接只读端⼝的select查询以轮询⽅式发往多个从库,从⽽实现读写分离的⽬的。读写返回的结果会交给MySQL Router,由MySQL Router返回给客户端的应⽤程序。
三、实验环境
编号主机名IP⾓⾊
1db_shenji10.10.81.134mysql router
2dba_test_00110.10.50.60主库
3dba_test_00210.10.117.231从库
四、MySQL-Router安装配置
1、主从搭建,此处省略
2、MySQL-Router配置
2.1、下载
[root@db_shenji ~]# cd /data/
[root@db_shenji data]# wget sql/get/Downloads/MySQL-Router/mysql-router-8.0.18-linux-glibc2.12-x86_
[root@db_shenji data]# tar -Jxvf mysql-router-8.0.18-linux-glibc2.12-x86_
[root@db_shenji data]# ln -s /data/mysql-router-8.0.18-linux-glibc2.12-x86_64 /usr/local/mysql-router
2.2、配置
[root@db_shenji data]# mkdir /etc/mysql-route/
[root@db_shenji data]# cp /usr/local/mysql-router/share/doc/mysqlrouter/f /etc/f
修改后完整的内容
[DEFAULT]
# ⽇志存放⽬录
logging_folder = /data/log/mysql-route
# 插件存放⽬录
plugin_folder = /usr/local/mysql-router/lib/mysqlrouter
# 配置⽂件存放⽬录
config_folder = /etc/mysql-route
# 运⾏⽬录
runtime_folder = /var/run
[logger]
# ⽇志运⾏级别
level = debug
# 主节点故障转移配置
[routing:basic_failover]
# 写节点地址
bind_address=10.10.81.134
# 写节点端⼝
bind_port = 7001
# 模式,读写
mode = read-write
# 主节点地址:默认情况下第⼀台主数据库为写主库,当第⼀台主数据库DOWN机后,第⼆台数据库被提升为主库
destinations = 10.10.50.60:3306,10.10.117.231:3306
# 从节点负载均衡配置
[routing:balancing]
# 绑定的IP地址
bind_address=10.10.81.134
# 监听的端⼝
bind_port = 7002
# 连接超时时间
connect_timeout = 3
# 最⼤连接数
max_connections = 1024
# 后端服务器地址
destinations = 10.10.117.231:3306
# 模式:读还是写
mode = read-only
[keepalive]
interval = 60
创建⽬录
mkdir -p /data/log/mysql-route
chown root:root /data/log/mysql-route/
启动⽅式
/usr/local/mysql-router/bin/mysqlrouter -c /etc/f &
查看⽇志
[root@db_shenji ~]# tail -f /data/log/mysql-route/mysqlrouter.log
2019-12-0614:05:40 main DEBUG [7f48cf782720] Starting all plugins.
2019-12-0614:05:40 main DEBUG [7f48cc30e700]  plugin 'keepalive:' starting
2019-12-0614:05:40 main DEBUG [7f48cf782720]  plugin 'logger:' doesn't implement start()
2019-12-0614:05:40 keepalive INFO [7f48cc30e700] keepalive started with interval 60
2019-12-0614:05:40 keepalive INFO [7f48cc30e700] keepalive
2019-12-0614:05:40 main DEBUG [7f48cb90d700]  plugin 'routing:balancing' starting
2019-12-0614:05:40 main DEBUG [7f48caf0c700]  plugin 'routing:basic_failover' starting
2019-12-0614:05:40 main DEBUG [7f48cf782720] Running.
2019-12-0614:05:40 routing INFO [7f48cb90d700] [routing:balancing] started: listening on 10.10.81.1
34:7002, routing strategy = round-robin 2019-12-0614:05:40 routing INFO [7f48caf0c700] [routing:basic_failover] started: listening on 10.10.81.134:7001, routing strategy = first-available 登录验证
[root@db_shenji ~]# mysql -h 10.10.81.134 -uw -p'w' -P7001
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 130622
Server version: 5.6.41-84.1-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
mysql> show slave status\G;
Empty set (0.00 sec)
ERROR:
No query specified
mysql>
[root@db_shenji ~]# mysql -h 10.10.81.134 -uw -p'w' -P7002
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.41-84.1-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.50.60
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000021
Read_Master_Log_Pos: 3512
Relay_Log_File: relay-bin.000053
Relay_Log_Pos: 354
Relay_Master_Log_File: mysql-bin.000021
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3512
Relay_Log_Space: 521
Until_Condition: None
Until_Log_File:
mysql下载配置
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1574144089
Master_UUID: 0501f340-0a94-11ea-ad2b-5254007dcbb3
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it            Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 0501f340-0a94-11ea-ad2b-5254007dcbb3:10-5440876
Executed_Gtid_Set: 0501f340-0a94-11ea-ad2b-5254007dcbb3:1-5440876,
137347eb-0a94-11ea-ad2b-525400dd43f8:1-531071
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>