I\'m setting up a new server and keep running into this problem.
When I try to login to the MySQL database with the root user, I get the error:
There is a good and regularly updated guide on how to set a new password for the latest MySQL.
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04
It would be best to read whole topic from the link above but in short, this maybe could help,
Run the security script:
sudo mysql_secure_installation
Detailed Info for "mysql_secure_installation"
After that, you can change password by following the next steps
sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';
Detailed Info for changin root user password
If you have a problem maybe you will need to reinstall MySql.
Check here:
NEW Version of MYSQL does it this way.
In the new my-sql if the password is left empty while installing then it is based on the auth_socket
plugin.
The correct way is to login to my-sql with sudo
privilege.
$ sudo mysql -u root -p
And then updating the password using:
$ ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new-password';
Once this is done stop and start
the mysql server.
$ sudo service mysql stop
$ sudo service mysql start
For complete details you can refer to this link.
Do comment for any doubt.
No need of sudo
The database is initialised with 2 all-privilege accounts: the first one is "root" which is inaccessible and the second one with your user name (check with command whoami
).
To enable access to root account, you need to login with your user name
mysql -u $(whoami)
and manually change password for root
use mysql;
set password for 'root'@'localhost' = password('YOUR_ROOT_PASSWORD_HERE');
flush privileges;
quit
Login as 'root'
mysql -u root -p
os:Ubuntu18.04
mysql:5.7
add the skip-grant-tables
to the file end of mysqld.cnf
cp the my.cnf
sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/my.cnf
(base) ➜ ~ sudo service mysql stop
(base) ➜ ~ sudo service mysql start
(base) ➜ ~ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
Copyright (c) 2000, 2019, 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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed, 3 warnings
mysql> update mysql.user set authentication_string=password('newpass') where user='root' and Host ='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> update user set plugin="mysql_native_password";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 4 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
skip-grant-tables
from my.cnf(base) ➜ ~ sudo emacs /etc/mysql/mysql.conf.d/mysqld.cnf
(base) ➜ ~ sudo emacs /etc/mysql/my.cnf
(base) ➜ ~ sudo service mysql restart
(base) ➜ ~ mysql -uroot -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
Copyright (c) 2000, 2019, 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> select @@validate_password_policy;
+----------------------------+
| @@validate_password_policy |
+----------------------------+
| MEDIUM |
+----------------------------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
6 rows in set (0.08 sec)!
validate_password
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.05 sec)
mysql> set global validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_number_count=3;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_special_char_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=3;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_dictionary_file | |
| validate_password_length | 3 |
| validate_password_mixed_case_count | 0 |
| validate_password_number_count | 3 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 0 |
+--------------------------------------+-------+
6 rows in set (0.00 sec)
note you should know that you error caused by what? validate_password_policy?
you should decided to reset the your password to fill the policy or change the policy.
in my case,
dev@Dev-007:~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
I am sure my password was correct otherwise error code would be ERROR 1045 (28000): Access denied for user
so i relogin using sudo,
dev@Dev-007:~$ sudo mysql -u root -p
this time it worked for me . see the docs
and then change root password,
mysql> alter user 'root'@'%' identified with mysql_native_password by 'me123';
Query OK, 0 rows affected (0.14 sec)
mysql>
then restart server using sudo /etc/init.d/mysql restart