MySQL5.7-基于GTID复制模式搭建主从复制

环境:

公司主营业务:网站建设、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出南和免费做网站回馈大家。

MySQL5.7.24版本
CentOS release 6.5

注意:

MySQL5.7版本Slave可以不开启binlog了,可以节省这部分的磁盘I/O消耗,而MySQL5.6版本必须开启binlog,因为GTID信息需要在binlog中存储(log_slave_updates),只有开启binlog才能使用GTID的功能。MySQL5.7版本通过GTID系统表来记录GITD信息(mysql.gtid_executed),每个事务提交时,将GTID信息插入到表中

Master配置:
[root@master ~]# cat /etc/my.cnf
server_id=1
gtid_mode=on
enforce_gtid_consistency=on
log_bin=/var/lib/mysql/binlog
binlog_format=row
character_set_server=utf8

[root@master ~]# service mysqld restart

Slave配置:

[root@slave ~]# cat /etc/my.cnf
server_id=2
gtid_mode=on
enforce_gtid_consistency=on
binlog_format=row
relay_log=/var/lib/mysql/relaylog
replicate_do_db=edusoho_e
character_set_server=utf8

[root@slave ~]# service mysqld restart

Master:
查看当前binlog情况:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

授权复制连接用户:
mysql> grant replication slave on *.*to repliter@'192.168.32.2' identified by PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> show warnings;
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                               |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'IDENTIFIED BY PASSWORD' is deprecated and will be removed in a future release. Please use IDENTIFIED WITH AS instead |
| Warning | 1287 | Using GRANT for creating new user is deprecated and will be removed in future release. Create new user with CREATE USER statement.    |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

创建statistic库:
mysql> create database statistic;
Query OK, 1 row affected (0.01 sec)

创建statistic.t1表:
CREATE TABLE `statistic`.`t1` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`xname` VARCHAR(20) NOT NULL DEFAULT '',
`address` CHAR(20) NOT NULL DEFAULT '',
`sex` TINYINT(1) NOT NULL DEFAULT '1',
`hobby` VARCHAR(30) NOT NULL DEFAULT '',
`age` TINYINT(2) DEFAULT '18',
PRIMARY KEY (`id`),
KEY `idx_name` (`xname`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

创建edusoho_e库:
mysql> create database edusoho_e;
Query OK, 1 row affected (0.01 sec)

创建edusoho_e.t1表:
CREATE TABLE `edusoho_e`.`t1` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`xname` VARCHAR(20) NOT NULL DEFAULT '',
`address` CHAR(20) NOT NULL DEFAULT '',
`sex` TINYINT(1) NOT NULL DEFAULT '1',
`hobby` VARCHAR(30) NOT NULL DEFAULT '',
`age` TINYINT(2) DEFAULT '18',
PRIMARY KEY (`id`),
KEY `idx_name` (`xname`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO `statistic`.`t1` (`xname`, `address`, `hobby`) VALUES ('statistic', '北京', '游戏');
INSERT INTO `edusoho_e`.`t1` (`xname`, `address`, `hobby`) VALUES ('edusoho_e', '上海', '开发');

查看当前binlog情况:
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 |     2443 |              |                  | c13c1b45-2741-11e9-abb0-000c29b85ea6:1-7 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

准备复制数据:
[root@master ~]# mysqldump -uroot -p -B edusoho_e > `date +%F`.sql (警告什么信息,自行查阅帮助的)
Enter password:
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --even

Slave导入复制数据:
[root@slave ~]# mysql -uroot -p < 2019-05-29.sql
Enter password:

Slave开始数据复制:
mysql> change master to master_auto_position=1,master_host='192.168.32.3',master_port=3306;
Query OK, 0 rows affected (0.04 sec)

mysql> start slave user='repliter' password='123456';   (会滚动 relay log 日志文件)
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Retrieved_Gtid_Set:
Executed_Gtid_Set: c13c1b45-2741-11e9-abb0-000c29b85ea6:1-7
Auto_Position: 1

至此,MySQL5.7 基于GTID模式的主从复制搭建完毕。如果,你是MySQL5.6的环境,那么请参考 MySQL5.6 基于GTID模式的主从复制搭建,当然了,还有一些常见复制问题的介绍,需要对你有所帮助。


网页题目:MySQL5.7-基于GTID复制模式搭建主从复制
转载来于:http://pwwzsj.com/article/ipchpg.html