我刚刚在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命令行!
为什么会这样?
macOS v10.14 (Mojave)及更高版本,从Mac OS X DMG安装程序安装5.7.26。
当尝试使用其他用户发布的UPDATE命令时,会导致以下错误:
错误1820 (HY000):在执行此语句之前,您必须使用ALTER USER语句重置密码。
复制安装程序提供给您的密码,打开终端,然后执行以下操作:
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURPASSWORDHERE';
当您登录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';`