我刚刚在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命令行!

为什么会这样?


当前回答

停止MySQL服务器

sudo /usr/local/mysql/support-files / mysql。服务器停止

以安全模式启动MySQL

Sudo /usr/local/mysql/bin/mysqld_safe——skip-grant-tables &

修改root用户密码

/usr/local/mysql/bin/mysql -u root

use mysql;
UPDATE user SET authentication_string=PASSWORD('NEW_PASSWORD') WHERE user='root';
FLUSH PRIVILEGES;
exit

测试

执行/usr/local/mysql/bin/mysql -u root命令

现在输入新密码开始使用MySQL。

其他回答

我认为这是可行的:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURNEWPASSWORD'

(注意,如果你的用户名不是root,你可能应该用你的用户名替换root。)

如果您不记得设置的root密码,需要重置密码,请执行以下步骤:

Stop the mysqld server, this varies per install Run the server in safe mode with privilege bypass sudo mysqld_safe --skip-grant-tables; In a new window connect to the database, set a new password and flush the permissions & quit: mysql -u root For MySQL older than MySQL 5.7 use: UPDATE mysql.user SET Password=PASSWORD('your-password') WHERE User='root'; For MySQL 5.7+ use: USE mysql; UPDATE mysql.user SET authentication_string=PASSWORD("your-password") WHERE User='root'; Refresh and quit: FLUSH PRIVILEGES; \q Stop the safe mode server and start your regular server back. The new password should work now. It worked like a charm for me :)


Note

运行 更新mysql。SET authentication_string=null WHERE user ='root'; 如果您不想为root用户设置密码。或者PASSWORD()函数不适合您。

之前的评论都没有解决我的Mac上的问题。

我使用下面的命令,它工作。

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 -u root

要引用MySQL 8.0.15 +, password()函数不可用。使用下面的命令。

请使用

UPDATE mysql.user SET authentication_string='password' WHERE User='root';

现有答案中提到的方法不适用于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