이전 포스트 [CentOS 7 minimal 설치 #3] MySQL 5.7 설치에서도 한번 언급을 했었습니다만, 이전 버전과 방식이 좀 바뀐것 같습니다.
MySQL 5.6 까지는 설치후 콘솔에서 바로 root 로 접속 해서 비밀번호를 변경할 수 있었지만 MySQL 7 (ver 5.7) 부터는 설치 되면서 랜덤스트링을 암호화 한 비밀번호가 자동으로 부여되고 그것을 찾아서 접속해서 원하는 비밀번호를 부여하는 방식으로 바뀐것 같습니다.
왜 그럴까... 생각해 보면 많은 분들이 MySQL을 설치하고 root 비밀번호를 관리 하지 않아서 외부로 부터 쉽게 칩입을 당하는 케이스들 때문에 변경 된 것이라고 생각합니다.
그런데 이런 것을 모르는 상태에서 MySQL 7 버전을 설치 했다가 root 접속을 못해서 난감했던 기억이 나네요.
일단 설치는 위에 올린 링크를 참고 하시고 (이 포스트와 같은 내용이 설치에도 나옵니다.) mysqld-safe --skip-grant-tables 명령을 대신헤서 5.7이상 버전에서는 비밀번호 분실시 어떻게 비밀번호를 변경하는지 확인해 보겠습니다.
MySQL 5.7 공식 설치 문서 (https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html)에 보시면 중간쯤에 설치후 superuser 계정 'root'@'localhost' 에 대한 접근 방법이 나옵니다.
shell> sudo grep 'temporary password' /var/log/mysqld.log
이렇게 나오는데요... 유닉스 명령이 익숙하지 않은 분들을 위해 설명하면... root 권한으로 설치 로그에서 'temporary password' 라는 문자열이 들어 있는 행을 표시하라는 내용 정도로 이해하시면 될 것같습니다.
shell> cat /var/log/mysql.log | grep 'temporary password'
이런 거랑 비슷한 명령 일 것 같네요.
설치후 임시 비밀번호 바꾸는 방법
저의 경우 설치 후 데몬을 가동후에 확인해 보니
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2018-02-13T14:49:45.720116Z 1 [Note] A temporary password is generated for root@localhost: tur++-dvf7tI
tur++-dvf7tI 이게 초기 비밀번호 입니다.
임시 비밀번호로 로그인 해서 비번을 바꾸는 내용입니다.
[root@localhost ~]# mysql -u root -p
Enter password : tur++-dvf7tI
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21
Copyright (c) 2000, 2018, 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> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
/* 임시 비번을 변경하기 전에는 아무것도 할 수가 없네요 */
/* 새로운 비밀번호로 변경 */
mysql> alter user 'root'@'localhost' identified by '새비밀번호';
Query OK, 0 rows affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
비밀번호가 잘 바뀌었는지 확인해 보도록 하겠습니다.
[root@localhost download]# mysql -u root -p
Enter password:변경한비밀번호
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
mysql> quit
Bye
잘 변경이 된 것 같습니다.
root 계정의 비밀번호를 분실 했을 경우 바꾸는 방법
끝으로 이전 버전에서 root 비밀번호를 분실 했을때 유용하게 이용되던 mysqld-safe 명령에 대해서 알아 보겠습니다.
MySQL 5.7버전을 설치 한 후에 저 명령을 아무리 찾아도 없었습니다. 구글링 끝에
https://stackoverflow.com/questions/33510184/change-mysql-root-password-on-centos7
이런 내용의 글을 보았습니다.
간단히 요약해 보면
1. mysqld-safe 명령은 사라졌음
2. user 테이블의 구조가 바뀌었음
그래서 아래의 순서에 따라서 비밀번호를 바꿀 수 있습니다.
1. MySQL 서버 중지
[root@localhost ~]# systemctl stop mysqld
2. MySQL 환경변수 설정
[root@localhost ~]# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
3. MySQL 서버 가동
[root@localhost ~]# systemctl start mysqld
4. 비번 없이 root 로그인
[root@localhost ~]# mysql -u root
5. root 비밀번호 업데이트
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('새비밀번호')
-> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit
6. MySQL 중지
[root@localhost ~]# systemctl stop mysqld
7. MySQL 환경변수 초기화
[root@localhost ~]# systemctl unset-environment MYSQLD_OPTS
8. MySQL 서버 시작
[root@localhost ~]# systemctl start mysqld
9. 새 비밀번호로 로그인 확인
[root@localhost ~]# mysql -u root -p
참고로 user 테이블이 변경 된것은 user 테이블의 접근을 쿼리가 아닌 시스템 명령으로 하면 큰 차이는 없을 것 같습니다.
MySQL 공식 문서에서도
alter user 'root'@'localhost' identified by '새비밀번호';
이런 형태를 권장 하는 것 같습니다.
유저 생성 및 비밀번호, 권한 부여는 [CentOS 7 minimal 설치] MySQL DB생성, User 생성 글을 참고 하시기 바랍니다.
'DataBase' 카테고리의 다른 글
CentOS7 에 MariaDB 지난 버전(10.4.27) 설치 (0) | 2022.12.02 |
---|---|
MySQL root 계정의 패스워드 분실 했을 경우 바꾸는 방법 (0) | 2020.11.25 |
[CentOS 7 minimal 설치] MySQL DB생성, User 생성 (0) | 2018.02.15 |
필드명명 규칙 (0) | 2016.11.18 |
[Oracle] 현재월의 마지막 ?요일의 날짜 구하기 (0) | 2016.10.23 |