我刚刚在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 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

其他回答

如果你忘记了你的密码或者想把它改成你的MySQL:

Start your terminal and enter: sudo su Enter the password for you system Stop your MySQL server: sudo /usr/local/mysql/support-files/mysql.server stop Leave this window open, run second terminal window and enter here: mysql -u root And change your password for MySQL: UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root'; where "new_password" - your new password. You don't need old password for MySQL. Flush, quit and check your new password: FLUSH PRIVILEGES; Close all windows and check your new password for MySQL.

我认为这是可行的:

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

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

这就是对我有效的方法:

Make sure no other MySQL process is running. To check this do the following: From the terminal, run this command: lsof -i:3306 If any PID is returned, kill it using kill -9 PID Go to System Preferences → MySQL → check if any MySQL instances are running, stop them. Start MySQL with the command: sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables The password for every user is stored in the mysql.user table under columns User and authentication_string respectively. We can update the table as: UPDATE mysql.user SET authentication_string='your_password' where User='root'

停止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。

这个解决方法在我的笔记本电脑上工作!

Mac与macOS v10.14.5 (Mojave)。

MySQL 8.0.17是用Homebrew安装的。

执行如下命令找到MySQL的路径 编译mysql信息 一旦路径已知,我运行这个: /usr/local/Cellar/mysql/8.0.17/bin/mysqld_safe——skip-grant-table 在另一个终端,我运行: Mysql -u root 在该终端中,我使用以下方法更改了root密码: 更新mysql。user='root'; 说完我就跑: 冲洗特权;

voilà,密码被重置。

参考文献