注意:我这里采用编译安装的方式,如果想简单直接yum安装mysql5.7也行,mysql编译安装需要的磁盘空间还是比较大的,一般在7G左右,所以要提前规划好,用三个节点比较接近生产环境,而且更直接清晰。
详细部署信息如下:
主机名 | IP地址 | 安装软件 | 用途 |
apache | 192.168.2.25 | cmake、boost、mysql | 节点 |
nginx | 192.168.2.26 | cmake、boost、mysql | 节点 |
kibana | 192.168.2.30 | cmake、boost、mysql | 节点 |
点击(此处)折叠或打开
- rpm -qa mysql mariadb
写入hosts文件映射关系,集群用得到
192.168.2.25 apache
192.168.2.26 nginx
192.168.2.30 kibana
2、安装依赖包
点击(此处)折叠或打开
- yum install gcc gcc-c++ ncurses-devel -y
点击(此处)折叠或打开
- tar zxvf cmake-3.7.2.tar.gz
- cd make-3.7.2
- ./configure
- gmake && gmake install
点击(此处)折叠或打开
- tar zxvf boost_1_59_0.tar.gz
- cp -r boost_1_59_0 /usr/local/boost
点击(此处)折叠或打开
- groupadd mysql
- useradd -M -s /sbin/nologin mysql -g mysql
- tar zxvf mysql-5.7.17.tar.gz
- cd mysql-5.7.17
- cmake -DCMAKE_INSTALL_PREFIX=/data/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_BOOST=/usr/local/boost
- make
- make install
- chown -R mysql.mysql /data/mysql
- mv /etc/my.cnf /etc/my.cnf.default
- cp /data/mysql/support-files/my-default.cnf /etc/my.cnf
- /data/mysql/bin/mysqld --initialize --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data //注意初始化会生成一个随机的密码,请牢记
-
echo "PATH=$PATH:/data/mysql/bin" >> /etc/profile
- source /etc/profile
- cp /data/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
-
chmod +x /etc/rc.d/init.d/mysqld
6、开始搭建MGR集群环境,修改第一个节点的my.cnf文件,内容如下:
点击(此处)折叠或打开
- # For advice on how to change settings please see
- # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
- # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
- # *** default location during install, and will be replaced if you
- # *** upgrade to a newer version of MySQL.
- [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 = /data/mysql
- datadir = /data/mysql/data
- port = 3306
- socket = /data/mysql/data/mysql.sock
- log-error = /data/mysql/data/mysqld.log
- pid-file = /data/mysql/data/mysqld.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
- # Group Replication
- server_id = 1
- 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 = '192.168.2.25:33061'
- loose-group_replication_group_seeds ='192.168.2.25:33061,192.168.2.26:33061,192.168.2.30:33061'
- loose-group_replication_bootstrap_group = off
- [client]
- socket = /data/mysql/data/mysql.sock
/etc/init.d/mysqld start
点击(此处)折叠或打开
- set sql_log_bin=0;
- create user rpl_user@'%';
- grant replication slave on *.* to rpl_user@'%' identified by 'rpl_pass';
- flush privileges;
- set sql_log_bin=1;
- change master to master_user='rpl_user',master_password='rpl_pass' for channel 'group_replication_recovery';
- install PLUGIN group_replication SONAME 'group_replication.so';
- set global group_replication_bootstrap_group=ON;
- start group_replication;
- set global group_replication_bootstrap_group=OFF;
-
select * from performance_schema.replication_group_members;
如果出现ONLINE,说明正常,这就是主节点,再搭建两个从节点。
7、第二个节点加入集群,复制刚刚的第一个节点的主配置文件my.cnf,只需要修改两个地方就行,已经用红色标注
点击(此处)折叠或打开
- # For advice on how to change settings please see
- # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
- # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
- # *** default location during install, and will be replaced if you
- # *** upgrade to a newer version of MySQL.
- [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 = /data/mysql
- datadir = /data/mysql/data
- port = 3306
- socket = /data/mysql/data/mysql.sock
- log-error = /data/mysql/data/mysqld.log
- pid-file = /data/mysql/data/mysqld.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
- # Group Replication
- 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 = '192.168.2.26:33061'
- loose-group_replication_group_seeds ='192.168.2.25:33061,192.168.2.26:33061,192.168.2.30:33061'
- loose-group_replication_bootstrap_group = off
- [client]
- socket = /data/mysql/data/mysql.sock
点击(此处)折叠或打开
- set sql_log_bin=0;
-
create user rpl_user@'%';
- grant replication slave on *.* to rpl_user@'%' identified by 'rpl_pass';
- set sql_log_bin=1;
- change master to master_user='rpl_user',master_password='rpl_pass' for channel 'group_replication_recovery';
- install plugin group_replication SONAME 'group_replication.so';
- set global group_replication_allow_local_disjoint_gtids_join=ON;
- start group_replication;
同理第三个节点加入操作方法也和第二个节点一样。
截图如下:
查询哪个是主节点:
从上图来看很明显apache主机是主节点。
测试步骤:
1、在主库上创建一个库,然后创建表,在两个从库上查询数据是否同步?
2、两个从库只能执行查询操作?
2、手动关闭主库,确认两个从库其中一个是否会变成主库?而且是MEMBER_ID第一个字母按优先级排列的接管主库?
日常维护步骤:
1、如果从库某一节点关闭
点击(此处)折叠或打开
- start group_replication;
点击(此处)折叠或打开
- set global group_replication_bootstrap_group=ON;
- start group_replication;
点击(此处)折叠或打开
- start group_replication;
点击(此处)折叠或打开
- start group_replication;