[MySQL] mysql_config_editor를 이용한 자격정보 저장 (login-path)

2020. 10. 26. 18:19DB/MySQL

반응형
  1. mysql_config_editor?
  2. login-path 설정하기
    1. client login-path 설정하기
    2. login-path 설정하기
  3. login-path 조회
    1. 특정 login-path 조회
    2. login-path 전체 조회
  4. login-path 조회
    1. login-path의 인증정보 제거
    2. login-path 제거
  5. login-path 리셋

1. mysql_config_editor?

MySQL Server에 client 툴을 이용하여 접속할 때, username, password, host, port 등의 접속 정보를 입력해야합니다.

MySQL 서버에 접속할 때마다 이런 접속정보를 입력하는 것은 매우 번거롭고 귀찮은 일입니다. 이런 접속정보를 ssh처럼 별도의 구성파일에 저장하여 접속에 이용할 수 있다면 매우 편리할거 같습니다.

그 뿐만 아니라, 쉘 스크립트로 mysql 클라이언트를 이용하고자 할때, 스크립트 상에 비밀번호를 입력하는 것은 보안적으로도 매우 취약한 행위입니다.

이를 해소한 방법이 바로 mysql_config_editor를 이용한 login-path를 통한 로그인 방식입니다.

mysql_config_editor 는 MySQL 서버 연결에 대한 자격정보를 저장하는 유틸리티입니다.
이 자격정보는 현재 로그인 중인 계정의 홈디렉토리에 .mylogin.cnf라는 파일에 저장되며, "login-path" 라는 이름의 옵션 그룹에 난독화되어 저장됩니다.

저장되는 자격 정보에는 자격정보에는 login-path 이름, user, password, host, port, socket 정보가 난독화되어 들어있습니다.

설정한 login-path는 mysql, mysqladmin과 같은 MySQL client 에서 MySQL Server에 접속할 때 사용할 수 있습니다.

mysql --login-path=이름

설정한 login-path는 --login-path= 옵션을 이용하여 사용할 수 있으며, --login-path= 옵션을 생략할 경우 client 옵션그룹을 이용합니다.


2. login-path 설정하기

mysql_config_editor set [옵션]

login-path를 설정하는 명령어는 print입니다.
기존에 설정한 login-path를 업데이트할 때에도 set을 사용하면 됩니다.

2.1 client login-path 설정하기

login-path에 client 그룹을 추가할 경우, --login-path옵션을 생략하고 mysql client를 이용할 수 있습니다.

mysql_config_editor set -G client -h 호스트명 -u 계정명 -p
  • -G, --login-path
    • login-path 이름
    • default : client
  • -h, --host
    • 호스트명
    • default: localhost
  • -u, --user
    • mysql 계정명
    • default: 로그인 중인 계정명(CentOS 7)
  • -p, --password
    • mysql 비밀번호
  • -P, --port
    • 포트번호
    • default: 3306

host, port, user 정보는 설정하지 않을 경우 기본값으로 이용됩니다.


localhost에 jini계정으로 client login-path를 설정해 보겠습니다.

mysql_config_editor set -G client -u jini -p
Enter password:

client login-path를 설정한 후, mysql 명령어를 실행해봅시다.

[jini@apple ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7421
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> select current_user();
+----------------+
| current_user() |
+----------------+
| jini@localhost |
+----------------+

current_user() 함수로 현재 로그인 중인 계정이 jini@localhost인것도 확인할 수 있습니다.

2.2. login-path 설정하기

login-path는 여러개 설정 할 수 있습니다.

test계정에 대한 인증정보 설정을 test 라는 이름으로 login-path를 설정해봅시다.

mysql_config_editor set -G test -u test -h localhost -P 3306 -p
Enter password:

설정한 login-path를 이용하여 MySQL Server에 접속해봅시다

mysql --login-path=test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10026
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> select current_user();
+----------------+
| current_user() |
+----------------+
| test@localhost |
+----------------+
1 row in set (0.00 sec)

mysql 클라이언트에서 login-path 이름을 설정할 때에는 -G 옵션으로는 인식되지 않기 때문에 반드시 --login-path=이름과 같은 형태로 사용해야 한다는 점을 주의해야 합니다.


3. login-path 조회

mysql_config_editor print [옵션]

login-path를 조회하는 명령어는 print입니다.

3.1. 특정 login-path 조회

mysql_config_editor print -G 이름
  • -G, --login-path=
    • login-path 이름
    • default : client

login-path 이름을 설정하지 않을 경우 client login-path가 조회됩니다.

mysql_config_editor print -G test
[test]
user = test
password = *****
host = localhost
port = 3306

3.2. login-path 전체 조회

--all 옵션을 이용하면 설정된 login-path가 모두 조회됩니다.

mysql_config_editor print --all
[client]
user = jini
password = *****
[test]
user = test
password = *****
host = localhost
port = 3306

4. login-path 제거

4.1. login-path의 인증정보 제거

mysql_config_editor remove -G [이름] [옵션명]

login-path에 설정한 인증정보 중 일부를 지우고 싶다면 loginpath이름을 설정하고 제거하고 싶은 옵션을 쓰면 됩니다.

mysql_config_editor print -G test
[test]
user = test
password = *****
host = localhost
port = 3306
mysql_config_editor remove -G test -hPu
mysql_config_editor print -G test
[test]
password = *****

위의 예시에서는 test login-path에서 host, Port, user정보를 지운 결과입니다.


4.2. login-path 제거

mysql_config_editor remove -G [이름]

추가 옵션을 설정하지 않고 remove 명령을 실행할 경우, login-path가 제거됩니다.

client, test login-path가 저장된 상태에서 test login-path를 제거하는 예제를 함께 확인해봅시다.

mysql_config_editor print --all
[client]
user = jini
password = *****
[test]
password = *****
mysql_config_editor remove -G test
mysql_config_editor print --all
[client]
user = jini
password = *****

5. login-path 리셋

저장된 login-path 정보를 모두 지웁니다.

mysql_config_editor reset

++

  • MySQL login-path를 이용한 자격정보 저장
728x90
반응형