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. I also disabled login into MySQL through anonymous users.
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.
Also, I was able to login without the password using just $ sudo mysql
.
After this I tried to set the password using ALTER USER 'root'@'localhost' IDENTIFIED BY 'PASSWORD';
followed by FLUSH PRIVILEGES;
in MySQL but got the same results as before.
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';
but was met with a syntax error : –
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right s
yntax to use near '('NEW_USER_PASSWORD') WHERE User='root' AND Host='localhost'' at line 1
Edit: The mysql.user table before I changed the authentication plugin of root user from auth_socket
to caching_sha2_password
.
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| localhost | debian-sys-maint | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | auth_socket |
+-----------+------------------+-----------------------+
How can I solve these problems and set my password for the root user?
2
Answers
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. Dont use sudo if you want to test a non-root user login.
In MySQL 8.0,
caching_sha2_password
is the default authentication plugin rather thanmysql_native_password
You can change root password like bellow: