환경 : Mac M1
여러 방법을 시도
Mysql을 Spring과 연동하기 위해서 실행을 하고 데이터베이스 스키마를 생성할려고 했는데 갑자기 이런 오류가 발생했다!!!

brew services start mysql@8.0
=>Successfully started mysql@8.0 (label: homebrew.mxcl.mysql@8.0)
mysql을 시작하는 과정에는 오류가 발생하지 않았다.
mysql -u root -p
Enter password :
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)//오류 발생!
문제는 여기서 발생했다. 갑자기 잘 되던게 왜 안되나 싶은 생각이다...
먼저 오류를 번역을 해봤다.
"로컬 MySQL 서버에 '/tmp/mysql.sock' 소켓을 통해 연결할 수 없습니다(2)"
이게 무슨 말이지!!!!
(2)는 파일이 없다는 시스템 오류 코드를 나타낸다고 한다. 그래서 해당 경로로 이동을 해봤다.
해당 경로로 이동을 해보니까
- mysql.sock.lock
- mysqlx.sock
- mysqlx.sock.lock
해당 파일들이 존재하는 걸 확인할 수 있었다. 우리가 찾고자 하는 mysql.sock파일이 없는 걸 알았다!! 그래서 연결이 안되는 거 같았다.
mysql.sock 파일을 직접 생성하는 방법도 있을까 싶었지만 그렇게 하면 안되는 거 같았다.
mysql.sock??
Unix Domain Socket로 다른 컴퓨터의 프로세스와는 통신하는게 아닌 같은 컴퓨터 내의 프로세스끼리 통신을 위해 사용되는 일종의 IPC다
MySQL의 서버프로그램(Mysqld)와 클라이언트 프로그램(mysql)이 서로 다른 프로세스로 되어 있어서 두 프로세스가 서로 통신을 위한 것이다.
출처 : [MariaDB] 5. MySQL Socket, mysql.sock 위치 및 설명
그럼 어떻게 해야할까??
먼저 mysql설정 파일을 확인해볼려고 했다.
% mysql --help | grep "my.cnf" //명령어
//결과
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /opt/homebrew/etc/my.cnf ~/.my.cnf
my.cnf파일이 어디 있는지 찾기 위해서 명령어를 수행했을 때
- /etc/my.cnf
- /etc/mysql/my.cnf
- /opt/homebrew/etc/my.cnf
- ~./my.cnf
이렇게 4가지 위치에 있을 수 있다는걸 확인했고
ls -l /etc/my.cnf
ls -l /etc/mysql/my.cnf
ls -l /usr/local/etc/my.cnf
ls -l ~/.my.cnf
해당 명령어들을 통해서 각 위치에 파일이 있는지 확인했다.
그런데!!!!!!
//결과
ls: /etc/my.cnf: No such file or directory
ls: /etc/mysql/my.cnf: No such file or directory
ls: /usr/local/etc/my.cnf: No such file or directory
ls: /Users/imjunsik/.my.cnf: No such file or directory
아무곳에도 my.cnf파일이 없었다!!!!!!
그래서 설정 파일을 만들어주기로 다짐했다.
% sudo nano /etc/my.cnf
//파일에 입력 값
[mysqld]
socket=/tmp/mysql.sock
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/data/mysql.err
pid-file=/usr/local/mysql/data/mysqld.pid
[client]
socket=/tmp/mysql.sock
그리고 희망찬 마음으로 다시 실행했다.
결과는!!
% mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
실패했다....
왜 이럴까 대체....
이번에는 Mysql이 제대로 작동하고 있는지 확인해 보기로 했다!!
~ % brew services list
//결과
Name Status User File
mysql@8.0 error 256 imjunsik ~/Library/LaunchAgents/homebrew.mxcl.mysql@8.0.plist
분명 성공이라고 했었는데 상태를 에러였다!!!!
왜 에러가 발생했을까?? 로그를 확인해본다!!
~ % cat /opt/homebrew/var/mysql/$(hostname).err
//결과
2025-01-01T05:44:58.255227Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2025-01-01T05:44:58.255256Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2025-01-01T05:44:58.256049Z 0 [ERROR] [MY-010274] [Server] Could not open unix socket lock file /tmp/mysql.sock.lock.
2025-01-01T05:44:58.256060Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
2025-01-01T05:44:58.256068Z 0 [ERROR] [MY-010119] [Server] Aborting
2025-01-01T05:45:00.279788Z 0 [System] [MY-010910] [Server] /opt/homebrew/opt/mysql@8.0/bin/mysqld: Shutdown complete (mysqld 8.0.40) Homebrew.
2025-01-01T05:45:00.6NZ mysqld_safe mysqld from pid file /opt/homebrew/var/mysql/imjunsig-ui-MacBookAir.local.pid ended
어우 복잡해..
위에서 발견했던 mysql.sock.lock 파일을 열수가 없고 Unix Socket Lock 파일을 설정할 수 없다는 에러 로그를 확인했다!!
이 문제의 이유
- 파일 권한 문제
- tmp 디렉토리에 잘못된 락 파일이 남아있음.
% sudo rm -f /tmp/mysql.sock.lock //tmp파일에 있는 mysql.sock.lock파일을 제거
% sudo chmod 1777 /tmp //모든 사용자에게 읽기, 쓰기, 실행 권한을 부여한다.
그리고 다시 실행했다!!!!
결과는 실패....
드디어 해결!!
다 삭제하고 완전 재설치를 수행했다.
brew services stop mysql@8.0
brew uninstall --force mysql@8.0
sudo rm -rf /opt/homebrew/var/mysql
sudo rm -rf /opt/homebrew/etc/my.cnf
sudo rm -rf /usr/local/mysql sudo rm -rf /usr/local/mysql*
sudo rm -rf /usr/local/var/mysql
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/MySQL.prefPane
sudo rm -rf ~/Library/Preferences/com.oracle.mysql.plist
sudo rm -rf ~/Library/Application\ Support/MySQL
mysql --version
brew list | grep mysql
그리고 상태를 확인했다.
% brew services list
Name Status User File
mysql@8.0 stopped imjunsik ~/Library/LaunchAgents/homebrew.mxcl.mysql@8.0.plist
이전의 error 상태가 아닌 stopped상태로 변경된 걸 확인했다.
계속 오류가 발생을 했다. 파일에 접근하는 권한 문제일 수 있지만 /tmp 파일을 확인했을 때 /private/tmp파일로 접근되는걸 확인할 수 있었고 권한의 문제는 없었다.
그리고 /tmp파일을 확인했을 때
- mysql.sock
- mysql.sock.lock
파일이 있는 걸 확인할 수 있었다.
두개의 파일을 삭제하고 다시 실행을 했다!!! (두 파일은 재시작 할때 다시 생성된다)
sudo rm /tmp/mysql.sock.lock
sudo rm /tmp/mysqlx.sock.lock
그리고 재시작을 하니까!!!!!
/tmp % brew services start mysql@8.0
==> Successfully started `mysql@8.0` (label: homebrew.mxcl.mysql@8.0)
/tmp % brew services list
Name Status User File
mysql@8.0 started (유저이름) ~/Library/LaunchAgents/homebrew.mxcl.mysql@8.0.plist
그리고 접근을 하니까!!!!
/tmp % mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.40 Homebrew
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

드디어 성공한 화면을 볼 수 있었다!!!!!!
새롭게 알게된 부분
- mysql도 log를 확인해서 오류를 확인할 수 있다.
- 파일에 접근해서 재시작을 해서 구동이 가능한건지 확실하게 모르겠지만 tmp파일에 이전의 기록이 남아있으면 문제가 발생하는 거 같다.
- homebrew로 mysql을 설치하고 파일의 위치를 알 수 있었다.
다음에는 오류의 이유를 명확하게 알면 좋겠다.