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

为什么会这样?


当前回答

在终端试试这个:

/usr/local/bin/mysql_secure_installation

其他回答

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

如果你忘记了MySQL的根密码,不记得或者想要闯入.....你可以在Linux或OS X的命令行中重置MySQL数据库密码,只要你知道你所在的机器的root用户密码:

(1)停止MySQL

sudo /usr/local/mysql/support-files/mysql.server stop

(2)安全模式启动:

sudo mysqld_safe --skip-grant-tables

(3)这将是一个持续的命令,直到进程结束,所以打开另一个shell/终端窗口,登录没有密码:

mysql -u root

UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';

在上面的UPDATE命令中,只需将'password'替换为您自己的新密码,请确保保留引号

(4)节省和安静

FLUSH PRIVILEGES;

\q

启动MySQL

sudo /usr/local/mysql/support-files/mysql.server start

停止mysqld服务器。

Mac OS X:系统首选项→MySQL→停止MySQL服务器 Linux(从终端):sudo systemctl停止mysqld.service

Start the server in safe mode with privilege bypass From Terminal: sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables In a new terminal window: sudo /usr/local/mysql/bin/mysql -u root This will open the MySQL command-line client. From here enter: UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword') WHERE User='root'; FLUSH PRIVILEGES; quit Stop the mysqld server again and restart it in normal mode. Mac OS X (From Terminal): sudo /usr/local/mysql/support-files/mysql.server restart Linux Terminal: sudo systemctl restart mysqld

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

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à,密码被重置。

参考文献

之前的评论都没有解决我的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