實(shí)驗(yàn)?zāi)康?
渭源ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
搭建atlas-MySQL的基本環(huán)境,實(shí)現(xiàn)讀寫分離。并且創(chuàng)建一個(gè)用戶表,以‘id’字段作為條件進(jìn)行水平切分,切分為3個(gè)子表。
實(shí)驗(yàn)步驟:
1.部署兩個(gè)mysql-master節(jié)點(diǎn),做雙向同步。并且配置keepalived,通過連接VIP保證高可用性。
2.部署兩個(gè)mysql-slave節(jié)點(diǎn),通過VIP從mysql-master同步數(shù)據(jù)
3.部署一個(gè)atlas節(jié)點(diǎn),通過VIP連接mysql-master,再添加2個(gè)mysql-slave節(jié)點(diǎn)
實(shí)驗(yàn)環(huán)境:
mysql-master1 Centos 6.8
serverid 11
192.168.1.102
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
一、 安裝mysql:
在所有mysql節(jié)點(diǎn)上操作
1.準(zhǔn)備mysql組和用戶
groupadd -r -g 306 mysql
useradd -r -g mysql -u 306 -s /sbin/nologin mysql
2.準(zhǔn)備程序包
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.修改文件權(quán)限
chown -R root:mysql /usr/local/mysql/*
4.初始化數(shù)據(jù)庫(kù)
mkdir -p /mydata/dbdata
chown mysql:mysql /mydata/dbdata
//創(chuàng)建數(shù)據(jù)庫(kù)存放目錄并修改權(quán)限
/usr/local/mysql/scripts/mysql_install_db--user=mysql --basedir=/usr/local/mysql --datadir=/mydata/dbdata
//初始化數(shù)據(jù)庫(kù)
5.準(zhǔn)備基本配置文件
rm -f /etc/my.cnf
//刪除系統(tǒng)自帶的配置文件
mkdir /etc/mysql
cp /usr/local/mysql/support-files/my-large.cnf/etc/mysql/my.cnf
//復(fù)制配置模板
6.添加環(huán)境變量等
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.配置啟動(dòng)腳本
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直接復(fù)制程序包中提供的腳本即可
vim /usr/lib/systemd/system/mariadb.service
//Centos 7手動(dòng)配置啟動(dòng)腳本
[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
二、 進(jìn)一步配置兩個(gè)mysql-master,實(shí)現(xiàn)雙向同步:
在兩個(gè)mysql-master節(jié)點(diǎn)上操作
vim/etc/mysql/my.cnf
servicemysqld start
//啟動(dòng)服務(wù)
mysql
//登錄當(dāng)前節(jié)點(diǎn)上的mysql進(jìn)一步操作
INSTALL PLUGIN rpl_semi_sync_masterSONAME 'semisync_master.so';
INSTALL PLUGIN rpl_semi_sync_slave SONAME'semisync_slave.so';
//安裝半同步插件,兩個(gè)master節(jié)點(diǎn)都裝
GRANTREPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@'192.168.1.%' IDENTIFIEDBY '12345';
FLUSHPRIVILEGES;
//每個(gè)master節(jié)點(diǎn)上創(chuàng)建一個(gè)同步用賬戶,保持一致
CHANGE MASTER TOMASTER_HOST='192.168.1.103',MASTER_USER='repl',MASTER_PASSWORD='12345';
//在master1上執(zhí)行,指向master2的ip地址
CHANGE MASTER TOMASTER_HOST='192.168.1.102',MASTER_USER='repl',MASTER_PASSWORD='12345';
//在master2上執(zhí)行,指向master1的ip地址
START SLAVE;
//所有master節(jié)點(diǎn)啟動(dòng)SLAVE功能
exit
vim/etc/mysql/my.cnf
//再次修改配置文件,使半同步插件自動(dòng)啟動(dòng)
servicemysqld restart
//重啟服務(wù)
驗(yàn)證
mysql
//分別登錄兩個(gè)master節(jié)點(diǎn)進(jìn)行驗(yàn)證
SHOW SLAVESTATUS \G;
//查看同步是否正常
SHOW STATUS LIKE'rpl_%';
//查看半同步狀態(tài)
三、 配置keepalived,實(shí)現(xiàn)mysql-master的高可用性:
在兩個(gè)mysql-master節(jié)點(diǎn)上操作,master1為主且配置為非搶占模式,master2為備
yuminstall -y keepalived
//安裝
vim/etc/keepalived/keepalived.conf
//配置keepalived
master1節(jié)點(diǎn)的配置:
! 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節(jié)點(diǎn)的配置:
! 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節(jié)點(diǎn)上的監(jiān)控腳本
chmod+x /etc/keepalived/chk_mysqld.sh
vim/etc/rc.d/init.d/mysqld
//修改master2節(jié)點(diǎn)上的mysql啟動(dòng)腳本
chkconfigkeepalived on
servicekeepalived start
//啟動(dòng)keepalived
驗(yàn)證
1.初始狀態(tài)
2.關(guān)閉master1上的mysql服務(wù)
3.重啟master1上的mysql服務(wù)
master1節(jié)點(diǎn)不會(huì)奪回VIP資源
4.關(guān)閉master2上的mysql服務(wù)
5.重啟master2上的mysql服務(wù)
VIP仍然在master1節(jié)點(diǎn)上
四、配置mysql-slave節(jié)點(diǎn):
在兩個(gè)mysql-slave節(jié)點(diǎn)上操作
vim/etc/mysql/my.cnf
systemctlrestart mariadb
//啟動(dòng)slave節(jié)點(diǎn)上的mysql服務(wù)
mysql
//登錄當(dāng)前slave節(jié)點(diǎn)的mysql服務(wù)做進(jìn)一步配置
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
//再次修改配置文件,使半同步插件自動(dòng)啟動(dòng)
systemctlrestart mariadb
//重啟mysql服務(wù)
驗(yàn)證
登錄slave節(jié)點(diǎn)上的mysql服務(wù)進(jìn)行查看
VIP目前在mysql-master1節(jié)點(diǎn)上,登錄該節(jié)點(diǎn)上的mysql服務(wù)進(jìn)行查看
測(cè)試數(shù)據(jù)同步
登錄mysql-master1節(jié)點(diǎn)的mysql服務(wù)進(jìn)行操作
GRANT ALL ON *.*TO 'dbadmin'@'%' IDENTIFIED BY '12345';
GRANT ALL ON *.*TO 'dbadmin'@'localhost' IDENTIFIED BY '12345';
FLUSHPRIVILEGES;
//創(chuàng)建兩個(gè)賬戶,去其他mysql節(jié)點(diǎn)上查看
五、安裝并配置atlas:
在atlas節(jié)點(diǎn)上操作
1.安裝atlas
軟件獲取網(wǎng)址: 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提供的程序?qū)?shù)據(jù)庫(kù)密碼進(jìn)行加密操作,配置文件中必須填寫密文
cp/usr/local/mysql-proxy/conf/test.cnf /usr/local/mysql-proxy/conf/mymysql.cnf
//復(fù)制配置模板來創(chuàng)建個(gè)實(shí)例,實(shí)例名為mymysql
vim /usr/local/mysql-proxy/conf/mymysql.cnf
[mysql-proxy] #帶#號(hào)的為非必需的配置項(xiàng)目 #管理接口的用戶名 admin-username =user #管理接口的密碼 admin-password =pwd #Atlas后端連接的MySQL主庫(kù)的IP和端口,可設(shè)置多項(xiàng),用逗號(hào)分隔 #指定mysql-master的VIP proxy-backend-addresses= 192.168.1.110:3306 #Atlas后端連接的MySQL從庫(kù)的IP和端口,@后面的數(shù)字代表權(quán)重,用來作負(fù)載均衡,若省略則默認(rèn)為1,可設(shè)置多項(xiàng),用逗號(hào)分隔 #指定兩個(gè)mysql-slave的ip,用逗號(hào)隔開 proxy-read-only-backend-addresses= 192.168.1.200:3306@1,192.168.1.202:3306@1 #用戶名與其對(duì)應(yīng)的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼! #使用前面創(chuàng)建的數(shù)據(jù)庫(kù)賬號(hào),密碼必須使用密文 pwds =dbadmin:tO5B+RteaNo= #設(shè)置Atlas的運(yùn)行方式,設(shè)為true時(shí)為守護(hù)進(jìn)程方式,設(shè)為false時(shí)為前臺(tái)方式,一般開發(fā)調(diào)試時(shí)設(shè)為false,線上運(yùn)行時(shí)設(shè)為true,true后面不能有空格。 daemon = true #設(shè)置Atlas的運(yùn)行方式,設(shè)為true時(shí)Atlas會(huì)啟動(dòng)兩個(gè)進(jìn)程,一個(gè)為monitor,一個(gè)為worker,monitor在worker意外退出后會(huì)自動(dòng)將其重啟,設(shè)為false時(shí)只有worker,沒有monitor,一般開發(fā)調(diào)試時(shí)設(shè)為false,線上運(yùn)行時(shí)設(shè)為true,true后面>不能有空格。 keepalive = true #工作線程數(shù),對(duì)Atlas的性能有很大影響,可根據(jù)情況適當(dāng)設(shè)置 event-threads =2 #日志級(jí)別,分為message、warning、critical、error、debug五個(gè)級(jí)別 log-level =warning #日志存放的路徑 log-path =/var/log/mysql-proxy/ #SQL日志的開關(guān),可設(shè)置為OFF、ON、REALTIME,OFF代表不記錄SQL日志,ON代表記錄SQL日志,REALTIME代表記錄SQL日志且實(shí)時(shí)寫入磁盤,默認(rèn)為OFF #sql-log = OFF #慢日志輸出設(shè)置。當(dāng)設(shè)置了該參數(shù)時(shí),則日志只輸出執(zhí)行時(shí)間超過sql-log-slow(單位:ms)的日志記錄。不設(shè)置該參數(shù)則輸出全部日志。 #sql-log-slow =10 #實(shí)例名稱,用于同一臺(tái)機(jī)器上多個(gè)Atlas實(shí)例間的區(qū)分 instance =mymysql #Atlas監(jiān)聽的工作接口IP和端口 proxy-address =0.0.0.0:1234 #Atlas監(jiān)聽的管理接口IP和端口 admin-address =0.0.0.0:2345 #分表設(shè)置,此例中testdb為庫(kù)名,user為表名,id為分表字段,3為子表數(shù)量(如果設(shè)置為偶數(shù),貌似其中一個(gè)子表不起作用),可設(shè)置多項(xiàng),以逗號(hào)分隔,若不分表則不需要設(shè)置該項(xiàng) tables =testdb.user.id.3 #默認(rèn)字符集,設(shè)置該項(xiàng)后客戶端不再需要執(zhí)行SET NAMES語句 #charset = utf8 #允許連接Atlas的客戶端的IP,可以是精確IP,也可以是IP段,以逗號(hào)分隔,若不設(shè)置該項(xiàng)則允許所有IP連接,否則只允許列表中的IP連接 #client-ips =127.0.0.1, 192.168.1 #Atlas前面掛接的LVS的物理網(wǎng)卡的IP(注意不是虛IP),若有LVS且設(shè)置了client-ips則此項(xiàng)必須設(shè)置,否則可以不設(shè)置 #lvs-ips =192.168.1.1
mkdir/var/log/mysql-proxy/
//創(chuàng)建日志存放路徑
vim/etc/rc.d/init.d/mymysql-proxy
//配置啟動(dòng)腳本
#!/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
//啟動(dòng)服務(wù)
測(cè)試:
mysql-h 192.168.1.104 --port 1234 -udbadmin -p12345
//連接atlas的代理地址
CREATE DATABASEtestdb;
use testdb;
CREATE TABLEuser_0 (id int UNSIGNED NOT NULLPRIMARY KEY,name VARCHAR(20) NOT NULL,age tinyint UNSIGNED);
CREATE TABLEuser_1(id int UNSIGNED NOT NULLPRIMARY KEY,name VARCHAR(20) NOT NULL,age tinyint UNSIGNED);
CREATE TABLEuser_2 (id int UNSIGNED NOT NULLPRIMARY KEY,name VARCHAR(20) NOT NULL,age tinyint UNSIGNED);
//按照格式,創(chuàng)建3個(gè)user表的子表,序號(hào)從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);
//執(zhí)行5條插入語句,每個(gè)語句插入兩條數(shù)據(jù)
mysql-h 192.168.1.104 --port 2345 -uuser -ppwd
//連接atlas的管理地址進(jìn)行查看
注:
如果要測(cè)試讀寫分離效果,可以手動(dòng)向兩個(gè)mysql-slave節(jié)點(diǎn)插入不同的數(shù)據(jù)來查看效果
網(wǎng)站題目:從零開始部署一個(gè)atlas-mysql基本架構(gòu)
瀏覽路徑:http://aaarwkj.com/article30/psohpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站收錄、網(wǎng)頁(yè)設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、電子商務(wù)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)