mysql user 설정

mhlee·2021년 2월 15일

mysql user 설정

0. 배경

권한 설정은 아주 중요하다.
데이터가 저장되는 DBMS에서의 권한 설정은 두말하면 잔소리!

예를 들어 많이 사용하는 springboot jpa의 예를 들어보자.
test에서는 ddl-auto를 통해서 drop, create를 반복한다.
하지만, production에 drop, create가 발생하면 재앙!!이다.
그것도 돌이킬수 없는 대재앙!!

일반적으로 application은 crud, test application은 crud + create/drop 권한을 가지면 된다.
또, 개발팀(사람)에서는 디폴트로 select권한만 가지고 필요시에만 insert/update/delete를 수행하면 된다.

빠르게 user 설정을 알아보자!!

1. user 생성

create user '<user-name>'@'<allow-ip>' identified by '<password>';

//localhost에서만 접속가능한 user1을 생성
create user 'user1'@'localhost' identified by 'hello1!';

//192.168.0.*에서만 접속가능한 user1을 생성
create user 'user1'@'192.168.0.%' identified by 'hello1!';

//아무곳에서나 접속가능한 user1을 생성
create user 'user1'@'%' identified by 'hello1!';

2. user 삭제

drop user '<user-name>'@'<allow-ip>';

//localhost에서만 접속가능한 user1을 삭제.
create user 'user1'@'localhost' identified by 'hello1!';

3. 권한 부여

grant <privileges> on <db-name>.<table-name> to '<user-name>'@'<allow-ip>';

grant 항목들은 공식문서를 참조하자.

//user1에게 test_db의 모든 테이블에 대해 모든권한을 부여.
grant all on test_db.* to 'user1'@'localhost';

//user1에게 test_db의 모든 테이블에 대해 insert/select/update/delete 권한을 부여.
grant insert,select,update,delete on test_db.* to 'user1'@'localhost';

권한 부여후 바로 반영을 원할경우, 아래 명령을 수행해야 한다.
단, 현재 접속중인 세션에는 반영이 되지 않고, 이후 접속한 세션에 대해서 정책이 반영된다.

flush privileges;

4. 권한 제거

revoke <privileges> on <db-name>.<table-name> from '<user-name>'@'<allow-ip>';

//user1에게 test_db의 모든 테이블의 무든 권한을 제거.
revoke all on test_db.* from 'user1'@'localhost';

//user1에게 test_db의 모든 테이블에 대해 update/delete 권한을 제거.
revoke update,delete on test_db.* from 'user1'@'localhost';

5. grant 부여 내용 확인

show grants for <user-name>'@'<allow-ip>';

//'select_only'@'%'의 grant 내역을 확인
show grants for 'select_only'@'%';
profile
삽질하는 개발자

0개의 댓글