> 文章列表 > Ubuntu 下 mysql 8.0 基于GTID主从配置

Ubuntu 下 mysql 8.0 基于GTID主从配置

Ubuntu 下 mysql 8.0 基于GTID主从配置

按照网上文章以及官方推荐,基于 GTID 主从配置比起基于二进制日志主从配置要优越。作为学习,也必然要完成的一步。

按照 MariaDB : Setting Up Replication 文档中写到:

MariaDB 10.0 introduced global transaction IDs (GTIDs) for replication. It is generally recommended to use (GTIDs) from MariaDB 10.0, as this has a number of benefits.

本人实际学习经验总结:

  1. 如果停止已经完成的基于二进制日志主从配置的服务器,重新找一个新的从服务器,从头开始,确实很简单!

  2. 如果还在已经完成的基于二进制日志主从配置的服务器上继续进行,可能会遇到各种奇怪的问题!

    建议新旧从服务器两种方式都尝试一下!

    接上一篇
    Ubuntu 下 mysql 8.0 基于二进制日志主从配置

    一下只记录本人在已经完成的基于二进制日志主从配置的服务器上继续进行的过程

    官方文档
    17.1.3.4 使用 GTID 设置复制

我的环境

主服务器

Ubuntu 20.04 :
192.1568.0.101
$ mysql -V
mysql  Ver 8.0.32 for Linux on x86_64 (MySQL Community Server - GPL)

从服务器

Ubuntu 20.04 : 
192.168.0.103
$ mysql -V
mysql  Ver 8.0.32 for Linux on x86_64 (MySQL Community Server - GPL)

2 个服务器上的mysql 都是直接使用 apt 安装后,都是缺省配置
假设所有密码都是 Password@123

操作步骤

抄录一下官方文档实现步骤

对于最简单的 GTID 复制拓扑(由一个源和一个副本组成),此启动过程中的关键步骤如下:1. 如果复制已经在运行,通过将它们设置为只读来同步两个服务器。2. 停止两个服务器。3. 在启用 GTID 并配置正确选项的情况下重新启动两台服务器。启动服务器所需的mysqld选项将在本节后面的示例中讨论。4. 指示副本使用源作为复制数据源并使用自动定位。完成此步骤所需的 SQL 语句在本节后面的示例中进行了描述。5. 进行新的备份。包含没有 GTID 的事务的二进制日志不能在启用 GTID 的服务器上使用,因此在此之前进行的备份不能用于您的新配置。6. 启动副本,然后在两台服务器上禁用只读模式,以便它们可以接受更新。

其中步骤5. 进行新的备份,与学习无关,先不必关注

完全按照以上步骤,一步一步的进行
如果是新找了一个从服务器,从头开始,那么,直接跳到第 3 步

  1. 第 1 步:同步服务器。 只有在使用已经在不使用 GTID 的情况下进行复制的服务器时才需要执行此步骤。对于新服务器,请继续执行步骤 3。通过发出以下命令,在每个服务器上将 read_only系统变量 设置为只读 :ON

    在主服务器(192.1568.0.101)上

     mysql> SET @@GLOBAL.read_only = ON;
    

    在从服务器(192.1568.0.102)上

     mysql> SET @@GLOBAL.read_only = ON;文档上没有写,但是要求启动时 --skip-log-bin ,所以,这里先 stop ,以免后续造成同步错误mysql> stop replica;
    
  2. 第 2 步:停止两个服务器。 使用mysqladmin 停止每个服务器,如下所示,其中username是具有足够权限关闭服务器的 MySQL 用户的用户名:

     $> mysqladmin -uusername -p shutdown
    

    因为 mysqladmin 只能停止,不能开启,所以,本次操作我直接使用 systemctl 来完成

     在主服务器(192.1568.0.101)和从服务器(192.1568.0.103)上sudo systemctl stop mysql
    
  3. 第 3 步:在启用 GTID 的情况下启动两台服务器。 gtid_mode要启用基于 GTID 的复制,必须通过将变量 设置为启用 GTID 模式来启动每个服务器 ON,并 enforce_gtid_consistency 启用该变量以确保仅记录对基于 GTID 的复制安全的语句。例如:

    gtid_mode=ON
    enforce-gtid-consistency=ON

    在主服务器(192.1568.0.101)和从服务器(192.1568.0.103)上
    $ cd /etc/mysql/mysql.conf.d
    $ cat mysqld.cnf

     ......[mysqld]pid-file	= /var/run/mysqld/mysqld.pidsocket		= /var/run/mysqld/mysqld.sockdatadir		= /var/lib/mysqllog-error	= /var/log/mysql/error.log# wzh 20230413server-id=101# wzh 20230419gtid_mode=ONenforce-gtid-consistency=ON
    

    按照文档要求,从服务器最好是为副本指定 --skip-log-bin 来启动
    但是我还是使用 systemctl

     sudo mysqld_safe --skip-replica-start --skip-log-bin &
    

    先启动从服务器,因为我在前面已经 stop replica ,并且使用的 systemctl stop
    先启动从服务器也可以防止前面操作时,忘记 stop replica

     	sudo systemctl start mysql
    

    再启动主服务器

     sudo systemctl start mysql
    
  4. 第 4 步:配置副本以使用基于 GTID 的自动定位。 告诉副本使用基于 GTID 事务的源作为复制数据源,并使用基于 GTID 的自动定位而不是基于文件的定位。在副本上发出 CHANGE REPLICATION SOURCE TO 语句

    在从服务器(192.1568.0.103)上

     mysql> CHANGE REPLICATION SOURCE TOSOURCE_HOST = '192.168.0.101',SOURCE_PORT = 3306,SOURCE_USER = 'repl',SOURCE_PASSWORD = 'Password@123',SOURCE_AUTO_POSITION = 1;
    

    如果 repl 用户报错(实际我这里也是报错!),先使用 root 用户

     mysql> CHANGE REPLICATION SOURCE TOSOURCE_HOST = '192.168.0.101',SOURCE_PORT = 3306,SOURCE_USER = 'root',SOURCE_PASSWORD = 'Password@123',SOURCE_AUTO_POSITION = 1;
    
  5. 第 5 步:进行新备份。
    这一步先跳过

  6. 第 6 步:启动副本并禁用只读模式。
    在从服务器(192.1568.0.103)上

     mysql> START REPLICA;可能是因为我使用的 systemctl stop mysql ,再次启动后,主从服务器都已经没有 read_only 了mysql> show global variables like '%read_only%';
    

    到这里就算完成了!

     mysql> show replica status\\G;* 1. row *Replica_IO_State: Waiting for source to send eventSource_Host: 192.168.0.101Source_User: replSource_Port: 3306Connect_Retry: 60Source_Log_File: binlog.000001Read_Source_Log_Pos: 620Relay_Log_File: ubuntu2004-103-relay-bin.000002Relay_Log_Pos: 411Relay_Source_Log_File: binlog.000001Replica_IO_Running: YesReplica_SQL_Running: Yes......
    

    在主服务器上确认

     mysql> show replicas;+-----------+------+------+-----------+--------------------------------------+| Server_Id | Host | Port | Source_Id | Replica_UUID                         |+-----------+------+------+-----------+--------------------------------------+|       103 |      | 3306 |       101 | a0ab8577-ddc1-11ed-8e3f-00155d5aa69a |+-----------+------+------+-----------+--------------------------------------+1 row in set (0.00 sec)
    

