Some issues about failing to connect mysql

 · 1 min read

Today when I tried to use mysql, I encountered this problem first:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

I can’t find the mysql.sock file in my mac, it seems starting mysql service would create one, so input mysql.server start

but I got this output:

Starting MySQL
... ERROR! The server quit without updating PID file (/usr/local/var/mysql/xxxx.pid).

I attempted to update or reinstall mysql via Homebrew, but it didn’t work either.

And I tried many methods from others’ experience, yet still not working well.

Practice proves that the best way is to check the log yourself, analyzing your own specific problem.

cd /usr/local/var/mysql/
view xxxx.err
2018-04-08T08:54:06.495097Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
2018-04-08T08:54:06.495370Z 0 [ERROR] Aborting

Oh, maybe it’s need to initialize with mysqld --initialize, but it shows I already have this directory, so I have to delete it(better to make backup).

2018-04-08T09:56:51.978123Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2018-04-08T09:56:51.978186Z 0 [ERROR] Aborting

Then initializing sucess, please remeber the password

To have launchd start mysql now and restart at login:

brew services start mysql

Or, if you don’t want/need a background service you can just run:

mysql.server start

Finally, after starting the mysql service, I was able to connect as root, thank god…

mysql -uroot -pyourpassword