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

为什么会这样?


当前回答

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

其他回答

MySQL 8

关闭MySQL服务器

进入系统首选项-> MySQL 单击停止MySQL服务器按钮

打开两个终端[命令行]窗口 在第一个终端窗口中运行以下命令:

mysqld_safe --skip-grant-tables

在第二个终端窗口中执行以下操作:

4.1. 登录MySQL

mysql -u root

4.2. 在MySQL提示符中执行如下命令:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEWPASSWORD';

4.3. 退出MySQL

exit;

回到第一个终端窗口并关闭mysqld_safe

5.1. 按CTRL + Z

5.2. 运行以下命令

mysqladmin -u root -p shutdown

5.3. 输入在4.2中设置的新密码。当提示。

启动MySQL服务器[见1.]

在新的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'

如果你忘记了你的密码或者想把它改成你的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网站上提供的说明非常清楚,比上面提到的还要清楚

$ sudo /usr/local/mysql/support-files/mysql服务器停止 $ sudo /usr/local/mysql/support-files/mysql服务器启动——skip-grant-tables /usr/local/mysql/bin/mysql mysql> FLUSH PRIVILEGES; mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass'; mysql>退出或按Ctrl + z $ sudo /usr/local/mysql/support-files/mysql服务器停止 $ sudo /usr/local/mysql/support-files/mysql服务器启动 /usr/local/mysql/support-files/mysql -u root -p 输入新密码,即MyNewPass

参考:http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

如果你不记得密码,@radtek的答案对我来说是有效的,除了在我的情况下,我已经使用brew设置了MySQL,这意味着他的答案的第1步和第2步必须更改为:

/usr/local/bin/mysql.服务器停止 /usr/local/bin/mysqld_safe——skip-grant-tables

注意:缺少sudo。