环境部署
主服务器IP:192.168.11.11
从服务器IP:192.168.11.12
代理服务器IP:192.168.11.13
关闭防火墙
systemctl stop firewalld
systemctl status firewalld
setenforce 0
getenforce
sed -i 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
grep '^SELINUX=' /etc/selinux/config
MySQL部署
主从服务器安装并启动MySQL
yum install -y mariadb mariadb-server
systemctl restart mariadb
MySQL主从
主服务器配置
vim /etc/my.cnf
[mysqld] 添加如下内容
server_id=11 #配置id号标识
log-bin=mysql-bin
skip_name_resolv=1 #跳过域名解析
systemctl restart mariadb
mysql
mysql> grant replication slave,
->replication client on *.* to zo(用户)@'192.168.11.%' identified by 'zo'(密码); 创建账号
mysql> flush privileges;
查看并记下
show master status;
若互为主从则要加如下命令
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.11.12',
-> MASTER_USER='用户名',
-> MASTER_PASSWORD='密码',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=0,
-> MASTER_CONNECT_RETRY=10;
在从服务器的库创建用户和密码
mysql> grant replication slave,
->replication client on *.* to 用户名@'192.168.11.%' identified by '密码'; 创建账号
从服务器
测试账号
[root@slave1 ~]# mysql -h 192.168.11.11 -uzo -pzo 指向主 测试连通性
vim /etc/my.cnf
[mysqld] 添加如下内容
server_id=12 #配置id号标识
log-bin=slave1-bin
skip_name_resolv=1 #跳过域名解析
systemctl restart mariadb
mysql
MariaDB [(none)]> help change master to
CHANGE MASTER TO
MASTER_HOST='192.168.11.11',
MASTER_USER='zo',
MASTER_PASSWORD='zo',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=485,
MASTER_CONNECT_RETRY=10;
mysql> slave start;
mysql> show slave status\G
MySQL读写分离
主服务器创建库用户及查看
mysql -uroot -e "grant all on *.* to admin@'%' identified by '01';flush privileges;"
mysql -uroot -e "grant replication slave on *.* to rep@'%' identified by '01';flush privileges;"
mysql -uroot -e "select user,host,password from mysql.user;"
mysql -uroot -e "reset master;show master status;"
从服务器创建库用户及查看
mysql -uroot -e "grant all on *.* to admin@'%' identified by '01';flush privileges;"
mysql -uroot -e "grant replication slave on *.* to rep@'%' identified by '01';flush privileges;"
mysql -uroot -e "select user,host,password from mysql.user;"
mysql -uroot -e "change master to master_host='192.168.11.11',master_user='rep',master_password='01',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=245;"
sleep 30s
mysql -uroot -e "start slave;show slave status\G"
代理服务器配置
下载代理软件包atlas
MySQL代理软件工具 down.51cto.com/data/2459346
安装软件包
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
rpm -ql Atlas
echo "PATH=$PATH:/usr/local/mysql-proxy/bin/" > /etc/profile.d/atlas.sh
source /etc/profile.d/atlas.sh 加载环境配置文件
ll /usr/local/mysql-proxy/
cd /usr/local/mysql-proxy/bin/
[root@node13 bin]# ./encrypt 01 创建密码为01的密钥
cd /usr/local/mysql-proxy/conf/
cp -av test.cnf test.cnf.bak 备份文件
vim test.cnf 修改后的读写分离的完整配置文件内容
[mysql-proxy]
#带#号的为非必需的配置项目
#管理接口的用户名
admin-username = user
#管理接口的密码
admin-password = pwd
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 127.0.0.1:3306
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认>为1,可设置多项,用逗号分隔
#proxy-read-only-backend-addresses = 127.0.0.1:3305@1
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行
的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = admin:VFnEp9P4Vu4=, rep:VFnEp9P4Vu4=
daemon = true
keepalive = true
event-threads = 8
log-level = message
log-path = /usr/local/mysql-proxy/log
#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:3306
#Atlas监听的管理接口IP和端口
admin-address = 0.0.0.0:2345
启动atlas服务:/usr/local/mysql-proxy/bin/mysql-proxyd test start
查atlas端口号:lsof -i :3306 和 lsof -i :2345
重启atlas服务:/usr/local/mysql-proxy/bin/mysql-proxyd test restart
设置mysql-proxyd开机启动:
echo "/usr/local/mysql-proxy/bin/mysql-proxyd test start" >> /etc/profile
source /etc/profile
tcpdump抓包: tcpdump -i ens33 port 3306
说明:抓取经过192.168.10.11代理主机的ens33网卡的3306端口的数据包,验证读写分离效果。
访问atlas代理服务器的测试:
yum install -y mariadb 安装mariadb客户端软件
mysql -uadmin -p01 -h 192.168.11.13 -P3306
登录到atlas管理端:mysql -uuser -ppwd -h 192.168.11.13 -P2345