Mysql is no doubt one of the best relational database management system available today. This oracle developed RDBMS is used by millions of users worldwide for their small and large scale application purposes.
So chances are there most of the time you’ll find these types of errors while trying to access mysql from a terminal. I’m a quite a fan of managing applications using terminal. Today I got this error while I was trying to connect mysql with terminal.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
There are plenty of guides available online to solve any of these types of errors. But I found most of them aremisleading or not solving the problem. Today I’ll guide you to solve this issue with some simple commands. So keep reading.
Step1: Stop mysql
sudo /etc/init.d/mysql stop
Step2: Start mysql safe mode
sudo mysqld_safe --skip-grant-tables &
Step3: Log into mysql as root user
mysql -uroot
Step4: Select mysql database
use mysql;
Step5: Reset password
Mysql version < 5.7
update user set password=PASSWORD("mynewpassword") where User='root';
Mysql version > 5.7
update user set authentication_string=password('mynewpassword') where user='root';
Step6: Flush privileges
flush privileges
Restart server
quit;
Step7: Stop and server mysql-server again
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysqld start
Step8: Login with new password
mysql -u root -p
Now you can successfully able to connect mysql database from terminal. I have found many alternative method to solve this issue. But this way of solving the issue will fix the bug for a long time.
Thanks for reading the article. Hope I helped you.