天天看點

資料庫應用——Atlas代理MySQL叢集實作讀寫分離一、Atlas簡介和架構二、主伺服器配置三、從伺服器配置四、Atlas的配置

Atlas代理MySQL叢集實作讀寫分離

  • 一、Atlas簡介和架構
    • 1.1 環境準備
    • 1.2 配置時間伺服器
  • 二、主伺服器配置
    • 2.1 master節點1的配置
    • 2.2 master節點2的配置
    • 2.3 主伺服器配置完成後的測試
  • 三、從伺服器配置
    • 3.1 slave節點1的配置
    • 3.1 slave節點2的配置
    • 3.3 從伺服器配置完成後的測試
  • 四、Atlas的配置
    • 4.1 下載下傳并安裝Atlas
    • 4.2 配置讀寫分離
    • 4.3 啟動Atlas軟體
    • 4.4 測試Atlas代理MySQL

一、Atlas簡介和架構

  • Atlas是由 Qihoo 360, Web平台部基礎架構團隊開發維護的一個基于MySQL協定的資料中間層項目。它是在mysql-prox 0.8.2版本的基礎上,對其進行了優化,增加了一些新的功能特性。360内部使用Atlas運作的mysql業務,每天承載的讀寫請求數達幾十億條。
  • Atlas是一個位于應用程式與MySQL之間中間件。在後端DB看來,Atlas相當于連接配接它的用戶端,在前端應用看來,Atlas相當于一個DB。Atlas作為服務端與應用程式通訊,它實作了MySQL的用戶端和服務端協定,同時作為用戶端與MySQL通訊。它對應用程式屏蔽了DB的細節,同時為了降低MySQL負擔,它還維護了連接配接池。Atlas的整體架構,可參考下面這兩幅圖:
    資料庫應用——Atlas代理MySQL叢集實作讀寫分離一、Atlas簡介和架構二、主伺服器配置三、從伺服器配置四、Atlas的配置
資料庫應用——Atlas代理MySQL叢集實作讀寫分離一、Atlas簡介和架構二、主伺服器配置三、從伺服器配置四、Atlas的配置

1.1 環境準備

IP位址 主機參數

atlas_agent

192.168.100.10

mysql_master_node01

192.168.100.20

mysql_master_node02

192.168.100.21

mysql_slave_node01

192.168.100.11

mysql_slave_node02

192.168.100.12
#MySQL主從節點安裝MySQL5.7
yum -y install mysql-community-server*

#啟動MySQL
systemctl start mysqld

#查找MySQL初始密碼并進行修改
grep password /var/log/mysqld.log
mysqladmin -uroot -p'g<jrr-jrU8-D' password '[email protected]'
           

1.2 配置時間伺服器

#建立時間同步環境,在主節點上搭建時間同步伺服器
[[email protected]_master_node01 ~]# yum -y install ntp

#配置NTP
[[email protected]_master_node01 ~]# vim /etc/ntp.conf
server 127.127.1.0
Fudge 127.127.1.0 stratum 8           #注釋原有pool下的server配置,設定時區為+08區

#重新開機服務并設定為開機啟動
[[email protected]_master_node01 ~]# systemctl restart ntpd && systemctl enable ntpd

#在其餘節點上進行時間同步
yum -y install ntpdate
ntpdate 192.168.100.20
           

二、主伺服器配置

2.1 master節點1的配置

#啟動二進制日志
[[email protected]_master_node01 ~]# vim /etc/my.cnf
[mysqld]
log_bin                                                 #開啟二進制目錄
server-id=1                                             #指明伺服器ID
gtid_mode=ON                               
enforce_gtid_consistency=1
[[email protected]_master_node01 ~]# systemctl restart mysqld  #重新開機以使配置生效

#登入MySQL授權複制使用者
[[email protected]_master_node01 ~]# mysql -uroot -p
mysql> grant replication slave,replication client on *.* to 'myslave'@'192.168.100.%' identified by '[email protected]';
#該動作并不是叢集中必要的動作,是一次授權行為,該賬戶僅用于叢集同步時使用
#replication:複制
mysql> flush privileges;

#設定對應主伺服器
mysql> change master to
    -> master_host='192.168.100.21',
    -> master_user='myslave',
    -> master_password='[email protected]',
    -> master_auto_position=1;                           #自動進行位置記錄

mysql> start slave;
mysql> show slave status\G
           

2.2 master節點2的配置

