我刚刚在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的根密码,不记得或者想要闯入.....你可以在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

当您登录MySQL终端时,请尝试执行FLUSH PRIVILEGES命令。如果这不起作用,请在MySQL终端中尝试以下命令集

mysql -u root

mysql> USE mysql;
mysql> UPDATE user SET password=PASSWORD("NEWPASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit

用你想要的任何密码更改NEWPASSWORD。应该都准备好了!

更新:从MySQL 5.7开始,密码字段被重命名为authentication_string。修改密码时,使用以下查询来修改密码。所有其他命令保持不变:

mysql> UPDATE user SET authentication_string=PASSWORD("NEWPASSWORD") WHERE User='root';

请不要使用MySQL 8.0+

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

因为它覆盖了authentication_string,它应该是一个哈希而不是纯文本,而是使用:

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

这就是对我有效的方法:

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 5.7版本中,由于某些原因MySQL的二进制命令没有附加到shell中,你必须这样做:

安装完成后重新启动Mac。 MySQL开始: System Preferences→MySQL→Start按钮 进入终端的MySQL install文件夹: cd /usr/local/mysql/bin/ 访问MySQL: ./mysql -u root -p 并输入为安装提供的初始密码。 在MySQL客户端修改密码: mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword'

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重启时,我都需要这样做。