본문 바로가기

WebHacking

WebHacking - (1.1)CentOS7 MySQL 초기 root 비밀번호 설정 오류

노트북을 포맷하게 되면서 다시 웹 서버를 구축하게 됐는데 CentOS7 에서 MySQL 설치 후에 root 계정으로 접속이 안되는 일이 발생했다.

원래는 초기 비밀번호는 없는 걸로 알고 있는데 아래와 같은 오류 메세지가 뜬다.

 

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

 

내가 알고있는 경우로는 mysql -u root -p 후 비밀번호 입력창이 나오면 그냥 엔터를 누르면 DB로 접속이 된다.

 

검색해보니 가끔가다 설치 하고나서 초기 비밀번호로 접속이 안되는 사람들이 꽤 있어서 따라해 봤는데 해도 안되는 것들이 많아서 삽질 좀 오래한거 같다.

 

해결 방법은 아래와 같다.

1. vi /etc/my.cnf 로 파일을 열어 아래 그림과 같이 [mysqld] 부분에 skip-grant-tables 명령어를 추가해준다. (22번 줄)

## [mysqld] 바로 밑에 명령어를 추가하게 되면 DB 재시작 과정에서 오류가 발생했었으니 그림과 같은 위치에 명령어를 추가해주자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
 
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
 
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
 
cs

2. systemctl restart mysqld 명령어로 DB를 재시작한다.

3. myslq -u root -p 명령어를 입력하면 바로 DB로 접속이 된다.

4. flush privileges 명령어를 입력한다.

5. ALTER USER 'root'@'localhost' IDENTIFIED BY '원하는 비밀번호'; 를 입력한다.

6. 다시 /etc/my.cnf 파일에 들어가 아까 추가해주었던 명령어인 skip-grant-tables 를 주석 처리하거나 삭제한다.

7. 마지막으로 변경된 설정이 적용 될 수 있도록 DB를 재시작한다.

 

이렇게 하게되면 방금 과정에서 설정한 비밀번호로 DB에 접속할 수 있다.