Pages

Wednesday, November 20, 2013

Recovering from InnoDB Corruption in MySQL

InnoDB is a robust storage engine for MySQL, but like any system, it can encounter issues such as corruption. Understanding how to address these problems is crucial for database administrators. This guide will outline steps to recover from an InnoDB corruption using the innodb_force_recovery option.

Understanding the Error

The error log indicates an assertion failure within InnoDB, suggesting data corruption. Typical causes include hardware faults, sudden shutdowns, or bugs. The error message suggests steps for recovery and forces InnoDB into a special "recovery" mode.


130306 22:02:18 mysqld_safe Number of processes running now: 0
130306 22:02:18 mysqld_safe mysqld restarted
130306 22:02:18 [Note] Plugin 'FEDERATED' is disabled.
130306 22:02:18 InnoDB: The InnoDB memory heap is disabled
130306 22:02:18 InnoDB: Mutexes and rw_locks use GCC atomic builtins
130306 22:02:18 InnoDB: Compressed tables use zlib 1.2.3
130306 22:02:18 InnoDB: Using Linux native AIO
130306 22:02:18 InnoDB: Initializing buffer pool, size = 128.0M
130306 22:02:18 InnoDB: Completed initialization of buffer pool
130306 22:02:18 InnoDB: highest supported file format is Barracuda.
130306 22:02:18 InnoDB: 5.5.30 started; log sequence number 1629186928
130306 22:02:18 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
130306 22:02:18 [Note] - '0.0.0.0' resolves to '0.0.0.0';
130306 22:02:18 [Note] Server socket created on IP: '0.0.0.0'.
130306 22:02:18 [Note] Event Scheduler: Loaded 0 events
130306 22:02:18 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.30-cll' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
130306 22:02:19 InnoDB: Assertion failure in thread 47204348393792 in file trx0purge.c line 840
InnoDB: Failing assertion: purge_sys->purge_trx_no <= purge_sys->rseg->last_trx_no
InnoDB: We intentionally generate a memory trap.
InnoDB: Submit a detailed bug report to http://bugs.mysql.com.
InnoDB: If you get repeated assertion failures or crashes, even
InnoDB: immediately after the mysqld startup, there may be
InnoDB: corruption in the InnoDB tablespace. Please refer to
InnoDB: http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: about forcing recovery.
03:02:19 UTC - mysqld got signal 6 ;

This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.

We will try our best to scrape up some info that will hopefully help diagnose the problem, but since we have already crashed, something is definitely wrong and this may fail.


Steps to Recovery

1. Stop mysqld:

First, ensure that MySQL is not running to prevent further data corruption.

service mysqld stop

2. Backup Critical Files:

Before proceeding with any recovery steps, back up the existing InnoDB data files.

mkdir /root/mysql_backup cp /var/lib/mysql/ib* /root/mysql_backup/

3. Configure MySQL for Recovery:

Edit the MySQL configuration file (/etc/my.cnf) and add the following under the [mysqld] section:

innodb_force_recovery = 4

There are different levels of innodb_force_recovery you can use, from 1 to 6. Levels 1-4 are generally considered safe, while 5 and 6 are used as a last resort.

4. Restart mysqld:

Restart the MySQL server. It will start in recovery mode due to the innodb_force_recovery setting.

service mysqld start

5. Dump All Tables:

Create a dump of all databases. This might take some time depending on the size of your databases.

mysqldump -A > /root/mysql_backup/dump.sql

6. Drop Corrupted Databases:

Identify and drop the corrupted databases. If you're unsure which ones are corrupted, you might need to drop all databases and restore them from the dump later.

mysql -u root -p -e "DROP DATABASE corrupted_db"

7. Stop mysqld:

Once again, stop the MySQL server to proceed with the next steps.

service mysqld stop

8. Remove InnoDB Data Files:

Delete the old InnoDB data files. Be cautious with this command – ensure you have backups!

rm /var/lib/mysql/ib*

9. Comment out innodb_force_recovery:

Edit /etc/my.cnf and comment out or remove the innodb_force_recovery option.

10. Restart mysqld:

Start the MySQL server. It should recreate the InnoDB data files.

service mysqld start

11. Restore Databases:

Restore the databases from the dump you created earlier.Copy code

mysql < /root/mysql_backup/dump.sql

12. Repair and Optimize:

Finally, check and repair all databases and tables.

mysqlcheck –all-databases –repair
innodb force recovery options

Mode 1 - Doesn't crash MySQL when it sees a corrupt page
Mode 2 - Doesn't run background operations
Mode 3 - Doesn't attempt to roll back transactions
Mode 4 - Doesn't calculate stats or apply stored/buffered changes
Mode 5 - Doesn't look at the undo logs during start-up
Mode 6 - Doesn't roll-forward from the redo logs (ib_logfiles) during start-up
1 (SRV_FORCE_IGNORE_CORRUPT)
Let the server run even if it detects a corrupt page. Try to make SELECT * FROM tbl_name jump
over corrupt index records and pages, which helps in dumping tables.
2 (SRV_FORCE_NO_BACKGROUND)
Prevent the main thread from running. If a crash would occur during the purge operation,
this recovery value prevents it.
3 (SRV_FORCE_NO_TRX_UNDO)
Do not run transaction rollbacks after recovery.
4 (SRV_FORCE_NO_IBUF_MERGE)
Prevent insert buffer merge operations. If they would cause a crash, do not do them.
Do not calculate table statistics.
5 (SRV_FORCE_NO_UNDO_LOG_SCAN)
Do not look at undo logs when starting the database: InnoDB treats even incomplete transactions as committed.
6 (SRV_FORCE_NO_LOG_REDO)
Do not do the log roll-forward in connection with recovery.
The database must not otherwise be used with any nonzero value of innodb_force_recovery.
As a safety measure, InnoDB prevents users from performing INSERT, UPDATE, or
DELETE operations when innodb_force_recovery is greater than 0.

