QUERY. 사용자 생성 및 권한 추가하기

2023. 4. 30. 08:08데이터베이스/MYSQL


Create User

 

CREATE USER '사용자' IDENTIFIED BY '패스워드' COMMENT '설명';

사용자는 '이름'@'주소'처럼 한정할 수도 있다.

 

 

 

 

 

 


Grant User

 

그냥 지정해서 줘도 되지만, 직접 role을 만들어 지정해주는 것이 좋다.

CREATE ROLE 이름;
GRANT ALL PRIVILEGES ON 데이터베이스.테이블 TO 사용자(또는 롤);

 

만든 role을 사용자에게 부여하고 싶다면

GRANT '롤 이름' TO '사용자';
SET DEFAULT ROLE ALL TO '사용자';

 

확인은 해당 사용자로 접속하여 아래 쿼리를 실행하면 된다.

SELECT CURRENT_ROLE();

 

 

 

표현식

GRANT
    priv_type [(column_list)]
      [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    TO user_or_role [, user_or_role] ...
    [WITH GRANT OPTION]
    [AS user
        [WITH ROLE
            DEFAULT
          | NONE
          | ALL
          | ALL EXCEPT role [, role ] ...
          | role [, role ] ...
        ]
    ]
}

GRANT PROXY ON user_or_role
    TO user_or_role [, user_or_role] ...
    [WITH GRANT OPTION]

GRANT role [, role] ...
    TO user_or_role [, user_or_role] ...
    [WITH ADMIN OPTION]

object_type: {
    TABLE
  | FUNCTION
  | PROCEDURE
}

priv_level: {
    *
  | *.*
  | db_name.*
  | db_name.tbl_name
  | tbl_name
  | db_name.routine_name
}

user_or_role: {
    user (see Section 6.2.4, “Specifying Account Names”)
  | role (see Section 6.2.5, “Specifying Role Names”)
}

 

 

 

 

 


Remote Access

 

여담으로 외부에서 데이터베이스에 연결하고자 할 경우 특정 수준까지 ip와 port를 열어주어야 한다.

 

1. mysqld.cnf 찾기 (본인의 경우는 /etc/mysql/mysql.conf.d/mysqld.cnf 였음)

2. 위 파일을 열어 bind-address에 열고자 하는 수준까지 입력

3. mysql 서버 다시 시작

sudo systemctl restart mysql

4. port 열기

sudo ufw allow 3306/tcp​

5. 사용자 확인 후 업데이트하기

SELECT host, user FROM mysql.user;
UPDATE mysql.user SET host='사용자 ip' WHERE user='사용자 이름';

 

 

 

 

 


References

 

MySQL :: MySQL 8.0 Reference Manual :: 13.7.1.3 CREATE USER Statement

13.7.1.3 CREATE USER Statement CREATE USER [IF NOT EXISTS] user [auth_option] [, user [auth_option]] ... DEFAULT ROLE role [, role ] ... [REQUIRE {NONE | tls_option [[AND] tls_option] ...}] [WITH resource_option [resource_option] ...] [password_option | l

dev.mysql.com

 

MySQL :: MySQL 8.0 Reference Manual :: 13.7.1.6 GRANT Statement

13.7.1.6 GRANT Statement GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level TO user_or_role [, user_or_role] ... [WITH GRANT OPTION] [AS user [WITH ROLE DEFAULT | NONE | ALL | ALL EXCEPT role [, role ] ... | role

dev.mysql.com

 

MySQL :: MySQL 8.0 Reference Manual :: 13.7.1.2 CREATE ROLE Statement

13.7.1.2 CREATE ROLE Statement CREATE ROLE [IF NOT EXISTS] role [, role ] ... CREATE ROLE creates one or more roles, which are named collections of privileges. To use this statement, you must have the global CREATE ROLE or CREATE USER privilege. When the

dev.mysql.com

 

MySQL :: MySQL 8.0 Reference Manual :: 6.2.10 Using Roles

A MySQL role is a named collection of privileges. Like user accounts, roles can have privileges granted to and revoked from them. A user account can be granted roles, which grants to the account the privileges associated with each role. This enables assign

dev.mysql.com

 

'데이터베이스 > MYSQL' 카테고리의 다른 글

QUERY. 데이터베이스 생성  (0) 2023.04.30