Cant connect to local mysql server through socket

MySQL manages connections to the database server through the use of a socket file, a special kind of file that facilitates communications between different processes. The MySQL server’s socket file is named mysqld.sock and on Ubuntu systems it’s usually stored in the /var/run/mysqld/ directory. This file is created by the MySQL service automatically.

Sometimes, changes to your system or your MySQL configuration can result in MySQL being unable to read the socket file, preventing you from gaining access to your databases. The most common socket error looks like this:

Output

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

There are a few reasons why this error may occur, and a few potential ways to resolve it.

One common cause of this error is that the MySQL service is stopped or did not start to begin with, meaning that it was unable to create the socket file in the first place. To find out if this is the reason you’re seeing this error, try starting the service with systemctl:

  1. sudo systemctl start mysql

Then try accessing the MySQL prompt again. If you still receive the socket error, double check the location where your MySQL installation is looking for the socket file. This information can be found in the mysqld.cnf file:

  1. sudo nano /etc/mysql/mysql.conf.d/mysql.cnf

Look for the socket parameter in the [mysqld] section of this file. It will look like this:

/etc/mysql/mysql.conf.d/mysqld.cnf

. . .
[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
. . .

Close this file, then ensure that the mysqld.sock file exists by running an ls command on the directory where MySQL expects to find it:

  1. ls -a /var/run/mysqld/

If the socket file exists, you will see it in this command’s output:

Output

. .. mysqld.pid mysqld.sock mysqld.sock.lock

If the file does not exist, the reason may be that MySQL is trying to create it, but does not have adequate permissions to do so. You can ensure that the correct permissions are in place by changing the directory’s ownership to the mysql user and group:

  1. sudo chown mysql:mysql /var/run/mysqld/

Then ensure that the mysql user has the appropriate permissions over the directory. Setting these to 775 will work in most cases:

  1. sudo chmod -R 755 /var/run/mysqld/

Finally, restart the MySQL service so it can attempt to create the socket file again:

  1. sudo systemctl restart mysql

Then try accessing the MySQL prompt once again. If you still encounter the socket error, there’s likely a deeper issue with your MySQL instance, in which case you should review the error log to see if it can provide any clues.

Please fill out the detail below, it helps me investigate the bug:

  1. Driver :MySQL 5.7.23

  2. DBngin build number: Version 3.1 (32)

  3. macOS version:10.14.2

  4. The steps to reproduce this issue:

    When I connect the MySQL, suggesting this exception
    Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock'

In addition, if I have a Mysql how local has been installed, How to let DBngin association

Issue

When the MySQL client connects to localhost, it attempts to use a socket file instead of TCP/IP. The socket file used is specified in /etc/mysql/my.cnf when the MySQL client is installed on the system. This is a MySQL socket file, which SingleStoreDB Cloud does not use by default. Therefore, connecting with localhost attempts to connect to MySQL and not SingleStoreDB Cloud.

Solutions

There are two solutions to solve this problem:

  1. Specify 127.0.0.1 as the host instead of localhost. That is, mysql -h 127.0.0.1 -u root instead of mysql -h localhost -u root. If you omit the host (mysql -u root), the MySQL client will implicitly use localhost.

  2. For SingleStoreDB Cloud, change the socket value in the /etc/mysql/my.cnf file to the location of your SingleStoreDB Cloud socket file as shown in the example below:

[client]
port          = 3306
socket        = /var/lib/memsql/data/memsql.sock

Can not connect to MySQL server through socket?

Solution #2: Connect with 127.0. Another possible solution to the >can't connect to local mysql server through socket> error message is to try and connect to the MySQL using the 127.0. 0.1 ip address instead of localhost. When you use localhost to connect to MySQL, the operating system uses the socket connector.

How do I connect to a local MySQL server through socket?

Connect to MySQL with unix sockets On Unix, you can connect to the mysqld server by using two different ways: a Unix socket file (for example, /var/run/mysqld/mysqld. sock), or by using TCP/IP (for example, 127.0. 0.1:3306 ).

Can't connect to local MySQL server through socket Ubuntu?

To fix the MySQL socket issue and access denied error for root@localhost , follow the below steps. Stop the MySQL server by executing the command “ sudo service mysql stop “. Create socket location as a placeholder by executing the command “ sudo mkdir -p /var/run/mysqld “.

Can't connect to local MySQL server socket error 2002?

The ERROR 2002 above happens when the mysql. sock socket file can't be found in your filesystem. This file is created when MySQL server is started and removed when you stop the server. To fix this error, you need to see if MySQL server is already installed and running on your computer.