[[email protected]_master_node02 ~]# vim /etc/my.cnf
[mysqld]
log_bin                                                 #開啟二進制目錄
server-id=2                                             #指明伺服器ID
gtid_mode=ON
enforce_gtid_consistency=1
[[email protected]_master_node02 ~]# systemctl restart mysqld  #重新開機以使配置生效

#登入MySQL授權複制使用者
[[email protected]_master_node02 ~]# mysql -uroot -p
mysql> grant replication slave,replication client on *.* to 'myslave'@'192.168.100.%' identified by '[email protected]';
mysql> flush privileges;

#設定對應主伺服器
mysql> change master to
    -> master_host='192.168.100.20',
    -> master_user='myslave',
    -> master_password='[email protected]',
    -> master_auto_position=1;                           #自動進行位置記錄

mysql> start slave;
mysql> show slave status\G
           

2.3 主伺服器配置完成後的測試

# master_node01建表,node02檢視
mysql> create database master;
           
資料庫應用——Atlas代理MySQL叢集實作讀寫分離一、Atlas簡介和架構二、主伺服器配置三、從伺服器配置四、Atlas的配置
# master_node02建表,node01檢視
mysql> create table Y2101(id int);
           
資料庫應用——Atlas代理MySQL叢集實作讀寫分離一、Atlas簡介和架構二、主伺服器配置三、從伺服器配置四、Atlas的配置

雙方同步成功,雙主設定完成!

三、從伺服器配置

#同步現有資料庫并發送至從伺服器

[[email protected]_master_node01 ~]# mysqldump -p'[email protected]' --all-databases --single-transaction --master-data=2 --flush-logs > mmss-mysql-all.sql
#所有的庫,保持資料可用性,不關機的備份 InnoDB一緻性服務可用性,注釋掉二進制日志記錄,切斷日志重定向到備份檔案

[[email protected]_master_node01 ~]# scp -r mmss-mysql-all.sql 192.168.100.11:/tmp
[[email protected]_master_node01 ~]# scp -r mmss-mysql-all.sql 192.168.100.12:/tmp
           

3.1 slave節點1的配置

#從伺服器資料還原
[[email protected]_slave_node01 ~]# mysql -uroot -p'[email protected]' </tmp/mmss-mysql-all.sql
#配置MySQL配置檔案
[[email protected]_slave_node01 ~]# vim /etc/my.cnf
[mysqld]
server-id=3
gtid_mode=ON                                           #GTID開啟
enforce_gtid_consistency=1                             #開啟自動協商ID
master-info-repository=TABLE                           #将主伺服器的資訊存在表,更安全
relay-log-info-repository=TABLE                        #把中繼日志也存在表裡,更安全
[[email protected]_slave_node01 ~]# systemctl restart mysqld  #重新開機以配置生效

mysql> change master to
    -> master_host='192.168.100.20',                   #改變主伺服器
    -> master_user='myslave',                          #拷貝時所使用的使用者
    -> master_password='[email protected]',                   #賬号的密碼
    -> master_auto_position=1                          #自動進行位置記錄
    -> for channel '192.168.100.20';                   #第一通道
mysql> change master to
    -> master_host='192.168.100.21',
    -> master_user='myslave',
    -> master_password='[email protected]',
    -> master_auto_position=1
    -> for channel '192.168.100.21';                   #第二通道
mysql> start slave;                                    #啟動從伺服器
mysql> show slave status\G
           
資料庫應用——Atlas代理MySQL叢集實作讀寫分離一、Atlas簡介和架構二、主伺服器配置三、從伺服器配置四、Atlas的配置

3.1 slave節點2的配置

#從伺服器資料還原
[[email protected]_slave_node02 ~]# mysql -uroot -p'[email protected]' </tmp/mmss-mysql-all.sql
#配置MySQL配置檔案
[[email protected]_slave_node02 ~]# vim /etc/my.cnf
[mysqld]
server-id=4
gtid_mode=ON                                           #GTID開啟
enforce_gtid_consistency=1                             #開啟自動協商ID
master-info-repository=TABLE                           #将主伺服器的資訊存在表,更安全
relay-log-info-repository=TABLE                        #把中繼日志也存在表裡,更安全
[[email protected]_slave_node02 ~]# systemctl restart mysqld  #重新開機以配置生效

[[email protected]_slave_node02 ~]# mysql -uroot -p
mysql> change master to
    -> master_host='192.168.100.20',                   #改變主伺服器
    -> master_user='myslave',                          #拷貝時所使用的使用者
    -> master_password='[email protected]',                   #賬号的密碼
    -> master_auto_position=1                          #自動進行位置記錄
    -> for channel '192.168.100.20';                   #第一通道
