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
In my case, the culprit was found in the logfiles:
$ tail /usr/local/var/mysql/<hostname>.lan.err
2019-09-19 7:32:21 0 [ERROR] InnoDB: redo log file './ib_logfile0' exists. Creating system tablespace with existing redo log files is not recommended. Please delete all redo log files before creating new system tablespace.
2019-09-19 7:32:21 0 [ERROR] InnoDB: Database creation was aborted with error Generic error. You may need to delete the ibdata1 file before trying to start up again.
So I renamed ib_logfile0
to get rid of the error (I had to do the same with ib_logfile1
afterwards).
mv /usr/local/var/mysql/ib_logfile0 /usr/local/var/mysql/ib_logfile0_bak
mv /usr/local/var/mysql/ib_logfile1 /usr/local/var/mysql/ib_logfile1_bak
brew services restart mariadb
For me, I had installed mariadb
long time ago, then installed mysql@5.7
.
When I executed mysql -uroot
, I get the error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Reading the answers:
mariadb
/usr/local/var/mysql
mysqld --initialize
Then I was able to mysql -uroot -p
I manually started mysql in the system preferences pane by initialising the database and then starting it. This solved my problem.
Looks like your mysql server is not started. I usually run the stop command and then start it again:
mysqld stop
mysql.server start
Same error, and this works for me.
After a restart I could not connect with the local mariadb, a search also brought me to this page and I wanted to share my solution with you.
I noticed that the directory my.cnf.d in /usr/local/etc/ is missing.
This is a known bug with homebrew that is described and solved there. https://github.com/Homebrew/homebrew-core/issues/36801
fast way to fix: mkdir /usr/local/etc/my.cnf.d
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.