헤르메스 LIFE

[MySQL] root 패스워드 분실 본문

Database

[MySQL] root 패스워드 분실

헤르메스의날개 2020. 11. 18. 23:51
728x90

1 mysqld 중지

[root@localhost ~]# service mysqld stop

Stopping MySQL:                                            [  OK  ]

 

2 mysqld_safe 실행

인증 생략 옵션 + 안전모드로 데몬 실행

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

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

[root@localhost ~]# /usr/bin/mysqld_safe --skip-grant-tables &

[1] 32055

Starting mysqld daemon with databases from /var/lib/mysql

→ 이제 패스워드 없이 mysql에 접속할 수 있게 되었다.[1]

 

3 새 패스워드 지정

mysql 콘솔로 들어가자.[2]

/usr/bin/mysql -u root mysql

 

다음 SQL 명령어를 입력하여 원하는 패스워드로 변경한다.

 

MySQL 5.7.6 이상

ALTER USER 'root'@'localhost' IDENTIFIED BY '패스워드';

 

MySQL 5.7.5 이하

-- MySQL 5.6에서 성공 ★

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('패스워드');

 

-- MySQL 5.7 이상

UPDATE mysql.user SET authentication_string=PASSWORD('패스워드') WHERE user='root' AND Host='localhost';

FLUSH PRIVILEGES;

 

-- MySQL 5.7 미만

UPDATE mysql.user SET password=PASSWORD('패스워드') WHERE user='root' AND Host='localhost';

FLUSH PRIVILEGES;

 

 

실행 예시

[root@localhost ~]# /usr/bin/mysql -uroot mysql

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

 

 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.0.77 Source distribution

 

 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

 

mysql> update user set password=password('P@ssw0rd') where user='root';

Query OK, 3 rows affected (0.00 sec)

Rows matched: 3  Changed: 3  Warnings: 0

 

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

 

mysql> quit

Bye

 

4 mysqld 재시작

이제 패스워드를 입력해야만 mysql을 사용할 수 있도록 하기 위해, mysqld를 재시작한다.

[root@localhost ~]# service mysqld restart

STOPPING server from pid file /var/run/mysqld/mysqld.pid

120229 13:08:54  mysqld ended

 

 

Stopping MySQL:                                            [  OK  ]

Starting MySQL:                                            [  OK  ]

[1]+  Done                    /usr/bin/mysqld_safe --skip-grant

→ 백그라운드로 실행 중이던 무인증 안전모드 mysqld 프로세스가 중지되고 정상모드 mysqld로 재시작되었다.

728x90

'Database' 카테고리의 다른 글

[Tibero] 시작 / 종료  (0) 2020.11.19
[MySQL] 테이블 및 컬럼, 코멘트 조회  (0) 2020.11.18
[Oracle] 대량 Insert 예제  (0) 2020.11.18
[Oracle] IMP/EXP 시 에러 발생 (EXP-00008)  (0) 2020.11.18
[MariaDB] MariaDB Tool - DBeaver  (0) 2020.11.18