mysql實現讀寫分離(proxy)與高可用(MGR)


---恢復內容開始---

實驗目的是MySQL高可用且根據規則實現讀寫分離

首先准備三台MySQL服務器,版本是二進制的mysql-5.7.27

第一台:192.168.110.113

第二台:192.168.110.112

第三台:192.168.110.110

三台機器配置/etc/hosts,三台機器的目錄都是/data,第一,二台機器設置為種子節點

如果三台機器是新裝的數據庫,不用先進行mysqldump更新至一致,如果有數據且不小的話建議先mysqldump更新至一致

在第一台上面,設置/etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
 basedir = /usr/local/mysql
 datadir = /data
# port = .....
# server_id = .....
# socket = .....
 pid-file = /data/rabbitmq4.pid
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#datadir=/data
socket=/tmp/mysql.sock
server-id=100                      # 必須
gtid_mode=on                       # 必須
enforce_gtid_consistency=on        # 必須
log-bin=/data/master-bin           # 必須
binlog_format=row                  # 必須
binlog_checksum=none               # 必須
master_info_repository=TABLE       # 必須
relay_log_info_repository=TABLE    # 必須
relay_log=/data/relay-log          # 必須,如果不給,將采用默認值
log_slave_updates=ON               # 必須
sync-binlog=1                      # 建議
log-error=/data/error.log
#pid-file=/data/mysqld.pid
transaction_write_set_extraction=XXHASH64         # 必須
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"  # 必須
loose-group_replication_start_on_boot=off        # 建議設置為OFF
loose-group_replication_member_weigth = 40   # 非必需,mysql 5.7.20才開始支持該選項
loose-group_replication_local_address="192.168.110.113:20001"   # 必須,下一行也必須
loose-group_replication_group_seeds="192.168.110.113:20001,192.168.110.112:20002"
 
啟動數據庫
配置遠程復制用戶密碼
create user repl@'192.168.110.%' identified by 'P@ssword1!';
grant replication slave on *.* to repl@'192.168.110.%';
加載組復制插件
install plugin group_replication soname 'group_replication.so';
查看插件情況
show plugins;
配置族復制通道
change master to
            master_user='repl',
            master_password='P@ssword1!'
            for channel 'group_replication_recovery';
查看relay log的元數據
select * from mysql.slave_relay_log_info\G
第一台配置組復制引導組
mysql> set @@global.group_replication_bootstrap_group=on;
mysql> start group_replication;
mysql> set @@global.group_replication_bootstrap_group=off;
查看組復制成員狀態
select * from performance_schema.replication_group_members;
 
第二台數據庫
配置/etc/my.cnf
[mysqld]
datadir=/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
server-id=110                      # 必須,每個節點都不能相同
gtid_mode=on                       # 必須
enforce_gtid_consistency=on        # 必須
log-bin=/data/master-bin           # 必須
binlog_format=row                  # 必須
binlog_checksum=none               # 必須
master_info_repository=TABLE       # 必須
relay_log_info_repository=TABLE    # 必須
relay_log=/data/relay-log          # 必須,如果不給,將采用默認值
log_slave_updates=ON               # 必須
sync-binlog=1                      # 建議
log-error=/data/error.log
pid-file=/data/mysqld.pid
transaction_write_set_extraction=XXHASH64         # 必須
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"  # 必須
loose-group_replication_start_on_boot=off        # 建議設置為OFF
loose-group_replication_member_weigth = 20   # 非必需,mysql 5.7.20才開始支持該選項
loose-group_replication_local_address="192.168.110.112:20002"   # 必須,下一行也必須
loose-group_replication_group_seeds="192.168.110.113:20001,192.168.110.112:20002"
加載組復制插件
install plugin group_replication soname 'group_replication.so';
配置組復制通道
change master to
            master_user='repl',
            master_password='P@ssword1!'
            for channel 'group_replication_recovery';
開啟組復制功能,由於是第二台,所以只需要開啟組復制功能即可
start group_replication;
查看組復制成員狀態
select * from performance _schema.replication_group_members
 
 第三台數據庫
配置/etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
 #log_bin= mysql-bin
 #server_id= 1
# These are commonly set, remove the # and set as required.
 basedir = /usr/local/mysql
 datadir = /data
 #character_set_database = utf8
 #character_set_server = utf8
# port = .....
# server_id = .....
 socket = /tmp/mysql.sock
 #pid = /data-mysql/docker.pid
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
server-id=120                      # 必須
gtid_mode=on                      
enforce_gtid_consistency=on       
log-bin=/data/master-bin          
binlog_format=row                 
binlog_checksum=none              
master_info_repository=TABLE      
relay_log_info_repository=TABLE   
relay_log=/data/relay-log         
log_slave_updates=ON              
sync-binlog=1     

