一、GTID复制简介
GTID (global transaction id)全局事务标识符,MySQL5.6版本开始支持,GTID复制不像传统的复制方式(导步复制、半同步复制)需要找到binlog和POS点,只需要知道master的IP、端口、账号、密码即可。
二、相关实验配置
实验用到两台主机,一台作为主服务器(192.168.214.17),一台作为从服务器(192.168.214.27),系统为CentOS7.6,数据库这mysql-5.7.26,使用二进制安装包进行安装。
1、二进制安装mysql
1). 准备安装包文件,可以在官网下载:https://dev.mysql.com/downloads/mysql/
[root@centos7 ~]# ll mysql-5.7.26-el7-x86_64.tar.gz -rw-r--r-- 1 root root 714050311 Dec 6 16:03 mysql-5.7.26-el7-x86_64.tar.gz
2). 创建mysq用户
[root@centos7 ~]# useradd -r -s /sbin/nologin mysql
3). 准备二进制程序
[root@centos7 ~]# tar -zxvf mysql-5.7.26-el7-x86_64.tar.gz -C /usr/local/ [root@centos7 ~]# cd /usr/local/ [root@centos7 local]# ln -s mysql-5.7.26-el7-x86_64/ mysql #创建软链接
4). 初始化数据库
[root@centos7 mysql]# cd /usr/local/mysql [root@centos7 mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql 2019-12-06T08:14:52.399886Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-12-06T08:14:52.651036Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-12-06T08:14:52.680622Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-12-06T08:14:52.758031Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7acd902a-1800-11ea-8349-000c290ae9d3. 2019-12-06T08:14:52.759777Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-12-06T08:14:52.761255Z 1 [Note] A temporary password is generated for root@localhost: _RluMerNq90# #记住此处数据库初始密码
5). 修改配置文件并配置环境变量
[root@centos7 mysql]# vim /etc/my.cnf [root@centos7 mysql]# cat /etc/my.cnf [mysqld] datadir=/data/mysql socket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.log pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
[root@centos7 mysql]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [root@centos7 mysql]# . /etc/profile.d/mysql.sh
6). 准备服务启动脚本,并启动数据库服务
[root@centos7 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@centos7 mysql]# chkconfig --add mysqld [root@centos7 mysql]# chkconfig --list |grep mysqld Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@centos7 mysql]# service mysqld start Starting MySQL.Logging to '/data/mysql/mysql.log'. SUCCESS!
7). 修改初始密码,连接测试
[root@centos7 mysql]# mysqladmin -uroot -p"_RluMerNq90#" password 'centos' mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. [root@centos7 mysql]# mysql -uroot -pcentos mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 4 Server version: 5.7.26 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
8). 从服务器也按以上步骤安装即可
2、GTID复制配置
1). 在主服务器(192.168.214.17)上修改配置文件 /etc/my.cnf
[root@centos7 mysql]# vim /etc/my.cnf [mysqld] server-id=1 #设置ID log-bin #开始二进制日志 gtid-mode=on #开启gtid模式 enforce-gtid-consistency #开启gtid的一些安全限制 datadir=/data/mysql socket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.log pid-file=/data/mysql/mysql.pid [client] socket=/data/mysql/mysql.sock
2). 重启主服务器mysqld服务,登录数据库,创建拥有复制权限的用户账号
[root@centos7 mysql]# service mysqld restart [root@centos7 mysql]# mysql -uroot -pcentos mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 3 Server version: 5.7.26-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> grant replication slave on *.* to repluser@'192.168.214.%' identified by 'centos'; Query OK, 0 rows affected, 1 warning (0.01 sec)
3). 在从服务器(192.168.214.27)上修改配置文件 /etc/my.cnf
[root@centos7-27 mysql]# vim /etc/my.cnf [mysqld] server-id=2 #设置ID gtid-mode=on #开启gtid模式 enforce-gtid-consistency #开启gtid的一些安全限制 datadir=/data/mysql socket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.log pid-file=/data/mysql/mysql.pid [client] socket=/data/mysql/mysql.sock
4). 重启从服务器mysqld服务,登录数据库,配置用户用于连接主服务器,并启动复制线程
[root@centos7-27 mysql]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@centos7-27 mysql]# mysql -uroot -pcentos mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 2 Server version: 5.7.26 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.214.17', -> MASTER_USER='repluser', -> MASTER_PASSWORD='centos', -> MASTER_PORT=3306, -> MASTER_AUTO_POSITION=1; Query OK, 0 rows affected, 2 warnings (0.05 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec)
5). 在主服务器上测试同步是否正常
[root@centos7 mysql]# mysql -uroot -pcentos mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 5 Server version: 5.7.26-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> create database db1; #主服务器建库 Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) [root@centos7-27 mysql]# mysql -uroot -pcentos mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 5 Server version: 5.7.26 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | #可以看到从服务器已同步成功 | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)