我刚刚在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 5.7.6或更高版本。根据MySQL文档,这是推荐的方法。

B.5.3.2.3重置Root用户密码:通用说明

MySQL 5.7.6及以上版本:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

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

当我安装OS X v10.10 (Yosemite)时,MySQL出现了一个问题。我试了很多方法,但都没用。其实我找到了一个很简单的方法。试试这个。

首先以超级用户(su)权限登录到终端。 sudo苏 停止MySQL sudo /usr/local/mysql/support-files / mysql。服务器停止 安全模式启动: Sudo mysqld_safe——skip-grant-tables 打开另一台终端,以su权限登录,然后无需密码登录MySQL客户端(MySQL) Mysql -u root 修改密码 更新mysql。SET Password ('new_password') WHERE user ='root'; 冲洗的特权 冲洗特权; 现在你完成了。

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

关闭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';

在终端中,写入mysql -u root -p并点击Return。

输入您必须记下的当前MySQL密码。

并设置密码:

SET PASSWORD = PASSWORD('new_password');

更多细节请参考这里的文档。

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

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

注意:缺少sudo。