一 主库

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=
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=
innodb_flush_log_at_trx_commit =
log_warnings =
connect_timeout =
net_read_timeout =
performance_schema_max_table_instances =
server-id =
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-----.  mysql mysql        Jul   : auto.cnf
-rw-r-----. mysql mysql Jul : ib_buffer_pool
-rw-r-----. mysql mysql Jul : ibdata1
-rw-r-----. mysql mysql Jul : ib_logfile0
-rw-r-----. mysql mysql Jul : ib_logfile1
-rw-r-----. mysql mysql Jul : ibtmp1
-rw-r-----. mysql mysql Jul : master-bin.
-rw-r-----. mysql mysql Jul : master-bin.
-rw-r-----. mysql mysql Jul : master-bin.index
-rw-r-----. mysql mysql Jul : mater1-bin.000001 #改变之后的日志名
-rw-r-----. mysql mysql Jul : mater1-bin.index
drwxr-x---. mysql mysql Jul : mysql
-rw-r-----. mysql mysql Jul : mysql-slow.log
srwxrwxrwx. mysql mysql Jul : mysql.sock
-rw-------. mysql mysql Jul : mysql.sock.lock
drwxr-x---. mysql mysql Jul : performance_schema
drwxr-x---. mysql mysql Jul : sys

查看slave的状态

mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State:
Master_Host: 192.168.132.121
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: master-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: master-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
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:
Last_IO_Error: Got fatal error from master when reading data from binary log: 'Could not find first log file name in binary log index file' #错误信息
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 77278e78-9da8-11e9-bc6c-000c2991dd19
Master_Info_File: /data/mysql/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp: ::
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:

主端创建库

mysql> create database darren;
Query OK, row affected (0.00 sec) mysql> use darren;
Database changed
mysql> create table test (id int);
Query OK, 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. | |
+-------------------+-----------+
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. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: |
| mater1-bin. | | Previous_gtids | | | |
| mater1-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
......
| mater1-bin. | | Query | | | BEGIN |
| mater1-bin. | | Table_map | | | table_id: (darren.test) |
| mater1-bin. | | Write_rows | | | table_id: flags: STMT_END_F |
| mater1-bin. | | Xid | | | COMMIT /* xid=209 */ |
+-------------------+-------+----------------+-----------+-------------+------------------------------------------+
rows in set (0.00 sec)

日志的第一个位置4

从端配置

mysql> stop slave;
Query OK, rows affected (0.00 sec) mysql> reset slave;
Query OK, rows affected (0.01 sec) mysql> change master to master_host='192.168.132.121',master_port=,master_user='replication',master_password='',master_log_file='mater1-bin.000001',master_log_pos=;
Query OK, rows affected, warnings (0.01 sec) mysql> start slave;
Query OK, rows affected (0.00 sec) mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.132.121
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: mater1-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: mater1-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 77278e78-9da8-11e9-bc6c-000c2991dd19
Master_Info_File: /data/mysql/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.00 sec) 

验证数据

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| darren |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql> use darren;
Database changed
mysql> show tables;
+------------------+
| Tables_in_darren |
+------------------+
| test |
+------------------+
row in set (0.00 sec) mysql> select * from test;
+------+
| id |
+------+
| |
| |
| |
| |
| |
| |

验证成功

