how to fix MySQL can't connect to local server

How to Fix MySQL “Can’t connect to local server”

MySQL is an open-source relational database management system used for storing data. It is written in C and C++, offering various features that are very useful for developers and other users familiar with it. Often, it’s very useful to block off access to your MySQL server to prevent unauthorized access. Sometimes, you may lock yourself out. In this blog post we will show you how to fix the MySQL connectivity issues when accessing MySQL as a local server. This blog post will teach you how to fix the dreaded “can’t connect to local server” issue. Let’s get started!

Prerequisites

  • A server with a Linux-based distribution (we used Ubuntu 22.04)
  • User privileges: root or non-root user with sudo privileges

MySQL Can’t connect to local server

The error message “can’t connect to local MySQL server through socket” can appear due to various reasons. One possibility is that the server cannot establish a connection to the MySQL server using its default socket due to an incorrect path to the socket file. Other reasons include incorrect permissions, misconfiguration in the MySQL setup, or the MySQL service is crashing unexpectedly due to a broken database, not enough RAM on the server, etc.

The socket is an endpoint where two processes communicate with each other. When we say the MySQL socket, we are talking about the mysqld.sock file located at /var/run/mysqld/mysqld.sock. Let’s go over the possible solutions for this connecting issue with the local MySQL server.

Let’s go over each possibility – we’re confident your MySQL server will be accessible locally once you try all of these solutions.

Check Your MySQL Service

Sometimes the MySQL service can stop unexpectedly due to various reasons. It may be due to insufficient disk space, not enough RAM, or the server has been rebooted and the MySQL service was not enabled to start on boot previously.

So, when we try to log in to the MySQL console with the following command:

mysql -u root -p

The connection cannot be established, and we will receive the following output:

root@host:~# mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

First, we need to check if the MySQL service is up and running with the following command:

systemctl status mysql

If the service is not running, you should receive the following output:

root@host:~# systemctl status mysql
○ mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: inactive (dead)  since Mon 2024-01-08 18:19:37 CST; 13min ago
    Process: 30718 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
    Process: 30749 ExecStart=/usr/sbin/mysqld (code=exited, status=0/SUCCESS)
   Main PID: 30749 (code=exited, status=0/SUCCESS)
     Status: "Server shutdown complete"
        CPU: 1.981s

We just need to start and enable the service with the command below:

sudo systemctl start mysql && sudo systemctl enable mysql

After checking the status again, you should receive the following output:

root@host:~# systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2024-01-08 18:32:21 CST; 8min ago
   Main PID: 30950 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 4558)
     Memory: 365.3M
        CPU: 5.241s
     CGroup: /system.slice/mysql.service
             └─30950 /usr/sbin/mysqld

Try to log into your server now – it should be working again. If you are still seeing the same error, move onto the next potential solution.

MySQL Socket Permissions

Let’s say that we started and enabled the MySQL service, but we are still experiencing the error message:

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

Now we need to check if the socket file exists and is in the right directory with the correct permissions. Execute the command below:

ls -al /var/run/mysqld/

If you get this output:

root@host:~# ls -al /var/run/mysqld/
drwxr-xr-x  2 root root 120 Jan 8 19:12 .
drwxr-xr-x 32 root root 960 Jan 8 19:12 ..
srwxrwxrwx  1 root root   0 Jan 8 19:12 mysqld.sock
-rw-------  1 root root   6 Jan 8 19:12 mysqld.sock.lock

This output shows us that the permissions are incorrect – the root user owns the files. In this case, we need to set the right permissions:

chown -R mysql:mysql /var/run/mysqld/
chmod 755 /var/run/mysqld/

Once the permissions are set, restart the MySQL service and try to connect again to the MySQL console.

MySQL Configuration File

If you are still not able to connect to the MySQL console, or in other words your MySQL client can’t connect to a local server, then you should check the MySQL configuration file usually located at /etc/mysql/mysql.conf.d/mysqld.cnf – other locations for this include /etc/mysql/my.cnf, or even /etc/my.cnf. Find where your configuration file is and open it using your preferred text editor.

Look for the socket variable and make sure to put the full path of the socket file. If there is no socket variable, add it to your file like so:

socket = /var/run/mysqld/mysqld.sock

Make sure the permissions for that folder are set correctly as well. Save the file, close it and restart the MySQL service. Your MySQL client should be able to connect to your local server.

If none of this is solving the issue and if you have a managed Linux support plan with us, you can ask our support team to help you with your MySQL service. We are available 24/7 and are experts in system configuration, including with MySQL. We will be able to help you with the configuration of MySQL as well as any other issues you’re having with your server.

Leave a Reply

Your email address will not be published. Required fields are marked *