mysql主从之主机名导致主从机制失败的问题

一 主库

mysql主服务器的正确配置需要指定log-bin、log-bin-index

server-id = 1

log-bin=master-bin

log-bin-index = master-bin.index

如果使用正确的配置,主机名改变后,即使mysql重启了,主从同步是能够照常进行的。

如不指定的话,binlog文件名就会依赖于主机名,如下配置

server-id = 1

log-bin

例如配置变为:

[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=1
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
connect_timeout = 60
net_read_timeout = 120
performance_schema_max_table_instances = 400
server-id = 1
log-bin    #之开启斌log日志,不指定日志名

[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

主机名改变会导致binlog文件名改变,binlog文件名改变,主从同步会出问题,多台从库的话,所有从库都会出问题

[[email protected] mysql]# vim /etc/my.cnf

[[email protected] mysql]# hostnamectl set-hostname mater1
[[email protected] mysql]# hostname
mater1
[[email protected] mysql]# systemctl restart mysqld
[[email protected] mysql]# ll

-rw-r-----. 1 mysql mysql       56 Jul  3 11:37 auto.cnf
-rw-r-----. 1 mysql mysql      304 Jul  4 10:27 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jul  4 10:27 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jul  4 10:27 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jul  3 11:37 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Jul  4 10:27 ibtmp1
-rw-r-----. 1 mysql mysql     1984 Jul  4 10:25 master-bin.000001
-rw-r-----. 1 mysql mysql      177 Jul  4 10:27 master-bin.000002
-rw-r-----. 1 mysql mysql       40 Jul  4 10:25 master-bin.index
-rw-r-----. 1 mysql mysql      154 Jul  4 10:27 mater1-bin.000001   #改变之后的日志名
-rw-r-----. 1 mysql mysql       20 Jul  4 10:27 mater1-bin.index
drwxr-x---. 2 mysql mysql     4096 Jul  3 11:37 mysql
-rw-r-----. 1 mysql mysql      890 Jul  4 10:27 mysql-slow.log
srwxrwxrwx. 1 mysql mysql        0 Jul  4 10:27 mysql.sock
-rw-------. 1 mysql mysql        6 Jul  4 10:27 mysql.sock.lock
drwxr-x---. 2 mysql mysql     8192 Jul  3 11:37 performance_schema
drwxr-x---. 2 mysql mysql     8192 Jul  3 11:37 sys

查看slave的状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 192.168.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-log.000005
                Relay_Log_Pos: 369
        Relay_Master_Log_File: master-bin.000002
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           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: 154
              Relay_Log_Space: 737
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1236
                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file‘    #错误信息
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 77278e78-9da8-11e9-bc6c-000c2991dd19
             Master_Info_File: /data/mysql/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: 190704 10:28:05
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version: 

主端创建库

mysql> create database darren;
Query OK, 1 row affected (0.00 sec)

mysql> use darren;
Database changed
mysql> create table test (id int);
Query OK, 0 rows affected (0.01 sec)

测试shell脚本,一直在主库插入数据

[[email protected] mysql]# while true ;  do mysql -uroot -p123456  -e ‘use darren;insert into test values (1);‘; sleep 1; done

演示主从同步失败

Last_IO_Errno: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file‘

处理方法

stop slave; #停止同步

reset slave; #重置slave,这个操作会使用slave的配置丢失,由于binlog文件名改变了,所以slave得重新配置

#从新binlog的第一个binlog文件+第一个位置

主端信息:

mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| mater1-bin.000001 |     10330 |
+-------------------+-----------+
1 row in set (0.00 sec)

mysql> show binlog events in ‘mater1-bin.000001‘;
+-------------------+-------+----------------+-----------+-------------+------------------------------------------+
| Log_name          | Pos   | Event_type     | Server_id | End_log_pos | Info                                     |
+-------------------+-------+----------------+-----------+-------------+------------------------------------------+
| mater1-bin.000001 |     4 | Format_desc    |         1 |         123 | Server ver: 5.7.26-log, Binlog ver: 4    |
| mater1-bin.000001 |   123 | Previous_gtids |         1 |         154 |                                          |
| mater1-bin.000001 |   154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= ‘ANONYMOUS‘     |
......
| mater1-bin.000001 | 10136 | Query          |         1 |       10210 | BEGIN                                    |
| mater1-bin.000001 | 10210 | Table_map      |         1 |       10259 | table_id: 108 (darren.test)              |
| mater1-bin.000001 | 10259 | Write_rows     |         1 |       10299 | table_id: 108 flags: STMT_END_F          |
| mater1-bin.000001 | 10299 | Xid            |         1 |       10330 | COMMIT /* xid=209 */                     |
+-------------------+-------+----------------+-----------+-------------+------------------------------------------+
196 rows in set (0.00 sec)

日志的第一个位置4

从端配置

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to master_host=‘192.168.132.121‘,master_port=3306,master_user=‘replication‘,master_password=‘1234567‘,master_log_file=‘mater1-bin.000001‘,master_log_pos=4;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mater1-bin.000001
          Read_Master_Log_Pos: 10330
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 10545
        Relay_Master_Log_File: mater1-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           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: 10330
              Relay_Log_Space: 10746
              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: 77278e78-9da8-11e9-bc6c-000c2991dd19
             Master_Info_File: /data/mysql/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:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec) 

验证数据

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| darren             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use darren;
Database changed
mysql> show tables;
+------------------+
| Tables_in_darren |
+------------------+
| test             |
+------------------+
1 row in set (0.00 sec)

mysql> select * from test;
+------+
| id   |
+------+
|    1 |
|    1 |
|    1 |
|    1 |
|    1 |
|    1 |

验证成功

二 从库

mysql从库正确的配置需要指定relay-log、relay-log-index

server-id = 2

relay-log = relay-log

relay-log-index = relay-log.index

如果是使用正确的配置,从库重启的话,主从同步能够照常进行。

没指定relay-log、relay-log-index

默认依赖于主机名,主机名改变,有发生mysql重启,重启后主从同步失败

配置如下:

[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=1
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
connect_timeout = 60
net_read_timeout = 120
performance_schema_max_table_instances = 400
server-id = 2

[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid                    

[[email protected] ~]# hostnamectl set-hostname slave1

[[email protected] ~]# hostname
slave1

[[email protected] ~]# systemctl start mysqld
[[email protected] ~]# cd /data/mysql/
[[email protected] mysql]# ll

-rw-r-----. 1 mysql mysql       56 Jul  3 11:56 auto.cnf
drwxr-x---. 2 mysql mysql       52 Jul  4 10:41 darren
-rw-r-----. 1 mysql mysql      416 Jul  4 10:58 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jul  4 11:00 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jul  4 11:00 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jul  3 11:56 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Jul  4 11:00 ibtmp1
-rw-r-----. 1 mysql mysql      139 Jul  4 11:00 master.info
drwxr-x---. 2 mysql mysql     4096 Jul  3 11:56 mysql
-rw-r-----. 1 mysql mysql      530 Jul  4 11:00 mysql-slow.log
srwxrwxrwx. 1 mysql mysql        0 Jul  4 11:00 mysql.sock
-rw-------. 1 mysql mysql        5 Jul  4 11:00 mysql.sock.lock
drwxr-x---. 2 mysql mysql     8192 Jul  3 11:56 performance_schema
-rw-r-----. 1 mysql mysql      201 Jul  4 10:41 relay-log.000001
-rw-r-----. 1 mysql mysql    10568 Jul  4 10:58 relay-log.000002
-rw-r-----. 1 mysql mysql       38 Jul  4 10:41 relay-log.index
-rw-r-----. 1 mysql mysql       58 Jul  4 10:58 relay-log.info
-rw-r-----. 1 mysql mysql      177 Jul  4 11:00 slave1-relay-bin.000001   #新的日志名
-rw-r-----. 1 mysql mysql       26 Jul  4 11:00 slave1-relay-bin.index
drwxr-x---. 2 mysql mysql     8192 Jul  3 11:56 sys

进入数据库

mysql> start slave;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 192.168.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mater1-bin.000001
          Read_Master_Log_Pos: 10330
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 10545
        Relay_Master_Log_File: mater1-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 1872
                   Last_Error: Slave failed to initialize relay log info structure from the repository
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 10330
              Relay_Log_Space: 0
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 1872
               Last_SQL_Error: Slave failed to initialize relay log info structure from the repository
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
                  Master_UUID: 77278e78-9da8-11e9-bc6c-000c2991dd19
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp: 190704 11:02:52
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

Last_Errno: 1872

Last_Error: Slave failed to initialize relay log info structure from the repository

解决方案

需要记录当前主从同步的位置信息,一定得记录,不然同步的数据就会不全

Master_Log_File: mater1-bin.000001

Read_Master_Log_Pos: 10330

模拟主库正在写数据

[[email protected] mysql]# while true ;  do mysql -uroot -p123456  -e ‘use darren;insert into test values (1);‘; sleep 1; done

从库修复

mysql> change master to master_host=‘192.168.132.121‘,master_port=3306,master_user=‘replication‘,master_password=‘1234567‘,master_log_file=‘mater1-bin.000001‘,master_log_pos=10330;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mater1-bin.000001
          Read_Master_Log_Pos: 31050
               Relay_Log_File: slave1-relay-bin.000002
                Relay_Log_Pos: 21041
        Relay_Master_Log_File: mater1-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           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: 31050
              Relay_Log_Space: 21249
              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: 77278e78-9da8-11e9-bc6c-000c2991dd19
             Master_Info_File: /data/mysql/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:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version: 

验证数据一致性

主库:
mysql> select * from darren.test;
118 rows in set (0.00 sec)
从库:
mysql> select * from darren.test;
118 rows in set (0.00 sec)

实验完成,回复原有正确配置

原文地址:https://www.cnblogs.com/zyxnhr/p/11135412.html

时间: 2024-09-30 02:07:13

mysql主从之主机名导致主从机制失败的问题的相关文章

修改主机名导致复制失败

发现主备库的主机名命名命名反了,重命名主机名后发现mysql slave无法复制,怀疑是slave的相关进程没有起来,于是执行start slave,报错 Slave failed to initialize relay log info structure from the repository, Error_code: 1872 执行 reset slave; change master to master_host=' '; change master to  master_host='1

MySQL 在云主机上搭建主从

1:已经事先购买了两台云主机 10.4.14.168      101.69.178.208 [联通] Master 10.4.5.9         101.69.178.219 [联通] Slave 2:分别安装MySQL #yum install mysql-server –enablerepo=remi #yum install mysql-devel 3:查看MySQL安装情况 [[email protected] ~]#  rpm -qa | grep mysql* mysql-5.

MySQL使用主机名(127.0.0.1)连接不了

最近遇到一个问题,连接MySQL的时候主机名用localhost可以,用127.0.0.1则不行.于是乎google了一番,总结下,以做备忘吧. 一. MySQL启动的配置 首先MySQL启动时会加载配置文件,即my.cnf文件.在这个文件中我们可以设置连接数据库的IP限制, 如下图 二.  MySQL验证连接的过程. 我们知道MySQL服务器是通过权限表来控制用户访问的,权限表放置在mysql数据库中,由mysql_install_db脚本初始化.这些MySQL权限表分别user,db,tab

【20181101】MySQL text类型的column设置为NOT NULL 导致主从1364

环境 系统版本 : CentOS release 6.8 (Final) MySQL版本:5.6.29-log MySQL Community Server (GPL) MySQL主从配置信息 binlog format :MIXED sql_mode: NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 问题描述 从库show slave status监控的时候发现sql_thread进程已经变成NO,并且爆出了1362错误,仔细查看报错的是一条insert

Mysql的主主备份与主从备份--同步

Mysql的主主备份与主从备份--同步 一:实验目标 mysql主从原理 实战:mysql主从配置 实战:mysql主主配置 二:实验环境 导入测试数据库book: Mysql主xuegod63   IP:192.168.1.63 Mysql从xuegod64   IP:192.168.1.64 三:实验代码 实战:mysql主从配置  服务概述:主从复制 复制解决的基本问题是让一台服务器的数据和另外的服务器保持同步. 一台主服务器可以连接多台从服务器,并且从服务器也可以反过来作主服务器.主服务

《Mycat学习笔记》 第二篇. MySql 读写分离与日志分析——主从多结点

1    环境说明 接上篇环境   <Mycat学习笔记> 第一篇. MySql 读写分离与日志分析——主从单结点 http://www.cnblogs.com/kaye0110/p/5134588.html 增加一套 mysql 实例,端口为3308 ,通过Binlog方式同步主机情况 localhost : 3306 主机,    在mycat 中配置为 writehost 1 localhost : 3307 从机 a ,在mycat 中配置为 readhost localhost :

mysql安装部署,备份,主从

MySQL数据库 By:大官人 一.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的RDBMS (Relational Database Management System,关系数据库管理系统)应用软件之一.MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.M

MySQL高可用之——keepalivd+互为主从

目标:配置一个keepalived双机热备架构,并配置主从复制 规划: master1     zlm177     192.168.17.177 master2     zlm188     192.168.17.188 vip                             192.168.17.166 环境: Red Hat Enterprise Linux 6.4 Percona Server 5.6.15 一.软件安装 可以去官网http://www.keepalived.or

《Mycat学习笔记》 第三篇. MySql 主从同步异常后,主从切换

1)系统环境说明 MySql 5.5 主从节点 127.0.0.1:3306   主结点,为验证主从切换效果,手动停止服务 127.0.0.1: 3307    从结点 1 127.0.0.1:338     从结点 2 ,为验证主从切换效果,在主结点停止后,新增两个记录. MyCat 1.5 schema.xml 配置 具体配置说明,参考上篇: <Mycat学习笔记> 第二篇. MySql 读写分离与日志分析——主从多结点 <dataHost name="localhost1