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

June 18, 2019 1.9k views
MySQL

ramanasharma@ramana:~$ mysql –version
mysql Ver 14.14 Distrib 5.7.26, for Linux (x86
64) using EditLine wrapper
ramanasharma@ramana:~$ mysql -uroot -proot0
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ’/var/run/mysqld/mysqld.sock’ (2)
ramana
sharma@ramana:~$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ’/var/run/mysqld/mysqld.sock’ (2)
ramanasharma@ramana:~$ services mysql start
No command ‘services’ found, did you mean:
Command 'service’ from package 'init-system-helpers’ (main)
services: command not found
ramana
sharma@ramana:~$ /etc/init.d/mysql start
bash: /etc/init.d/mysql: No such file or directory
ramanasharma@ramana:~$ #/usr/sbin/mysqld –defaults-file=/etc/mysql/my.cnf –basedir=/usr –datadir=/var/lib/mysql –pid-file=/var/run/mysqld/mysqld.pid –socket=/var/run/mysqld/mysqld.sock
ramana
sharma@ramana:~$ /etc/init.d/mysql start
bash: /etc/init.d/mysql: No such file or directory
ramanasharma@ramana:~$ sudo service mysql restart
[sudo] password for ramana
sharma:
Failed to restart mysql.service: Unit mysql.service not found.
ramanasharma@ramana:~$ sudo nano /etc/mysql/my.cnf
ramana
sharma@ramana:~$ systemctl enable mysql
Failed to execute operation: No such file or directory

2 Answers

Hi there @ramanakarasarma,

It sounds like possibly you only have the mysql client installed and not the server:

This should be able to be solved by installing mysql-server, so make sure that you have installed the mysql-server, not the mysql-client or something else.

The error you are seeing means that the file /var/run/mysqld/mysqld.sock doesn’t appear to exist, if you didn’t install mysql-server, then the file would not exist.

You should be able to quickly verify if the mysql server is running locally by doing the following two things:

First, attempt to login to the server with the localhost IP and specific mysql port:
mysql -h 127.0.0.1 -P 3306 -u root -p <database>

You can also use telnet to see if anything is being served locally on the mysql port at all:
telnet 127.0.0.1 3306

If those don’t work, it is very likely that mysql-server isn’t installed or running. You can install it by using sudo apt-get install mysql-server or similar depending on the linux distro you are using.

However, if the mysql-server is already installed and is running, then you need to check the config files to ensure they are looking for the right sock file:

/etc/my.cnf
/etc/mysql/my.cnf
/var/lib/mysql/my.cnf

In /etc/my.cnf, the socket file may be configured to use /tmp/mysql.sock or similar, but in the /etc/mysql/my.cnf file, the socket file may be set to /var/run/mysqld/mysqld.sock.

So you may also be able to resolve this by removing or renaming /etc/mysql/my.cnf, which will then let mysql use /etc/my.cnf instead.

Hope that helps!
- Matt.

  • thanks for kind reply
    but when i install mysql server
    i got the following error please help this

    ramanasharma@ramana:~$ sudo apt-get install mysql-server
    [sudo] password for ramana
    sharma:
    Reading package lists… Done
    Building dependency tree

    Reading state information… Done
    mysql-server is already the newest version (8.0.16-2ubuntu16.04).
    0 upgraded, 0 newly installed, 0 to remove and 194 not upgraded.
    6 not fully installed or removed.
    Need to get 0 B/1,082 kB of archives.
    After this operation, 0 B of additional disk space will be used.
    Do you want to continue? [Y/n] Y
    Setting up mysql-common (8.0.16-2ubuntu16.04) …
    update-alternatives: error: alternative path /etc/mysql/my.cnf.fallback doesn’t exist
    dpkg: error processing package mysql-common (–configure):
    subprocess installed post-installation script returned error exit status 2
    dpkg: dependency problems prevent configuration of mysql-community-client:
    mysql-community-client depends on mysql-common (>= 8.0.16-2ubuntu16.04); however:
    Package mysql-common is not configured yet.
    Version of mysql-common on system, provided by mysql-common:amd64, is <none>.

    dpkg: error processing package mysql-community-client (–configure):
    dependency problems - leaving unconfigured
    dpkg: dependency problems prevent configuration of mysql-client:
    mysql-client depends on mysql-community-client (= 8.0.16-2ubuntu16.04); however:
    Package mysql-community-client is not configured yet.

    dpkg: error processing package mysql-client (–configure):
    dependency problems - leaving unconfigured
    dpkg: dependency problems prevent configuration of mysql-community-server:
    mysql-community-server depends on mysql-common (>= 8.0.16-2ubuntu16.04); however:
    Package mysql-common is not configured yet.
    Version of mysql-common on system, provided by mysql-common:amd64, is <none>.
    mysql-community-sNo apport report written because the error message indicates its a followup error from a previous failure.
    No apport report written because the error message indicates its a followup error from a previous failure.
    No apport report written because MaxReports is reached already
    No apport report written because MaxReports is reached already
    No apport report written because MaxReports is reached already
    erver depends on mysql-client (= 8.0.16-2ubuntu16.04); however:
    Package mysql-client is not configured yet.

    dpkg: error processing package mysql-community-server (–configure):
    dependency problems - leaving unconfigured
    dpkg: error processing package gettext (–configure):
    package is in a very bad inconsistent state; you should
    reinstall it before attempting configuration
    dpkg: dependency problems prevent configuration of mysql-server:
    mysql-server depends on mysql-community-server (= 8.0.16-2ubuntu16.04); however:
    Package mysql-community-server is not configured yet.

    dpkg: error processing package mysql-server (–configure):
    dependency problems - leaving unconfigured
    Errors were encountered while processing:
    mysql-common
    mysql-community-client
    mysql-client
    mysql-community-server
    gettext
    mysql-server
    E: Sub-process /usr/bin/dpkg returned an error code (1)

    • Hi @ramanakarasarma,

      Oh dear, not great!

      Could you connect to the mysql server using mysql -h 127.0.0.1 -P 3306 -u root -p <database> or did that fail?

      You might be able to convince mysql-server to reinstall fully by using sudo apt-get install --reinstall mysql-server.

      - Matt.

      • sudo apt-get install –reinstall mysql-server
        Reading package lists… Done
        Building dependency tree

        Reading state information… Done
        0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 194 not upgraded.
        6 not fully installed or removed.
        Need to get 0 B/1,082 kB of archives.
        After this operation, 0 B of additional disk space will be used.
        E: Internal Error, No file name for mysql-server:amd64

        • Replied to your answer with the successful connection to the mysql server :)

          - Matt.

mysql -h 127.0.0.1 -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.16 MySQL Community Server - GPL

Copyright © 2000, 2019, 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> show databases;
+——————–+
| Database |
+——————–+
| informationschema |
| kataragamaKiosk |
| mysql |
| performance
schema |
| sys |
+——————–+
5 rows in set (0.63 sec)

mysql>

  • Looks like mysql-server is running correctly then, so don’t worry about reinstalling it.
    Follow my later steps on checking the configuration files are correct :)

    - Matt.

Have another answer? Share your knowledge.