Linux Server

[Linux/Ubuntu]MySQL 사용자 계정 추가하기

톰아저씨의 오두막 2022. 1. 12. 13:13

[Linux/Ubuntu]MySQL 사용자 계정 추가하기

[Linux/Ubuntu] Adding MySQL User Accounts

 

 

작업 환경

Server Version : Ubuntu 18.04, Ubuntu 20.04
php Version 7.2
Apache/2.4.29 (Ubuntu)
MySql Version 5.7.36

 

개요

MySQL을 설치하고 실제로 데이터베이스를 운영하다 보면 데이터베이스 별로 권한에 맞는 사용자 계정이 필요합니다. 즉 'root' 계정이 아닌 별도의 개별적인 권한을 가진 사용자 계정이 있어야 보안관리가 가능합니다.

 

이번 글에는 MySQL에서 사용자 계정을 추가하고 권한을 부여하는 방법에 대하여 소개합니다.

 

MySQL Database 생성

 

ⓐMySQL에 사용자 계정을 추가하기 전에 사용자 계정을 사용할 데이터베이스를 생성해 보겠습니다.  'root' 계정으로 MySQL에 접속합니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root@uncletom:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.7.36-0ubuntu0.18.04.1 (Ubuntu)
 
Copyright (c) 20002021, Oracle and/or its affiliates.
 
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>

 

ⓑ현재 데이터베이스 목록을 조회합니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
 
mysql>

 

ⓒ이제 새로운 데이터베이스 계정 'testdb'를 추가하기 위해 쿼리를 입력 후 실행을 합니다. 

 

mysql> create database testdb;

 

실행한 후 다시 데이터베이스 목록을 조회해 보면 새로운 데이터베이스('testdb') 하나가 추가된 것을 확인할 수 있습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)
 
mysql>

 

MySQL 사용자 계정 추가하기

 

ⓐMySQL 에 사용할 신규 사용자 계정을 추가하기 전에 먼저 현재 등록된 사용자 계정 정보를 확인해 보도록 하겠습니다. 

 

mysql> SELECT User, Host FROM mysql.user;

 

1
2
3
4
5
6
7
8
9
10
11
12
mysql> SELECT User, Host FROM mysql.user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
 
mysql>

 

ⓑ신규 사용자 계정을 추가하기 위해 다음의 쿼리를 입력 후 실행합니다. 사용자 ID는 'testuser', 패스워드는 'TestPassword~1' 로 부여합니다. 패스워드 정책을 'MEDIUM'으로 설정하였으므로 대소문자, 숫자, 특수문자를 모두 사용해야 합니다.

mysql> CREATE USER 'testuser'@'%' IDENTIFIED BY 'TestPassword~1';

 

사용자 계정 추가를 위한 쿼리를 실행한 후 사용자 계정 정보를 다시 확인해 봅니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> CREATE USER 'testuser'@'%' IDENTIFIED BY 'TestPassword~1';
Query OK, 0 rows affected (0.08 sec)
 
mysql> SELECT User, Host FROM mysql.user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| testuser         | %         |
| debian-sys-maint | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)
 
mysql>

 

참고로 @ 다음의 '%' 를 입력하면 외부 시스템에서 접근이 가능하고 'localhost'로 입력하면 MySQL 이 설치된 현재 시스템에서만 접근이 가능합니다. 여기서 사용자 계정 'testuser'는 외부 시스템에서 접근이 허용된 ID로 생성되었습니다.

 

ⓒ신규 생성된 사용자 계정에 권한을 부여합니다.

사용자 계정 권한은 특정 데이터베이스만 사용이 가능하도록 권한을 부여하거나 'root' 계정처럼 모든 권한을 부여할 수 있습니다.

 

특정 데이터베이스(testdb)만 사용이 가능하도록 권한을 부여하는 경우 다음과 같이 쿼리를 실행합니다.

mysql> grant all privileges on testdb.* to 'testuser'@'%';
mysql> flush privileges;

 

1
2
3
4
5
6
7
mysql> grant all privileges on testdb.* to 'testuser'@'%';
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql>

 

MySQL을 종료하고 다시 신규로 생성된 사용자 계정인 'testuser'로 접속해서 데이터베이스를 조회해 보면 특정 데이터베이스 'testdb' 만 사용이 가능한 것을 확인할 수 있습니다.

 

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
mysql> exit
Bye
root@uncletom:~# mysql -u testuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.36-0ubuntu0.18.04.1 (Ubuntu)
 
Copyright (c) 20002021, Oracle and/or its affiliates.
 
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| testdb             |
+--------------------+
2 rows in set (0.00 sec)
 
mysql>
 

 

반면에 모든 데이터베이스를 사용할 수 있도록 권한을 부여하고자 하는 경우에는 다음과 같이 쿼리를 실행합니다. 물론 MySQL을 종료한 후 최고 관리자 권한인 'root' 계정으로 다시 접속하여 작업을 진행하여야 합니다.

mysql> grant all privileges on *.* to 'testuser'@'%';
mysql> flush privileges;

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@uncletom:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.7.36-0ubuntu0.18.04.1 (Ubuntu)
 
Copyright (c) 20002021, Oracle and/or its affiliates.
 
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> grant all privileges on *.* to 'testuser'@'%';
Query OK, 0 rows affected (0.01 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql>
 

 

다시 사용자 계정인 'testuser'로 접속해서 데이터베이스를 조회해 보면 모든 데이터베이스에 대하여 사용이 가능한 권한을 부여받은 것을 확인할 수 있습니다.

 

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
29
30
mysql> exit
Bye
 
root@uncletom:~# mysql -u testuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.36-0ubuntu0.18.04.1 (Ubuntu)
 
Copyright (c) 20002021, Oracle and/or its affiliates.
 
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)
 
mysql>

 

이상으로 MySQL 사용자 계정을 추가하는 방법에 대해서 소개 드렸습니다.

오늘도 즐거운 하루여~