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

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.

其他回答

您可以手动关闭Mac上的MySQL,通过点击Apple菜单并打开系统首选项。选择“MySQL”首选项面板,然后点击“停止MySQL服务器”按钮停止Mac上的MySQL服务器。

停止MySQL后,需要执行以下步骤。

你需要在skip-grant-tables模式下启动MySQL sudo /usr/local/mysql/support-files / mysql。服务器启动——skip-grant-tables 在您的终端中,输入此命令以刷新现有权限 /usr/local/mysql/bin/mysql 现在需要修改用户密码 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword' mysql >退出

然后,您可以转到Apple菜单并打开系统首选项。选择“MySQL”首选项面板,然后点击“停止MySQL服务器”按钮来停止Mac上的MySQL服务器。

最后,您可以再次进入Apple菜单并打开系统首选项。选择“MySQL”首选项面板,然后单击“启动MySQL服务器”按钮启动Mac上的MySQL服务器。

如果你忘记了你的密码或者想把它改成你的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.

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

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

如果您不记得设置的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()函数不适合您。