您好,登錄后才能下訂單哦!
環(huán)境:
MySQL5.7.24版本
CentOS release 6.5
注意:
MySQL5.7版本Slave可以不開啟binlog了,可以節(jié)省這部分的磁盤I/O消耗,而MySQL5.6版本必須開啟binlog,因?yàn)镚TID信息需要在binlog中存儲(chǔ)(log_slave_updates),只有開啟binlog才能使用GTID的功能。MySQL5.7版本通過GTID系統(tǒng)表來記錄GITD信息(mysql.gtid_executed),每個(gè)事務(wù)提交時(shí),將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:
查看當(dāng)前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)
授權(quán)復(fù)制連接用戶:
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 <plugin> AS <hash> 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)
創(chuàng)建statistic庫:
mysql> create database statistic;
Query OK, 1 row affected (0.01 sec)
創(chuàng)建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;
創(chuàng)建edusoho_e庫:
mysql> create database edusoho_e;
Query OK, 1 row affected (0.01 sec)
創(chuàng)建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', '上海', '開發(fā)');
查看當(dāng)前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)
準(zhǔn)備復(fù)制數(shù)據(jù):
[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導(dǎo)入復(fù)制數(shù)據(jù):
[root@slave ~]# mysql -uroot -p < 2019-05-29.sql
Enter password:
Slave開始數(shù)據(jù)復(fù)制:
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';?? (會(huì)滾動(dòng) 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模式的主從復(fù)制搭建完畢。如果,你是MySQL5.6的環(huán)境,那么請參考 MySQL5.6 基于GTID模式的主從復(fù)制搭建,當(dāng)然了,還有一些常見復(fù)制問題的介紹,需要對你有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。