Cant connect to local mysql server through socket /var/lib/mysql/mysql.sock 111

뭘 해야하나

mysql

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)

hansoul 2021. 6. 15. 19:23

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)

Cant connect to local mysql server through socket /var/lib/mysql/mysql.sock 111

이렇게 해결이 가능 합니다 그런데 이경우 지속적으로 되는게 아니고 잠깐 임시방편이라 근복적인 원인은

mysql을 root 계정을 이용해 RPM으로 설치했고 mysql.sock 소유권한이 없어서 접근을 못했던 것입니다

chmod,chown을 이용하여 권한을 변경해주면 해결됩니다

Cant connect to local mysql server through socket /var/lib/mysql/mysql.sock 111

sql을 멈추고 다시 실행을 해야 정확하게 구동이 됩니다

If you work with MySQL then you’re probably familiar with the can’t connect to local mysql server through socket error message. And you’ll see error message like:


Error:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2 “No such file or directory”)

In this short article, we’ll show you how to troubleshoot and fix this issue in a jiffy.

And, don’t worry it’s a pretty straight up fix and you have nothing to worry about.

So let’s go.

  • What Is Can’t Connect to Local MySQL Server Through Socket Error?
  • How to Fix Can’t Connect to Local MySQL Server Through Socket Error
    • 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
  • Conclusion on Can’t Connect to Local MySQL Server Through Socket

What Is Can’t Connect to Local MySQL Server Through Socket Error?

So what’s causing the can’t connect to local mysql server through socket error message?

This error message is basically telling you that the application is not able to locate the socket file needed to establish a MySQL connection.

And as you well know the socket file is used by the operating system to enable interface services such as MySQL or PHP to interact and communicate among each other.

For some people this appear message may occur when trying to restart MySQL and run the:

/usr/local/mysql/bin/mysql start

command resulting in the error message:

Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)

On the other hand, for others, this error message arises when they try to log into the MySQL database using any user credentials.

Then you have those we experience this same problem with their website application especially those built with PHP.

For example, all of a sudden the website refuses to work, and when you pry into the log file you see the error message logged there similar to this one:

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

[SPECIAL OFFER]: Fast MySQL & PHP Web Hosting

[BENEFITS]:

  • FREE 1-Click Install of Open Source Apps, Blog, CMS, and much more!
  • Support for PHP, MySQL, WordPress, Drupal, Joomla and much more!
  • Multi-Domain Hosting & 99.9% Uptime Guarantee!
  • Super Fast Servers with 24/7/365 Technical Support!

Click here to access this [SPECIAL OFFER]

How to Fix Can’t Connect to Local MySQL Server Through Socket Error

So how do you fix this can’t connect to local mysql server through socket error message?

Well, here are some steps you need to take to resolving this issue:

Solution #1: Check If MySQL Service is Runnning

First of all, check to make sure the mysqld service is running or not. To check if MySQL is running, run the following command:

mysqladmin -u root -p status

And if the service is not running, then, by all means, start it up by running the following command:

service mysqld start

Once you restart the service, try again to connect to MySQL.

Solution #2: Connect with 127.0.0.1

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.

However, if you use 127.0.0.1 ip address, the operating system will use the TCP/IP connector.

So as a possible solution when you’re having issues with the socket connector, you can try to establish the connection using TCP/IP by specifying the 127.0.0.1 ip address instead of localhost.

Solution #3: Modify the my.cnf file

The my.cnf file is a configuration file used by MySQL. And it’s processed by either the mysqld or mysqladmin service depending on the options specified.

So locate this my.cnf file which is normally in the /etc/ directory and modify it accordingly as follows:

[mysqld] socket=/var/lib/mysql/mysql.sock [client] socket=/var/lib/mysql/mysql.sock

After modifying the file restart your MySQL service and try connecting again.

Solution #4: Verify mysql.sock Location

Another possible cause of this problem is that the mysql.sock file in another directory.

So you need to locate where the mysql.sock file and create a symlink to it.

Most times the file is located in either the /data/mysql_datadir/mysql.sock or /tmp/mysql.sock

So for instance, if you locate the file in /data/mysql_datadir/mysql.sock, all you need to do is create a symlink for it using the following command:

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

Once you do this, go ahead and restart the MySQL service and try initiating the connection again.

[SPECIAL OFFER]: Fast MySQL & PHP Web Hosting

[BENEFITS]:

  • FREE 1-Click Install of Open Source Apps, Blog, CMS, and much more!
  • Support for PHP, MySQL, WordPress, Drupal, Joomla and much more!
  • Multi-Domain Hosting & 99.9% Uptime Guarantee!
  • Super Fast Servers with 24/7/365 Technical Support!

Click here to access this [SPECIAL OFFER]

Solution #5: Change MySQL Folder Permission

There one other possible solution fixing this issue and that is to change the MySQL folder’s permission.

And this can be done easily by running the following command if you’re working on a local environment:

sudo chmod -R 755 /var/lib/mysql/

After which you can restart mysql service:

service mysqld start

Then try to establish the connection again.

Solution #6: Multiple MySQL Instances

In some cases, the installation of multiple MySQL on the server environment causes this problem.

So you have a case whereby multiple instances of MySQL are running at the same time.

As such the issue running the following commands can fix the issue:

ps -A|grep mysql

Next, kill the mysql process by running this command:

sudo pkill mysql

Then do the same thing for mysqld:

ps -A|grep mysqld

Again, kill this process as well:

sudo pkill mysqld

Finally, go ahead run the following commands to restart and connect to the MySQL server:

sudo service mysql restart mysql -u root -p

Conclusion on Can’t Connect to Local MySQL Server Through Socket

We hope one of the above possible solutions to the can’t connect to local mysql server through socket error resolves your situation.

[SPECIAL OFFER]: Fast MySQL & PHP Web Hosting

[BENEFITS]:

  • FREE 1-Click Install of Open Source Apps, Blog, CMS, and much more!
  • Support for PHP, MySQL, WordPress, Drupal, Joomla and much more!
  • Multi-Domain Hosting & 99.9% Uptime Guarantee!
  • Super Fast Servers with 24/7/365 Technical Support!

Click here to access this [SPECIAL OFFER]

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

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..

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 through socket '/ tmp MySQL sock homebrew?

To fix this error, you need to see if MySQL server is already installed and running on your computer. That should start the server and generate the mysql. sock file. You can try to connect to your MySQL server again now.

How do I connect to a MySQL socket?

Connect to MySQL with unix sockets.
Locate a Unix socket file On the server host in the command line, run the following command: ... .
Check the Unix socket connection from the command line ... .
Download third-party libraries ... .
Configure the MySQL driver in DataGrip ... .
Create a connection to the MySQL server.