[MySQL] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 에러 해결 사례

Shyuuuuni·2022년 10월 28일
2

❌ Trouble-shooting

목록 보기
2/9
post-thumbnail

명확한 해결 방법을 보고 정리한 것이 아니라 여러가지 해결 방법을 실행하다가 결국 해결한 방식이여서 100% 맞는 방법이지 않을 수 있습니다.

개발 환경

  • Macbook Air (M1, 2020) macOS Monterey 12.4
  • homebrew : 3.6.4
Homebrew 3.6.4
Homebrew/homebrew-core (git revision 375065e9c3a; last commit 2022-10-05)
Homebrew/homebrew-cask (git revision 7068d45cf4; last commit 2022-10-05)

문제 상황

기존에 설치되어 있던 MariaDB를 제거하고 MySQL 설치 진행 중 발생한 상황이다.

  • homebrew 로 mysql 설치
  • $ mysql.server start 명령어로 서버 실행
  • $ mysql_secure_installation 명령어로 보안 설정 실행
  • 마지막으로 Enter password for user root: 키워드 뒤에 사용할 비밀번호를 입력 후 엔터
[MySQL] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

실패 사례

(1) 서비스 재시작 시도

  1. 서비스 확인
$ brew services list

Name  Status  User         File
mysql stopped kimseunghyun ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  1. 서비스 재시작
$ brew services restart mysql

Stopping `mysql`... (might take a while)
==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
  1. 다시 확인 (실패...)
$ brew services list

Name  Status  User         File
mysql stopped kimseunghyun ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

(2) 재설치 이후 ERROR! The server quit without updating PID file (/opt/homebrew/var/mysql/gimseunghyeon-ui-MacBookAir.local.pid). 오류 발생

(주의) 아래는 실패사례입니다.

  • MySQL 설치가 잘못되었나 싶어서 삭제 후 재설치를 진행하고 서버가 켜졌는지 확인했다.
$ mysql.server status
 ERROR! MySQL is not running

$ mysql.server start
Starting MySQL
. ERROR! The server quit without updating PID file (/opt/homebrew/var/mysql/gimseunghyeon-ui-MacBookAir.local.pid).
  • /opt/homebrew/var/mysql/gimseunghyeon-ui-MacBookAir.local.pid 위치에서 권한 문제가 발생하여 업데이트 할 수 없다고 판단하여 소유자를 확인하여 변경했었음.
  1. 소유자를 확인한다. : 현재 소유자 = user
# ls -al /opt/homebrew/var/ | grep mysql
drwxr-xr-x  20 kimseunghyun  wheel  640  9  8 12:45 mysql
  1. 소유자를 mysql로 변경한다.
$ sudo chown -R mysql:mysql /opt/homebrew/var/mysql
  1. 이후 권한을 변경해준다.
$ sudo chmod -R 777 /opt/homebrew/var/mysql

해결 사례

  • 비슷한 에러를 해결하는 방법이 검색했을때 꽤나 나왔는데, 노트북이 M1 mac이여서 homebrew가 설치하는 위치가 조금 달라서 확인이 어려웠다.
  • 여러가지 사례를 M1 mac에서 사용하는 homebrew에 맞춰서 설정해보고, mysql을 재설치하고를 반복하면서 해결했을때 사례를 정리했다.
  1. 재설치 : 서비스 종료 - brew에서 제거 - 폴더 제거 - 재설치 순서로 진행
# 서비스 종료
$ sudo brew services stop mysql

# mysql 제거
$ brew uninstall mysql

# 폴더 제거
$ rm -rf /opt/homebrew/var/mysql

# 재설치
$ brew install mysql
  1. homebrew의 파일 자체로 접근해서 서비스를 실행
# mysql 설치 경로로 이동
$ cd /opt/homebrew/Cellar/mysql/8.0.30_1/support-files

# 서버 파일 직접 시작
$ ./mysql.server start
  • 여기까지 진행했을 때, 기존에 발생했던 mysql_sequre_installation 실행 시 발생했던 문제가 발생하지 않고 잘 진행되었다.
  • 아래 화면으로 넘어가서 진행이 되면 성공!
mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No:
  • 설치 과정을 따라가면 된다.
profile
배짱개미 개발자 김승현입니다 🖐

4개의 댓글

comment-user-thumbnail
2023년 6월 22일

👍...

답글 달기
comment-user-thumbnail
2023년 10월 3일

스택오버플로우에도 잘 모르는 사람이 대부분이던데 정말 감사합니다

답글 달기
comment-user-thumbnail
2023년 10월 14일

설치하는 부분 계속 에러 떴었는데 진짜 감사합니다!1

답글 달기
comment-user-thumbnail
2023년 10월 31일

와 4시간 동안 다 해봤는데 이거 하니까 해결되네요 감사합니다

답글 달기