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

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use