본문 바로가기

sql

mysql user 생성 및 roll 권한 부여

#@localhost는 내부접근  @"%" 외부 접근 허용이고 ""를 붙이는건 상관없다. 

create user test2@localhost identified by "user1234";
create user "test3"@"%" identified by "1234";v

user 생성 후 권한 주기  all privileges on은 모든 권한을 주는것 

mysql 8.0 이후부터는 생성하면서 권한을 주지 못함 

create user "test4"@"localhost" identified by "1234";
grant all privileges on *.* to "test4"@"localhost" with grant option;
#권한 해제
revoke grant option on *.* from "test4"@"localhost";

roll 생성 해서 권한 주기 

#roll 생성 
CREATE ROLE 'app_read', 'app_write';
#roll에 권한 부여 
GRANT SELECT ON stock.* TO 'app_read';
GRANT INSERT, UPDATE, DELETE ON stock.* TO 'app_write';
#test1 유저에 roll 뷰여 
GRANT 'app_read' TO test1@localhost;
GRANT 'app_read','app_write' to 'test2'@'localhost';

#권한보기 
SHOW GRANTS FOR 'app_read'@'%';
SHOW GRANTS FOR 'test2'@'localhost'; 
SHOW GRANTS FOR 'test2'@'localhost' USING 'app_read'; 

select current_role();
#root로 접속하여 DEFALUT 활성화 셋팅 및 ROLE 활성화 계정 설정
SET DEFAULT ROLE ALL TO
'test2'@'localhost',
'test1'@'localhost';