二 从库

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=
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=
innodb_flush_log_at_trx_commit =
log_warnings =
connect_timeout =
net_read_timeout =
performance_schema_max_table_instances =
server-id = [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-----.  mysql mysql        Jul   : auto.cnf
drwxr-x---. mysql mysql Jul : darren
-rw-r-----. mysql mysql Jul : ib_buffer_pool
-rw-r-----. mysql mysql Jul : ibdata1
-rw-r-----. mysql mysql Jul : ib_logfile0
-rw-r-----. mysql mysql Jul : ib_logfile1
-rw-r-----. mysql mysql Jul : ibtmp1
-rw-r-----. mysql mysql Jul : master.info
drwxr-x---. mysql mysql Jul : mysql
-rw-r-----. mysql mysql Jul : mysql-slow.log
srwxrwxrwx. mysql mysql Jul : mysql.sock
-rw-------. mysql mysql Jul : mysql.sock.lock
drwxr-x---. mysql mysql Jul : performance_schema
-rw-r-----. mysql mysql Jul : relay-log.
-rw-r-----. mysql mysql Jul : relay-log.
-rw-r-----. mysql mysql Jul : relay-log.index
-rw-r-----. mysql mysql Jul : relay-log.info
-rw-r-----. mysql mysql Jul : slave1-relay-bin.000001 #新的日志名
-rw-r-----. mysql mysql Jul : slave1-relay-bin.index
drwxr-x---. mysql mysql Jul : sys

进入数据库

mysql> start slave;
ERROR (HY000): Slave failed to initialize relay log info structure from the repository
mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State:
Master_Host: 192.168.132.121
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: mater1-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: mater1-bin.
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:
Last_Error: Slave failed to initialize relay log info structure from the repository
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
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:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error: Slave failed to initialize relay log info structure from the repository
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 77278e78-9da8-11e9-bc6c-000c2991dd19
Master_Info_File: /data/mysql/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: ::
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
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=,master_user='replication',master_password='',master_log_file='mater1-bin.000001',master_log_pos=;
mysql> start slave;
Query OK, rows affected (0.01 sec) mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.132.121
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: mater1-bin.
Read_Master_Log_Pos:
Relay_Log_File: slave1-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mater1-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 77278e78-9da8-11e9-bc6c-000c2991dd19
Master_Info_File: /data/mysql/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:

验证数据一致性

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

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

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

  1. MySQL服务器修改主机名后问题解决

    1.单机MySQL主机名修改 今天无事看到自己的主机名不对,于是改了一下,以便区分服务器,那只重启MySQL时出现下面错误: MySQL manager or server PID file coul ...

  2. rabbitmq因为主机名更改导致连接不上

    今天创建rabbitmq用户的时候报错,说nodedown,定睛一看,发现主机名是localhost,而此时我们的主机名是controller01,所以猜测是主机名导致的,重启服务器,就成功添加了. ...

  3. discuz清空session,导致session保存机制失败,session无法更新与解决

    <?php function userErrorHandler() { $e = func_get_args(); echo '<pre style="color:red;&qu ...

  4. .NET Core解析DNS域名或主机名的方法

    在.NET Core中我们可以用System.Net.Dns类来解析域名或主机名的IP地址,我们新建一个.NET Core控制台项目,写入下面代码: using System; using Syste ...

  5. windows 系统本地做mysql 主从同步,最后面解决主从同步库名不一致,表结构一致

    原文:windows 系统本地做mysql 主从同步,最后面解决主从同步库名不一致,表结构一致 mysql主从同步的好处以及原理       之前看到很多新闻说某某的服务器奔溃,磁盘碎了,导致数据丢失 ...

  6. MySQL 主主同步配置和主从配置步骤

    ★预备知识 : 1.双机热备 对于双机热备这一概念,我搜索了很多资料,最后,还是按照大多数资料所讲分成广义与狭义两种意义来说. 从广义上讲,就是对于重要的服务,使用两台服务器,互相备份,共同执行同一服 ...

  7. 使用Mycat构建MySQL读写分离、主从复制、主从高可用

    数据库读写分离对于大型系统或者访问量很高的互联网应用来说,是必不可少的一个重要功能. 从数据库的角度来说,对于大多数应用来说,从集中到分布,最基本的一个需求不是数据存储的瓶颈,而是在于计算的瓶颈,即S ...

  8. MySQL高可用架构之Keepalived+主从架构部署

    针对目前公司的数据库架构要做统计规划和调整,所以针对不同的业务环境要选择合适的架构就比较重要,同时作为运维人员又要考虑到维护的便捷性(不能做过多架构维护),最终停留在mha+vip/主从+vip/my ...

  9. Centos7源码安装mysql及读写分离,互为主从

       Linux服务器 -源码安装mysql 及读写分离,互为主从   一.环境介绍: Linux版本: CentOS 7 64位 mysq版本: mysql-5.6.26 这是我安装时所使用的版本, ...

随机推荐

  1. jquery的操作

    jQuery jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行 ...

  2. 复杂SQL示例 (排行榜需求)

    公司项目要求做出排行榜,根据六组数据依次排行,关联多表,SQL记录下来方便日后查看 " ?><!DOCTYPE mapper PUBLIC "-//mybatis.or ...

  3. oralce CUBE

    select id,area,stu_type,sum(score) score from students group by cube(id,area,stu_type) order by id,a ...

  4. hdu 4128 Running relay (线性规划转半平面交)

    Problem - 4128 对偶线性规划转半平面交,这题的正解O(nlogn)解法,目前网上没有找到这样的正解. 原来的不等式组, sigma{-si*xi}>=-W+d*sigma{si} ...

  5. MapReduce数据流-概述

  6. uva 10566 Crossed Ladders (二分)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. H3C 使用命令视图

  8. MySQL基础内容

    数据类型 菜鸟教程(见最下方网友整理) : https://www.runoob.com/mysql/mysql-data-types.html 其他: 1字节(byte)=8位(bit),所以dou ...

  9. elasticsearch-倒排索引原理

    倒排索引 Elasticsearch 使用一种称为 倒排索引 的结构,它适用于快速的全文搜索.一个倒排索引由文档中所有不重复词的列表构成,对于其中每个词,有一个包含它的文档列表. 例如,假设我们有两个 ...

  10. Python--day70--ORM多对多的三种方式