经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MySQL » 查看文章
Mysql读写分离——主从数据库+Atlas
来源:cnblogs  作者:NinWoo  时间:2018/9/25 19:15:08  对本文有异议

mysql集群

最近在参加项目开发微信小程序后台,由于用户数量巨大,且后台程序并不是很完美,所以对用户的体验很是不友好(简单说就是很卡)。赶巧最近正在翻阅《大型网站系统与Java中间件实践》。
所以,先是使用Docker swarm构建了负载均衡集群,然后使用Atlas做了数据库的读写分离,尽可能对用户进行分流,降低对单机服务器的负载,提高访问体验。本片随笔仅对数据库的读写分离部分进行介绍。

目标:实现mysql数据库集群,一个主节点负责写入数据,多个从节点负责读取数据

实验环境

服务器:

  1. 阿里云服务器:centos7(10.0.0.1)

  2. 腾讯云服务器:ubuntu:16.04(10.0.0.2)

  3. 京东云服务器:ubuntu:16.04 (10.0.0.3)

mysql:5.7

docker:18.03

实现方案

mysql 主从数据库 + Atlas读写分离

步骤:

进入Master服务器进行配置

  1. 使用Docker,在服务器上创建MySQL服务

  1. docker run -3306:3306 --name mysql3306 -/opt/mysql/data/data3306:/var/lib/mysql  -/opt/mysql/logs/logs3306:/logs -e MYSQL_ROOT_PASSWORD=hello -d mysql:5.7
  1. 进Master服务器的MySQL数据库中,创建用户"niwoo"设置密码为"hello"

  1. mysql -127.0.0.1 -uroot -phello
  2.  
  3. mysql> grant all on *.* to ninwoo@'127.0.0.1' identified by "hello";
  4. Query OK, 0 rows affected, 1 warning (0.00 sec)
  1. 修改niwoo的访问权限

  1. mysql> use mysql
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4.  
  5. Database changed
  6. mysql> update user set host = '%' where user = 'ninwoo';
  7. Query OK, 0 rows affected (0.00 sec)
  8. Rows matched: 0  Changed: 0  Warnings: 0
  1. 确认是否修改成功

  1. mysql> update user set host = '%' where user = 'ninwoo';
  2. Query OK, 1 row affected (0.02 sec)
  3. Rows matched: 1  Changed: 1  Warnings: 0
  4.  
  5. mysql> select user, host from user;
  6. +---------------+-----------+
  7. | user          | host      |
  8. +---------------+-----------+
  9. | ninwoo        | %         |
  10. | root          | %         |
  11. | mysql.session | localhost |
  12. | mysql.sys     | localhost |
  13. | root          | localhost |
  14. +---------------+-----------+
  15. 5 rows in set (0.00 sec)
  1. 更新数据库,是配置生效

  1. mysql> flush privileges;
  2. Query OK, 0 rows affected (0.00 sec)

Master,Slave1的MySQL数据库上创建CampusHire数据库

  1. 进入Slave1执行相同1,2,3,4操作

配置主从数据库连接

进入Master服务器进行配置

  1. 进入mysql容器内部,修改配置文件

  1. docker exec -it mysql3306 bash
  1. 默认没有vim,无法修改文件

  1. # 更新软件源
  2. apt update
  3. # 安装vim
  4. apt install vim
  1. 修改mysql配置文件

  1. vim /etc/mysql/mysql.conf.d/mysqld.cnf

添加如下配置

  1. #主从复制配置  
  2. innodb_flush_log_at_trx_commit=1  
  3. sync_binlog=1  
  4. #需要备份的数据库  
  5. binlog-do-db=test
  6. #不需要备份的数据库  
  7. binlog-ignore-db=mysql  
  8.   
  9. #启动二进制文件  
  10. log-bin=mysql-bin  
  11.   
  12. #服务器ID  
  13. server-id=1
  1. 退出容器,重启docker应用

  1. docker restart mysql3306
  1. 进入数据库,配置主从复制的权限

  1. mysql -127.0.0.1 -uroot -phello
  2. mysql> grant replication slave on *.* to 'ninwoo'@'127.0.0.1' identified by 'hello';
  3. Query OK, 0 rows affected, 1 warning (0.01 sec)
  1. 锁定数据库

  1. mysql> flush tables with read lock;
  2. Query OK, 0 rows affected (0.00 sec)
  1. 查看主数据库信息,记住下面的“File”与“Position”的信息

  1. mysql> show master status;
  2. +------------------+----------+--------------+------------------+-------------------+
  3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +------------------+----------+--------------+------------------+-------------------+
  5. | mysql-bin.000001 |      439 | CampusHire   | mysql            |                   |
  6. +------------------+----------+--------------+------------------+-------------------+
  7. 1 row in set (0.00 sec)
  1. 进入slave服务器,进入mysql容器内部

  1. # 更新软件源
  2. apt update
  3. # 安装vim
  4. apt install vim
  1. 修改从数据库的配置文件

  1. vim /etc/mysql/mysql.conf.d/mysqld.cnf

