MGR集群搭建及配置过程
mgr全称mysql group replication(mysql组复制),是mysql官方于2016年12月推出的一个全新的高可用与高扩展的解决方案。mgr提供了高可用、高扩展、高可靠的mysql集群服务。在mgr出现之前,用户常见的mysql高可用方式,无论怎么变化架构,本质就是master-slave架构。mysql 5.7版本开始支持无损半同步复制(lossless semi-sync replication),从而进一步提示数据复制的强一致性。
mgr是mysql数据库未来发展的一个重要方向。
注意:根据本人测试group_replication.so插件是mysql-community-server安装包中携带,如果是rpm安装或yum安装存放地址为/usr/lib64/mysql/plugin/目录下,看下图。另外在安装5.7.16版本时是没有这个插件,而在安装5.7.20版本有这个插件,推测这是一个5.7.16到5.7.20之间新加的插件,个人建议安装5.7.20以上的版本。另外大家请在安装好mysql后查看一下是否存在这个插件。
如果提示group_replication.so不存在,或提示有问题并且查看时发现group_replication.so不存在,请重点看一下mysql的版本。(我搜了一大圈,没有一个人说这个问题。表示怀疑自己,如果我错了,请留言。)
(1).mgr的特性
高一致性。基于原生复制及paxos协议的组复制技术,并以插件的方式提供,提供一致数据安全保证;
高容错性。只要不是大多数节点坏掉就可以继续工作,有自动检测机制,当不同节点产生资源争用冲突时,不会出现错误,按照先到者优先原则进行处理,并且内置了自动化脑裂防护机制;
高扩展性。节点的新增和移除都是自动的,新节点加入后,会自动从其他节点上同步状态,直到新节点和其他节点保持一致,如果某节点被移除了,其他节点自动更新组信息,自动维护新的组信息;
高灵活性。有单主模式和多主模式,单主模式下,会自动选主,所有更新操作都在主上进行;多主模式下,所有server都可以同时处理更新操作。
(2).搭建mgr的基础结构要求和使用限制(重点)
基础结构要求:1.引擎必须为innodb,因为需事务支持在commit时对各节点进行冲突检查;2.每个表必须有主键,在进行事务冲突检测时需要利用主键值对比;3.必须开启binlog且为row格式;4.开启gtid,且主从状态信息存于表中(--master-info-repository=table 、--relay-log-info-repository=table),--log-slave-updates打开;5.一致性检测设置--transaction-write-set-extraction=xxhash64。
使用限制:1.rp和普通复制binlog校验不能共存,需设置--binlog-checksum=none;2.不支持gap lock(间隙锁),隔离级别需设置为read_committed;3.不支持对表进行锁操作(lock /unlock table),不会发送到其他节点执行 ,影响需要对表进行加锁操作的情况,列入mysqldump全表备份恢复操作;4.不支持serializable(序列化)隔离级别;5.ddl语句不支持原子性,不能检测冲突,执行后需自行校验是否一;6.多主模式下不支持外键,单主模式下支持外键;最多9个节点,超过9台无法加入集群
(3).实验环境
youxi1 192.168.1.6 centos7.6 mysql5.7.26 端口号3306 server-id=1
youxi2 192.168.1.7 centos7.6 mysql5.7.26 端口号3306 server-id=2
youxi3 192.168.1.8 centos7.6 mysql5.7.26 端口号3306 server-id=3
另外,三台服务器上都进行ip的映射(如果此处不映射,请修改/etc/my.cnf配置文件中对应的域名为ip地址)
[root@youxi1 ~]# vim /etc/hosts 192.168.1.6 youxi1.cn youxi1 //长域名和短域名只要映射一个即可 192.168.1.7 youxi2.cn youxi2 192.168.1.8 youxi3.cn youxi3
(1).单主模式
我是在空数据库下操作,如果主数据集已存在数据,需要将主数据库的数据导出再导入到从数据库。另外还需保证引擎为innodb,每个表必须存在主键。
1)以youxi1作为主服务器,对youxi1进行配置
创建一个复制用的用户
mysql> grant replication slave on *.* to 'repl'@'192.168.1.%' identified by '12345678'; query ok, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; query ok, 0 rows affected (0.00 sec)
修改配置文件,加入到[mysqld]模块下。然后重启mysqld
[root@youxi1 ~]# vim /etc/my.cnf server-id=1 //mysql服务id gtid-mode=on //全局事务 enforce-gtid-consistency=on //强制gtid的一致性 master-info-repository=table //将master.info元数据保存在系统表中 relay-log-info-repository=table //将relay.info元数据保存在系统表中 binlog-checksum=none //禁用二进制日志事件校验 log-slave-updates=on //级联复制 log-bin=binlog //开启二进制日志记录 binlog-format=row //以行的格式记录 transaction-write-set-extraction=xxhash64 //使用哈希算法将其编码为散列 loose-group_replication_group_name='ce9be252-2b71-11e6-b8f4-00212844f856' //加入的组名,可以修改,只要格式对 loose-group_replication_start_on_boot=off //不自动启用组复制集群 loose-group_replication_local_address='youxi1:33061' //以本机端口33061接受来自组中成员的传入连接 loose-group_replication_group_seeds='youxi1:33061,youxi2:33062,youxi3:33063' //组中成员访问表 loose-group_replication_bootstrap_group=off //不启用引导组 [root@youxi1 ~]# systemctl restart mysqld
注意:如果防火墙是打开的,记得添加mysql的端口号。
[root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port={3306,33061}/tcp success [root@youxi1 ~]# firewall-cmd --reload success [root@youxi1 ~]# firewall-cmd --zone=public --list-ports 3306/tcp 33061/tcp
修改master信息,构建组复制(group replication)集群信息
mysql> change master to master_user='repl',master_password='12345678' for channel 'group_replication_recovery'; query ok, 0 rows affected, 2 warnings (0.04 sec)
安装组复制(group replication)插件,并查看组件信息
mysql> install plugin group_replication soname 'group_replication.so'; query ok, 0 rows affected (0.21 sec) mysql> show plugins; //查看组件是否安装成功 +----------------------------+----------+--------------------+----------------------+---------+ | name | status | type | library | license | +----------------------------+----------+--------------------+----------------------+---------+ | binlog | active | storage engine | null | gpl | | mysql_native_password | active | authentication | null | gpl | | sha256_password | active | authentication | null | gpl | | csv | active | storage engine | null | gpl | | memory | active | storage engine | null | gpl | | innodb | active | storage engine | null | gpl | | innodb_trx | active | information schema | null | gpl | | innodb_locks | active | information schema | null | gpl | | innodb_lock_waits | active | information schema | null | gpl | | innodb_cmp | active | information schema | null | gpl | | innodb_cmp_reset | active | information schema | null | gpl | | innodb_cmpmem | active | information schema | null | gpl | | innodb_cmpmem_reset | active | information schema | null | gpl | | innodb_cmp_per_index | active | information schema | null | gpl | | innodb_cmp_per_index_reset | active | information schema | null | gpl | | innodb_buffer_page | active | information schema | null | gpl | | innodb_buffer_page_lru | active | information schema | null | gpl | | innodb_buffer_pool_stats | active | information schema | null | gpl | | innodb_temp_table_info | active | information schema | null | gpl | | innodb_metrics | active | information schema | null | gpl | | innodb_ft_default_stopword | active | information schema | null | gpl | | innodb_ft_deleted | active | information schema | null | gpl | | innodb_ft_being_deleted | active | information schema | null | gpl | | innodb_ft_config | active | information schema | null | gpl | | innodb_ft_index_cache | active | information schema | null | gpl | | innodb_ft_index_table | active | information schema | null | gpl | | innodb_sys_tables | active | information schema | null | gpl | | innodb_sys_tablestats | active | information schema | null | gpl | | innodb_sys_indexes | active | information schema | null | gpl | | innodb_sys_columns | active | information schema | null | gpl | | innodb_sys_fields | active | information schema | null | gpl | | innodb_sys_foreign | active | information schema | null | gpl | | innodb_sys_foreign_cols | active | information schema | null | gpl | | innodb_sys_tablespaces | active | information schema | null | gpl | | innodb_sys_datafiles | active | information schema | null | gpl | | innodb_sys_virtual | active | information schema | null | gpl | | myisam | active | storage engine | null | gpl | | mrg_myisam | active | storage engine | null | gpl | | performance_schema | active | storage engine | null | gpl | | archive | active | storage engine | null | gpl | | blackhole | active | storage engine | null | gpl | | federated | disabled | storage engine | null | gpl | | partition | active | storage engine | null | gpl | | ngram | active | ftparser | null | gpl | | validate_password | disabled | validate password | validate_password.so | gpl | | group_replication | active | group replication | group_replication.so | gpl | +----------------------------+----------+--------------------+----------------------+---------+ 46 rows in set (0.00 sec)
作为主服务器需要由这台服务器开启引导,开启组复制(group replication)集群
mysql> set global group_replication_bootstrap_group=on; //开启组复制引导 query ok, 0 rows affected (0.00 sec) mysql> start group_replication; //开启组复制 query ok, 0 rows affected (2.24 sec) mysql> set global group_replication_bootstrap_group=off; //关闭组复制引导 query ok, 0 rows affected (0.00 sec)
查看到添加到组复制集群的服务器信息
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | channel_name | member_id | member_host | member_port | member_state | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | c9e3662b-9020-11e9-94aa-000c29721e89 | youxi1 | 3306 | online | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 1 row in set (0.00 sec)
2)对youxi2进行配置
创建一个复制用的用户
mysql> grant replication slave on *.* to 'repl'@'192.168.1.%' identified by '12345678'; query ok, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; query ok, 0 rows affected (0.01 sec)
修改配置文件,加入到[mysqld]模块下。然后重启mysqld
[root@youxi2 ~]# vim /etc/my.cnf server-id=2 //修改 gtid-mode=on enforce-gtid-consistency=on master-info-repository=table relay-log-info-repository=table binlog-checksum=none log-slave-updates=on log-bin=binlog binlog-format=row transaction-write-set-extraction=xxhash64 loose-group_replication_group_name='ce9be252-2b71-11e6-b8f4-00212844f856' loose-group_replication_start_on_boot=off loose-group_replication_local_address='youxi2:33062' //修改 loose-group_replication_group_seeds='youxi1:33061,youxi2:33062,youxi3:33063' loose-group_replication_bootstrap_group=off [root@youxi2 ~]# systemctl restart mysqld
注意:如果防火墙是打开的,记得添加mysql的端口号。
[root@youxi2 ~]# firewall-cmd --permanent --zone=public --add-port={3306,33062}/tcp success [root@youxi2 ~]# firewall-cmd --reload success [root@youxi2 ~]# firewall-cmd --zone=public --list-ports 3306/tcp 33062/tcp
修改master信息,构建组复制(group replication)集群信息
mysql> change master to master_user='repl',master_password='12345678' for channel 'group_replication_recovery'; query ok, 0 rows affected, 2 warnings (0.05 sec)
安装组复制(group replication)插件
mysql> install plugin group_replication soname 'group_replication.so'; query ok, 0 rows affected (0.35 sec)
把youxi2加到之前的组复制(group replication)
mysql> set global group_replication_allow_local_disjoint_gtids_join=on; query ok, 0 rows affected, 1 warning (0.00 sec) mysql> start group_replication; query ok, 0 rows affected, 1 warning (5.92 sec)
查看到添加到组复制集群的服务器信息
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | channel_name | member_id | member_host | member_port | member_state | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 8fbf8b41-84fe-11e9-897e-000c29f27e52 | youxi2 | 3306 | online | | group_replication_applier | c9e3662b-9020-11e9-94aa-000c29721e89 | youxi1 | 3306 | online | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 2 rows in set (0.00 sec)
3)youxi3的配置与youxi2的配置几乎一样,只需在/etc/my.cnf修改server-id和loose-group_replication_local_address即可。
查看一下组复制集群信息
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | channel_name | member_id | member_host | member_port | member_state | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 74df3399-91b8-11e9-be5f-000c299fdf40 | youxi3 | 3306 | online | | group_replication_applier | 8fbf8b41-84fe-11e9-897e-000c29f27e52 | youxi2 | 3306 | online | | group_replication_applier | c9e3662b-9020-11e9-94aa-000c29721e89 | youxi1 | 3306 | online | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 3 rows in set (0.01 sec)
注意:如果防火墙是打开的,记得添加mysql的端口号。
[root@youxi3 ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp success [root@youxi3 ~]# firewall-cmd --permanent --zone=public --add-port=33063/tcp success [root@youxi3 ~]# firewall-cmd --reload success [root@youxi3 ~]# firewall-cmd --zone=public --list-ports 3306/tcp 33063/tcp
4)测试
在youxi1上创建数据
mysql> create database test_db; query ok, 1 row affected (0.00 sec) mysql> use test_db; database changed mysql> create table user_tb(id int key,name varchar(20)); //id是主键,引擎默认是innodb query ok, 0 rows affected (0.02 sec) mysql> insert into user_tb values(1,'zhangsan'); query ok, 1 row affected (0.08 sec)
在youxi2上查看
mysql> select * from test_db.user_tb; +----+----------+ | id | name | +----+----------+ | 1 | zhangsan | +----+----------+ 1 row in set (0.00 sec)
在youxi3上查看
mysql> select * from test_db.user_tb; +----+----------+ | id | name | +----+----------+ | 1 | zhangsan | +----+----------+ 1 row in set (0.00 sec)
5)那么怎么区分主从服务器
mgr区分主从服务器使用show variables like '%read_only%';查看read_only相关参数。如果是主服务器(youxi1),会显示如下:
mysql> show variables like '%read_only%'; +-----------------------+-------+ | variable_name | value | +-----------------------+-------+ | innodb_read_only | off | | read_only | off | | super_read_only | off | | transaction_read_only | off | | tx_read_only | off | +-----------------------+-------+ 5 rows in set (0.00 sec)
如果是从服务器(youxi2)会显示如下:
mysql> show variables like '%read_only%'; +-----------------------+-------+ | variable_name | value | +-----------------------+-------+ | innodb_read_only | off | | read_only | on | | super_read_only | on | | transaction_read_only | off | | tx_read_only | off | +-----------------------+-------+ 5 rows in set (0.01 sec)
6)查看当前服务器的组复制集群参数设置列表
mysql> show variables like 'group_replication%'; +----------------------------------------------------+----------------------------------------+ | variable_name | value | +----------------------------------------------------+----------------------------------------+ | group_replication_allow_local_disjoint_gtids_join | off | | group_replication_allow_local_lower_version_join | off | | group_replication_auto_increment_increment | 7 | | group_replication_bootstrap_group | off | | group_replication_components_stop_timeout | 31536000 | | group_replication_compression_threshold | 1000000 | | group_replication_enforce_update_everywhere_checks | off | | group_replication_exit_state_action | read_only | | group_replication_flow_control_applier_threshold | 25000 | | group_replication_flow_control_certifier_threshold | 25000 | | group_replication_flow_control_mode | quota | | group_replication_force_members | | | group_replication_group_name | ce9be252-2b71-11e6-b8f4-00212844f856 | | group_replication_group_seeds | youxi1:33061,youxi2:33062,youxi3:33063 | | group_replication_gtid_assignment_block_size | 1000000 | | group_replication_ip_whitelist | automatic | | group_replication_local_address | youxi1:33061 | | group_replication_member_weight | 50 | | group_replication_poll_spin_loops | 0 | | group_replication_recovery_complete_at | transactions_applied | | group_replication_recovery_reconnect_interval | 60 | | group_replication_recovery_retry_count | 10 | | group_replication_recovery_ssl_ca | | | group_replication_recovery_ssl_capath | | | group_replication_recovery_ssl_cert | | | group_replication_recovery_ssl_cipher | | | group_replication_recovery_ssl_crl | | | group_replication_recovery_ssl_crlpath | | | group_replication_recovery_ssl_key | | | group_replication_recovery_ssl_verify_server_cert | off | | group_replication_recovery_use_ssl | off | | group_replication_single_primary_mode | on | | group_replication_ssl_mode | disabled | | group_replication_start_on_boot | off | | group_replication_transaction_size_limit | 0 | | group_replication_unreachable_majority_timeout | 0 | +----------------------------------------------------+----------------------------------------+ 36 rows in set (0.00 sec)
7)如果主服务器出问题,mgr会自动切换主服务器
将youxi1的mysqld人为关闭
[root@youxi1 ~]# systemctl stop mysqld
之后前往youxi2和youxi3查看谁是主服务器
mysql> show variables like '%read_only%'; //这是youxi2的 +-----------------------+-------+ | variable_name | value | +-----------------------+-------+ | innodb_read_only | off | | read_only | on | | super_read_only | on | | transaction_read_only | off | | tx_read_only | off | +-----------------------+-------+ 5 rows in set (0.01 sec) mysql> show variables like '%read_only%'; //这是youxi3的 +-----------------------+-------+ | variable_name | value | +-----------------------+-------+ | innodb_read_only | off | | read_only | off | | super_read_only | off | | transaction_read_only | off | | tx_read_only | off | +-----------------------+-------+ 5 rows in set (0.02 sec)
可以看到youxi3变成了主服务器,那么尝试在youxi3中插入数据
mysql> insert into test_db.user_tb values(2,'lisi'); query ok, 1 row affected (0.14 sec)
再到youxi2中查看数据
mysql> select * from test_db.user_tb; +----+----------+ | id | name | +----+----------+ | 1 | zhangsan | | 2 | lisi | +----+----------+ 2 rows in set (0.00 sec)
(2).多主模式
1)在创建时就启用多主模式
在创建时就启用多主模式只需在修改配置文件/etc/my.cnf时多加两行参数。其余保持不变
loose-group_replication_single_primary_mode=off //关闭单master模式 loose-group_replication_enforce_update_everywhere_checks=on //多主一致性检查
2)由单主模式改为多主模式
由单主改为多主时,一样需要所有服务器配置信息增加两行参数,为了下次开启就是多主模式
loose-group_replication_single_primary_mode=off //关闭单master模式 loose-group_replication_enforce_update_everywhere_checks=on //开启多主一致性检查
然后全部服务器停止组复制(group_replication)集群,并设置参数
mysql> stop group_replication; query ok, 0 rows affected (9.51 sec) mysql> set global group_replication_single_primary_mode=off; //关闭单主模式 query ok, 0 rows affected (0.00 sec) mysql> set global group_replication_enforce_update_everywhere_checks=on; //开启多主一致性检查 query ok, 0 rows affected (0.00 sec)
选择其中一台引导组复制(group_replication)集群
mysql> set global group_replication_bootstrap_group=on; query ok, 0 rows affected (0.00 sec) mysql> start group_replication; query ok, 0 rows affected (2.13 sec) mysql> set global group_replication_bootstrap_group=off; query ok, 0 rows affected (0.01 sec)
剩下的开启组复制(group_replication)即可
mysql> start group_replication; query ok, 0 rows affected, 1 warning (6.05 sec)
最后查看非引导组复制的服务器
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | channel_name | member_id | member_host | member_port | member_state | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 74df3399-91b8-11e9-be5f-000c299fdf40 | youxi3 | 3306 | online | | group_replication_applier | 8fbf8b41-84fe-11e9-897e-000c29f27e52 | youxi2 | 3306 | online | | group_replication_applier | c9e3662b-9020-11e9-94aa-000c29721e89 | youxi1 | 3306 | online | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 3 rows in set (0.00 sec) mysql> show variables like '%read_only%'; +-----------------------+-------+ | variable_name | value | +-----------------------+-------+ | innodb_read_only | off | | read_only | off | | super_read_only | off | | transaction_read_only | off | | tx_read_only | off | +-----------------------+-------+ 5 rows in set (0.01 sec)
(3).如果宕机了
停掉youxi1的mysqld模拟宕机
[root@youxi1 ~]# systemctl stop mysqld
这时候查看youxi2和youxi3谁是新的master
[root@youxi2 ~]# mysql -uroot -p123456 mysql> show variables like '%read_only%'; //这是youxi2的 +-----------------------+-------+ | variable_name | value | +-----------------------+-------+ | innodb_read_only | off | | read_only | on | | super_read_only | on | | transaction_read_only | off | | tx_read_only | off | +-----------------------+-------+ 5 rows in set (0.00 sec) [root@youxi3 ~]# mysql -uroot -p123456 mysql> show variables like '%read_only%'; //这是youxi3的,可以看出youxi3成为了新的master +-----------------------+-------+ | variable_name | value | +-----------------------+-------+ | innodb_read_only | off | | read_only | off | | super_read_only | off | | transaction_read_only | off | | tx_read_only | off | +-----------------------+-------+ 5 rows in set (0.00 sec)
在youxi3上写入数据,这是因为生产环境数据库是一直在使用的状态,不可能等你修复后再用
[root@youxi3 ~]# mysql -uroot -p123456 mysql> insert into test_db.user_tb values(2,'lisi'); query ok, 1 row affected (0.03 sec) mysql> select * from test_db.user_tb; +----+----------+ | id | name | +----+----------+ | 1 | zhangsan | | 2 | lisi | +----+----------+ 2 rows in set (0.00 sec)
将修好的youxi1添加回复制组
[root@youxi1 ~]# systemctl start mysqld [root@youxi1 ~]# mysql -uroot -p123456 mysql> select * from performance_schema.replication_group_members; +---------------------------+-----------+-------------+-------------+--------------+ | channel_name | member_id | member_host | member_port | member_state | +---------------------------+-----------+-------------+-------------+--------------+ | group_replication_applier | | | null | offline | +---------------------------+-----------+-------------+-------------+--------------+ 1 row in set (0.00 sec) mysql> change master to master_user='repl',master_password='12345678' for channel 'group_replication_recovery'; //这一步是为了以防万一, query ok, 0 rows affected, 2 warnings (0.01 sec) mysql> set global group_replication_allow_local_disjoint_gtids_join=on; //将这台服务器重新添加进复制组 query ok, 0 rows affected (0.00 sec) mysql> start group_replication; query ok, 0 rows affected (3.36 sec) mysql> select * from performance_schema.replication_group_members; //查看是否加入复制组 +---------------------------+--------------------------------------+-------------+-------------+--------------+ | channel_name | member_id | member_host | member_port | member_state | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 07c6f159-964f-11e9-b2c7-000c2934a723 | youxi3 | 3306 | online | | group_replication_applier | 8247f048-962f-11e9-a210-000c2975fa5d | youxi2 | 3306 | online | | group_replication_applier | 8b703193-962a-11e9-b582-000c29e6d627 | youxi1 | 3306 | online | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 3 rows in set (0.00 sec) mysql> select * from test_db.user_tb; //查看是否同步数据,没有同步请耐心等待,时间可能会长点 +----+----------+ | id | name | +----+----------+ | 1 | zhangsan | | 2 | lisi | +----+----------+ 2 rows in set (0.00 sec)
到此这篇关于mgr集群搭建的文章就介绍到这了,更多相关mgr集群搭建内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 长青春痘吃什么好?这两种类一定要多吃
下一篇: 删除列表的三个方式(python)