I recently tried installing MySQL with homebrew (brew install mysql
) and when I try to run it I get the following error:
ERROR 2002 (HY00
If "mysqld" IS running, it's possible your data is corrupted. Try running this:
mysqld
Read through the wall of data, and check if mysqld is reporting that the database is corrupted. Corruption can present in many unintuitive ways:
mysql -uroot
returns "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)".mysql.server start
returns "ERROR! The server quit without updating PID".To recover your data, open my.cnf and add the following line in the [mysqld]
section:
innodb_force_recovery=1
Restart mysqld:
$ brew services restart mysql@5.6
Now you can connect to it, but it’s in limited read-only mode.
If you're using InnoDB, run this to export all your data:
$ mysqldump -u root -p --all-databases --add-drop-database --add-drop-table > data-recovery.sql
The file is created in your ~ dir. It may take some time.
Once finished, remove innodb_force_recovery=1
from my.cnf, then restart mysql in normal mode:
$ brew services restart mysql@5.6
Drop all the databases. I did this using Sequel Pro. This deletes all your original data. Make sure your data-recovery.sql looks good before doing this. Also consider backing up /usr/local/var/mysql
to be extra careful.
Then restore the databases, tables, and data with this:
$ mysql -uroot < ~/data-recovery.sql
This can be a long import/restoration process. Once complete, you’re good to go!
Thanks go to https://severalnines.com/database-blog/my-mysql-database-corrupted-what-do-i-do-now for the recovery instructions. The link has further instructions on MyISAM recovery.