Rest MySQL root forgotten password in Ubuntu 16.04

Error: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
Solutions:

Stop mysql server
sudo service mysql stop

Kill all mysql running process
sudo ps -aux | grep mysql
sudo kill -9 type_processid

Make sure no mysql process are running
sudo ps -aux | grep mysql

Run the command as below to create a new mysqld directory
sudo mkdir /var/run/mysqld

Give mysql user access to it.
sudo chown mysql /var/run/mysqld/

After that, run the commands as below to start MySQL in safe mode
sudo mysqld_safe –skip-grant-tables &

You should see something like this… you may have to press the Enter key

[2] 23177
root@itsupport-virtual-machine:~# 2018-08-26T09:25:18.648593Z mysqld_safe Logging to syslog.
2018-08-26T09:25:18.655356Z mysqld_safe Logging to ‘/var/log/mysql/error.log’.
2018-08-26T09:25:18.689289Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

Next, run the commands below to logon to the database server with the root account without typing a password
sudo mysql -u root

mysql> use mysql;
mysql> update user set authentication_string=PASSWORD(“New_Passwore_Here“) where User=’root’;
mysql> flush privileges;
mysql> exit;

Finally, stop MySQL safe_mode and start MySQL default service
sudo service mysql stop
sudo service mysql start

Now finally login to database with changed passwrod
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2018, 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*