728x90
반응형

Mysql 사용자 생성 및 권한을 부여하는 방법에 대해 간단하게 정리합니다. 

Mysql8 기준으로 작성되어  있습니다. 

Mysql 기본 정보 확인

유저를 생성하기에 앞서 우선 Mysql 기본 정보를 확인 합니다. 

제가 테스트하는 Mysql 은 도커로 설치되었습니다. 

설치 방법은 Mysql 도커 설치 글을 참고 하세요 

root 접속 (mysql -u root  -p --host 127.0.0.1)

root@24a15adb0cb3:/# mysql -u root  -p --host 127.0.0.1        
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1938
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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.

Database 생성 삭제 및 변경 

#database 생성 
create database book_db;

#utf8 로 database 생성 
create database book_db character set utf8mb4 collate utf8mb4_general_ci;

#database 삭제
drop database book_db ;


SET character_set_client = utf8mb4 ;
SET character_set_results = utf8mb4 ;
SET character_set_connection = utf8mb4 ;
ALTER DATABASE [DB명] DEFAULT CHARACTER SET utf8mb4 ;

 

mysql> drop database book_db ;
Query OK, 0 rows affected (0.05 sec)

mysql> create database book_db;
Query OK, 1 row affected (0.00 sec)

mysql>  drop database book_db ;
Query OK, 0 rows affected (0.00 sec)

mysql>  create database book_db character set utf8mb4 collate utf8mb4_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| book_db            |
| information_schema |
| mysql              |
| mysql8             |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

mysql database 선택(use mysql)

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

유저정보 확인 (select user, host from user;)

  • 현재 생성되어 있는 유저 정보를 확인합니다. 
mysql> select user, host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| bookadmin        | %         |
| devuser1         | %         |
| root             | %         |
| testuser         | %         |
| devuser1         | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
9 rows in set (0.00 sec)

데이터베이스 정보 확인(show databases;)

  • Mysql에 생성된 database 정보를 확인합니다. 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mysql8             |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

 

반응형

 

유저 생성 및 권한 추가 삭제

  • 유저를 생성하기 위한 기본 정보가 확인 되었으므로 이제 유저를 생성해 보겠습니다. 

유저 생성 및 권한 부여 

  • 'localhost' 대신 '%' 을 사용할 경우 외부에서도 접속 가능합니다.
# create user '[유저명]'@'[접속허용IP]' identified by '[비밀번호]';
create user 'manager'@'localhost' identified by 'test123';

grant all privileges on [권한 허용 database].* to manager@'[접속허용IP]';
grant all privileges on mysql8.* to manager@'localhost';

 

mysql> create user 'manager'@'localhost' identified by 'test123';
Query OK, 0 rows affected (0.20 sec)

mysql> grant all privileges on mysql8.* to manager@'localhost';
Query OK, 0 rows affected, 1 warning (0.04 sec)

 

유저 정보 변경 

mysql> alter user  manager@'%' identified with mysql_native_password by 'test123';
Query OK, 0 rows affected (0.01 sec)

 

부여된 권한 정보 확인( SHOW GRANTS FOR manager@localhost;)

mysql> SHOW GRANTS FOR manager@localhost;
+-------------------------------------------------------------+
| Grants for manager@localhost                                |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO `manager`@`localhost`                 |
| GRANT ALL PRIVILEGES ON `mysql8`.* TO `manager`@`localhost` |
+-------------------------------------------------------------+
2 rows in set (0.00 sec)

생성된 계정에 접속 

mysql -u manager  -p                 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1943
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> 

테이블 생성

databasee 와 유저 생성에 대해 알아보았습니다. 

이제 해당 databse 에 table 생성해 보겠습니다. 

 

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| book_db            |
| information_schema |
| mysql              |
| mysql8             |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)
mysql> show tables;
+-------------------+
| Tables_in_book_db |
+-------------------+
| book              |
+-------------------+
1 row in set (0.00 sec)
  • book_db database 에 book 테이블 을 만들고 bookadmin 유저에 권한을 부여하는 명령입니다. 
CREATE TABLE `book` (
 `book_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
 `title` VARCHAR(200) NOT NULL,
 `category` VARCHAR(200) NOT NULL DEFAULT '',
 `price` INT NULL,
 `insert_date` DATETIME NOT NULL DEFAULT NOW(),
 PRIMARY KEY (`book_id`)
);

GRANT EXECUTE, SELECT, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, INDEX, INSERT, REFERENCES, TRIGGER, UPDATE, LOCK TABLES  ON `book_db`.* TO 'bookadmin'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
mysql> CREATE TABLE `book` (
    ->  `book_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  `title` VARCHAR(200) NOT NULL,
    ->  `category` VARCHAR(200) NOT NULL DEFAULT '',
    ->  `price` INT NULL,
    ->  `insert_date` DATETIME NOT NULL DEFAULT NOW(),
    ->  PRIMARY KEY (`book_id`)
    -> );
Query OK, 0 rows affected (0.04 sec)

mysql> 
mysql> GRANT EXECUTE, SELECT, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, INDEX, INSERT, REFERENCES, TRIGGER, UPDATE, LOCK TABLES  ON `book_db`.* TO 'bookadmin'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
728x90
반응형

'05.DB > Mysql' 카테고리의 다른 글

Mysql 실행 SQL 확인 방법  (0) 2023.03.07
[Mysql] DBeaver Mysql 접속  (0) 2022.04.19

+ Recent posts