简体   繁体   English

密码登录在 MySQL 中不起作用

[英]Password login not functioning in MySQL

I installed MySQL server 8.0 in my ubuntu 20.04 EC2 instance and also did the secure installation using $ sudo mysql_secure_installation where I set the password for the root user.我在我的 ubuntu 20.04 EC2 实例中安装了 MySQL 服务器 8.0,并且还使用$ sudo mysql_secure_installation进行了安全安装,我在其中为 root 用户设置了密码。 I also disabled login into MySQL through anonymous users.我还禁止通过匿名用户登录 MySQL。

I used $ sudo mysql -u root -p to login to MySQL and used the wrong password to confirm whether or not the correct password was enabled and was surprisingly able to login.我使用$ sudo mysql -u root -p登录到 MySQL 并使用错误的密码来确认是否启用了正确的密码并且令人惊讶地能够登录。

Also, I was able to login without the password using just $ sudo mysql .此外,我只需使用$ sudo mysql就可以在没有密码的情况下登录。

After this I tried to set the password using ALTER USER 'root'@'localhost' IDENTIFIED BY 'PASSWORD';在此之后,我尝试使用ALTER USER 'root'@'localhost' IDENTIFIED BY 'PASSWORD'; followed by FLUSH PRIVILEGES;其次是FLUSH PRIVILEGES; in MySQL but got the same results as before.在 MySQL 但得到与以前相同的结果。

I also tried modifying the user table directly using UPDATE mysql.user SET authentication_string=PASSWORD('NEW_USER_PASSWORD') WHERE User='root' AND Host='localhost';我还尝试使用UPDATE mysql.user SET authentication_string=PASSWORD('NEW_USER_PASSWORD') WHERE User='root' AND Host='localhost';直接修改用户表but was met with a syntax error.但遇到语法错误。

How can I solve these problems and set my password for the root user?如何解决这些问题并为 root 用户设置密码?

In MySQL 8.0, caching_sha2_password is the default authentication plugin rather than mysql_native_password在 MySQL 8.0 中, caching_sha2_password是默认的身份验证插件,而不是mysql_native_password

You can change root password like bellow:您可以更改 root 密码,如下所示:

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

-- If first fail because you have changed default authentication plugin you can use mysql_native_password 

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_pass';

FLUSH PRIVILEGES;

Mysql will permit the (operating system) root user to login without a password based on the principle that if you're root, you have access to the data anyway. Mysql 将允许(操作系统)root 用户无需密码即可登录,其原则是如果您是 root,则无论如何您都可以访问数据。 Dont use sudo if you want to test a non-root user login.如果要测试非 root 用户登录,请不要使用 sudo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM