Introduction Show
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. Prerequisites
Resolving the 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' ErrorThere 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 Service1. Check the status of the MySQL service with: 2. If the service is not running, restart it by using: sudo systemctl start mysql3. To prevent this issue from happening, set the MySQL service to automatically start at boot: sudo systemctl enable mysqlMethod 2: Verify the mysqld.sock LocationThe '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 s2. Open the MySQL configuration file in a text editor of your choice. In this example, we use nano: sudo nano /etc/mysql/my.cnf3. 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:
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.sock4. 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: Method 3: Check the MySQL Folder PermissionAnother 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/mysqldSetting 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 mysqlMethod 4: Check for Multiple MySQL InstancesThe 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 mysqld2. If there are multiple MySQL instances running, terminate them with: sudo pkill mysqld3. Restart the MySQL service to start a single instance of MySQL: sudo systemctl restart mysqlConclusion 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. Can't connect to local server through socket TMP MySQL sock?It means either the MySQL server is not installed/running, or the file mysql. sock doesn't exist in /var/lib/mysql/ . There are a couple of solutions for this error. Then try to connect again.
Can't connect to local MySQL server?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.
How do I start MySQL server on Mac?Open macOS system preferences and select the MySQL preference panel, and then execute Start MySQL Server. The Instances page includes an option to start or stop MySQL, and Initialize Database recreates the data/ directory.
How do I uninstall MySQL on a Mac?How to uninstall MySQL from Mac manually. Open System Preferences. ... . Find the MySQL icon and click on it.. In the MySQL Instances tab, click the Uninstall button. ... . Open the Terminal from the Launchpad.. Back up your data to text files with the following command: ... . Check MySQL processes that are working in the background.. |