이전 편에서 CentOS 7에 MySQL 5.7을 설치 하는 법을 살펴 보았습니다.

MySQL 5.7에서 User 테이블에 구조가 변경되었다는 언급이 있었는데요 어떻게 바뀌었는지는 살펴보지는 않겠습니다.

MySQL 시스템 명령어로  datebase와 user을 생성하면 실제 테이블 구조와는 관계 없이 작업이 가능합니다.

USER 테이블에 Insert 쿼리 문장을 만들어서 사용자를 생성했던 분들은 MySQL 버전이 올라가면서 바뀌면서 테이블 구조가 약간씩 달라지고 하는 통에 애를 먹었을 것입니다.

오라클에서 처럼 문장으로 만들어진 명령어로 사용할 DB와 사용자를 생성해 보도록 하겠습니다.

 

Character set  확인

요즘은 대부분 UTF-8이 표준이기 때문에 나중에 골치 아픈 문제를 만나기 전에 미리 설정을 확인하고 넘어가겠습니다.

[root@localhost ~]# mysql -uroot -p

Enter password:

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

Your MySQL connection id is 2

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> status;

--------------

mysql  Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using  EditLine wrapper

 

Connection id:          2

Current database:

Current user:           root@localhost

SSL:                    Not in use

Current pager:          stdout

Using outfile:          ''

Using delimiter:        ;

Server version:         5.7.21 MySQL Community Server (GPL)

Protocol version:       10

Connection:             Localhost via UNIX socket

Server characterset:    utf8

Db     characterset:    utf8

Client characterset:    utf8

Conn.  characterset:    utf8

UNIX socket:            /var/lib/mysql/mysql.sock

Uptime:                 22 sec

 

Threads: 1  Questions: 5  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.227

--------------

 

mysql>

 

붉은색 표시한 부분이  utf8로 되어 있는지 확인합니다.

만일 latin1 같은 형식이라면 /etc/my.cnf 에 아래 두줄을 추가 한 후  MySQL 데몬을 재시작 합니다.(systemctl restart mysqld)

[mysqld]

collation-server = utf8_unicode_ci

character-set-server = utf8

... 생략 ...

한글 설정이 확실히 정해지지 않은 상태에서 database를 생성하는 경우 나중에 테이블에 한글 입력 등의 문제가 봉착 될 수 있으니 반드시 확실하게 utf8로 지정이 되었는지 확인하고 넘어가도록 합니다.

다음은 WEBService 라는 DataBase를 생성해 보겠습니다.

[root@localhost ~]# mysql -u root -p

Enter password:

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

Your MySQL connection id is 5

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> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

5 rows in set (0.00 sec)

 

mysql> create database WEBService ;

Query OK, 1 row affected (0.05 sec)

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| WEBService          |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

5 rows in set (0.00 sec)

 

 

coludnine 라는 사용자를 추가 하도록 하겠습니다.

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> create user 'cloudnine'@'localhost' identified by '사용할비밀번호';

Query OK, 0 rows affected (0.00 sec)

 

mysql> create user 'cloudnine'@'%' identified by '사용할비밀번호';

Query OK, 0 rows affected (0.00 sec)

 

mysql> select host, user, password from user;

ERROR 1054 (42S22): Unknown column 'password' in 'field list'

※ mysql 구버전에서 사용하던 password 컬럼은 authentication_string 으로 이름이 바뀌었음

mysql> select host, user, authentication_string from user;

+-----------+---------------+-------------------------------------------+

| host      | user          | authentication_string                     |

+-----------+---------------+-------------------------------------------+

| localhost | root          | *암호화된비밀번호문자열 |

| localhost | mysql.session | *암호화된비밀번호문자열 |

| localhost | mysql.sys     | *암호화된비밀번호문자열 |

| localhost | cloudnine      | *암호화된비밀번호문자열 |

| %         | cloudnine      | *암호화된비밀번호문자열 |

+-----------+---------------+-------------------------------------------+

mysql 특성상 user가 같아도 host가 다르면 별개의 사용자로 간주합니다.

위의 예에서 하나는 localhost용 이고 또 하나는 외부 모든 IP로 부터 접속을 허용한다는 의미입니다.

보안상 문제가 있을 수 있지만 우리는 방화벽에서 외부 접속을 엄격히 차단 할 것이므로 크게 상관은 없어 보입니다.

만일 특정 IP에서만 접속을 허용하고 싶다면 '%' 대신 IP주소를 입력하면 됩니다.

 

위에서 User테이블의 스키마가 약간 바뀐것 같다고 말씀드렸었는데 바로 password 컬럼이 authentication_string 컬럼으로 변경된것 같습니다.

user 테이블을 조회 해 보면 계정이 잘 생성 되었습니다.

 

이제 생성한 user에 database 사용권한을 부여하고 잘 접속 되는지 확인 하도록 하겠습니다.

mysql> grant all privileges on WEBService.* to 'cloudnine'@'localhost';

Query OK, 0 rows affected (0.00 sec)

 

mysql>  grant all privileges on WEBService.* to 'cloudnine'@'%';

Query OK, 0 rows affected (0.00 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

mysql> quit

Bye

[root@localhost ~]# mysql -u cloudnine-p

Enter password:

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

Your MySQL connection id is 8

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 WEBService

Database changed

mysql> show tables;

Empty set (0.01 sec)

 

mysql> quit

 

권한 역시 localhost용과 외부접속용 두가지 모두  부여합니다.

 

 

+ Recent posts