MySQL 사용자 조회, 생성, 제거, 권한부여

2024. 9. 20. 11:25 Database/mysql

root 계정 로그인

mysql -u root -p



MySQL 사용자(user) 조회

MySQL의 사용자 목록을 조회하기 위해 MySQL의 기본 스키마인 mysql안에 user 테이블에서 아래와 같은 명령어를 통해 조회할 수 있다.

use mysql;

select user, host from user;	# 사용자 목록 조회



사용자 생성

사용자 생성시에는 create 명령어를 사용해서 사용자를 추가할 수 있다. 내부 접근만 가능하도록 만들기 위해 host에 localhost를 넣어줬다.

create user '사용자'@'host' identified by '비밀번호';

# ex) 내부 접근을 허용하는 사용자 추가
create user 'test'@'localhost' identified by '0000';



사용자에 권한 부여

grant 명령어를 통해 사용자에게 권한을 부여할 수 있다.

# 전체 DB에 전체 권한 추가
grant all on *.* to test@localhost;


# 전체 DB에 대한 select, insert 권한 추가
grant select, insert on *.* to test@localhost;


# 특정 DB(mydb)에 대한 전체 권한 추가
grant all on mydb.* to test@localhost;


# 특정 DB(mydb)에 대한 select, insert 권한 추가
grant select, insert on mydb.* to test@localhost;


# 특정 DB(mydb)에 포함된 특정 Table(mytb)대한 전체 권한 추가
grant all on mydb.mytb to test@localhost;


# 특정 DB(mydb)에 포함된 특정 Table(mytb)에 대한 select, insert 권한 추가
grant select, insert on mydb.mytb to test@localhost;



사용자 생성과 권한 부여 한번에 하기

grant all privileges on *.* to '사용자'@'localhost' identified by '비밀번호';

# example
grant all privileges on *.* to 'test'@'localhost' identified by '0000';


# 권한 반영
FLUSH PRIVILEGES;



사용자 권한 확인

show grants for test@localhost;



사용자 권한 수정, 삭제

revoke 명령어를 사용하여 사용자의 권한을 수정, 삭제할 수 있다. 특정 권한을 명시하거나 DB, table을 명시할 수 있다.

# db1에 대한 insert, update 권한을 삭제
revoke insert, update on db1.* from user;

# 사용자 전체 insert 권한 삭제
revoke insert on *.* to test@localhost;



사용자 삭제

drop user test@localhost;