错误处理

  1. 先直接使用 repl 用户时,报错
    Last_IO_Errno: 2061
    Last_IO_Error: Error connecting to source ‘repl@192.168.0.101:3306’. This was attempt 1/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin ‘caching_sha2_password’ reported error: Authentication requires secure connection.
    Last_SQL_Errno: 0

    解决:
    先确认权限没有问题

     mysql> show grants for repl;+----------------------------------------------+| Grants for repl@%                            |+----------------------------------------------+| GRANT REPLICATION SLAVE ON *.* TO `repl`@`%` |+----------------------------------------------+1 row in set (0.00 sec)
    

    改用 root 完成,等 root 正确完成后,再改用 repl

    mysql主从复制只能root,使用 repl 报错:Authentication plugin ‘caching_sha2_password‘ reported error

  2. 改用 root 后也报错
    Last_IO_Errno: 13114
    Last_IO_Error: Got fatal error 1236 from source when reading data from binary log: ‘Slave has more GTIDs than the master has, using the master’s SERVER_UUID. This may indicate that the end of the binary log was truncated or that the last binary log file was lost, e.g., after a power or disk failure when sync_binlog != 1. The master may or may not have rolled back transactions that were already replicated to the slave. Suggest to replicate any transactions that master has rolled back from slave to master, and/or commit empty transactions on master to account for transactions that have been’

    解决:

    去主服务器 reset master

     mysql> reset master;
    

    然后再去从服务器上尝试 root ,OK!
    再改用 repl 试试, OK

     mysql> stop replica;Query OK, 0 rows affected (0.05 sec)mysql> CHANGE REPLICATION SOURCE TO-> SOURCE_HOST = '192.168.0.101',-> SOURCE_PORT = 3306,-> SOURCE_USER = 'repl',-> SOURCE_PASSWORD = 'Password@123',-> SOURCE_AUTO_POSITION = 1;Query OK, 0 rows affected, 2 warnings (0.31 sec)mysql> start replica;Query OK, 0 rows affected (0.13 sec)mysql> show replica status\\G;* 1. row *Replica_IO_State: Waiting for source to send eventSource_Host: 192.168.0.101Source_User: replSource_Port: 3306Connect_Retry: 60Source_Log_File: binlog.000001Read_Source_Log_Pos: 620Relay_Log_File: ubuntu2004-103-relay-bin.000002Relay_Log_Pos: 411Relay_Source_Log_File: binlog.000001Replica_IO_Running: YesReplica_SQL_Running: Yes......
    
  3. 导出警告

    $ mysqldump -uroot -pPassword@123 --all-databases --source-data > dbdump101-20230417.db

     mysqldump: [Warning] Using a password on the command line interface can be insecure.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 --events.
    

解决:
按照warning 补充完整

$ mysqldump -uroot -pPassword@123 --all-databases --source-data --triggers --routines --events --set-gtid-purged=OFF > dbdump101-20230417.db
  1. 导入报错
    $ mysql -uroot -pPassword@123 < dbdump101-20230417.db

    mysql: [Warning] Using a password on the command line interface can be insecure.
    ERROR 1776 (HY000) at line 24: Parameters MASTER_LOG_FILE, MASTER_LOG_POS, RELAY_LOG_FILE and RELAY_LOG_POS cannot be set when MASTER_AUTO_POSITION is active.

    解决:

    mysql 导入是报错:Parameters SOURCE_LOG_FILE, … cannot be set when SOURCE_AUTO_POSITION is active.

     mysql> stop replica;mysql> change master to master_auto_position=0;