MySQL 비밀번호를 분실했을 때 어떻게 복구하나요?
💡 요약 정리
- MySQL 루트 비밀번호를 잃어버렸다면, 보안 권한 검사를 무시하는
--skip-grant방식으로 데몬을 재시작해야 합니다. - 이후, mysql 콘솔에 접속하여
update쿼리를 사용해 루트 계정의 패스워드를 수동으로 재설정할 수 있습니다. - 이 과정 후에는 반드시
FLUSH PRIVILEGES와 재시작을 통해 변경사항을 적용해야 합니다.
1. MySQL 데몬 중지
[root@localhost ~]# /etc/init.d/mysqld stop
Shutting down MySQL. [ OK ]
2. --skip-grant 옵션으로 MySQL 데몬 실행
[root@localhost ~]# /etc/init.d/mysqld start --skip-grant
Starting MySQL. [ OK ]
또는 다른 실행 방법:
[root@localhost ~]# /home/mysql/bin/mysqld_safe --skip-grant &
3. 비밀번호 없이 MySQL 접속 후 비밀번호 변경
주의:--skip-grant 방식에서는 mysqladmin이나 SET PASSWORD 문법을 사용하여 비밀번호를 변경할 수 없습니다. update 쿼리만 사용 가능합니다.
[root@localhost ~]# /home/mysql/bin/mysql -u root
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.5.28-log Source distribution
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
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> update user set password=password('1234!@#$') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
4. 권한 테이블 적용 및 MySQL 데몬 재시작
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.05 sec)
mysql> quit
Bye
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL. [ OK ]
Starting MySQL. [ OK ]
5. 변경한 비밀번호로 MySQL 접속 테스트
[root@localhost ~]# /home/mysql/bin/mysql -u root -p
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.5.28-log Source distribution
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
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>