본문 바로가기
데이터베이스/MySQL

[MySQL] Mac Mysql Password reset

by ordinary_daisy 2024. 10. 8.

개요

  • mysql 서버에 가끔 접근하다 보면 password를 까먹어서 접근하기 어려운 경우가 발생한다. password를 틀리면 아래와 같은 오류가 발생하는데 Mac/Linux 환경에서 해결 방법을 알아보자.
  • error message: ERROR (28000): Access denied for user 'root'@'localhost' (using password: YES)

Mac Command

  1. mysql server 중지
    mysql.server stop

  2. 종료가 되지 않는 pid가 존재한다면 강제로 종료시켜주자.
    2-1) 경로 이동
    cd /opt/homebrew/var/mysql
    2-2) 폴더 삭제
    rm -rf ./pid
    2-3) 남아있는 pid 조회
    ps aux | grep mysql
    --------------------------
    user             28022   0.0  0.0 410886272    448 s004  T     3:07PM   0:00.02 mysql -u root
    user             29636   0.0  0.0 410742480   1600 s004  S+    3:20PM   0:00.00 grep mysql

    2-4) 조회된 pid 강제 종료
    sudo kill [pid]

  3. safe 모드로 서버 재시작
    mysqld_safe --skip-grant-tables &

  4. mysql 접속
    mysql -u root

  5. 비밀번호 null 값 갱신
    update mysql.user set authentication_string= null where user='root';

  6. 권한 적용
    flush privileges;
    exit;

  7. mysql 비밀번호 재설정
    mysql -u root
    alter user 'root'@'localhost' identified with mysql_native_password by '{new_password}';
    flush privileges;
    exit;

  8. 새롭게 설정한 비밀번호로 mysql 로그인 확인
    mysql -u root -p
    -> password 입력