解决 mysql主从失败问题

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
日志服务 SLS,月写入数据量 50GB 1个月
简介:
环境
Linux 系统:redhat as4.5
Mysql 版本:mysql 4.1.22
 
         星期一上班,就听到 开发说一台mysql数据库down掉(此台数据库只做备份用)。连上系统,用ps -ef |grep mysql查看下进程,果真以个进程都没有。那就重启mysql服务,呀启动不起来。就查看mysql的错误日志。
InnoDB: Doing recovery: scanned up to log sequence number 0 48155
InnoDB: Last MySQL binlog file position 0 79, file name /var/log/mysql/updatelog.000006
100621 5:36:33 InnoDB: Flushing modified pages from the buffer pool...
100621 5:36:33 InnoDB: Started; log sequence number 0 48155
100621 5:36:34 [ERROR] /mysql/libexec/mysqld: Error writing file '/data/db2.ihome.com.pid' (Errcode: 28)
100621 5:36:34 [ERROR] Can't start server: can't create PID file: No space left on device
 
Number of processes running now: 0
100621 05:36:34 mysqld restarted
/mysql/libexec/mysqld: Error writing file './db2-slow.log' (Errcode: 28)
100621 5:36:34 [ERROR] Could not use db2-slow.log for logging (error 28). Turning logging off for the whole duration of the MySQL server process. To turn it on again: fix the cause, shutdown the MySQL server and restart it.
100621 5:36:34 InnoDB: Database was not shut down normally!
 
      哦原来磁盘空间满了,使用df -h 查看下,存放data的目录已经使用100%.进入data目录查看。原来是一个备份数据库文件在里面占用的了大量的磁盘空间。查看备份脚本原来是备份脚本里面的路径写错了。修改路径,把备份文件移动到备份目录。启动mysql服务正常启动。
 
 
    使用命令登陆上mysql,发现主从已经不同步了。查看mysql的错误日志得到
100621 9:32:21 [Note] Slave SQL thread initialized, starting replication in log 'mysql-bin.000023' at position 211845, relay log './db2-relay-bin.000013' position: 217010
100621 9:32:21 [ERROR] Error in Log_event::read_log_event(): 'read error', data_len: 112, event_type: 2
100621 9:32:21 [ERROR] Error reading relay log event: slave SQL thread aborted because of I/O error
100621 9:32:21 [ERROR] Slave: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave. Error_code: 0
100621 9:32:21 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000023' position 211845
 
 
首先想到尝试着手动来解决主从同步。连接上数据库
 
[root@db2 data]# mysql -u root -p
Enter password:      输入密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 55 to server version: 4.1.12-log
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
 
mysql> change master to
mysql->MASTER_LOG_POS=211845
mysql ->MASTER_LOG_FILE = 'mysql-bin.000023';
mysql>start slave;
mysql>show slave status\G;
查看同步依然是不成功。继续查看mysql 错误日志
100621 9:41:35 [Note] Slave SQL thread initialized, starting replication in log 'binglog mysql-bin.000023' at position 211845, relay log './db2-relay-bin.000001' position: 4
100621 9:41:35 [Note] Slave I/O thread: connected to master 'repl@192.168.1.41:3306', replication started in log 'binglog mysql-bin.000023' at position 211845
100621 9:41:35 [ERROR] Error reading packet from server: Could not find first log file name in binary log index file (server_errno=1236)
100621 9:41:35 [ERROR] Got fatal error 1236: 'Could not find first log file name in binary log index file' from master when reading data from binary log
100621 9:41:35 [ERROR] Slave I/O thread exiting, read up to log 'binglog mysql-bin.000023', position 211845
 
删除掉data目录下文件
db2-relay-bin.000001 
db2-relay-bin.index
master.info 
relay-log.info # 这些文件里面存放着同步时的信息。
     然后重启下mysql 服务,查看同步信息。问题依旧。 然后 查看上次错误日志 与此次的错误日志。找不到 log文件,连接上主的mysql数据库。查看mysql信息一切正常。
#mysqlbinlog --start-position=211845 mysql-bin.000023 > a.sql
把从 211845到现在不同步的二进制文件导入到一个文本文件里面。然后使用scp 传到从数据库上
scp a.sql root@192.168.1.43:/    输入密码自动copy到mysql 从服务器上的根目录下。
打开a.sql 查看最后一行的
然后到从数据库上 停止mysql slave 服务。stop slave
使用#mysql -uuser -p passwd database < /a.sql #手动导入没有同步的数据
导入完成之后 删除掉 db2-relay-bin.000001 db2-relay-bin.index master.info relay-log.info 这些文件。 使用
mysql>stop slave ;
mysql>change master to MASTER_LOG_POS = 155383248 MASTER_LOG_FILE = 'mysql-bin.000023';
Query OK, 0 rows affected (0.01 sec)
然后重启mysql服务,service mysqld restart
启动slave
mysql>start slave;
查看mysql主从状态mysql>show slave status\G;
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
看到这两行状态为Yes我心甚慰。看到这两行为Yes表示已经开始同步数据了。
   下面是 CHANGE MASTER TO语法
