오류

[MySQL 오류해결] ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

jjsana 2025. 2. 12. 21:43

[ 첫번째 방법 ] - 소켓 파일 경로 추가 
ls -l /etc/my.cnf

ls -l /etc/mysql/my.cnf

ls -l /opt/homebrew/etc/my.cnf

ls -l ~/.my.cnf

-> 하나씩 명령어 쳐서 파일 위치 찾기

 

sudo nano /opt/homebrew/etc/my.cnf -> 파일 위치 찾은 후 이렇게 명령어 친 후

# Default Homebrew MySQL server config

[mysqld]

# Only allow connections from localhost

bind-address = 127.0.0.1

mysqlx-bind-address = 127.0.0.1

# 소켓 파일 경로 추가

socket=/tmp/mysql.sock

-> 소켓 파일 경로 추가 해준 후 control+o(저장) 그다음 엔터 control+x(나가기) 그 다음 엔터

이렇게 한 후

brew services restart mysql -> mysql 서버 재시작

ls /tmp/mysql.sock -> 파일 생성됐는지 확인! (만약 생성이 안됐다면 두번째 해결방법으로,,)

 

rm -rf /opt/homebrew/var/mysql/* -> Mysql 서버 초기화

mysqld_safe --skip-grant-tables & -> 서버 시작

ps aux | grep mysql -> 상태확인 mysql 떴는지 잘 뜨면

ls /tmp/mysql.sock -> 파일 있는지 확인

mysql -u root -> 들어가서 확인

비밀번호 설정하고 싶으면

brew services stop mysql

mysqld_safe --skip-grant-tables & -> 서버 잠깐 내리기

mysql -u root 들어간 후

FLUSH PRIVILEGES;

ALTER USER 'root'@'localhost' IDENTIFIED BY '새로운비밀번호'; -> 비밀번호 설정

mysqladmin -u root -p shutdown -> 서버 다시 내린 후

brew services start mysql -> 재실행

 

[ 두번째 방법 ] - 위와 대부분 동일하지만 다른점은 brew를 그냥 아에 삭제하고 재설치

brew 삭제.. 꼭 mysql 계속 재설치 해도 어차피 안됐어서,, 깔끔하게 brew삭제해주세요,, 삭제하기 전 뭘 설치했었는지 list 확인하시구요,,, ㅠ

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)" -> brew 삭제

rm -rf Homebrew/ -> 폴더 완전히 삭제

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" -> 재설치

출처 : https://wonkang.tistory.com/21

brew install mysql -> mysql 설치

명령어

ls -l /etc/my.cnf

ls -l /etc/mysql/my.cnf

ls -l /opt/homebrew/etc/my.cnf

ls -l ~/.my.cnf

-> 파일 위치 찾기

sudo nano /opt/homebrew/etc/my.cnf -> 파일 위치 찾은 후 이렇게 명령어 친 후

# Default Homebrew MySQL server config

[mysqld]

# Only allow connections from localhost

bind-address = 127.0.0.1

mysqlx-bind-address = 127.0.0.1

# 소켓 파일 경로 추가

socket=/tmp/mysql.sock

-> 소켓 파일 경로 추가 해준 후 control+o(저장) 그다음 엔터 control+x(나가기) 그 다음 엔터

이렇게 한 후

brew services restart mysql -> mysql 서버 재시작

ls /tmp/mysql.sock -> 파일 생성됐는지 확인 ! 만약 생성이 안됐다

cd /opt/homebrew/var/mysql/ -> 어떤파일이 있는지 확인

ls -l

 

rm -rf /opt/homebrew/var/mysql/* -> Mysql 서버 초기화

mysqld_safe --skip-grant-tables & -> 서버 시작

ps aux | grep mysql -> 상태확인 mysql 떴는지 잘 뜨면

ls /tmp/mysql.sock -> 파일 있는지 확인

mysql -u root -> 들어가서 확인

비밀번호 설정하고 싶으면

brew services stop mysql

mysqld_safe --skip-grant-tables & -> 서버 잠깐 내리기

mysql -u root 들어간 후

FLUSH PRIVILEGES;

ALTER USER 'root'@'localhost' IDENTIFIED BY '새로운비밀번호'; -> 비밀번호 설정

mysqladmin -u root -p shutdown -> 서버 다시 내린 후

brew services start mysql -> 재실행

mysql -u root -p 이렇게 들어간 후 설정한 비밀번호 입력하면 끝

이 블로그로인해 이 오류로 인해 더 이상 고통받는 사람이 없길 바라며,,,