brew安装的mysql忘记密码

cp /usr/local/Cellar/mysql/5.7.17/homebrew.mxcl.mysql.plist /usr/local/Cellar/mysql/5.7.17/homebrew.mxcl.mysql.plist.bak
vi /usr/local/Cellar/mysql/5.7.17/homebrew.mxcl.mysql.plist

添加下面这个参数

<string>--defaults-file=/etc/my.cnf</string>
echo 'skip-grant-tables' >> /etc/my.cnf

结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>homebrew.mxcl.mysql</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/opt/mysql/bin/mysqld_safe</string>
    <string>--defaults-file=/etc/my.cnf</string>
    <string>--bind-address=127.0.0.1</string>
    <string>--datadir=/usr/local/var/mysql</string>
    <string>--general_log=1</string>
    <string>--general_log_file=/usr/local/var/mysql/query.log</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>WorkingDirectory</key>
  <string>/usr/local/var/mysql</string>
</dict>
</plist>

重启:

brew services restart mysql

无密码进入:

mysql -u -h localhost -p

不输入任何密码,直接回车。

执行如下命令,重新给root赋值权限:

grant all on *.* to root@'localhost' identified by '';

可能会有如下错误:

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

解决办法:

flush privileges;

再次执行:

grant all on *.* to root@'localhost' identified by '';
flush privileges;

最后,去除 /etc/my.cnf里的skip-grant-tables后,重启:

brew services restart mysql

OK.

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注