记一次mysql数据库binlog丢失引起的故障
线上某业务需要对日志信息入库并进行分析最后呈现在管理后台上。某天突然发现后台没有前一天的分析数据。首先认为是java程序问题,于是查看应用程序日志,发现数
线上某业务需要对日志信息入库并进行分析最后呈现在管理后台上。某天突然发现后台没有前一天的分析数据。首先认为是java程序问题,于是查看应用程序日志,发现数据缺失的那天应用程序日志也没有记录,很是奇怪。接着手动执行jar包,本想看屏幕输出的报错信息,结果程序刚启动运行,执行了3条入库的sql语句(insert)后便卡住不动了,反复尝试了2-3次都是这种情况,接着怀疑是远程的mysql数据库问题导致无法插入新数据。
连接到远程数据库上,首先查看磁盘的分区df-h,果然mysql所在的数据库分区(/data)磁盘空间使用率居然达到了100%,太吓人了。果断du-sch*/data定位到具体的目录下的大文件,发现均为mysql的binlog文件
每个文件均为1.1G,这是mysql数据库binlog的默认值
查看mysql错误日志,如下信息:
131019 3:00:12 [ERROR] /usr/local/mysql/bin/mysqld: Disk is full writing './mysql-bin.000261' (Errcode: 28). Waiting for someone to free space... (Expect up to 60 secs delay for server to continue after freeing disk space) 131019 3:00:12 [ERROR] /usr/local/mysql/bin/mysqld: Retry in 60 secs. Message reprinted in 600 secs 131019 3:10:12 [ERROR] /usr/local/mysql/bin/mysqld: Retry in 60 secs. Message reprinted in 600 secs基本可以断定是应用程序在insert新的数据时,由于磁盘空间不足导致无法写入binlog文件导致无法插入新数据。
情急之下先删除了部分binlog,后设置max_binlog_size=500M,文件被truncate了,然后重启mysql数据库,悲剧的事情发生了,数据无法正常启动,再次查看错误日志:
/usr/local/mysql/bin/mysqld: File './mysql-bin.index' not found (Errcode: 13)有时出现这个错误是文件权限不正确导致,确定了mysql-bin.index文件的权限是没有问题的,属主和属组都是mysql
关闭mysqlbinlog的功能,在/etc/my.cnf中加入log-bin=0,数据库还是无法启动。
最后查看mysql-bin.index文件里的内容,描述的binlog文件都被删除了,问题就在这里。
只有重新初始化数据库,第一次没有指定datadir重启后出错:
[ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
再次初始化数据库,记得在/etc/my.cnf里把log-bin=0删除或者注释,因为需要开启binlog功能,连同binlog相关参数一起初始化,否则会报错。
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql --datadir=/data/mysql/core再次重启终于,数据终于起来了,执行应用程序入库也正常了,将之前的测试数据清除,,以免插入重复数据,最后执行应用程序入库。
以上为对一次案例的排错主要过程,期间还有一些小的经过,最终把问题解决。
总结:binlog是可以关闭的,但是很少有人这么做,可以通过showvariableslike'expire_logs_days'查看binlog的过期时间;setglobalexpire_logs_days=10设置binlog的过期时间,但是一般都是会对binlog定期删除,比如7天以上的打tar包,一个月以上的删除tar包等,当然重要的需要单独保留。
本文出自 “老徐的私房菜” 博客,谢绝转载!

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

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

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

How to create tables using SQL statements in SQL Server: Open SQL Server Management Studio and connect to the database server. Select the database to create the table. Enter the CREATE TABLE statement to specify the table name, column name, data type, and constraints. Click the Execute button to create the table.

Methods to judge SQL injection include: detecting suspicious input, viewing original SQL statements, using detection tools, viewing database logs, and performing penetration testing. After the injection is detected, take measures to patch vulnerabilities, verify patches, monitor regularly, and improve developer awareness.

MySQL uses shared locks and exclusive locks to manage concurrency, providing three lock types: table locks, row locks and page locks. Row locks can improve concurrency, and use the FOR UPDATE statement to add exclusive locks to rows. Pessimistic locks assume conflicts, and optimistic locks judge the data through the version number. Common lock table problems manifest as slow querying, use the SHOW PROCESSLIST command to view the queries held by the lock. Optimization measures include selecting appropriate indexes, reducing transaction scope, batch operations, and optimizing SQL statements.

The methods to check SQL statements are: Syntax checking: Use the SQL editor or IDE. Logical check: Verify table name, column name, condition, and data type. Performance Check: Use EXPLAIN or ANALYZE to check indexes and optimize queries. Other checks: Check variables, permissions, and test queries.

This article introduces a detailed tutorial on joining three tables using SQL statements to guide readers step by step how to effectively correlate data in different tables. With examples and detailed syntax explanations, this article will help you master the joining techniques of tables in SQL, so that you can efficiently retrieve associated information from the database.

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.
