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的路径;

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
