我刚刚在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,你需要建立“根”密码。如果您没有设置根密码,那么就没有根密码,您也不需要密码来登录。

因此,话虽如此,您需要建立一个根密码。

使用终端输入如下:

安装:设置root用户密码:

/usr/local/mysql/bin/mysqladmin -u root password NEW_PASSWORD_HERE

如果你犯了一个错误,或者需要修改root密码,请使用以下方法:

修改root密码:

cd /usr/local/mysql/bin/
./mysql -u root -p
> Enter password: [type old password invisibly]

use mysql;
update user set password=PASSWORD("NEW_PASSWORD_HERE") where User='root';
flush privileges;
quit

其他回答

当我安装OS X v10.10 (Yosemite)时,MySQL出现了一个问题。我试了很多方法,但都没用。其实我找到了一个很简单的方法。试试这个。

首先以超级用户(su)权限登录到终端。 sudo苏 停止MySQL sudo /usr/local/mysql/support-files / mysql。服务器停止 安全模式启动: Sudo mysqld_safe——skip-grant-tables 打开另一台终端,以su权限登录,然后无需密码登录MySQL客户端(MySQL) Mysql -u root 修改密码 更新mysql。SET Password ('new_password') WHERE user ='root'; 冲洗的特权 冲洗特权; 现在你完成了。

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

如果您不记得设置的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的根密码,不记得或者想要闯入.....你可以在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

这就是对我有效的方法:

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'