mysql> change master to
    -> master_host='192.168.100.21',
    -> master_user='myslave',
    -> master_password='[email protected]',
    -> master_auto_position=1
    -> for channel '192.168.100.21';                   #第二通道
mysql> start slave;                                    #啟動從伺服器
mysql> show slave status\G
           
資料庫應用——Atlas代理MySQL叢集實作讀寫分離一、Atlas簡介和架構二、主伺服器配置三、從伺服器配置四、Atlas的配置

3.3 從伺服器配置完成後的測試

#插入資料後分别進入從伺服器檢視資料

[[email protected]_master_node01 ~]# mysql -uroot -p'[email protected]' -e 'insert into master.Y2101 values (20)';
[[email protected]_master_node02 ~]# mysql -uroot -p'[email protected]' -e 'insert into master.Y2101 values (21)';
           

四、Atlas的配置

4.1 下載下傳并安裝Atlas

  • Atlas官方網站
#下載下傳Atlas
[[email protected]_agent ~]# wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm

#安裝Atlas
[[email protected]_agent ~]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
           
  • 安裝完成後會預設在

    /usr/local/mysql-proxy

    下生成4個檔案夾,以及需要配置的檔案
    • bin

      目錄下放的都是可執行檔案
      1. encrypt

        是用來生成MySQL密碼加密的,在配置的時候會用到
      2. mysql-proxy

        是MySQL自己的讀寫分離代理
      3. mysql-proxyd

        是360的,後面有個“d”,服務的啟動、重新開機、停止。都是用他來執行的
    • conf

      目錄下放的是配置檔案
      1. test.cnf

        隻有一個檔案,用來配置代理的,可以使用vim來編輯
    • lib

      目錄下放的是一些包,以及Atlas的依賴
    • log

      目錄下放的是日志,如報錯等錯誤資訊的記錄

4.2 配置讀寫分離

#配置4台伺服器節點開放權限給Atlas,四台伺服器同時操作
mysql> grant all on *.* to [email protected]'192.168.100.%' identified by '[email protected]';
mysql> flush privileges;

#加密使用者密碼
[[email protected]_agent ~]# /usr/local/mysql-proxy/bin/encrypt [email protected]
Tw8uck69VjyTZ6zxvGQr9A==

#編輯test.cnf配置檔案
[[email protected]_agent ~]# vim /usr/local/mysql-proxy/conf/test.cnf

[mysql-proxy]

#帶#号的為非必需的配置項目

#管理接口的使用者名
admin-username = user

#管理接口的密碼
admin-password = pwd

#Atlas後端連接配接的MySQL主庫的IP和端口,可設定多項,用逗号分隔
proxy-backend-addresses = 192.168.100.20:3306,192.168.100.21:3306

#Atlas後端連接配接的MySQL從庫的IP和端口,@後面的數字代表權重,用來作負載均衡,若省略則預設為1,可設定多項,用逗号分隔
proxy-read-only-backend-addresses = 192.168.100.11:[email protected],[email protected]

#使用者名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程式encrypt加密,下行的user1和user2為示例,将其替換為你的MySQL的使用者名和加密密碼!
pwds = test:Tw8uck69VjyTZ6zxvGQr9A==

#Atlas監聽的工作接口IP和端口
proxy-address = 0.0.0.0:1234

#Atlas監聽的管理接口IP和端口
admin-address = 0.0.0.0:2345
           

4.3 啟動Atlas軟體

#配置無誤後,啟動Atlas軟體
[[email protected]_agent ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started

#關閉軟體
/usr/local/mysql-proxy/bin/mysql-proxyd test stop
           

4.4 測試Atlas代理MySQL

#安裝MySQL
[[email protected]_agent ~]# yum -y install mysql-community-server*

#用管理賬号user登入2345管理接口IP和端口
[[email protected]_agent ~]# mysql -h 127.0.0.1 -P 2345 -uuser -ppwd

#檢視幫助
mysql> select * from help;

#檢視伺服器叢集中的伺服器狀态
mysql> select * from backends;
           
資料庫應用——Atlas代理MySQL叢集實作讀寫分離一、Atlas簡介和架構二、主伺服器配置三、從伺服器配置四、Atlas的配置
#通過代理通路Mysql
[[email protected]_agent ~]# mysql -h192.168.100.10  -utest -p'[email protected]' -P1234
#查詢
mysql> select * from master.Y2101;
           
資料庫應用——Atlas代理MySQL叢集實作讀寫分離一、Atlas簡介和架構二、主伺服器配置三、從伺服器配置四、Atlas的配置