Mariadb can t connect to local mysql server through socket /tmp/mysql sock

Check that you have a /tmp/mysql.sock file, you may have the sock file in another place, like

/var/lib/mysql/mysql.sock

If that’s the case, link to the requested file:

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

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

Introduction

Users working with MySQL can run into the error 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock (2)' when logging into the MySQL interface. This problem usually arises if MySQL can't access the mysqld.sock socket file.

In this tutorial, we will go over the potential causes of the 'Can't connect to local MySQL server through socket' error and show you different methods of resolving this issue.

Mariadb can t connect to local mysql server through socket /tmp/mysql sock

Prerequisites

  • A system running Ubuntu 20.04
  • A user account with sudo privileges
  • Access to the terminal window/command line
  • A copy of MySQL installed and ready to use (learn how to install it with our guide to installing MySQL on Ubuntu 20.04)

Resolving the 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' Error

There are multiple ways to solve the 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock (2)' error. If one solution doesn't work, move down the list until you find the one that resolves the issue.

Method 1: Check the MySQL Service

1. Check the status of the MySQL service with:

sudo systemctl status mysql

Mariadb can t connect to local mysql server through socket /tmp/mysql sock

2. If the service is not running, restart it by using:

sudo systemctl start mysql

3. To prevent this issue from happening, set the MySQL service to automatically start at boot:

sudo systemctl enable mysql

Mariadb can t connect to local mysql server through socket /tmp/mysql sock

Method 2: Verify the mysqld.sock Location

The 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' error also happens if MySQL can't find the mysql.sock socket file.

1. Find the current mysqld.sock location by using the find command to list all the socket files on your system:

sudo find / -type s

Mariadb can t connect to local mysql server through socket /tmp/mysql sock

2. Open the MySQL configuration file in a text editor of your choice. In this example, we use nano:

sudo nano /etc/mysql/my.cnf

3. Then, add the following lines at the end of the MySQL configuration file:

[mysqld]
socket=[path to mysqld.sock]
[client]
socket=[path to mysqld.sock]

Where:

  • [path to mysqld.sock]: Path to the mysqld.sock socket file you found in Step 1.

Mariadb can t connect to local mysql server through socket /tmp/mysql sock

Another method is to create a symlink from the location of mysqld.sock to the /var/run/mysqld directory:

ln -s [path to mysqld.sock] /var/run/mysqld/mysqld.sock

4. Press Ctrl+X to close the configuration file and type Y and press Enter to save the changes you made.

4. Finally, restart the MySQL service:

sudo systemctl restart mysql

Method 3: Check the MySQL Folder Permission

Another potential cause could be that the MySQL Service can't access the /var/run/mysqld directory due to permission restrictions:

1. To resolve this issue, change the permission settings for the mysqld directory with:

sudo chmod -R 755 /var/run/mysqld

Setting the permission to 755 allows the root user to read, write, and execute the directory, while other users can only read and execute.

2. Restart the MySQL service for the changes to take effect:

sudo systemctl restart mysql

Method 4: Check for Multiple MySQL Instances

The error also occurs if there are multiple instances of MySQL running at the same time.

1. To list all the instances of MySQL, use:

ps -A|grep mysqld

Mariadb can t connect to local mysql server through socket /tmp/mysql sock

2. If there are multiple MySQL instances running, terminate them with:

sudo pkill mysqld

3. Restart the MySQL service to start a single instance of MySQL:

sudo systemctl restart mysql

Conclusion

After reading this tutorial, you should have identified the cause of the 'Can't connect to local MySQL server through socket' error and applied the appropriate solution.

For more help with using MySQL, consult our MySQL Commands Cheat Sheet.

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

On the server host in the command line, run the following command: mysql -u root -p -h 127.0.0.1 -e "select @@socket".
Type a password for your root user and press Enter ..

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

Solution #1: Check If MySQL Service is Runnning..
Solution #2: Connect with 127.0.0.1..
Solution #3: Modify the my.cnf file..
Solution #4: Verify mysql.sock Location..
Solution #5: Change MySQL Folder Permission..
Solution #6: Multiple MySQL Instances..

Can not connect to MySQL through socket?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

Can't connect to local MySQL server through socket '[ path to mysqld sock ]'?

How to Fix 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'.
Method 1: Check the MySQL Service..
Method 2: Verify the mysqld.sock Location..
Method 3: Check the MySQL Folder Permission..
Method 4: Check for Multiple MySQL Instances..