CHANGE MASTER TO master_def[,master_def] ...
 
master_def:
      MASTER_HOST = 'host_name'
    | MASTER_USER = 'user_name'
    | MASTER_PASSWORD = 'password'
    | MASTER_PORT =port_num
    | MASTER_CONNECT_RETRY =count
    | MASTER_LOG_FILE = 'master_log_name'
    | MASTER_LOG_POS =master_log_pos
    | RELAY_LOG_FILE = 'relay_log_name'
    | RELAY_LOG_POS =relay_log_pos
    | MASTER_SSL = {0|1}
    | MASTER_SSL_CA = 'ca_file_name'
    | MASTER_SSL_CAPATH = 'ca_directory_name'
    | MASTER_SSL_CERT = 'cert_file_name'
    | MASTER_SSL_KEY = 'key_file_name'
    | MASTER_SSL_CIPHER = 'cipher_list'
可以更改从属服务器用于与主服务器进行连接和通讯的参数。
MASTER_USER,MASTER_PASSWORD,MASTER_SSL,MASTER_SSL_CA,MASTER_SSL_CAPATH,MASTER_SSL_CERT,MASTER_SSL_KEY和MASTER_SSL_CIPHER用于向从属服务器提供有关如何与主服务器连接的信息。
 

本文转自holy2009 51CTO博客,原文链接:http://blog.51cto.com/holy2010/362339

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
11月前
|
关系型数据库 MySQL 开发工具
MySQL5.7主从配置(Docker)
MySQL5.7主从配置(Docker)
852 0
|
11月前
|
Ubuntu 关系型数据库 MySQL
使用Ubuntu和Windows电脑实现Mysql主从同步(详细操作步骤)
使用Ubuntu和Windows电脑实现Mysql主从同步(详细操作步骤)
144 2
|
11月前
|
SQL 关系型数据库 MySQL
解决MySQL主从慢同步问题的常见的解决方案:
解决MySQL主从慢同步问题的方法有很多,以下是一些常见的解决方案: 1. 检查网络连接:确保主从服务器之间的网络连接稳定,避免网络延迟或丢包导致数据同步缓慢。 2. 优化数据库配置:调整MySQL的配置参数,如增大binlog文件大小、调整innodb_flush_log_at_trx_commit等参数,以提高主从同步性能。 3. 检查IO线程和SQL线程状态:通过SHOW SLAVE STATUS命令检查IO线程和SQL线程的状态,确保它们正常运行并没有出现错误。 4. 检查主从日志位置:确认主从服务器的binlog文件和位置是否正确,避免由于错误的日志位置导致同步延迟。 5.
1468 1
|
11月前
|
Kubernetes Cloud Native 关系型数据库
提升数据安全与性能,掌握Helm一键部署MySQL 8.0主从技巧
【4月更文挑战第9天】提升数据安全与性能,掌握Helm一键部署MySQL 8.0主从技巧
564 0
|
11月前
|
SQL 存储 关系型数据库
MySQL的主从复制&主从同步
MySQL的主从复制&主从同步
180 0
|
11月前
|
负载均衡 关系型数据库 MySQL
MySQL主从架构的搭建
MySQL主从架构的搭建
148 3
|
11月前
|
SQL 关系型数据库 MySQL
MySQL-主从架构的搭建
MySQL-主从架构的搭建
309 0
|
10月前
|
Prometheus 监控 关系型数据库
数据库同步革命:MySQL GTID模式下主从配置的全面解析
数据库同步革命:MySQL GTID模式下主从配置的全面解析
956 0
|
9月前
|
存储 关系型数据库 MySQL
利用 MySQL 克隆插件搭建主从
MySQL 的 Clone 插件是一个强大的功能,首次引入于 MySQL 8.0.17 版本。简单来说,Clone Plugin 是一款物理克隆数据工具,它能够帮助我们快速、高效地克隆或复制数据库,极大地简化了数据库迁移、备份和恢复的过程,让我们在处理大量数据时更加得心应手。本篇文章我们一起来学习下如何使用克隆插件。
115 2
|
9月前
|
运维 关系型数据库 MySQL
【实操记录】MySQL主从配置
本文使用MySQL原生支持的主从同步机制,详细记录了配置步骤及运维操作方法,可供大家直接参考、使用。 本文假设已经部署了两台主机的MySQL软件,且数据库服务正常,详细部署步骤可本站搜索:"mysql二进制安装包部署"
431 0