我刚刚在Mac OS x上安装了MySQL,下一步是设置root用户密码,所以我接下来这样做:

启动终端应用程序以访问Unix命令行。 在Unix提示符下,我执行了以下命令: cd /usr/local/mysql/bin ./mysqladmin -u root password' password'

但是,当我执行命令时

./mysql -u root,这是答案:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 224
Server version: 5.5.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, 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>

我不需要密码就能进入mysql命令行!

为什么会这样?


当前回答

如果你不记得密码,@radtek的答案对我来说是有效的,除了在我的情况下,我已经使用brew设置了MySQL,这意味着他的答案的第1步和第2步必须更改为:

/usr/local/bin/mysql.服务器停止 /usr/local/bin/mysqld_safe——skip-grant-tables

注意:缺少sudo。

其他回答

mysql网站上提供的说明非常清楚,比上面提到的还要清楚

$ sudo /usr/local/mysql/support-files/mysql服务器停止 $ sudo /usr/local/mysql/support-files/mysql服务器启动——skip-grant-tables /usr/local/mysql/bin/mysql mysql> FLUSH PRIVILEGES; mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass'; mysql>退出或按Ctrl + z $ sudo /usr/local/mysql/support-files/mysql服务器停止 $ sudo /usr/local/mysql/support-files/mysql服务器启动 /usr/local/mysql/support-files/mysql -u root -p 输入新密码,即MyNewPass

参考:http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

MySQL 8有了很大的变化。我发现MySQL 8.0“如何重置根密码”文档的以下修改适用于Mac OS X。

创建一个临时文件$HOME/mysql.root.txt,用SQL更新根密码:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '<new-password>';

这使用mysql_native_password来避免身份验证插件'caching_sha2_password'无法加载错误,如果我省略该选项,我就会得到这个错误。

停止服务器,使用——init-file选项来设置根密码,然后重新启动服务器:

mysql.server stop
mysql.server start --init-file=$HOME/mysql.root.txt
mysql.server stop
mysql.server start
mysqld_safe --skip-grant-tables
mysql -u root
UPDATE mysql.user SET authentication_string='yourpasswd' WHERE User='root';
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

每次我的MacBook重启时,我都需要这样做。

macOS v10.14 (Mojave)及更高版本,从Mac OS X DMG安装程序安装5.7.26。

当尝试使用其他用户发布的UPDATE命令时,会导致以下错误:

错误1820 (HY000):在执行此语句之前,您必须使用ALTER USER语句重置密码。

复制安装程序提供给您的密码,打开终端,然后执行以下操作:

mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURPASSWORDHERE';

我用以下方法解决了这个问题:

关闭MySQL服务器:MySQL。服务器停止 以安全模式运行MySQL: mysqld_safe——skip-grant-tables 在另一个终端,使用mysql -u root登录 在同一终端上运行UPDATE mysql。SET authentication_string=null WHERE user ='root';然后用exit退出; 使用mysql停止安全模式服务器。服务器停止后再正常启动;mysql。服务器启动

现在您可以使用

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';