Troubleshooting Homebrew MySQL on OS X

I get MySQL upgrade to 5.7.9 from Homebrew on OS X El Capitan (10.11.1), and got ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when start the service.

$ brew upgrade mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.9.el_capitan.bott
######################################################################## 100.0%
==> Pouring mysql-5.7.9.el_capitan.bottle.1.tar.gz
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

To connect run:
    mysql -uroot

To have launchd start mysql at login:
  ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/5.7.9: 12629 files, 464M
$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

I solved it by initialize method, and lost all database.

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
$ rm -rf /usr/local/var/mysql
$ mysqld --initialize

The initialize method will create the data dir, and also an root user with a temporary password, be sure you copy this password, and than login and change the password. The password from the below output.

[Note] A temporary password is generated for [email protected]: A:>s6r<6IeLh

Restart service via Homebrew and reset root user password.

$ brew services start mysql
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
MacBook Pro:Caches xuri$ mysql -uroot -p
Enter password: ****  <- enter root password here
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.9

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
Troubleshooting Homebrew MySQL on OS X
6 votes, 4.83 avg. rating (96% score)