권한 설정은 아주 중요하다.
데이터가 저장되는 DBMS에서의 권한 설정은 두말하면 잔소리!
예를 들어 많이 사용하는 springboot jpa의 예를 들어보자.
test에서는 ddl-auto를 통해서 drop, create를 반복한다.
하지만, production에 drop, create가 발생하면 재앙!!이다.
그것도 돌이킬수 없는 대재앙!!
일반적으로 application은 crud, test application은 crud + create/drop 권한을 가지면 된다.
또, 개발팀(사람)에서는 디폴트로 select권한만 가지고 필요시에만 insert/update/delete를 수행하면 된다.
빠르게 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!';
drop user '<user-name>'@'<allow-ip>';
//localhost에서만 접속가능한 user1을 삭제.
create user 'user1'@'localhost' identified by 'hello1!';
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;
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';
show grants for <user-name>'@'<allow-ip>';
//'select_only'@'%'의 grant 내역을 확인
show grants for 'select_only'@'%';