【mysql案例】Failed toopen log--datadir物理迁移报错
1.1.1.mysql5.6.14的datadir迁移时遇到报错 【环境描述】 在机器A上安装了perconamysql 5.6.14,数据库停启正常,datadir路径为pathA,并且已经做了应用数据库的初始化工作,然后关闭了这个数据库实例,把它的datadir和/etc/my.cnf迁移到另外一台机器B上的pe
1.1.1. mysql5.6.14的datadir迁移时遇到报错
【环境描述】
在机器A上安装了perconamysql 5.6.14,数据库停启正常,datadir路径为pathA,并且已经做了应用数据库的初始化工作,然后关闭了这个数据库实例,把它的datadir和/etc/my.cnf迁移到另外一台机器B上的percona mysql 5.6.14,迁移后的datadir路径修改成路径pathB,在启动mysql服务的时候遇到问题,启动失败。
操作步骤:
1) 停掉机器A上的mysql
service mysql stop
2) 对机器A上mysql的datadir(路径pathA)和my.cnf做打包,传输到机器B,并把datadir解压到pathB
3) 在机器B上安装percona mysql 5.6.14
4) 使用机器A传输过来的my.cnf覆盖机器B的/etc/my.cnf
5) 修改机器B的/etc/my.cnf中datadir路径为pathB
6) 在机器B上执行service mysql start启动mysql服务
7) 启动失败,发生报错
【mysql报错】
启动时的报错servicemysql start:
Starting MySQL(Percona Server)......Theserver quit without[FAILED]ng PID file(/home/mysql_3306_bak/mysql.pid).
错误日志中的报错:
/usr/sbin/mysqld: File '/home/mysql_3306/mysql-bin.000003'not found (Errcode: 2 - No such file or directory)
2014-04-25 22:26:47 27048 [ERROR] Failed toopen log (file '/home/mysql_3306/mysql-bin.000003', errno 2)
2014-04-25 22:26:47 27048 [ERROR] Could notopen log file
2014-04-25 22:26:47 27048 [ERROR] Can'tinit tc log
2014-04-25 22:26:47 27048 [ERROR] Aborting
2014-04-25 22:26:47 27048 [Note] Binlog end
2014-04-25 22:26:47 27048 [Note] Shuttingdown plugin 'partition'
已经修改了my.cnf配置文件中所有的路径,但是Mysql仍然说找不到'/home/mysql_3306/mysql-bin.000003'路径的文件,从报错看上去很诡异。
【问题原因】
Mysql报错提示找不到binlog,是由于my.cnf中配置了:
log-bin= /home/mysql_3306/mysql-bin
log-bin-index= /home/mysql_3306/bin-index
mysql会在log-bin-index参数指定的文件中维护log-bin的索引列表,并且它是以绝对路径的方式记录的:
/home/mysql_3306?mysql-bin.000001
/home/mysql_3306?mysql-bin.000002
/home/mysql_3306?mysql-bin.000003
虽然已经把/etc/my.cnf中的所有路径都修改正确了,但是mysql服务在启动时,是通过读取log-bin-index来查找log-bin日志文件的,查找的文件还是在机器A上指定的位置,所以mysql服务启动失败。
【解决方法】
手动修改log-bin-index指定的二进制日志索引文件,修改里面所有log-bin的路径,指定到当前datadir下的二进制日志,然后尝试启动mysql服务,启动成功,问题解决。
【问题思考】
Whatis log-bin-index paramter ?
Mysql官方手册中说明“如果没有在my.cnf中配置log-bin-index参数指定,mysql会自动创建一个以host_name-bin.index命名的二进制索引文件(实验证明是mysql-bin.index)”。
所以,尝试去掉my.cnf中配置的log-bin-index参数,然后启动mysql服务,此时mysql服务正常启动,查看log-bin-index文件:
#cat mysql-bin.index
/home/mysql_3306_bak/mysql-bin.000006
我们发现mysql自动创建了名为“mysql-bin.index”的二进制索引文件,并且文件中只包含在启动时重新生成的二进制文件路径信息。
此时,mysql只知道此次启动时生成的二进制文件路径信息,那么也就意味着此时mysql丢失了编号000006之前的所有日志文件,我们进行如下的测试:
执行flush logs命令,让mysql再刷出来几个二进制日志;
#cat mysql-bin.index
/home/mysql_3306_bak/mysql-bin.000006
/home/mysql_3306_bak/mysql-bin.000007
/home/mysql_3306_bak/mysql-bin.000008
然后,执行purgebinary logs to 'mysql-bin.000004' 命令:
>purge binary logs to 'mysql-bin.000004';
ERROR1373 (HY000): Target log not found in binlog index
此时,mysql提示无法找到000004号二进制日志文件,接下来尝试删除000006号二进制日志文件:
> purge binary logs to'mysql-bin.000007';
QueryOK, 0 rows affected (0.03 sec)
查看mysql-bin.index二进制日志索引文件:
#cat mysql-bin.index
/home/mysql_3306_bak/mysql-bin.000007
/home/mysql_3306_bak/mysql-bin.000008
查看二进制日志文件:
#ls -ltr mysql-bin.00000*
mysql-bin.000004
mysql-bin.000001
mysql-bin.000002
mysql-bin.000003
mysql-bin.000005
mysql-bin.000007
mysql-bin.000008
mysql已经彻底删除了编号000006的二进制日志文件。
接下来,我们尝试欺骗mysql,配置一个指向虚机路径的二进制日志文件:
#cat mysql-bin.index
/tmp/mysql-bin.0000005
/home/mysql_3306_bak/mysql-bin.000007
/home/mysql_3306_bak/mysql-bin.000008
在mysql中尝试删除编号000007之前的日志:
>purge binary logs to 'mysql-bin.000007';
ERROR 29 (HY000): File '/tmp/mysql-bin.0000005' not found (Errcode:2 - No such file or directory)
mysql在读取log-bin-index日志索引文件删除日志的时候发现日志文件不存在,报错;
我们在尝试重启mysql,来判断它是如何读取和加载log-bin-index日志索引文件 以及索引文件中指定的二进制日志文件的:
# service mysql stop
Shutting down MySQL (PerconaServer)... [ OK ]
# service mysql start
Starting MySQL (PerconaServer).... [ OK ]
查看error.log日志:
2014-04-2600:53:28 7f752cddc700 InnoDB: Buffer pool(s) load completed at 140426 0:53:28
^G/usr/sbin/mysqld:File '/tmp/mysql-bin.0000005' not found (Errcode: 2 - No such file or directory)
2014-04-2600:53:28 16723 [ERROR] Failed to open log (file '/tmp/mysql-bin.0000005', errno2)
2014-04-2600:53:28 16723 [ERROR] Could not open log file
【总结】
虽然mysql的error日志中有error信息,但是mysql仍然成功启动了,也就是说mysql在启动过程会读取log-bin-index文件,然后也会判断索引指定的log-bin文件是否存在,结合本案例中遇到的情况,可以知道只有当最后一个路径指定的log-bin不存在时,mysql服务才会中断启动操作,即启动失败。
【Sum Up】
1) 如果mysql没有开启binlog,则不会遇到这个问题;
2) 如果mysql开启了binlog,并且在Datadir物理迁移的过程中,修改了datadir的路径,就会遇到这个问题,此时,可以编辑log-bin-index文件修复log-bin文件的路径或者直接删除,然后启动mysql服务;
3) 为了避免遇到这个问题,在进行datadir迁移的时候,尽量不要改变datadir的路径;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
