前言node
爲了下降一臺數據庫的IO,遠程鏈接數據庫的時候,能夠實現讀寫分離進行調度。這裏就出現了一個單點,因此必需要作一個高可用。固然數據庫服務器也須要作主從複製。mysql
實驗結構nginx
說明:上圖的拓撲只是整個架構中的一個小部分,其他功能的實現此圖並未規劃出來。此拓撲實現的目的是利用proxysql實現數據讀寫分離,並對proxysql高可用。兩臺安裝了Keepalived和proxysql虛擬成一個VIP對外提供服務。這兩臺mysql服務器作的半同步複製,192.168.32.111是主節點負責用戶的寫操做,192.168.32.112從節點負責用戶的讀操做。
sql
實驗步驟數據庫
一、安裝數據庫並配置半同步複製vim
主mysql:192.168.32.111centos
]#yum install mariadb -y <===centos6爲mysql ]#vim /etc/my.cnf.d/server.cnf [server] skip_name_resolve = ON innodb_file_per_table = ON max_connection = 2000 log_bin = master-log server_id = 1 ]#systemctl start mariadb ]#mysql MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'ma'@'192.168.32.%' IDENTIFIED BY 'centos'; <===受權從節點複製的賬號 MariaDB [(none)]> SHOW MASTER LOGS; <===查看當前二進制日誌文件 +-------------------+-----------+ | Log_name |File_size | +-------------------+-----------+ | master-log.000001 | 490 | +-------------------+-----------+ MariaDB [(none)]> SHOW BINLOG EVENTS IN 'master-log.000001'; <===查看日誌內容保存點,從節點能夠根據須要選擇從哪開始複製,咱們這裏是測試環境,數據庫爲空,先從最開始複製。 +-------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------------+ | Log_name | Pos |Event_type | Server_id | End_log_pos |Info | +-------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------------+ | master-log.000001 | 4 | Format_desc | 1 | 245 | Server ver: 5.5.52-MariaDB, Binlog ver: 4 | | master-log.000001 | 245 | Query | 1 | 415 | grant replication slave,replication client on *.* to'ma'@'192.168.32.%' identified by 'centos' | | master-log.000001 | 415 | Query | 1 | 490 | flush privileges | +-------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------------+ MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master'; <===安裝插件 MariaDB [(none)]> SHOW GLOBALVARIABLES LIKE '%rpl%'; <===安裝完之後不表明開啓。 +------------------------------------+-------+ | Variable_name | Value | +------------------------------------+-------+ | rpl_recovery_rank | 0 | | rpl_semi_sync_master_enabled | OFF | | rpl_semi_sync_master_timeout | 10000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | ON | +------------------------------------+-------+ MariaDB [(none)]> SHOW GLOBAL STATUS LIKE '%rpl%'; <===查看複製各類狀態 MariaDB [(none)]> SET @@GLOBAL.rpl_semi_sync_master_enabled=ON; <===開啓插件 MariaDB [(none)]> FLUSH PRIVILEGES;
從mysql配置:192.168.32.112安全
]#yum install mariadb -y <===centos6爲mysql ]#vim /etc/my.cnf.d/server.cnf [server] skip_name_resolve = ON innodb_file_per_table = ON max_connection = 2000 relay_log = relay-log server_id = 2 ]#systemctl start mariadb ]#mysql > CHANGE MASTER TO MASTER_HOST='192.168.32.111',MASTER_USER='ma',MASTER_PASSWORD='centos',MASTER_LOG_FILE='master-log.000001',MASTER_LOG_POS=245; >START SLAVE; > SHOW SLAVE STATUS\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.32.111 <===主節點 Master_User: ma <===用戶 Master_Port: 3306 <===端口 Connect_Retry: 60 Master_Log_File: master-log.000001 <===從哪一個文件複製 Read_Master_Log_Pos: 490 <===哪一個位置 Relay_Log_File: relay-log.000002 <===複製到本地的中繼日誌 Relay_Log_Pos: 775 <===中繼日誌的位置 Relay_Master_Log_File: master-log.000001 Slave_IO_Running: Yes <===複製要靠IO線程 Slave_SQL_Running: Yes <===重放要靠SQL線程 … > INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave'; > SET @@GLOBAL.rpl_semi_sync_slave_enabled=ON; > SHOW GLOBAL VARIABLES LIKE '%rpl%'; <===查看變量裏已經開啓了 +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_recovery_rank | 0 | | rpl_semi_sync_slave_enabled | ON | | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ > SHOW GLOBAL STATUS LIKE '%rpl%'; <===可是,這個時候看狀態依然是關閉的! +----------------------------+-------------+ | Variable_name | Value | +----------------------------+-------------+ | Rpl_semi_sync_slave_status | OFF | | Rpl_status | AUTH_MASTER | +----------------------------+-------------+ > STOP SLAVE IO_THREAD; > START SLAVEIO_THREAD; <===須要重啓下複製線程
總結:到這裏咱們已經實現了數據庫的半同步複製,若是在主節點上建立數據從節點能同步到,證實實驗第一步成功了。爲了後面讀寫分離能夠看到效果,能夠考慮在從節點上加個過濾器,某一個數據庫中數據不
bash
同步到從節點上。過濾機制有兩種方法:一、主節點二進制日誌中進行過濾;二、在從節點的中繼日誌中進行過濾。這裏咱們使用第二種方法。服務器
SET @@GLOBAL.replicate_ignore_DB=hidb; <===在從節點上設置過濾hidb的庫
二、配置proxysql:192.168.32.104
]#yum install -y \.proxysql-1.4.2-1-centos7.x86_64.rpm <===我是下載好上傳到本地的,安裝包見附件。不排除有依賴關係,因此用yum安裝。 ]#rpm -ql proxysql <===包組不多 /etc/init.d/proxysql /etc/proxysql.cnf /usr/bin/proxysql /usr/share/proxysql/tools/proxysql_galera_checker.sh /usr/share/proxysql/tools/proxysql_galera_writer.pl ]# vim /etc/proxysql.cnf admin_variables= <===管理接口的用戶和帳戶:mysql -S /tmp/proxysql_admin.sock -uadmin -padmin.安全起見,建議更改 { admin_credentials="admin:admin" mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock" } mysql_variables= { threads=4 max_connections=2048 default_query_delay=0 default_query_timeout=36000000 have_compress=true poll_timeout=2000 interfaces="0.0.0.0:3306;/tmp/mysql.sock" default_schema="information_schema" stacksize=1048576 server_version="5.5.30" connect_timeout_server=3000 monitor_username="monitor" monitor_password="monitor" monitor_history=600000 monitor_connect_interval=60000 monitor_ping_interval=10000 monitor_read_only_interval=1500 monitor_read_only_timeout=500 ping_interval_server_msec=120000 ping_timeout_server=500 commands_stats=true sessions_sort=true connect_retries_on_failure=10 } mysql_servers = <===定義兩個mysql服務器地址、端口、屬於哪一個組(下面有定義)、是否壓縮、權重 ( { address = "192.168.32.111" port = 3306 hostgroup = 0 status = "ONLINE" weight = 1 compression = 0 }, { address = "192.168.32.112" port = 3306 hostgroup = 1 status = "ONLINE" weight = 1 compression = 0 } ) mysql_users: <===鏈接數據庫的用戶和帳戶,和以前受權複製主從的一致 ( { username = "dbadmin" password = "centos" default_hostgroup = 0 active = 1 } ) mysql_query_rules: <===數據查詢規則定義 ( { rule_id=1 active=1 match_pattern="^SELECT .* FOR UPDATE$" destination_hostgroup=0 apply=1 }, { rule_id=2 active=1 match_pattern="^SELECT" destination_hostgroup=1 apply=1 } ) mysql_replication_hostgroups= ( { writer_hostgroup=0 <===0組爲寫 reader_hostgroup=1 <===1組爲讀 comment="test repl 1" <===描述信息 } ) ]#service proxysql start ]# mysql -udbadmin -pcentos -h172.18.32.104 <===經過本地訪問數據庫 >INSERT INTO hidb.tal1 VALUES (4,'li'); <===插入一個數據,寫入主節點 >SELECT * FROM hidb.tal1; <===讀數據是從從點讀,而從節點作了hidb的過濾,所以是讀不到插入的這一行數據。實現了讀寫分離。
總結:到這裏咱們是已經實現了數據庫的讀寫分離。下面咱們還須要對proxysql作高可用。
三、在另一臺主機192.168.32.105上配置proxysql,具體配置和上面相同。
四、電腦配置有限,這裏就在proxysql服務器上配置Keepalived。
keepalived:192.168.32.104
]# yum install -y keepalived ]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id node1 <===添加 vrrp_mcast_group4 224.100.100.100 <===組播地址 } vrrp_script chk_nginx { <===檢測服務的腳本 script "killall -0 proxysql &> /dev/null && exit 0 || exit 1" interval 1 weight -30 fall 3 rise 3 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 22 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.99/24 <===虛擬一個VIP對外提供服務 } track_script { <===調用上面的腳本 chk_nginx } } ]#systemctl start keepalived
keepalived:192.168.32.104
]# yum install -y keepalived ]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id node2 vrrp_mcast_group4 224.100.100.100 } vrrp_script chk_nginx { script "killall -0 proxysql &> /dev/null && exit 0 || exit 1" interval 1 weight -10 fall 3 rise 3 } vrrp_instance VI_1 { state backup interface ens33 virtual_router_id 22 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.99/24 } track_script { chk_nginx } } ]#systemctl start keepalived
五、測試
]#tcpdump -i ens33 -nn host 224.100.100.100 <===對廣播地址進行抓包
在客戶端使用VIP使用,實現效果:當主proxysql服務down掉之後,從proxysql服務器拿到VIP開始提供服務。實現了高可用
]# mysql -udbadmin -pcentos -h192.168.32.99
六、補充
咱們能夠在proxysql服務器上使用管理接口登陸上去看看狀態
]# mysql -S /tmp/proxysql_admin.sock -uadmin -padmin > SHOW DATABASES; +-----+---------+-------------------------------+ | seq | name | file | +-----+---------+-------------------------------+ | 0 | main | | | 2 | disk | /var/lib/proxysql/proxysql.db | | 3 | stats | | | 4 | monitor | | +-----+---------+-------------------------------+ > SELECT * FROM mysql_servers; +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | 0 | 192.168.32.111 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | | 1 | 192.168.32.112 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | | +--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
總結:還有不少表就不一一展現了。這裏經過proxysql+keepalived實現了數據庫簡單的讀寫分離,簡單的高可用。實際應用中比這複雜的多,並非一我的能完成的。