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

时间:2019-07-04
本文章向大家介绍mysql主从之主机名导致主从机制失败的问题,主要包括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 |

 

验证成功

恢复正确的配置

log-bin=mater-bin

log-bin-index = master-bin.index

$flag 上一页 下一页