實驗目的:mysql
搭建atlas-mysql的基本環境,實現讀寫分離。而且建立一個用戶表,以‘id’字段做爲條件進行水平切分,切分爲3個子表。linux
實驗步驟:git
1.部署兩個mysql-master節點,作雙向同步。而且配置keepalived,經過鏈接VIP保證高可用性。github
2.部署兩個mysql-slave節點,經過VIP從mysql-master同步數據redis
3.部署一個atlas節點,經過VIP鏈接mysql-master,再添加2個mysql-slave節點sql
實驗環境:數據庫
mysql-master1 Centos 6.8vim
serverid 11後端
192.168.1.102bash
192.168.1.110(VIP)
mysql-master2 Centos 6.8
serverid 12
192.168.1.103
192.168.1.110(VIP)
mysql-slave1 Centos 7.2
serverid 21
192.168.1.200
mysql-slave2 Centos 7.2
serverid 22
192.168.1.202
atlas Centos 6.8
192.168.1.104
1、 安裝mysql:
在全部mysql節點上操做
1.準備mysql組和用戶
groupadd -r -g 306 mysql
useradd -r -g mysql -u 306 -s /sbin/nologin mysql
2.準備程序包
tar -xf mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/mariadb-5.5.46-linux-x86_64//usr/local/mysql
3.修改文件權限
chown -R root:mysql /usr/local/mysql/*
4.初始化數據庫
mkdir -p /mydata/dbdata
chown mysql:mysql /mydata/dbdata
//建立數據庫存放目錄並修改權限
/usr/local/mysql/scripts/mysql_install_db--user=mysql --basedir=/usr/local/mysql --datadir=/mydata/dbdata
//初始化數據庫
5.準備基本配置文件
rm -f /etc/my.cnf
//刪除系統自帶的配置文件
mkdir /etc/mysql
cp /usr/local/mysql/support-files/my-large.cnf/etc/mysql/my.cnf
//複製配置模板
6. 添加環境變量等
echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysqld.sh
source /etc/profile.d/mysqld.sh
echo '/usr/local/mysql/lib/' >/etc/ld.so.conf.d/mysqld.conf
ldconfig
ln -s /usr/local/mysql/include/mysql/ /usr/include/mysqld
echo 'MANPATH /usr/local/mysql/man/' >>/etc/man.config
7. 配置啓動腳本
cp /usr/local/mysql/support-files/mysql.server/etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
//Centos 6直接複製程序包中提供的腳本便可
vim /usr/lib/systemd/system/mariadb.service
//Centos 7手動配置啓動腳本
[Unit] Description=MariaDBdatabase server After=syslog.target After=network.target [Service] Type=simple User=mysql Group=mysql ExecStart=/usr/local/mysql/bin/mysqld_safe--basedir=/usr/local/mysql TimeoutSec=300 [Install] WantedBy=multi-user.target
systemctldaemon-reload
//重載systemctl
2、 進一步配置兩個mysql-master,實現雙向同步:
在兩個mysql-master節點上操做
vim/etc/mysql/my.cnf
servicemysqld start
//啓動服務
mysql
//登陸當前節點上的mysql進一步操做
INSTALL PLUGIN rpl_semi_sync_masterSONAME 'semisync_master.so';
INSTALL PLUGIN rpl_semi_sync_slave SONAME'semisync_slave.so';
//安裝半同步插件,兩個master節點都裝
GRANTREPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@'192.168.1.%' IDENTIFIEDBY '12345';
FLUSHPRIVILEGES;
//每一個master節點上建立一個同步用帳戶,保持一致
CHANGE MASTER TOMASTER_HOST='192.168.1.103',MASTER_USER='repl',MASTER_PASSWORD='12345';
//在master1上執行,指向master2的ip地址
CHANGE MASTER TOMASTER_HOST='192.168.1.102',MASTER_USER='repl',MASTER_PASSWORD='12345';
//在master2上執行,指向master1的ip地址
START SLAVE;
//全部master節點啓動SLAVE功能
exit
vim/etc/mysql/my.cnf
//再次修改配置文件,使半同步插件自動啓動
servicemysqld restart
//重啓服務
驗證
mysql
//分別登陸兩個master節點進行驗證
SHOW SLAVESTATUS \G;
//查看同步是否正常
SHOW STATUS LIKE'rpl_%';
//查看半同步狀態
3、 配置keepalived,實現mysql-master的高可用性:
在兩個mysql-master節點上操做,master1爲主且配置爲非搶佔模式,master2爲備
yuminstall -y keepalived
//安裝
vim/etc/keepalived/keepalived.conf
//配置keepalived
master1節點的配置:
! ConfigurationFile for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_fromAlexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_scriptchk_mysqld { script "/usr/sbin/ss -tanl | grep 3306&> /dev/null" interval 1 weight -2 } vrrp_instancemysqld { state BACKUP nopreempt interface eth0 virtual_router_id 100 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 12345 } virtual_ipaddress { 192.168.1.110 dev eth0 label eth0:0 } track_interface { eth0 } track_script { chk_mysqld } }
master2節點的配置:
! ConfigurationFile for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_fromAlexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_scriptchk_mysqld { script "/etc/keepalived/chk_mysqld.sh" interval 1 weight -2 } vrrp_instancemysqld { state BACKUP interface eth0 virtual_router_id 100 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 12345 } virtual_ipaddress { 192.168.1.110 dev eth0 label eth0:0 } track_interface { eth0 } track_script { chk_mysqld } }
/etc/keepalived/chk_mysqld.sh
//配置master2節點上的監控腳本
chmod+x /etc/keepalived/chk_mysqld.sh
vim/etc/rc.d/init.d/mysqld
//修改master2節點上的mysql啓動腳本
chkconfigkeepalived on
servicekeepalived start
//啓動keepalived
驗證
1.初始狀態
2.關閉master1上的mysql服務
3.重啓master1上的mysql服務
master1節點不會奪回VIP資源
4.關閉master2上的mysql服務
5.重啓master2上的mysql服務
VIP仍然在master1節點上
4、配置mysql-slave節點:
在兩個mysql-slave節點上操做
vim/etc/mysql/my.cnf
systemctlrestart mariadb
//啓動slave節點上的mysql服務
mysql
//登陸當前slave節點的mysql服務作進一步配置
INSTALL PLUGIN rpl_semi_sync_slave SONAME'semisync_slave.so';
//安裝半同步插件
CHANGE MASTER TOMASTER_HOST='192.168.1.110',MASTER_USER='repl',MASTER_PASSWORD='12345';
//將master指向mysql-master的VIP
START SLAVE;
exit
vim/etc/mysql/my.cnf
//再次修改配置文件,使半同步插件自動啓動
systemctlrestart mariadb
//重啓mysql服務
驗證
登陸slave節點上的mysql服務進行查看
VIP目前在mysql-master1節點上,登陸該節點上的mysql服務進行查看
測試數據同步
登陸mysql-master1節點的mysql服務進行操做
GRANT ALL ON *.*TO 'dbadmin'@'%' IDENTIFIED BY '12345';
GRANT ALL ON *.*TO 'dbadmin'@'localhost' IDENTIFIED BY '12345';
FLUSHPRIVILEGES;
//建立兩個帳戶,去其餘mysql節點上查看
5、安裝並配置atlas:
在atlas節點上操做
1.安裝atlas
軟件獲取網址: https://github.com/Qihoo360/Atlas/releases
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
2.配置atlas
/usr/local/mysql-proxy/bin/encrypt12345
//使用atlas提供的程序對數據庫密碼進行加密操做,配置文件中必須填寫密文
cp/usr/local/mysql-proxy/conf/test.cnf /usr/local/mysql-proxy/conf/mymysql.cnf
//複製配置模板來建立個實例,實例名爲mymysql
vim /usr/local/mysql-proxy/conf/mymysql.cnf
[mysql-proxy] #帶#號的爲非必需的配置項目 #管理接口的用戶名 admin-username =user #管理接口的密碼 admin-password =pwd #Atlas後端鏈接的MySQL主庫的IP和端口,可設置多項,用逗號分隔 #指定mysql-master的VIP proxy-backend-addresses= 192.168.1.110:3306 #Atlas後端鏈接的MySQL從庫的IP和端口,@後面的數字表明權重,用來做負載均衡,若省略則默認爲1,可設置多項,用逗號分隔 #指定兩個mysql-slave的ip,用逗號隔開 proxy-read-only-backend-addresses= 192.168.1.200:3306@1,192.168.1.202:3306@1 #用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2爲示例,將其替換爲你的MySQL的用戶名和加密密碼! #使用前面建立的數據庫帳號,密碼必須使用密文 pwds =dbadmin:tO5B+RteaNo= #設置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 =2 #日誌級別,分爲message、warning、critical、error、debug五個級別 log-level =warning #日誌存放的路徑 log-path =/var/log/mysql-proxy/ #SQL日誌的開關,可設置爲OFF、ON、REALTIME,OFF表明不記錄SQL日誌,ON表明記錄SQL日誌,REALTIME表明記錄SQL日誌且實時寫入磁盤,默認爲OFF #sql-log = OFF #慢日誌輸出設置。當設置了該參數時,則日誌只輸出執行時間超過sql-log-slow(單位:ms)的日誌記錄。不設置該參數則輸出所有日誌。 #sql-log-slow =10 #實例名稱,用於同一臺機器上多個Atlas實例間的區分 instance =mymysql #Atlas監聽的工做接口IP和端口 proxy-address =0.0.0.0:1234 #Atlas監聽的管理接口IP和端口 admin-address =0.0.0.0:2345 #分表設置,此例中testdb爲庫名,user爲表名,id爲分表字段,3爲子表數量(若是設置爲偶數,貌似其中一個子表不起做用),可設置多項,以逗號分隔,若不分表則不須要設置該項 tables =testdb.user.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
mkdir/var/log/mysql-proxy/
//建立日誌存放路徑
vim/etc/rc.d/init.d/mymysql-proxy
//配置啓動腳本
#!/bin/sh # # atlas init file for starting up the atlasdaemon # # chkconfig: - 20 80 # description:Starts and stops the redis daemon. # Sourcefunction library. ./etc/rc.d/init.d/functions instance=mymysql start() { /usr/local/mysql-proxy/bin/mysql-proxyd$instance start } stop() { /usr/local/mysql-proxy/bin/mysql-proxyd$instance stop } restart() { stop start } status() { /usr/local/mysql-proxy/bin/mysql-proxyd$instance status } case"$1" in start) $1 ;; stop) $1 ;; restart) $1 ;; status) $1 ;; *) echo $"Usage: $0{start|stop|status|restart}" exit 2 esac
chmod+x /etc/rc.d/init.d/mymysql-proxy
chkconfig--add mymysql-proxy
chkconfigmymysql-proxy on
servicemymysql-proxy start
//啓動服務
測試:
mysql-h 192.168.1.104 --port 1234 -udbadmin -p12345
//鏈接atlas的代理地址
CREATE DATABASEtestdb;
use testdb;
CREATE TABLE user_0 (id int UNSIGNED NOT NULLPRIMARY KEY,name VARCHAR(20) NOT NULL,age tinyint UNSIGNED);
CREATE TABLE user_1 (id int UNSIGNED NOT NULLPRIMARY KEY,name VARCHAR(20) NOT NULL,age tinyint UNSIGNED);
CREATE TABLE user_2 (id int UNSIGNED NOT NULLPRIMARY KEY,name VARCHAR(20) NOT NULL,age tinyint UNSIGNED);
//按照格式,建立3個user表的子表,序號從0開始
INSERT testdb.userVALUES (1,'user01',11),(2,'user02',12);
INSERT testdb.userVALUES (3,'user03',13),(4,'user04',14);
INSERT testdb.userVALUES (5,'user05',15),(6,'user06',16);
INSERT testdb.userVALUES (7,'user07',17),(8,'user08',18);
INSERT testdb.userVALUES (9,'user09',19),(10,'user10',20);
//執行5條插入語句,每一個語句插入兩條數據
mysql-h 192.168.1.104 --port 2345 -uuser -ppwd
//鏈接atlas的管理地址進行查看
注:
若是要測試讀寫分離效果,能夠手動向兩個mysql-slave節點插入不一樣的數據來查看效果