避免'访问被拒绝用户'root'@'localhost'(使用密码:NO)'

时间:2015-11-13 00:50:39

标签: mysql install

如何安装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)
$ 

5 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,这就是我如何解决它的问题(但是如果你的数据库中没有数据,只能尝试这个问题):

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

initialize方法将创建数据目录,也是具有临时密码的root用户,请确保复制此密码,然后登录并更改密码。

答案 1 :(得分:0)

尝试使用CLI:

mysql -uroot -p

您需要指定此用户有密码。

答案 2 :(得分:0)

使用mysqld --initialize为我工作。我只需要从下面的输出中剪切粘贴密码。

[Note] A temporary password is generated for root@localhost: ?(A+3F48ed.Y

答案 3 :(得分:0)

对我来说,我的计算机上某处已经有了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

归功于@Ghrua

答案 4 :(得分:0)

尝试使用此命令:

sudo mysql -u root -p

然后输入密码:*******