一. 介绍 Atlas 及架构图
Atlas 源代码用 C 语言编写, 它对于 web Server 相当于是 DB, 相对于 DB 相当于是 Client, 如果把 Atlas 的逻辑放到 Web Server 程序里去处理, 这样会大大增加 Web Server 程序的复杂度, 同时 Web Server 和 DB 之间的耦合度也相当高, 因为只要 DB 增加 / 减少服务, Web Server 就有可能要发生代码改变, 若代码不改变, 就得通过中间关系表与心跳机制来维护 Server 之间的关系, 这样会带来性能的损耗, 而 Atlas 是架设在 Web Server 与 DB 之间的一个中间件, Web Server 与 DB 之间的耦合关系放到了 Atlas 来处理, 既做到了灵活也保留了性能, 这也是 Atlas 存在的价值.
Atlas 支持表的水平切分, 支持读写分离, 对数据实时性要求较高的项目可以在 select 语句前增加 /*master*/ 强制读主库
二. 实验环境
- MySQL master:192.168.200.101
- MySQL slave :192.168.200.102
- atlas :192.168.200.103
atlas 有两个 port, 分别为
工作端口: proxy-address 项配置, 例如 proxy-address = 0.0.0.0:1234 代表客户端应该使用 1234 这个端口连接 Atlas 来发送 SQL 请求.
管理端口: admin-address 项配置, 例如 admin-address = 0.0.0.0:2345 代表 DBA 应该使用 2345 这个端口连接 Atlas 来执行运维管理操作.
二. 安装 Atlas
注意: 只能安装在 64 位的 Linux 操作系统上, CentOS 官方建议 rpm 安装方式
三. 实施案例
1. 目录结构
bin: 该目录存放 atlas 启动程序, 还有加密脚本
conf: 该目录则存放 atlas.conf 配置档, 可以有多个实例
lib: 该目录存放程序的一些库文件及依赖文件
log: 该目录则存放实例产生的日志
安装位置:
/usr/local/MySQL-proxy
配置文件:
/usr/local/MySQL-proxy/conf/test.cnf
2. 配置 MySQL 主从
全部主机关闭防火墙和 SElinux
- [root@zha ~]# /etc/init.d/iptables stop
- iptables: Setting chains to policy ACCEPT: filter [ OK ]
- iptables: Flushing firewall rules: [ OK ]
- iptables: Unloading modules: [ OK ]
- [root@zha ~]# setenforce 0
- setenforce: SELinux is disabled
修改主机名 (200.101)
- [root@zha ~]# hostname master
- [root@zha ~]# bash
- [root@master ~]# VIM /etc/sysconfig/network
- NETWORKING=yes
- HOSTNAME=master
- (200.102)
- [root@zha ~]# hostname slave
- [root@zha ~]# bash
- [root@slave ~]# VIM /etc/sysconfig/network
- (200.103)
- [root@zha ~]# hostname Atlas
- [root@zha ~]# bash
- [root@Atlas ~]# VIM /etc/sysconfig/network
- NETWORKING=yes
- HOSTNAME=zha
主从分别用 yum 安装 MySQL
- [root@master ~]# yum -y install MySQL MySQL-server
- [root@slave ~]# yum -y install MySQL MySQL-server
主从分别修改配置文件并启动服务
- [root@master ~]# VIM /etc/my.cnf
- [mysqld]
- datadir=/var/lib/MySQL
- socket=/var/lib/MySQL/MySQL.sock
- user=MySQL
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-links=0
- log-error=/var/lib/MySQL/MySQL.err
- log=/var/lib/MySQL/mysql_log.log
- log-slow-queries=/var/lib/MySQL/mysql_slow_queris.log
- default-character-set=utf8
- log-bin=MySQL-bin
- server-id=1
- log-slave-updates
- sync_binlog=1
- auto_increment_increment=2
- auto_increment_offset=1
- [client]
- default-character-set=utf8
- [mysqld_safe]
- log-error=/var/log/mysqld.log
- pid-file=/var/run/mysqld/mysqld.pid
- # 注意 server-id 不能相同
- [root@master ~]# /etc/init.d/mysqld start
配置主从复制
master 操作:
查看 file 和 pos 值
- MySQL> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | MySQL-bin.000003 | 106 | | |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
- MySQL> grant replication slave on *.* to 'replication'@'192.168.200.%' identified by '123456';
- Query OK, 0 rows affected (0.04 sec)
- MySQL> flush privileges;
slave 操作:
- MySQL> change master to master_host='192.168.200.101',master_user='replication',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=106;
- Query OK, 0 rows affected (0.09 sec)
- MySQL> slave start;
- Query OK, 0 rows affected (0.00 sec)
- MySQL> show slave status\G
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- (确保这俩线程为 YES)
主从配置完成!
3. 安装配置 Atlas
下载地址: https://github.com/Qihoo360/Atlas/releases
- [root@Atlas ~]# rpm -ivh Atlas-2.2.1.el5.x86_64.rpm
- Preparing... ########################################### [100%]
- 1:Atlas ########################################### [100%]
- [root@Atlas ~]# VIM /usr/local/MySQL-proxy/conf/test.cnf
- # 管理接口的用户名
- admin-username = user
- # 管理接口的密码
- admin-password = pwd
- #Atlas 后端连接的 MySQL 主库的 IP 和端口, 可设置多项, 用逗号分隔
- proxy-backend-addresses = 192.168.200.101:3306
- #Atlas 后端连接的 MySQL 从库的 IP 和端口,@后面的数字代表权重, 用来作负载均衡, 若省略则默认为 1, 可设置多项, 用逗号分隔
- proxy-read-only-backend-addresses = 192.168.200.102:3305@1
- # 用户名与其对应的加密过的 MySQL 密码, 密码使用 PREFIX/bin 目录下的加密程序 encrypt 加密, 下行的 user1 和 user2 为示例, 将其替换为你的 MySQL 的用户名和加密密码!
- pwds = user1:+jKsgB3YAG8=, user2:GS+tr4TPgqc=
- # 设置 Atlas 的运行方式, 设为 true 时为守护进程方式, 设为 false 时为前台方式, 一般开发调试时设为 false, 线上运行时设为 true,true 后面不能有空格.
- daemon = true
- # 设置 Atlas 的运行方式, 设为 true 时 Atlas 会启动两个进程, 一个为 monitor, 一个为 worker,monitor 在 worker 意外退出后会自动将其重启, 设为 false 时只有 worker, 没有 monitor, 一般开发调试时设为 false, 线上运行时设为 true,true 后面不能有空格.
- keepalive = true
- # 工作线程数, 对 Atlas 的性能有很大影响, 可根据情况适当设置
- event-threads = 8
- # 日志级别, 分为 message,warning,critical,error,debug 五个级别
- log-level = message
- # 日志存放的路径
- log-path = /usr/local/MySQL-proxy/log
- #SQL 日志的开关, 可设置为 OFF,ON,REALTIME,OFF 代表不记录 SQL 日志, ON 代表记录 SQL 日志, REALTIME 代表记录 SQL 日志且实时写入磁盘, 默认为 OFF
- #sql-log = OFF
- # 慢日志输出设置. 当设置了该参数时, 则日志只输出执行时间超过 sql-log-slow(单位: ms) 的日志记录. 不设置该参数则输出全部日志.
- #sql-log-slow = 10
- # 实例名称, 用于同一台机器上多个 Atlas 实例间的区分
- #instance = test
- #Atlas 监听的工作接口 IP 和端口
- proxy-address = 0.0.0.0:1234
- #Atlas 监听的管理接口 IP 和端口
- admin-address = 0.0.0.0:2345
- # 分表设置, 此例中 person 为库名, mt 为表名, id 为分表字段, 3 为子表数量, 可设置多项, 以逗号分隔, 若不分表则不需要设置该项
- #tables = person.mt.id.3
- # 默认字符集, 设置该项后客户端不再需要执行 SET NAMES 语句
- charset = utf8
- # 允许连接 Atlas 的客户端的 IP, 可以是精确 IP, 也可以是 IP 段, 以逗号分隔, 若不设置该项则允许所有 IP 连接, 否则只允许列表中的 IP 连接
- #client-ips = 127.0.0.1, 192.168.1
- #Atlas 前面挂接的 LVS 的物理网卡的 IP(注意不是虚 IP), 若有 LVS 且设置了 client-ips 则此项必须设置, 否则可以不设置
- #lvs-ips = 192.168.1.1
改工作端口和管理端口 ip 为 masterip
4. 启动 Atlas
- [root@Atlas ~]# /usr/local/MySQL-proxy/bin/MySQL-proxyd test start
- OK: MySQL-Proxy of test is started
如果报错:
- /usr/local/MySQL-proxy/bin/MySQL-proxy: error while loading shared libraries: libcrypto.so.6: cannot open shared object file: No such file or directory
- error: failed to start MySQL-Proxy of test
那么 [root@Atlas ~]# yum provides */libcrypto.so.6 #寻找包含 libcrypto.so.6 的安装包
发现缺少这个
- openssl098e-0.9.8e-17.el6.CentOS.2.x86_64 : A
- ...: compatibility version of a general
- ...: cryptography and TLS library
- Repo : c6-media
- Matched from:
- Filename : /usr/lib64/libcrypto.so.6
安装 [root@Atlas ~]# yum -y install openssl098e-0.9.8e-17.el6.CentOS.2.x86_64
[root@Atlas ~]# ln -s libcrypto.so.0.9.8e libcrypto.so.6 #创建连接文件
给 Atlas 默认用户授权
master 和 slave 操作:
- MySQL> grant replication slave on *.* to 'user'@'192.168.200.%' identified by 'pwd';
- Query OK, 0 rows affected (0.04 sec)
- MySQL> flush privileges;
- Query OK, 0 rows affected (0.12 sec)
5. 查看读写分离效果
- [root@Atlas ~]# cat /usr/local/MySQL-proxy/log/test.log
- 2018-05-04 16:40:53: (message) proxy listening on port 0.0.0.0:1234
- 2018-05-04 16:40:53: (message) added read/write backend: 192.168.200.101:3306
- 2018-05-04 16:40:53: (message) added read-only backend: 192.168.200.102:3305
来源: http://www.linuxidc.com/Linux/2018-11/155152.htm