Change root user's password in Mysql Linux
If you can use sudo mysql -u root -p
to log into your mysql service, but when you use mysql -u root -p
, it shows an error like ERROR 1698 (28000): Access denied for user 'root'@'localhost'
. This means you haven't setup the password for user root in mysql (not the machine user), which you can use the following commands to check:
sudo mysql -u root -p
enter your password for your sudo user- In mysql connection, enter command
use mysql
select * from user;
If you see the password column of root user is invalid, you can use the following command to set password for the root user.
└─$ sudo mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.11.6-MariaDB-2 Debian n/a
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> 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
MariaDB [mysql]> set password for root@localhost=password('<The password you want to use for the root user>');
Query OK, 0 rows affected (0.002 sec)