翼度科技»论坛 编程开发 mysql 查看内容

MySQL数据读写分离MaxScale相关配置

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
一、概念:


  • MySQL数据读写分离是存储数据的一种服务架构
  • 执行select命令必须连接 slave角色服务器
  • 执行insert命令必须连接 maste角色服务器
  • 提供数据读写分离功能的中间件软件有: mysql-proxy maxscale mycat
  • 拓扑架构只支持一主一从或者一主多从架构

二、实现读写分离的拓扑图:



三、MaxScale相关配置:

指令/路径/...说明maxscale-2.1.2-1.rhel.7.x86_64.rpm软件包/etc/maxscale.cnf主配置文件maxscale /etc/maxscale.cnf启动服务/var/log/maxscale/maxscale.log日志路径(可查看报错信息)4006读写分离服务使用端口号4016管理服务使用端口号
四、读写分离的配置流程:


  • 配置Mysql服务器一主一从
  • 配置代理服务器(读写分离服务器)
  • 启动读写分离服务
  • 客户机50测试配置读写分离服务的配置

五、实操:


第一步:配置Mysql服务器一主一从


  • 把host61配置为master数据库服务器
  1. [root@host61 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. Server_id = 61
  4. log_bin=master61
  5. :wq
  6. [root@host61 ~]# systemctl restart mysqld
  7. [root@host61 ~]# mysql -uroot –p123qqq...A
  8. Mysql>   grant replication slave on  *.* to repluser@"%" identified by "123qqq...A";
  9. Mysql>  show  master status ;
  10. +-----------------+----------+--------------+------------------+-------------------+
  11. | File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  12. +-----------------+----------+--------------+------------------+-------------------+
  13. | master61.000001 |      441 |              |                  |                   |
  14. +-----------------+----------+--------------+------------------+-------------------+
  15. 1 row in set (0.00 sec)
复制代码

  • 把host62 配置为slave数据库服务器
  1. [root@host62 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. Server_id = 62
  4. :wq
  5. [root@host62 ~]# systemctl  restart  mysqld
  6. [root@host62 ~]# mysql -uroot -p密码
  7. Mysql> change master to  master_host="192.168.88.61" ,
  8. Master_user="repluser" ,  
  9. Master_password="123qqq...A" ,
  10. Master_log_file="master61.000001" ,
  11. Master_log_pos=441 ;
  12. Mysql>  start  slave;
  13. Mysql> show  slave status \G
  14.              Slave_IO_Running: Yes
  15.              Slave_SQL_Running: Yes
复制代码
第二步:配置代理服务器(读写分离服务器)


  • 安装软件
  1. [root@host60 ~]# yum -y install maxscale-2.1.2-1.rhel.7.x86_64.rpm  
复制代码

  • 修改主配置文件
  1. [root@host60 ~]# cp /etc/maxscale.cnf /root/  备份主配置文件
  2. [root@host60 ~]# vim /etc/maxscale.cnf
  3. [maxscale]
  4. threads=auto # 服务启动后线程的数量,根据CPU 核数创建
  5. [server1]   
  6. type=server
  7. address=192.168.88.61 # 指定第1台数据库服务器的ip地址
  8. port=3306
  9. protocol=MySQLBackend
  10. [server2]   
  11. type=server
  12. address=192.168.88.62 # 指定第2台数据库服务器的ip地址
  13. port=3306
  14. protocol=MySQLBackend
  15. [MySQL Monitor]   # 定义监视的数据库服务器
  16. type=monitor
  17. module=mysqlmon
  18. servers=server1,server2    # 监视server1和server2
  19. user=mysqla      # 监控用户账号
  20. passwd=123qqq...A  # 监控用户连接密码
  21. monitor_interval=10000
  22. #禁止只读服务(注释)
  23. #[Read-Only Service]
  24. #type=service
  25. #router=readconnroute
  26. #servers=server1
  27. #user=myuser
  28. #passwd=mypwd
  29. #router_options=slave
  30. [Read-Write Service]   # 启用读写分离服务
  31. type=service
  32. router=readwritesplit
  33. servers=server1,server2   # 读写分离在server1和server2服务器之间进行
  34. user=mysqlb   # 路由用户
  35. passwd=123qqq...A  # 连接密码
  36. max_slave_connections=100%
  37. [MaxAdmin Service]   # 管理服务(通过访问管理服务可以查看监控信息)
  38. type=service
  39. router=cli
  40. # 因为只读服务没有启用 ,不需要定义服务使用的端口号(注释)
  41. #[Read-Only Listener]
  42. #type=listener
  43. #service=Read-Only Service
  44. #protocol=MySQLClient
  45. #port=4008
  46. [Read-Write Listener]   # 定义读写分离服务使用端口号
  47. type=listener
  48. service=Read-Write Service
  49. protocol=MySQLClient
  50. port=4006    # 端口号
  51. [MaxAdmin Listener]   # 定义管理服务使用端口号
  52. type=listener
  53. service=MaxAdmin Service
  54. protocol=maxscaled
  55. socket=default
  56. port=4016  # 端口号
  57. :wq
复制代码

  • 配置数据库服务器(在数据库服务器上添加监控用户和路由用户)
  • 注意:因为是主从结构 ,所以只需要在主服务器添加,从服务器会自动同步
  1. [root@host61 ~]# mysql -uroot -p123qqq...A                     
  2. # 添加监控用户 mysqla 用户
  3. mysql> grant  replication   slave , replication  client  on  *.*  to  
  4. mysqla@"%" identified by "123qqq...A";
  5. # 权限说明:
  6. # replication client   监视数据库服务的运行状态  
  7. # replication slave      数据库服务器的主从角色   
  8. # 添加路由用户 mysqlb 用户
  9. mysql> grant  select on  mysql.*  to  mysqlb@"%" identified by "123qqq...A";  # 对授权库下的表有查询权限
  10. # 在从服务器查看用户是否同步
  11. [root@host62 ~]# mysql -uroot -p123qqq...A
  12. select user from mysql.user where user="mysqla";
  13. select user from mysql.user where user="mysqlb";
复制代码
第三步:启动读写分离服务


  • 验证数据库服务器的授权用户 mysqla 和 mysqlb
  1. # 安装提供mysql命令的软件
  2. [root@host60 ~]# which  mysql || yum -y install mariadb   
  3. [root@host60 ~]# mysql -h192.168.88.61 -umysqla -p123qqq...A
  4. [root@host60 ~]# mysql -h192.168.88.62 -umysqla -p123qqq...A
  5. [root@host60 ~]# mysql -h192.168.88.61 -umysqlb -p123qqq...A
  6. [root@host60 ~]# mysql -h192.168.88.62 -umysqlb -p123qqq...A
  7. # 说明:能连接成功才是对的,如果连接失败:执行如下操作
  8. # 在主数据库服务器host61 把添加 mysqla用户 和 mysqlb 用户的命令再执行一遍
  9. # 启动服务
  10. [root@host60 ~]# maxscale   /etc/maxscale.cnf     
  11. # 查看日志文件
  12. [root@host60 ~]# ls /var/log/maxscale/   
  13. maxscale.log
  14. # 查看读写分离服务端口号
  15. [root@host60 ~]# netstat  -utnlp  | grep 4006     
  16. tcp6       0      0 :::4006       :::*            LISTEN      1580/maxscale      
  17. # 查看读写分离服务端口号
  18. [root@host60 ~]# netstat  -utnlp  | grep 4016  
  19. tcp6       0      0 :::4016       :::*        LISTEN      1580/maxscale      
  20. #把服务杀死 再启动  相当于重启服务 (修改了配置文件后要重启服务使其配置生效)
  21. # 通过杀进程的方式停止服务
  22. [root@host60 ~]# killall -9 maxscale   
  23. # 启动服务
  24. [root@host60 ~]# maxscale  /etc/maxscale.cnf     
  25. # 在host60本机访问管理服务查看数据库服务的监控信息
  26. [root@host60 ~]# maxadmin -uadmin -pmariadb -P4016
  27. MaxScale> list servers
  28. Servers.
  29. -------------------+-----------------+-------+-------------+--------------------
  30. Server             | Address         | Port  | Connections | Status              
  31. -------------------+-----------------+-------+-------------+--------------------
  32. server1            | 192.168.88.61    |  3306 |           0 | Master, Running
  33. server2            | 192.168.88.62    |  3306 |           0 | Slave, Running
  34. -------------------+-----------------+-------+-------------+--------------------
  35. MaxScale> exit      
复制代码

  • 排错方法 : 查看日志里的报错信息 vim /var/log/maxscale/maxscale.log

第四步:测试配置读写分离服务的配置


  • 客户端能够连接读写分离服务器访问数据库服务
  1. # 首先在主数据库服务器host61 添加客户端连接使用的用户
  2. [root@host61 ~]# mysql -uroot -p密码
  3. create database  bbsdb;
  4. create  table bbsdb.a(id int);
  5. grant select,insert on bbsdb.* to  yaya@"%"  identified by "123qqq...A";
  6. # 在从服务器host62查看存储数据库表和添加用户
  7. [root@host62 ~]# mysql -uroot -p密码
  8. desc  bbsdb.a;
  9. select  user from mysql.user where user="yaya";
  10. # 客户端host50连接读写分离服务器host60访问数据库服务
  11. mysql -h读写分离服务器的ip   -P读写分离服务的端口号  -u数据库授权用户名  -p密码
  12. [root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A  
复制代码

  • 连接读写分离服务后,可以对数据做查询和存储操作
  1. mysql> select  * from bbsdb.a;
  2. Empty set (0.00 sec)
  3. mysql> insert into bbsdb.a values(8888);
  4. Query OK, 1 row affected (0.06 sec)
  5. mysql> select  * from bbsdb.a;
  6. +------+
  7. | id   |
  8. +------+
  9. | 8888 |
  10. +------+
  11. 1 row in set (0.00 sec)
复制代码
第五步:验证


  • 怎么验证查询select 访问就在host62从服务器获取的数据呢?
  • 在从服务本机向表里添加1条记录(在从服务添加的新数据主服务器不会同步)
  1. # 从服务器插入1条数据
  2. [root@host62 ~]# mysql -uroot -p123qqq...A -e 'insert into bbsdb.a values(6262)'
  3. [root@host62 ~]# mysql -uroot -p123qqq...A -e 'select * from bbsdb.a'
  4. mysql: [Warning] Using a password on the command line interface can be insecure.
  5. +------+
  6. | id   |
  7. +------+
  8. | 8888 |
  9. | 6262 |
  10. +------+
  11. # 主服务器查询
  12. [root@host11 ~]# mysql -uroot -p123qqq...a  -e 'select  * from bbsdb.a'
  13. mysql: [Warning] Using a password on the command line interface can be insecure.
  14. +------+
  15. | id   |
  16. +------+
  17. | 8888 |
  18. +------+
  19. # 客户端访问读写分离服务器查询数据(查询结果为从服务器数据源)        
  20. [root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A -e 'select * from bbsdb.a'
  21. mysql: [Warning] Using a password on the command line interface can be insecure.
  22. +------+
  23. | id   |
  24. +------+
  25. | 8888 |
  26. | 6262 |
  27. +------+
复制代码

  • 怎么验证存储数据insert 访问 就是存储在了主机服务器host61上?
  1. # 客户端机插入数据
  2. [root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A -e 'insert into bbsdb.a values(666)'
  3. # 在主服务器本机查看数据
  4. [root@host61 ~]# mysql -uroot -p123qqq...a  -e 'select  * from bbsdb.a'
  5. mysql: [Warning] Using a password on the command line interface can be insecure.
  6. +------+
  7. | id   |
  8. +------+
  9. | 8888 |
  10. |  666 |
  11. +------+
  12. [root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A -e 'select * from bbsdb.a'
  13. mysql: [Warning] Using a password on the command line interface can be insecure.
  14. +------+
  15. | id   |
  16. +------+
  17. | 8888 |
  18. | 6262 |
  19. |  666 |
  20. +------+
复制代码

  • 还可以通过查看主服务器的position是否在客户端服务器插入数据后改动来确定是不是在主服务器中进行操作过数据

补充说明


  • 如果主从结构中的从服务器宕机了,就实现不了读写分离了,会把读写请求都给主服务器处理。
  • 如果主从结构中的主服务器宕机了,读写分离服务无法访问
以上就是MySQL数据读写分离MaxScale相关配置的详细内容,更多关于MySQL数据读写分离MaxScale的资料请关注脚本之家其它相关文章!

来源:https://www.jb51.net/database/291507he8.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具