添加server-id=2

  1. 重启Slave数据库

  1. docker restart mysql3306
  1. 进入数据库,输入刚刚记录下来的File和Position

  1. MySQL [(none)]> change master to master_host='10.0.0.1',
  2.     ->
  3.     -> master_user='ninwoo',
  4.     -> master_password='hello',
  5.     -> master_port=3306,
  6.     -> master_log_file='mysql-bin.000001',
  7.     -> master_log_pos=439,
  8.     -> master_connect_retry=10;
  1. 启动Slave进程

  1. mysql> start slave;
  2. Query OK, 0 rows affected (0.00 sec)
  1. 查看复制状态

  1. mysql> show slave status \G;
  2. *************************** 1. row ***************************
  3.                Slave_IO_State: Waiting for master to send event
  4.                   Master_Host: 139.199.194.49
  5.                   Master_User: ninwoo
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 10
  8.               Master_Log_File: mysql-bin.000001
  9.           Read_Master_Log_Pos: 439
  10.                Relay_Log_File: 2a8a4abdcf68-relay-bin.000002
  11.                 Relay_Log_Pos: 320
  12.         Relay_Master_Log_File: mysql-bin.000001
  13.              Slave_IO_Running: Yes
  14.             Slave_SQL_Running: Yes
  15.               Replicate_Do_DB:
  16.           Replicate_Ignore_DB:
  17.            Replicate_Do_Table:
  18.        Replicate_Ignore_Table:
  19.       Replicate_Wild_Do_Table:
  20.   Replicate_Wild_Ignore_Table:
  21.                    Last_Errno: 0
  22.                    Last_Error:
  23.                  Skip_Counter: 0
  24.           Exec_Master_Log_Pos: 439
  25.               Relay_Log_Space: 534
  26.               Until_Condition: None
  27.                Until_Log_File:
  28.                 Until_Log_Pos: 0
  29.            Master_SSL_Allowed: No
  30.            Master_SSL_CA_File:
  31.            Master_SSL_CA_Path:
  32.               Master_SSL_Cert:
  33.             Master_SSL_Cipher:
  34.                Master_SSL_Key:
  35.         Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37.                 Last_IO_Errno: 0
  38.                 Last_IO_Error:
  39.                Last_SQL_Errno: 0
  40.                Last_SQL_Error:
  41.   Replicate_Ignore_Server_Ids:
  42.              Master_Server_Id: 1
  43.                   Master_UUID: 4a0187fa-b0a5-11e8-a7e7-0242ac110003
  44.              Master_Info_File: /var/lib/mysql/master.info
  45.                     SQL_Delay: 0
  46.           SQL_Remaining_Delay: NULL
  47.       Slave_SQL_Running_State: Slave has read all relay log; waiting for more up                                                                                                                                                                                               dates
  48.            Master_Retry_Count: 86400
  49.                   Master_Bind:
  50.       Last_IO_Error_Timestamp:
  51.      Last_SQL_Error_Timestamp:
  52.                Master_SSL_Crl:
  53.            Master_SSL_Crlpath:
  54.            Retrieved_Gtid_Set:
  55.             Executed_Gtid_Set:
  56.                 Auto_Position: 0
  57.          Replicate_Rewrite_DB:
  58.                  Channel_Name:
  59.            Master_TLS_Version:
  60. 1 row in set (0.00 sec)

到这里主从数据的配置就已经完成,在向主库写入数据的同时,将在从库进行自动的备份。但在实际测试中遇到了配置完成之后无法向主数据库写入的问题,初步
判断是由于主库加锁而未解锁的问题,所以这里采用比较暴力的方法,重启主数据库容器。

  1. # master节点
  2. docker restart mysql3306

配置Atlas

Atlas是有360团队优化mysqlproxy而衍生出的版本,性能更好更稳定。

Atlas有两个版本,这里我选择普通版本,即不分表。

在阿里云主机(centos7)上进行一下安装步骤:

  1. 下载安装

  1. [root@localhost ~]# cd /home/
  2. [root@localhost home]# wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
  1. 安装

  1. [root@localhost home]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm

安装目录位于/usr/local/mysql-proxy/

  1. 使用安装目录下的bin/encrypt程序加密数据库密码

  1. [root@localhost bin]# ./encrypt hello
  1. 配置Atlas

  1. [root@localhost conf]# cd /usr/local/mysql-proxy/conf/
  2. [root@localhost conf]# vim test.cnf

修改以下配置:

  1. #管理接口的用户名
  2. admin-username = user
  3.  
  4. #管理接口的密码
  5. admin-password = pwd
  6.  
  7. ...
  8.  
  9. #Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
  10. proxy-backend-addresses = 10.0.0.1:3306
  11.  
  12. #Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
  13. proxy-read-only-backend-addresses = 10.0.0.2:3306@1
  14.  
  15. ...
  16.  
  17. #用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
  18. pwds = buck:RePBqJ+5gI4=
  19.  
  20. ...
  21.  
  22. #Atlas监听的工作接口IP和端口
  23. proxy-address = 0.0.0.0:1234
  24.  
  25. #Atlas监听的管理接口IP和端口
  26. admin-address = 0.0.0.0:2345

读者需要根据自己的实际情况修改以上配置

  1. 启动Atlas

  1. [root@localhost bin]# /etc/init.d/mysqld status
  2. OK: MySQL-Proxy of test is started
  1. 使用mysql命令连接数据库

其中2345端口是管理端口,用户可以在该管理数据库中修改数据库代理服务器的相关配置

  1. [root@localhost bin]# mysql -h127.0.0.1 -P2345 -uuser -ppwd

其中1234端口是数据库代理服务器的端口,用户可以直接访问该端口进行数据库的访问和更新

  1. [root@localhost bin]# mysql -h127.0.0.1 -P1234 -ubuck -phello
  1. 使用软件连接Atlas数据库代理服务器

这里发现,之前使用的mysql workbench无法正常使用,更换Navicat可以正常使用。

  1. 使用jdbc连接数据库

使用方法与普通数据库一样。

后记

到这里,配置就已经全部完成。有心的读者或者已经发现,还有一台服务器未使用,这台服务器将在以后接入到现有的mysql数据库集群当中,充当新的slave节点。该实验将在以后补上。

理论上说,我所做的这些或许会对负载过大的问题有所解决。但最近并未出现太高的访问现象,所以短期也没有看到效果。具体效果,将在实际场景中验证后补充上,敬请期待。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号