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

其他回答

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.]

在终端试试这个:

/usr/local/bin/mysql_secure_installation

这就是对我有效的方法:

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服务器:MySQL。服务器停止 以安全模式运行MySQL: mysqld_safe——skip-grant-tables 在另一个终端,使用mysql -u root登录 在同一终端上运行UPDATE mysql。SET authentication_string=null WHERE user ='root';然后用exit退出; 使用mysql停止安全模式服务器。服务器停止后再正常启动;mysql。服务器启动

现在您可以使用

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

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