環境部署node
主服務器IP:192.168.11.11mysql
從服務器IP:192.168.11.12linux
代理服務器IP:192.168.11.13web
關閉防火牆sql
systemctl stop firewalldvim
systemctl status firewalld後端
setenforce 0服務器
getenforce負載均衡
sed -i 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
grep '^SELINUX=' /etc/selinux/configtcp
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/profilesource /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