MySQL 基于 GTID 搭建主从

文章
林里克斯

MySQL 基于 GTID 搭建主从


试验平台:CentOS Linux release 7.6.1810

试验IP:MySQL01:10.10.13.8;MySQL02:10.10.13.9

MySQL Version:mysql 5.7.32


一、MySQL 安装

原理:

  • GTID

    • 全局唯一,一个事务对应一个 GTID
    • 替代传统的 binlog+pos 复制;使用 master_auto_position=1 自动匹配 GTID 断点进行复制
    • MySQL 5.6 开始支持 GTID
    • 在传统的主从复制中,slave 端不用开启 binlog;但是在 GTID 主从复制中,必须开启 binlog
    • slave 端在接受 masterbinlog 时,会校验 GTID
    • 为了保证主从数据的一致性,多线程同时执行一个 GTID
  • 复制原理

    • master 更新数据时,会在事务前产生 GTID,一同记录到 binlog 日志中。
    • slave 端的 i/o 线程将变更的 binlog,写入到本地的 relay log 中。
    • sql 线程从 relay log 中获取 GTID,然后对比 slave 端的 binlog 是否有记录。
    • 如果有记录,说明该 GTID 的事务已经执行,slave 会忽略。
    • 如果没有记录,slave 就会从 relay log 中执行该 GTID 的事务,并记录到 binlog
    • 在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描

1.下载 tar

$ wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.32-el7-x86_64.tar.gz
#若链接失效,请自行下载相应版本的 tar 包版本即可

2.卸载自带 MariaDB

$ sudo yum -y remove mariadb-libs-5.5.60-1.el7_5.x86_64

3.解压安装

$ tar xf mysql-5.7.32-el7-x86_64.tar.gz
$ mv mysql-5.7.32-el7-x86_64 mysql
$ cd /data/mysql
$ mkdir /data/mysql/data
$ mkdir /data/mysql/logs
$ touch /data/mysql/my.cnf
$ bin/mysqld --defaults-file=/data/mysql/my.cnf --initialize --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data
2021-01-19T07:16:34.730020Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2021-01-19T07:16:34.730120Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
2021-01-19T07:16:34.730413Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-19T07:16:34.732737Z 0 [Warning] One can only use the --user switch if running as root

2021-01-19T07:16:35.026182Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-19T07:16:35.074496Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-19T07:16:35.137533Z 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: 436c8765-5a26-11eb-8d28-005056299372.
2021-01-19T07:16:35.138764Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-19T07:16:35.878705Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-19T07:16:36.696113Z 1 [Note] A temporary password is generated for root@localhost: Vtrg)2Jkd8G>

这里要注意下 --defaults-file=/data/mysql/my.cnf 这个参数的顺序,不然会有以下错误

$ bin/mysqld --initialize --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data --defaults-file=/data/mysql/my.cnf
2020-12-22T22:56:22.192297Z 0 [ERROR] unknown variable 'defaults-file=/data/mysql/my.cnf'
#放后面识别不到这个参数

二、主从搭建

1.编辑配置文件

$ cat my.cnf 
[client]
port = 3306
socket = /data/mysql/mysql.sock

[mysqld]
join_buffer_size = 256K
query_cache_type = 0
max_heap_table_size = 32M

basedir = /data/mysql
port = 3306
socket = /data/mysql/mysql.sock
datadir = /data/mysql/data
pid-file = /data/mysql/logs/mysql.pid
log-error = /data/mysql/logs/mysql.err

lower_case_table_names=1
server_id = 1
gtid_mode=on
enforce_gtid_consistency=on
log-bin = mysql-bin
binlog_format = mixed
max_binlog_size=1024M
expire_logs_days = 10

replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
relay_log=mysql-relay-bin
log-slave-updates=on

skip-external-locking
performance_schema_max_table_instances=400
table_definition_cache=400

max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 8K

thread_cache_size = 16
query_cache_size = 0M
tmp_table_size = 32M

explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

slow-query-log-file=/data/mysql/logs/mysql-slow.log
long_query_time=3
default_storage_engine = InnoDB
innodb_data_home_dir = /data/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /data/mysql/data
innodb_buffer_pool_size = 32M
innodb_log_file_size = 32M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 120
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

注意以下三个参数

server_id = 1
#唯一性
gtid_mode=on
enforce_gtid_consistency=on
#打开 GTID

2.增加环境变量

$ vim ~/.bashrc 
export PATH=/data/mysql/bin:/data/mysql/lib:$PATH

$ source ~/.bashrc

3.启动 MySQL 服务

$ cp -rp ./support-files/mysql.server mysqld.sh
$ sh mysqld.sh start

4.登录服务

$ mysql -u root -p
Enter password:
#这里密码输入初始化时给的临时密码

5.修改 root 临时密码

> ALTER USER 'root'@'localhost' IDENTIFIED BY 'redhat';ALTER USER 'root'@'localhost' IDENTIFIED BY 'redhat';
> flush privileges;
#两台操作一样

6.使用新密码验证登陆

$ mysql -u root -predhat

三、配置主从同步

1.创建同步用户

> grant replication slave on *.* to 'repl'@'10.10.13.9' identified by 'qwe123';
> flush privileges;

2.配置从库,启动同步

> change master to master_host='10.10.13.8', master_user='repl',master_password='qwe123',master_port=3306,master_auto_position=1;
> start slave;

3.查看从库状态

> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.13.8
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1238
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 1451
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1238
              Relay_Log_Space: 1658
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 436c8765-5a26-11eb-8d28-005056299372
             Master_Info_File: /data/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 436c8765-5a26-11eb-8d28-005056299372:1-5
            Executed_Gtid_Set: 436c8765-5a26-11eb-8d28-005056299372:1-5,
94ac0984-5a26-11eb-9020-005056272355:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#均为 Yes 表示连接正常

#Executed_Gtid_Set 查看这个 Gtid 与主库的是否能对应上

4.查看主库状态

> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 |     1238 |              |                  | 436c8765-5a26-11eb-8d28-005056299372:1-5 |
+------------------+----------+--------------+------------------+------------------------------------------+
#Executed_Gtid_Set 值一致

四、验证

1.在主库上创建一个 test

> create database test;

2.在备库上查看

> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+

3.在主库创建表插入数据

> use test;
> CREATE TABLE IF NOT EXISTS `jarbo`(`jarbo_id` INT UNSIGNED AUTO_INCREMENT,`title` VARCHAR(100) NOT NULL,`author` VARCHAR(40) NOT NULL,`date` DATE,PRIMARY KEY ( `jarbo_id` ) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
> INSERT INTO jarbo (title, author, date) VALUES ("test", "Jarbo", NOW());

4.在备库上查看

> use test;
> show tables;
+----------------+
| Tables_in_test |
+----------------+
| jarbo          |
+----------------+

> select * from jarbo;
+----------+-------+--------+------------+
| jarbo_id | title | author | date       |
+----------+-------+--------+------------+
|        1 | test  | Jarbo  | 2021-01-19 |
+----------+-------+--------+------------+

> desc jarbo;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| jarbo_id | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title    | varchar(100)     | NO   |     | NULL    |                |
| author   | varchar(40)      | NO   |     | NULL    |                |
| date     | date             | YES  |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+

Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

1159 0 2021-01-19


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-12-28

今日天气
站点统计
  • 文章总数:241篇
  • 分类总数:29个
  • 评论总数:12条
  • 本站总访问量 353166 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!