Reset the root password when /var/run/mysqld for the UNIX socket file doesn't exist
suggest changeIf you forget the password, the client returns an error.
$ mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
I tried to solve the issue by first knowing the status:
$ systemctl status mysql.service
mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en Active: active (running) since Thu 2017-06-08 14:31:33 IST; 38s ago
Then I used the code mysqld_safe --skip-grant-tables & but I get the error:
mysqld_safe Directory ‘/var/run/mysqld’ for UNIX socket file doesn't exist.
$ systemctl stop mysql.service
$ ps -eaf|grep mysql
$ mysqld_safe --skip-grant-tables &
I solved:
$ mkdir -p /var/run/mysqld
$ chown mysql:mysql /var/run/mysqld
Now I use the same code mysqld_safe --skip-grant-tables & and get
mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
If I use $ mysql -u root I’ll get :
Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2017, 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>
Now time to change password:
mysql> use mysql
mysql> describe user;
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
mysql> FLUSH PRIVILEGES;
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');
These statements match this 5.7.18 server. PASSWORD() and the mysql.user.Password column are gone in 8.0. After FLUSH PRIVILEGES, use ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpwd';. On 8.0, --skip-grant-tables also enables --skip-networking unless you turn that off. If the account's plugin is auth_socket, a password alone still will not allow password login.
If a root account can connect from any host, change that account too:
SET PASSWORD FOR 'root'@'%' = PASSWORD('newpwd');
On 5.7 the same change can be written against mysql.user. Run FLUSH PRIVILEGES; before you quit.
UPDATE mysql.user SET Password = PASSWORD('newpwd')
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
Then leave the client and start the server normally:
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
Connect again with mysql -u root -p and the new password.
mysql>