MySQL 비밀번호 분실 시 변경 방법

MySQL 또는 MariaDB의 root 계정 비밀번호를 분실한 경우 mysql을 안전 모드로 실행하고 root 비밀번호를 변경하는 방법으로 대응할 수 있습니다. 다만 mysql 서비스를 중지하고 다시 시작해야 하기 때문에 서비스 중인 애플리케이션의 다운타임이 발생할 수 있는 점은 유의하셔야 합니다.

(사전 확인) OS root 계정으로 로그인 여부 확인

먼저 mysql root 계정의 비밀번호를 변경하기 전에 OS의 root 계정으로 비밀번호 없이 로그인이 가능한지 확인해봅니다. mysql 설치 후 root 비밀번호를 별도로 변경하지 않았다면 OS의 root 계정에선 비밀번호 없이 로그인이 가능합니다.

[root@localhost ~]# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

1. MySQL 서비스 중지

로그인이 되지 않는다면 어쩔 수 없이 비밀번호를 바꾸는 작업을 시작합니다.

[root@localhost ~]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

mysql 서비스를 중지합니다.

[root@localhost ~]# systemctl stop mariadb

2. 안전 모드로 MySQL 시작

mysqld_safe –skip-grant-tables & 명령어를 이용해서 안전모드로 mysql를 시작합니다.

[root@localhost ~]# mysqld_safe --skip-grant-tables &
[1] 3614
[root@localhost ~]# 240907 10:28:36 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
240907 10:28:36 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

mysqld 서비스가 올라온 것이 보입니다.

tcp6       0      0 :::3306                 :::*                    LISTEN      3730/mysqld

3. root 계정 비밀번호 없이 접속

안전모드에선 root 계정을 비밀번호 없이 로그인 할 수 있습니다.

[root@localhost ~]# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

flush privileges; 명령어로 사용자 권한 테이블을 갱신 합니다.

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

4. root 계정 비밀번호 변경

root 계정 비밀번호를 새로 설정합니다.

MariaDB [(none)]> alter user 'root'@'localhost' identified by 'new_password';
Query OK, 0 rows affected (0.000 sec)

5. MySQL 중지 / 시작

mysql 서비스를 중지합니다.

[root@localhost ~]# systemctl stop mariadb

그리고 다시 서비스를 시작합니다.

이때 에러가 발생하면서 서비스 시작에 실패할 수도 있습니다. 잘 실행되면 그대로 사용하시면 됩니다.

[root@localhost ~]# systemctl start mariadb
Job for mariadb.service failed because the control process exited with error code.
See "systemctl status mariadb.service" and "journalctl -xe" for details.

5-1. MySQL 시작에 실패한 경우

안전모드로 실행한 mysql 프로세스가 정상적으로 종료되지 않아서 그렇습니다. ps -ef | grep mysql 명령어로 mysql 프로세스를 확인해보면 /bin/sh /usr/bin/mysqld_safe 와 /usr/libexec/mysqld 두 개가 보입니다.

[root@localhost mysql]# ps -ef | grep mysql
root        3614    3243  0 10:28 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
mysql       3987    3614  0 10:35 pts/1    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mariadb/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock

PID를 확인해보면 /usr/libexec/mysqld의 부모 프로세스로 /bin/sh /usr/bin/mysqld_safe가 살아 있습니다. 순차적으로 kill -9 3614 와 kill -9 3987를 해서 프로세스를 모두 죽입니다.

mysql 관련 프로세스가 모두 정상 종료되면 실행에 성공할 것입니다.

[root@localhost ~]# systemctl start mariadb

6. MySQL root 계정 접속 확인

안전모드에서 변경한 root 비밀번호로 로그인이 되는지 확인합니다.

[root@localhost mysql]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

이제 mysql을 정상적으로 사용하시면 됩니다. root 계정에 로그인이 되면 다른 계정의 비밀번호도 문제 없이 바꿀 수 있습니다.

댓글 남기기