Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2) ubuntu

현재 우분투에서 서버 셋팅 및 테스트 진행 중, 

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 에러가 발생하여, 아래와 같이 해결하였다!!! 

우분투에서 my.cnf 변경 하고 재시작 후,  mysql 접속시도하였으나,

mysql -uroot -p

아래와 같은 에러가 발생 ㅜ.ㅜ

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

구글 검색을 통해 여러 사이트 찾아보았지만, 해결되지 않음.-_-;;;

http://stackoverflow.com/questions/11657829/error-2002-hy000-cant-connect-to-local-mysql-server-through-socket-var-run

http://dba.stackexchange.com/questions/98467/how-to-solve-cant-connect-to-local-mysql-server-through-socket-error

몇가지 확인차 명령어 실행 중, 이상한 점 발견...

아래 명령어로 mysql 이 종료되었다면, 외부 mysql 클라이언트(ex: mysql workbench)로 접속이 여전히 된다는 점.

# /etc/init.d/mysql restart => stop은 [OK] 하지만, start는 [fail]

# /etc/init.d/mysql stop  =>  [OK] 

정확한 것이지는 더 확인해 봐야겠지만, 우분투에서 패키지로 설치했을 경우에는 아래와 같은 명령어로 해야 제대로 동작하는 것 같다.

아래 명령어로 일단 해결되어 Pass!!

# service mysql stop

# service mysql start

# service mysql resrart

Tason

OS/Ubuntu

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

Yan g 2019. 10. 18. 14:16

Ubuntu(version 18.04)에 mysql 설치가 이미 완료된 상황이며, mysql -u root -p 명령어를 통해 mysql에 접속하려는데 위와같은 에러가 발생했다.

위의 에러는 다양한 원인이 존재하고 그에 맞는 여러 해결책이 존재한다.  여러가지 방법을 강구하다가 실패하고 결국 다음의 Trouble Shooting과 같이 해결했다. 정확한 원인과 해결과정에 대한 설명은 확인이 완료되는대로 업데이트 할 예정이고 우선적으로 이 게시물은 위의 오류를 해결하는데에 필요한 절차만 명시한다.

Trouble Shooting

ps -A|grep mysql

sudo pkill mysql

ps -A|grep mysqld

sudo pkill mysqld

service mysql restart

코드의 마지막 줄에 입력창이 mysql> 로 표기되면서 mysql 접속이 성공한 것을 확인할 수 있다.

[실행환경]
Windows 10
Ubuntu 18.04 LTS
mysql ver14.14 Distrib 5.7.31

ubuntu에서 mysql을 실행 시킬 때
/etc/init.d/mysql start 혹은 mysql -u root -p 등을 사용할 것이다.
만약 Windows에서도 mysql을 사용하고 있다면 이러한 에러를 발견할 수 있다.

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

저 에러는

 1. /var/run/mysqld/mysqld.sock 소켓파일이 없거나(찾을 수 없거나)

 2. port number가 같은 경우 충돌이 일어날 때 발생 할 수 있다.

$ cat /var/log/mysql/error.log

위 명령어로 mysql error log를 본 후 어떤 문제인지 판별 할 수 있다.

1의 경우에는 내가 겪은 경우가 아니라 자세히 알진 못하지만 재설치를 하면 해결된다는 것 같다.

2의 경우가 내가 겪은 상황인데 로그파일에 아래 에러 로그가 적혀있었다.

[ERROR] Do you already have another mysqld server running on port: 3306 ?

이미 3306 port number를 사용하고 있다는 말이다. port number를 바꿔주면 된다.윈도우에서 우분투를 사용할 때 mysql 설정파일 경로는 /etc/mysql/mysql.conf.d/mysqld.cnf 이며,
[mysqld] 아래에 있는 port 부분을 안쓰는 포트넘버로 바꿔주면 된다.

$ /etc/init.d/mysql restart

위의 명령어로 mysql을 재시작하고 다시 실행시키면 제대로 동작하는 것을 볼 수있다.

PS

위의 방법은 나에게 임시적인 방편(?)인 것 같다. 당장 에러가 날 때 포트넘버를 바꾸면 제대로 동작하지만 어찌된 일인지 며칠이 지나면 다시 포트넘버 충돌이 일어나서 포트넘버를 다른 넘버로 바꿔야한다. 여기에 대한 내용은 찾을 수 없어서 귀찮지만... 어쩔 수 없이 이렇게 쓰고 있다...

After sleepless nights trying to find a solution to this problem, I have finally come to one. Thus, I’ve decided to share it and I know I’ll need it again in the future (It’s not the first time I install MySQL in a recently formatted Windows 10 computer and face this problem).

Note that I’m running Debian in a Linux Subsystem for Windows 10 and I suppose it might work with any subsystem.

Cleaning the Backyard

First of all, let’s start from scratch, you want to make sure you don’t have any remaining MySQL garbage in your system:

sudo apt-get remove --purge mysql*

After that, check if everything is clean, run the following code, and hope nothing shows up

dpkg -l | grep mysql

If that doesn’t do the trick, like in my case I still had some installed…

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2) ubuntu

Try to purge them individually, like so (php is just an innocent amidst the sinful, so leave it be) :

sudo apt-get remove --purge mysql-apt-config

And now, clean everything else

sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean

Installing Mysql-server

Now, we can start from scratch and install it. First, we have to download our configuration file using (be sure to get the latest file, mine was 8.15–1. you can find them here http://repo.mysql.com):

wget http://repo.mysql.com/mysql-apt-config_0.8.15-1_all.deb

Once it is done, install it using:

sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb

It will prompt you to choose a MySQL version, take mysql-5.7. I was taken by the greed of choosing 8.0, but it gave me more problems than I had wished for, then humbly get 5.7:

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2) ubuntu

Now, we can properly install mysql-server:

sudo apt update && sudo apt install mysql-server

You’ll need to enter a password…make sure you’ll remember it ;)

Secure Mysql Installation

Now, we want to change some default settings that might not be so secure, then run:

sudo mysql_secure_installation

Enter root password (I’ve told you to remember it) and…

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2) ubuntu

Here’s when my nightmares started and I stumbled with:

Error: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

I noticed then that by default this service is not started, we have to start it, but using sudo service mysql start or sudo service mysqld start won’t start it, neither will sudo systemctl start mysql. They will just give you a bunch of problems, you should:

sudo /etc/init.d/mysql start

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2) ubuntu

Now, we can again run:

sudo mysql_secure_installation

The settings I’ve used were:

Would you like to setup VALIDATE PASSWORD plugin? y
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Change the password for root?: n
Remove anonymous users? y
Disallow root login remotely? n
Remove test database and access to it? y
Reload privilege tables now? y

Making Sure Everything is Set

Now, before trying to connect to mysql, make sure we have a root has an authentication string. Enter MySQL Monitor:

mysql -u root -p

And then enter:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

Make sure you see something this

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2) ubuntu

Now, if the authentication string is empty, we have to set up a new password for root. Still, in MySQL Monitor, run:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

And then reload grant tables:

mysql> FLUSH PRIVILEGES;

Now, check if our root user has an authentication_string:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

Using MySQL Workbench from Windows 10

I like connecting to the database from Windows itself, and once I have installed workbench in Windows (https://dev.mysql.com/downloads/workbench/), it opens up without any connections.

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2) ubuntu

Now we have to create a new connection, by clicking in the add button:

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2) ubuntu

Add a name to the connection and leave the other settings as they are:

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2) ubuntu

Now you can simply connect to the database in your Linux Subsystem from Windows.

Can't connect to local MySQL server through socket in 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 through socket run Mysqld 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..

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.

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.