카테고리 없음

MySQL root 계정 비밀번호 분실 시 해결방법

개발자 염상진 2022. 10. 14. 23:58

 

mysql 접속 계정 분실 해결방법

 

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

 

 

 

mysql 데몬을 중지하고 비밀번호 입력 없이 접속 옵션 설정

$ sudo service mysql stop 

// 둘 중에 하나만 입력하면 됨

$ /usr/bin/mysqld_safe --skip-grant &

$ sudo mysqld_safe --skip-grant-tables &

 

여기서 또 에러 발생

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

 

/var/run/mysqld/mysqld.sock 파일이 없다고 에러가 발생함. 

 

직접 파일 생성 해줌

$ mkdir -p /var/run/mysqld
$ chown mysql:mysql /var/run/mysqld

 

다시 안전모드로 실행

/usr/bin/mysqld_safe --skip-grant &
/usr/bin/mysqld_safe --skip-grant-tables &

 

mysql 접속 시도

$ /usr/bin/mysql -u root mysql

 

정상적으로 접속 됨

 

 

 

DB에 접속해서 root 계정 비밀번호 변경하니 안전모드에서는 변경안된다는 에러 발생

 

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

mysql 데몬 다시 켜서 접속한다.

정상 작동하다가 위와 같은 다시 에러발생

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

 

아예 PC 종료 후 다시 시도

$ sudo mysqld_safe --skip-grant-tables

2022-10-14T15:08:36.397319Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2022-10-14T15:08:36.427180Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

안전모드로 MySQL 데몬이 돌아가고

다시 MySQL 접속하면 성공적으로 접속 된다. 1시간만에 드디어 접속 성공

➜  mysql mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.30-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2022, 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> show databases;

 

root 비밀번호 변경하면 다시 에러가 발생함

 

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

$ FLUSH PRIVILEGES;

 

명령을 입력해주면 권한 관련 설정이 리로드 되면서 정상 반영된다. 비밀번호를 변경하면 정상적으로 변경완료되고, 데몬을 다시 실행해서 mysql 접속하면 된다. 

여기까지 오는데 2시간 넘게걸림;; MySQL 비밀번호는 웬만하면 잊어버리지 맙시다. 최악의 경우 MySQL 다시 설치하면서 데이터 파일 날아갈 수도 있으니 백업은 필수 🔨

 

🚀️ 도움이 되셨다면 구독좋아요 부탁드립니다 👍️