Wednesday, 31 October 2018

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


I installed LAMP on Ubuntu 12.04 LTS (Precise Pangolin) and then set root password on phpMyAdmin. I forgot the password and now I am unable to login. When I try to change password through terminal I get:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
How can I fix this? I am unable to open LAMP, uninstall it or reinstall it.

 Answers



I once had this problem and solved it by installing mysql-server, so make sure that you have installed the mysql-server, not the mysql-client or something else.
That error means the file /var/run/mysqld/mysqld.sock doesn't exists, if you didn't install mysql-server, then the file would not exist. But if the mysql-server is already installed and is running, then you need to check the config files.
The config files are:
/etc/my.cnf
/etc/mysql/my.cnf
/var/lib/mysql/my.cnf
In /etc/my.cnf, the socket file config may be /tmp/mysql.sock and in /etc/mysql/my.cnf the socket file config may be /var/run/mysqld/mysqld.sock. So, remove or rename /etc/mysql/my.cnf, let mysql use /etc/my.cnf, then the problem may solved.




I am seeing all these answers, but none offer the option to reset the password and no accepted answer. The actual question being he forgot his password, so he needs to reset, not see if it's running or not (installed or not) as most of these answers imply.

To reset the password

Follow these steps (can be helpful if you really forget your password and you can try it anytime, even if you're not in the situation at the moment):
  1. Stop mysql
    sudo /etc/init.d/mysql stop
    
    Or for other distribution versions:
    sudo /etc/init.d/mysqld stop
    
  2. Start MySQL in safe mode
    sudo mysqld_safe --skip-grant-tables &
    
  3. Log into MySQL using root
    mysql -uroot
    
  4. Select the MySQL database to use
    use mysql;
    
  5. Reset the password
    update user set password=PASSWORD("mynewpassword") where User='root';
    
  6. Flush the privileges
    flush privileges;
    
  7. Restart the server
    quit
    
  8. Stop and start the server again
    Ubuntu and Debian:
    sudo /etc/init.d/mysql stop
    ...
    sudo /etc/init.d/mysql start
    
    On CentOS, Fedora, and RHEL:
    sudo /etc/init.d/mysqld stop
    ...
    sudo /etc/init.d/mysqld start
    
  9. Login with a new password
    mysql -u root -p
    
  10. Type the new password and enjoy your server again like nothing happened


In MySQL 5.7, the password field in mysql.user table field was removed, and now the field name is 'authentication_string', so step 5 should be:
 update user set authentication_string=password('mynewpassword') where user='root';




I fixed this problem by executing the following command:
mysql.server start
And if you are on a mac and used brew to install mysql, simply use:
brew services start mysql




I solved this by killing the mysql process:
ps -ef | grep mysql
kill [the id]
And then I started the server again with:
sudo /etc/init.d/mysql restart
But start works as well:
sudo /etc/init.d/mysql start
Then I logged in as admin, and I was done.




Your mysql-server might not be running. Ensure it runs by typing mysql.server start into the terminal.




In my case it was that the disk was full and mysqld couldn't start anymore.
Try to restart mysql service.
service mysql restart
or
service mysql stop
service mysql start
If it doesn't recognize "stop" command then it's definitely the disk space. You should make some space in the partition mysql is allocated or make the disk larger.
Check the disk space with
df -h




If you're using Amazon EC2, and you're having this problem on the instance, then you only need to do:
sudo yum install mysql-server
sudo service mysqld restart
Amazon EC2 doesn't have a server installed (only the client is installed), so in case of that you need to install that on your instance, and after that try
 mysql -u root -p
to check if that worked.




If you have XAMPP installed on your Linux machine, try to copy your my.cnf file from /opt/lampp/etc/my.cnf to /etc/my.cnf.
Then, run the mysql -u root again... You should now have the correct socket and be able to run the MySQL client.




Instead of using localhost:
mysql -u myuser -pmypassword -h localhost mydatabase
Use 127.0.0.1
mysql -u myuser -pmypassword -h 127.0.0.1 mydatabase
(also note, no space between -p and mypassword)
Enjoy :)




In my case, the default port 3306 was being used by some other process and thus it was not starting. After I stopped the other service and did sudo service mysql start, it worked fine. BTW, you can use something like sudo lsof -Pn -iTCP:3306 to see who may be using the port.




In my case it worked by doing some R&D:
I am able to connect to MySQL using
root-debian#mysql -h 127.0.0.1 -u root -p
But it's not working with mysql -u root -p.
I did not find any bind-address in my.cnf. So I outcommented the parameter socket=/var/lib/mysql/mysqld.sock in my.cnf which was causing me a problem with login.
After restarting the service it went fine:
root@debian:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.19 MySQL Community Server (GPL)




On Debian server Jessie, my working solution was to simply do
service mysql restart
service mysql reload
as root user




By experience I say that you need to check if the server is running first and then try configuring MySQL. The last solution is to re-install MySQL.




Open the terminal and type:
sudo apt-get purge mysql-client-core-5.6

sudo apt-get autoremove

sudo apt-get autoclean

sudo apt-get install mysql-client-core-5.5

sudo apt-get install mysql-server  
Both MySQL database core client and MySQL Server packages will be the same version 5.5. MySQL Client 5.5 and MySQL Server 5.5 are the current "best" versions of these packages in Ubuntu 14.04 as determined by the package maintainers.
If you would rather install MySQL Client 5.6 and MySQL Server 5.6 you can also find the mysql-client-core-5.6 and mysql-server-5.6 packages in the Ubuntu Software Center. The important thing is that the client and server version numbers match in either case.
This worked for me.




Check if you have the correct rights:
sudo chmod 755 /var/lib/mysql/mysql
I had the same problems and this worked for me. After doing this I was able to start MySQL.

0 comments:

Post a Comment