码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-07-05 00:01:09      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:失败   reset   停止   repos   exec   warning   none   解决方案   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文件名改变,主从同步会出问题,多台从库的话,所有从库都会出问题

[root@master mysql]# vim /etc/my.cnf

[root@master mysql]# hostnamectl set-hostname mater1
[root@master mysql]# hostname
mater1
[root@master mysql]# systemctl restart mysqld
[root@master 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脚本,一直在主库插入数据

[root@master 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                    

 

[root@slave ~]# hostnamectl set-hostname slave1

[root@slave ~]# hostname
slave1

[root@slave ~]# systemctl start mysqld
[root@slave ~]# cd /data/mysql/
[root@slave 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

模拟主库正在写数据

[root@master 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)

 

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

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

标签:失败   reset   停止   repos   exec   warning   none   解决方案   mysql主从   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!