**Hint : A simple query for finding all of your InnoDB tables in case you want to specifically target the corruption.
SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE engine = 'innodb';

\

Sunday, November 17, 2013

Setting Up FFMPEG and FFMPEG-PHP on CentOS 5 & 6

Setting Up FFMPEG and FFMPEG-PHP on CentOS 5 & 6

This guide provides two straightforward methods to install FFMPEG and its PHP extension, FFMPEG-PHP, on CentOS 5 and 6 servers. FFMPEG is a powerful tool for handling multimedia files, and FFMPEG-PHP allows your PHP applications to interact with FFMPEG.


METHOD 1: USING THE INSTALLER SCRIPT

This method uses a pre-made script to automate most of the installation process.

DOWNLOAD AND RUN THE SCRIPT

First, download the installer script:

wget http://9xhost.net/scripts/ffmpeg.sh

Then, run the script:

sh ffmpeg.sh

CONFIGURE PHP

After the script finishes, you'll need to enable the FFMPEG-PHP extension in your PHP configuration. The script's output will show you the path where the PHP extension was installed, typically something like /usr/local/lib/php/extensions/no-debug-non-zts-20060613/.

Open your php.ini file for editing:

nano /usr/local/lib/php.ini

Add the following line at the end of the file to enable the extension:

extension="ffmpeg.so"

RESTART APACHE

For the changes to take effect, restart your Apache web server:

/scripts/restartsrv_httpd

VERIFY INSTALLATION

You can confirm that FFMPEG-PHP is working by checking your PHP information.

From the command line:

php -i | grep ffmpeg

This should display details about the FFMPEG and FFMPEG-PHP versions.

INSTALLATION PATHS

FFMPEG itself is typically installed at:

/usr/bin/ffmpeg

EXCLUDE FROM YUM UPDATES

To prevent potential conflicts with future system updates, it's recommended to exclude FFMPEG packages from Yum updates.

Open the yum.conf file:

nano /etc/yum.conf

Add ffmpeg* to the exclude line. If an exclude line doesn't exist, create it.


METHOD 2: MANUAL INSTALLATION VIA YUM AND COMPILATION

This method involves adding a repository, installing FFMPEG via Yum, and then compiling FFMPEG-PHP.

ADD DAG REPOSITORY

Create and open a new repository file:

nano /etc/yum.repos.d/dag.repo

Add the following content to the file:

[dag]
name=DAG RPM Repository
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1

Import the GPG key for the repository:

rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt

INSTALL FFMPEG

Update your Yum packages:

yum update

Now, install FFMPEG and its development libraries:

yum install ffmpeg ffmpeg-devel ffmpeg-libpostproc

PREPARING FFMPEG-PHP

Download the FFMPEG-PHP package:

wget http://downloads.sourceforge.net/ffmpeg-php/ffmpeg-php-0.6.0.tbz2

Untar the downloaded package:

tar xjf ffmpeg-php-0.6.0.tbz2

Navigate into the extracted directory:

cd ffmpeg-php-0.6.0

Apply a necessary patch for compatibility:

sed -i 's/PIX_FMT_RGBA32/PIX_FMT_RGB32/g' ffmpeg_frame.c

Prepare for compilation:

phpize

Configure the build:

./configure

Compile the extension:

make

Install the extension:

make install

The output of make install will show you the PHP extensions path, for example:

Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20060613/

CONFIGURE PHP

Edit your php.ini file:

nano /usr/local/lib/php.ini

Ensure extension_dir is set to the path identified by make install:

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613"

Add the following line directly below extension_dir to enable the FFMPEG-PHP extension:

extension="ffmpeg.so"

RESTART APACHE

Restart your Apache web server for changes to take effect:

/scripts/restartsrv_httpd

VERIFY INSTALLATION

Confirm FFMPEG-PHP is working from the command line:

php -i | grep ffmpeg

This should display FFMPEG and FFMPEG-PHP version information.

INSTALLATION PATHS

FFMPEG itself is typically installed at:

/usr/bin/ffmpeg

EXCLUDE FROM YUM UPDATES

To prevent potential conflicts, exclude FFMPEG packages from Yum updates.

Open the yum.conf file:

nano /etc/yum.conf

Add ffmpeg* to the exclude line.


TROUBLESHOOTING: 'list_entry' UNDECLARED ERROR

If you encounter an error during compilation similar to:

error: 'list_entry' undeclared

This usually indicates a compatibility issue between the FFMPEG-PHP version and your PHP version. The provided sed command (sed -i 's/PIX_FMT_RGBA32/PIX_FMT_RGB32/g' ffmpeg_frame.c) addresses one common issue, but newer PHP versions might require more extensive patches or a newer FFMPEG-PHP version. Consider searching for specific patches for your PHP version (e.g., "ffmpeg-php PHP 5.3 patch") or looking for a more updated FFMPEG-PHP release.