避免“用户‘root’@‘localhost’访问被拒绝(使用密码:NO)”

Mic*_*ant 2 mysql installation

如何安装 mysql 并使用它?

我试过了:

$ brew install mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.9.yosemite.bottle.tar.gz
Already downloaded: /Library/Caches/Homebrew/mysql-5.7.9.yosemite.bottle.tar.gz
==> Pouring mysql-5.7.9.yosemite.bottle.tar.gz
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To connect:
    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)
$ mysql.server start
Starting MySQL
 SUCCESS! 
$ mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
$ 
Run Code Online (Sandbox Code Playgroud)

sta*_*her 5

对我来说,我的电脑上已经有 mysql 了,所以在那里设置了密码之类的。在花了几个小时尝试了所有解决方案之后,这对我来说是有效的:

$ brew services stop mysql
$ pkill mysqld
$ rm -rf /usr/local/var/mysql/ # NOTE: this will delete your existing database!!!
$ brew postinstall mysql
$ brew services restart mysql
$ mysql -uroot
Run Code Online (Sandbox Code Playgroud)

所有功劳都归功于@Ghrua