[Solved-4 Solutions] ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)



Error Description:

    • When we try to change password through terminal, we get:
    • ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

    Solution 1:

      • You can solve this problem 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
      
      click below button to copy the code. By - mysql tutorial - team
      • 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.

      Solution 2:

        • Try this:
        mysql -h 127.0.0.1 -P 3306 -u root -p <database>
        
        click below button to copy the code. By - mysql tutorial - team
        • Also (to see if it's running):
        telnet 127.0.0.1 3306 
        
        click below button to copy the code. By - mysql tutorial - team

        Solution 3:

          To reset the password

          Follow these steps:

          • Stop mysql
          sudo /etc/init.d/mysql stop
          
          click below button to copy the code. By - mysql tutorial - team

          Or for other distribution versions:

          sudo /etc/init.d/mysqld stop
          
          click below button to copy the code. By - mysql tutorial - team
          • Start MySQL in safe mode
          sudo mysqld_safe --skip-grant-tables &
          
          click below button to copy the code. By - mysql tutorial - team
          • Log into MySQL using root
          mysql -uroot
          
          click below button to copy the code. By - mysql tutorial - team
          • Select the MySQL database to use
          use mysql;
          
          click below button to copy the code. By - mysql tutorial - team
          • Reset the password
          update user set password=PASSWORD("mynewpassword") where User='root';
          
          click below button to copy the code. By - mysql tutorial - team
          • Flush the privileges
          flush privileges;
          
          click below button to copy the code. By - mysql tutorial - team
          • Restart the server
          quit
          
          click below button to copy the code. By - mysql tutorial - team
          • Stop and start the server again

          Ubuntu and Debian:

          sudo /etc/init.d/mysql stop
          ...
          sudo /etc/init.d/mysql start
          
          click below button to copy the code. By - mysql tutorial - team

          On CentOS, Fedora, and RHEL:

          sudo /etc/init.d/mysqld stop
          ...
          sudo /etc/init.d/mysqld start
          
          click below button to copy the code. By - mysql tutorial - team
          • Login with a new password
          mysql -u root -p
          
          click below button to copy the code. By - mysql tutorial - team
          • Type the new password and enjoy your server again like nothing happened

          Solution 4:

            • Log in as super user or use sudo
            • Open /etc/mysql/my.cnf using gedit
            • Find bind-address, and change its value to the database server host machine's IP address. For me, it was localhost or 127.0.0.1
            • Save and close the file.
            • Come back to terminal and execute sudo service mysql start

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