transaction_write_set_extraction=XXHASH64        
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" 
loose-group_replication_start_on_boot=off
loose-group_replication_member_weigth = 30
loose-group_replication_local_address="192.168.110.110:20003"
loose-group_replication_group_seeds="192.168.110.113:20001,192.168.110.112:20002"
log-error=/data/error.log
pid-file=/data/docker.pid
啟動數據庫
加載插件
install plugin group_replication soname 'group_replication.so';
配置組復制
change master to
            master_user='repl',
            master_password='P@ssword1!'
            for channel 'group_replication_recovery';
開啟組復制功能
start group_replication;
查看節點狀態
select * from performance_schema.replication_group_members
 
實驗過程:提示無法更新mysqld.pid文件,一般是配置文件錯誤,數據目錄沒指定,數據目錄權限不對,還有就是數據庫初始化不正確,從這幾個方面入手
當開啟組復制后,成員狀態一直在recovering,可能是配置組復制通道的用戶名密碼設置有誤,如repl@'網段'設置成不與當前內網網段一致
也有可能是gtid事務,如確定以往事務可以跳過則執行 reset master,再加組
 
配置好組復制后,若關閉單台服務器,那么只需關閉組復制功能,即stop group_replication,恢復重新加入組即可,start group_replication
若關閉整個組復制,最好的做法是先關閉從節點(不止組復制功能,整個實例都要關閉),最后在關閉主節點,完后要開啟組復制,先在主節點開啟組復制引導組,即
mysql> set @@global.group_replication_bootstrap_group=on;
mysql> start group_replication;
mysql> set @@global.group_replication_bootstrap_group=off;
從節點在開啟組復制功能
start group_replication
 
 
使用proxysql組件進行讀寫分離
proxysql:192.168.110.114
配置yum源
cat <<EOF | tee /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
EOF
yum安裝
yum -y install proxysql
開啟proxysql
service start proxysql
使用mysql本地連接,若沒有mysql客戶端工具,可從上面機器/usr/local/mysql/bin下拷貝mysql過來
mysql -uadmin -padmin -P6032 -h127.0.0.1 --prompt 'admin> '
runtime_開頭的是運行時的配置,這些是不能修改的。要修改ProxySQL的配置,需要修改了非 runtime_表,修改后必須執行 LOAD ... TO RUNTIME才能加載到RUNTIME生效,執行 save ... to disk才能將配置持久化保存到磁盤
把上面三個節點加入proxyql中
insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.110.113',3306);
insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.110.112',3306);
insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.110.110',3306);
load mysql servers to runtime;
save mysql servers to disk;
在主庫上新建用戶來讓proxysql監控mysql
 在master上執行:
mysql> create user monitor@'192.168.110.%' identified by 'P@ssword1!';
mysql> grant replication client on *.* to monitor@'192.168.110.%';
回到proxysql配置
set mysql-monitor_username='monitor';
set mysql-monitor_password='P@ssword1!';
修改后,加載到RUNTIME,並保存到disk。
load mysql variables to runtime;
save mysql variables to disk;
指定寫組的id為10,讀組的id為20。
insert into mysql_replication_hostgroups values(10,20,30);    ---其中30是注釋,隨便寫
load mysql servers to runtime;
save mysql servers to disk;
這樣proxysql會根據后端數據庫的read-only屬性把機器加入到對應的組內,因為上面MGR配置單主模式,所以會自動設置read-only屬性
接着,設置客戶端通過proxysql訪問后端數據庫的用戶密碼

在master節點上執行:(只需master執行即可,會復制給兩個slave) 

grant all on *.* to root@'192.168.110.%' identified by 'P@ssword1!';
grant all on *.* to sqlsender@'192.168.110.%' identified by 'P@ssword1!';

回到proxysql上

insert into mysql_users(username,password,default_hostgroup) values('root','P@ssword1!',10);
insert into mysql_users(username,password,default_hostgroup) values('sqlsender','P@ssword1!',10);
load mysql users to runtime;
save mysql users to disk;

插入兩個規則,目的是將select語句分離到hostgroup_id=20的讀組,但由於select語句中有一個特殊語句SELECT...FOR UPDATE它會申請寫鎖,所以應該路由到hostgroup_id=10的寫組。

insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)
VALUES (1,1,'^SELECT.*FOR UPDATE$',10,1),
       (2,1,'^SELECT',20,1);

load mysql query rules to runtime;
save mysql query rules to disk;

至此讀寫分離規則生效

以上,組復制是2016年官方推出的高可用功能,並且5.7.20后才支持這個功能,所以舊版本的MySQL不能用,舊版本的要用這個功能只能更新到新版,但是要結合應用程序代碼是否能被新版本的數據庫兼容


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM