DB/mysql

MySQL root 계정 비밀번호 초기화

  • -

데이터베이스를 사용하다가 root 계정의 비밀번호를 분실하는 것은 정말 큰 일이다. 그나마 오라클의 경우 OS인증을 통해 좀 더 쉽게 처리할 수 있지만 MySQL은 갈길이 좀 멀다.

이 글은 윈도우용 MySQL 8.0.18 버전을 기준으로 작성되었다.

명령 프롬프트는 관리자 권한으로 사용한다.

 

일단 현재 작동중인 서비스를 중지시킨다.

C:\WINDOWS\system32>net stop MySql80 MySQL80 서비스를 멈춥니다... MySQL80 서비스를 잘 멈추었습니다.

 

기존에 windows에 install 형태로 설치했다면 datadir은 --datadir="C:/ProgramData/MySQL/MySQL Server 8.0/Data"이다. 이 부분을 본인의 Data 경로와 확인하자.

C:\WINDOWS\system32>mysqld --datadir="C:/ProgramData/MySQL/MySQL Server 8.0/Data" --console --skip-grant-tables --shared-memory 2020-08-20T13:39:10.331333Z 0 [System] [MY-010116] [Server] c:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.18) starting as process 35052 2020-08-20T13:39:11.099364Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. 2020-08-20T13:39:11.124690Z 0 [System] [MY-010931] [Server] c:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: ready for connections. Version: '8.0.18' socket: '' port: 0 MySQL Community Server - GPL. 2020-08-20T13:39:11.178260Z 0 [Warning] [MY-011311] [Server] Plugin mysqlx reported: 'All I/O interfaces are disabled, X Protocol won't be accessible'

이제 이 콘솔에서 MySql이 시작했으므로 별도의 새로운 콘솔을 이용해서 MySql 서버에 접속한다.

 

관리자 권한으로 새로운 명령 프롬프트를 실행하고 MySql 서버에 접속한다.

C:\WINDOWS\system32>mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 8.0.18 MySQL Community Server - GPL Copyright (c) 2000, 2019, 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>

여기서 주의할 점들이 나오는데..

일반적으로 alter user 'root'@'localhost' identified with mysql_native_password by '비밀번호'; 형태의 명령어를 사용하면 지금은 -skip-grant-tables 모드이므로 동작하지 않는다.

그렇다고 무작정 UPDATE user SET authentication_string='비밀번호' WHERE User='root'; 처럼 작성하면 해쉬화가 되지 않기 때문에 나중에 로그인 할 수가 없다.

따라서 일단 비밀 번호를 NULL 로 만들어둔다.

mysql> use mysql; Database changed mysql> UPDATE user SET authentication_string=null WHERE User='root'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select authentication_string from user; +------------------------------------------------------------------------+ | authentication_string | +------------------------------------------------------------------------+ | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | NULL | +------------------------------------------------------------------------+ 4 rows in set (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec)

 

기존 접속을 종료하고 다시 비번 없이 root로 접속한 후 Alter 명령으로 비빌번호를 변경한다.

C:\WINDOWS\system32>mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.18 Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '1234'; Query OK, 0 rows affected (0.01 sec) mysql> use mysql Database changed mysql> select authentication_string from user; +------------------------------------------------------------------------+ | authentication_string | +------------------------------------------------------------------------+ | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | $A$005$E;3PUIUjl"Y6Y@Q-fe])8w1c7ojurnUNrJfMnFYdX2r4AVnPXtLX4/A6RJSZrW3 | +------------------------------------------------------------------------+ 4 rows in set (0.00 sec) mysql>

다시 authentication_string을 조회해보면 해쉬화된 값이 저장됨을 확인할 수 있다. 드디어 비빌번호가 변경된 것이다. 이제 root/1234로 접속이 가능하다.

이제 mysql을 정상적으로 시작한다. 즉 기존의 -skip-grant-tables 모드를 종료시키고 mysql 서비스를 시작한다.

C:\WINDOWS\system32>net start mysql80 MySQL80 서비스를 시작합니다.. MySQL80 서비스가 잘 시작되었습니다.

 

비밀번호는 잊어먹지 말자!!!

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.