【MySQL】mysql router 读写分离中间件使用

1.检查MySQL router; 

[root@mysql1 mysqlrouter]# netstat -ntpl|grep mysqlrouter
tcp        0      0 192.168.1.11:7001       0.0.0.0:*               LISTEN      129393/mysqlrouter  
tcp        0      0 192.168.1.11:7002       0.0.0.0:*               LISTEN      129393/mysqlrouter  

2.测试主节点 

[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+

--可以看到每次都是连接到mysql1(主节点); 
--关闭主库.则 7001 无法访问主库。
systemctl start mysqld 
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to remote MySQL server for client connected to '192.168.1.11:7001'
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to remote MySQL server for client connected to '192.168.1.11:7001'
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to remote MySQL server for client connected to '192.168.1.11:7001'
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to remote MySQL server for client connected to '192.168.1.11:7001'

--启动主库  
systemctl start mysqld  
--7001 端口可以重新访问主库。
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7001 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+

3.测试读节点负载均衡 

[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7002 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7002 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql2     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7002 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql3     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7002 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql4     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7002 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7002 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql2     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7002 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql3     |
+------------+
[root@mysql1 etc]# mysql -uroot -prootroot -h192.168.1.11 -P7002 -e "select @@hostname;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql4     |
+------------+
--可以看到,只读节点按照mysql1,mysql2,mysql3,mysql4 的方式轮询。

4.总结 

mysqlrouter 是一款非常便捷的读写分离中间件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL主从读写分离部署是一种常见的数据库架构,它可以提高系统的性能和可用性。在这种架构中,有一个主数据库(Master)用于处理写操作,而多个从数据库(Slaves)用于处理读操作。 以下是MySQL主从读写分离部署的一般步骤: 1. 安装MySQL:在主服务器和从服务器上安装MySQL数据库软件。 2. 配置主服务器:在主服务器上进行以下配置操作: - 在my.cnf配置文件中启用二进制日志(binary log)功能,并设置一个唯一的服务器ID。 - 创建一个用于复制的账户,并授予该账户复制权限。 3. 配置从服务器:在每个从服务器上进行以下配置操作: - 在my.cnf配置文件中设置一个唯一的服务器ID。 - 配置从服务器连接到主服务器的信息,包括主服务器的IP地址、复制账户的用户名和密码。 4. 启动复制:在从服务器上启动复制进程,使其连接到主服务器并开始复制数据。 5. 验证复制:确认从服务器已经成功连接到主服务器并开始复制数据。可以通过查看主从服务器的状态信息来验证复制是否正常运行。 6. 读写分离配置:在应用程序中配置读写分离,使读操作通过从服务器处理,写操作通过主服务器处理。可以使用中间件或代理来实现读写分离,例如MySQL Proxy、MySQL Router或HAProxy。 7. 监控和维护:定期监控主从服务器的状态,确保复制正常运行。如果主服务器发生故障,可以手动将其中一个从服务器提升为新的主服务器。 需要注意的是,MySQL主从复制并不能实时同步数据,而是异步复制。因此,主从复制在高并发写入场景下可能存在一定的延迟。此外,主从复制也需要考虑数据一致性和容灾备份等问题。在部署过程中,建议参考MySQL官方文档和相关资料,并根据实际情况进行配置和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值