MySQL5.7.24 Gtid+MGRmysql
因爲服務器的資源有限,本次測試採用的是1臺機器測試:
一臺硬件物理機器:16G內存,一塊120G的ssd盤 系統是CentOS6.9 x86_64最小化安裝
系統內核:linux
[root@localhost ~]# uname -a Linux localhost.localdomain 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
關閉selinux,關閉iptables sql
服務部署規劃說明:
物理機器上以不一樣的mysql配置文件my.cnf來初始化mysql ,而且以三個不一樣的端口和添加MGR的配置參數來開啓3個MySQL實例
mysql的版本爲mysql5.7.24 二進制安裝bootstrap
第一,物理機器上開啓三個mysql5.7.24 實例:服務器
初始化mysql網絡
下載軟件: wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local mv /usr/local/mysql-5.7.24-linux-glibc2.12 /usr/local/mysql useradd mysql -s /sbin/nologin -M chown -R mysql.mysql /usr/local/mysql yum install numactl -y
建立MySQL初始化數據存放路徑:app
mkdir -p /home/mysql3306/{data,binlog,logs} mkdir -p /home/mysql3307/{data,binlog,logs} mkdir -p /home/mysql3308/{data,binlog,logs} chown -R mysql.mysql /home/{mysql3306,mysql3307,mysql3308}
準備初始化mysql的my.cnf的配置文件:less
[root@localhost ~]# ll /opt/my330* -rw-r--r-- 1 root root 9227 12月 26 18:11 /opt/my3306.cnf -rw-r--r-- 1 root root 9294 12月 27 18:09 /opt/my3307.cnf -rw-r--r-- 1 root root 9294 12月 27 18:10 /opt/my3308.cnf [root@localhost ~]#
mysql5.7.17開始引入mysql的MGR特性簡單介紹MySQL5.7開啓Gtid+MGR特性的參數說明:dom
這次演示環境以mysql3306實例做爲第一個MGR的啓動實例,
MySQL5.7開啓Gtid+MGR特性my3306cnf配置文件必須開啓參數以下:socket
server_id =1 socket =/tmp/mysql3306.sock ##:for binlog binlog_format =row log_bin =/home/mysql3306/binlog/mysql-bin binlog_checksum=NONE log_slave_updates=ON ##:for gtid #gtid_executed_compression_period =1000 gtid_mode =on enforce_gtid_consistency =on ####for group_replication master_info_repository=TABLE relay_log_info_repository=TABLE binlog_checksum=NONE log_slave_updates=ON transaction_write_set_extraction=XXHASH64 loose-group_replication_group_name="3db33b36-0e51-409f-a61d-c99756e90155" ##格式要和server-uuid一致,可是不能和機器上mysql實例的uuid重複 loose-group_replication_start_on_boot=off ##禁止在重啓mysql服務時開啓組複製 loose-group_replication_local_address= "192.168.1.233:33061" #####端口隨便指定一個,可是不要和機上其餘服務的端口衝突就行,告訴插件本機使用網絡地址192.168.1.233和端口33061與組中的其餘成員進行內部通訊。 loose-group_replication_group_seeds= "192.168.1.233:33061,192.168.1.233:33071,192.168.1.233:33081" loose-group_replication_ip_whitelist="192.168.1.233/24" ##此處必須設置ip白名單,不然在開啓MGR時,會報錯。 loose-group_replication_bootstrap_group= off ## ##loose-group_replication_single_primary_mode=off ##在配置MGR的mutl-master模式時開啓 ##loose-group_replication_enforce_update_everywhere_checks=on ##在配置MGR的mutl-master模式時開啓 my3307.cnf配置文件和my3306.cnf中不一樣的是把loose-group_replication_local_address修改成 "192.168.1.233:33071" my3308.cnf配置文件和my3306.cnf中不一樣的是把loose-group_replication_local_address修改成 "192.168.1.233:33081"
啓動mysql實例:
/usr/local/mysql/bin/mysqld --defaults-file=/opt/my3306.cnf --initialize /usr/local/mysql/bin/mysqld --defaults-file=/opt/my3307.cnf --initialize /usr/local/mysql/bin/mysqld --defaults-file=/opt/my3308.cnf --initialize
爲了在配置MGR的過程當中防止出錯,在啓動mysql以前把auto.cnf 修改成3個不相同的uuid
[root@localhost ~]# cat /home/mysql3306/data/auto.cnf [auto] server-uuid=1ec3ac79-08ed-11e9-8da8-bcaec502b368 [root@localhost ~]# cat /home/mysql3307/data/auto.cnf [auto] server-uuid=288e7bbe-08f3-11e9-a605-bcaec502b311 [root@localhost ~]# cat /home/mysql3308/data/auto.cnf [auto] server-uuid=883c9421-08f5-11e9-8d47-bcaec502b333
啓動mysql服務:
/usr/local/mysql/bin/mysqld --defaults-file=/opt/my3306.cnf & /usr/local/mysql/bin/mysqld --defaults-file=/opt/my3307.cnf & /usr/local/mysql/bin/mysqld --defaults-file=/opt/my3308.cnf &
第二.開啓MGR配置過程(默認是single-master模式):
mysql3306做爲第一個節點開啓MGR:
SET SQL_LOG_BIN=0; CREATE USER rpl_user@'%'; GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%' IDENTIFIED BY 'Zykjwujianwei'; FLUSH PRIVILEGES; SET SQL_LOG_BIN=1; CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Zykjwujianwei' FOR CHANNEL 'group_replication_recovery'; install PLUGIN group_replication SONAME 'group_replication.so'; set global group_replication_bootstrap_group=ON; start group_replication; select * from performance_schema.replication_group_members;
注意:只有在第一個開啓MGR的mysql3306節點上才執行這個參數set global group_replication_bootstrap_group=ON;
root@localhost [(none)]>select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | group_replication_applier | 1ec3ac79-08ed-11e9-8da8-bcaec502b368 | localhost.localdomain | 3306 | ONLINE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ 3 rows in set (0.00 sec) root@localhost [(none)]>
查看當前的leader:
select *from performance_schema.replication_group_members where member_id =(select variable_value from performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member'); select variable_value from performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member';
節點mysql3307操做:
SET SQL_LOG_BIN=0; CREATE USER rpl_user@'%'; GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%' IDENTIFIED BY 'Zykjwujianwei'; FLUSH PRIVILEGES; SET SQL_LOG_BIN=1; CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Zykjwujianwei' FOR CHANNEL 'group_replication_recovery'; install PLUGIN group_replication SONAME 'group_replication.so'; start group_replication; select * from performance_schema.replication_group_members; root@localhost [(none)]>select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | group_replication_applier | 1ec3ac79-08ed-11e9-8da8-bcaec502b368 | localhost.localdomain | 3306 | ONLINE | | group_replication_applier | 288e7bbe-08f3-11e9-a605-bcaec502b311 | localhost.localdomain | 3307 | ONLINE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+
節點mysql3308操做:
SET SQL_LOG_BIN=0; CREATE USER rpl_user@'%'; GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%' IDENTIFIED BY 'Zykjwujianwei'; FLUSH PRIVILEGES; SET SQL_LOG_BIN=1; CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Zykjwujianwei' FOR CHANNEL 'group_replication_recovery'; install PLUGIN group_replication SONAME 'group_replication.so'; start group_replication; select * from performance_schema.replication_group_members; root@localhost [(none)]>select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | group_replication_applier | 1ec3ac79-08ed-11e9-8da8-bcaec502b368 | localhost.localdomain | 3306 | ONLINE | | group_replication_applier | 288e7bbe-08f3-11e9-a605-bcaec502b311 | localhost.localdomain | 3307 | ONLINE | | group_replication_applier | 883c9421-08f5-11e9-8d47-bcaec502b333 | localhost.localdomain | 3308 | ONLINE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ 3 rows in set (0.00 sec)
第三.測試效果:
root@localhost [(none)]>select @@port;create database test01;show databases; +--------+ | @@port | +--------+ | 3306 | +--------+ 1 row in set (0.00 sec) Query OK, 1 row affected (0.00 sec) +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test01 | +--------------------+ 5 rows in set (0.00 sec) root@localhost [(none)]>select @@port;create database test02;show databases; +--------+ | @@port | +--------+ | 3307 | +--------+ 1 row in set (0.00 sec) ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test01 | +--------------------+ 5 rows in set (0.00 sec) root@localhost [(none)]> root@localhost [(none)]>select @@port;create database test02;show databases; +--------+ | @@port | +--------+ | 3308 | +--------+ 1 row in set (0.00 sec) ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test01 | +--------------------+ 5 rows in set (0.00 sec) root@localhost [(none)]>
測試說明:
發現mysql3306上建立的test01同步到了其餘的2個mysql。然而在MGR組中mysql3307 和mysql3308是隻讀,不能寫入的。
關掉mysql3306服務會發生什麼?? 直接會從mysql3307和mysql3308實例中選擇一個做爲mysql主庫,另一個做爲新主庫的slave庫
關掉mysql3306服務一段時間後,有從新開啓mysql3306,而且start group_replication;從新加入到MGR組會發生什麼??
此時mysql3306不會切換爲master庫,而是成爲了宕機後新主庫的slave庫。
MGR默認的模式也就是我們演示的single-master模式(單主寫入模式)。那如何開啓mysql的mult-master多主寫入的模式呢??下面簡單介紹下:
首先要先關閉只讀的從庫mysql3307和mysql3308實例最後關閉mysql3306
其次在各自的my.cnf配置文件中開啓以下參數:
loose-group_replication_single_primary_mode=off ##在配置MGR的mutl-master模式時開啓 loose-group_replication_enforce_update_everywhere_checks=on ##在配置MGR的mutl-master模式時開啓
最後啓動mysql實例,按照配置MGR-single-master的順序步驟來進行配置MGR的multi-master模式
特別提示:
multi-master模式下,3個節點mysql實例都是可讀寫的。可是在第一個啓動的mysql實例上須要執行set global group_replication_bootstrap_group=ON;(注意:後面的節點不須要執行這個sql)
mysql5.7 開啓MGR multi-master模式後,查看實例的狀態發現不存在所謂的leader
(root@localhost:mysql.sock)[(none)]>select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | group_replication_applier | 862addac-10c5-11e9-9af5-bcaec502b317 | localhost.localdomain | 3307 | ONLINE | | group_replication_applier | a36b8f24-10c3-11e9-bf76-bcaec502b318 | localhost.localdomain | 3308 | ONLINE | | group_replication_applier | da3809c4-10bc-11e9-bb9e-bcaec502b368 | localhost.localdomain | 3306 | ONLINE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ 3 rows in set (0.00 sec) (root@localhost:mysql.sock)[(none)]>select *from performance_schema.replication_group_members where member_id =(select variable_value from performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member'); Empty set (0.00 sec) (root@localhost:mysql.sock)[(none)]>select variable_value from performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member'; +----------------+ | variable_value | +----------------+ | | +----------------+ 1 row in set (0.00 sec)
mysql3307完整的配置文件以下:
[root@localhost ~]# cat /opt/my3307.cnf [client] port = 3307 [mysql] auto-rehash prompt="\u@\h [\d]>" #pager="less -i -n -S" #tee=/opt/mysql/query.log [mysqld] ####: for global user =mysql basedir =/usr/local/mysql/ datadir =/home/mysql3307/data server_id =2 port =3307 character_set_server =utf8 explicit_defaults_for_timestamp =off log_timestamps =system socket =/tmp/mysql3307.sock read_only =0 skip_name_resolve =1 auto_increment_increment =1 auto_increment_offset =1 lower_case_table_names =1 secure_file_priv = open_files_limit =65536 max_connections =1000 thread_cache_size =64 table_open_cache =81920 table_definition_cache =4096 table_open_cache_instances =64 max_prepared_stmt_count =1048576 ####: for binlog binlog_format =row log_bin =/home/mysql3307/binlog/mysql-bin binlog_rows_query_log_events =on #log_slave_updates =on expire_logs_days =7 binlog_cache_size =65536 #binlog_checksum =none sync_binlog =1 slave-preserve-commit-order =ON ####: for error-log log_error =/home/mysql3307/logs/error.log general_log =off general_log_file =/home/mysql3307/logs/general.log ####: for slow query log slow_query_log =on slow_query_log_file =/home/mysql3307/logs/slow.log #log_queries_not_using_indexes =on long_query_time =1.000000 ####: for gtid #gtid_executed_compression_period =1000 gtid_mode =on enforce_gtid_consistency =on ####: for replication skip_slave_start =1 #master_info_repository =table #relay_log_info_repository =table slave_parallel_type =logical_clock slave_parallel_workers =4 #rpl_semi_sync_master_enabled =1 #rpl_semi_sync_slave_enabled =1 #rpl_semi_sync_master_timeout =1000 #plugin_load_add =semisync_master.so #plugin_load_add =semisync_slave.so binlog_group_commit_sync_delay =100 binlog_group_commit_sync_no_delay_count = 10 ####for group_replication master_info_repository=TABLE relay_log_info_repository=TABLE binlog_checksum=NONE log_slave_updates=ON transaction_write_set_extraction=XXHASH64 loose-group_replication_group_name="3db33b36-0e51-409f-a61d-c99756e90155" loose-group_replication_start_on_boot=off loose-group_replication_local_address= "192.168.1.233:33071" loose-group_replication_group_seeds= "192.168.1.233:33061,192.168.1.233:33071,192.168.1.233:33081" loose-group_replication_ip_whitelist="192.168.1.233/24" ##loose-group_replication_single_primary_mode=off ##在配置MGR的mutl-master模式時開啓 ##loose-group_replication_enforce_update_everywhere_checks=on ##在配置MGR的mutl-master模式時開啓 ####: for innodb default_storage_engine =innodb default_tmp_storage_engine =innodb innodb_data_file_path =ibdata1:1024M:autoextend innodb_temp_data_file_path =ibtmp1:12M:autoextend innodb_buffer_pool_filename =ib_buffer_pool innodb_log_group_home_dir =/home/mysql3307/data innodb_log_files_in_group =3 innodb_log_file_size =1024M innodb_file_per_table =on innodb_online_alter_log_max_size =128M innodb_open_files =65535 innodb_page_size =16k innodb_thread_concurrency =0 innodb_read_io_threads =4 innodb_write_io_threads =4 innodb_purge_threads =4 innodb_page_cleaners =4 # 4(刷新lru髒頁) innodb_print_all_deadlocks =on innodb_deadlock_detect =on innodb_lock_wait_timeout =20 innodb_spin_wait_delay =128 innodb_autoinc_lock_mode =2 innodb_io_capacity =200 innodb_io_capacity_max =2000 #--------Persistent Optimizer Statistics innodb_stats_auto_recalc =on innodb_stats_persistent =on innodb_stats_persistent_sample_pages =20 innodb_adaptive_hash_index =on innodb_change_buffering =all innodb_change_buffer_max_size =25 innodb_flush_neighbors =1 #innodb_flush_method = innodb_doublewrite =on innodb_log_buffer_size =128M innodb_flush_log_at_timeout =1 innodb_flush_log_at_trx_commit =1 innodb_buffer_pool_size =4096M innodb_buffer_pool_instances =4 autocommit =1 #--------innodb scan resistant innodb_old_blocks_pct =37 innodb_old_blocks_time =1000 #--------innodb read ahead innodb_read_ahead_threshold =56 innodb_random_read_ahead =OFF #--------innodb buffer pool state innodb_buffer_pool_dump_pct =25 innodb_buffer_pool_dump_at_shutdown =ON innodb_buffer_pool_load_at_startup =ON
到此處mysql5.7.24的Grid+MGR模式演示完畢