본문 바로가기
KDT/SQL

MySQL 사용자

by jhwannabe 2023. 3. 21.

1. 사용자 추가하기

  • MySQL 8.0 Command Line Client 'root' 계정으로 로그인
  • 로컬에서 접속 가능한 사용자 추가하기(계정만 생성)
  • create user '사용자명'@'localhost' identified by '사용자 비밀번호';
create user 'apple'@'localhost' identified by '1111';

 

2. DB 권한 부여하기

grant all privileges on *.* to '사용자'@'localhost'; # 모든 DB에 접근 가능
grant all privileges on 'DB명' to '사용자'@'localhost';
flush privileges; # 새로운 세팅을 적용함

 

✔ 할당 권한 상세 옵션

해당 함수 설 명
create
drop
alter
테이블에 대한 생성, 삭제, 변경 권한
select
insert
update
delete
테이블의 데이터를 조회, 삽입, 변경, 삭제 권한
all 모든 권한
usage 권한을 부여하지 않고 계정만 생성(default)
  • ex) grant select on DB명.테이블명 to '사용자'@'localhost';

    

✔ IP 권한 상세 옵션

  • %: 모든 IP에서 접근이 가능
  • 127.0.0.1: localhost에서 접근이 가능

ex) grant select on DB명.테이블명 to '사용자'@'%';

ex) grant select on DB명.테이블명 to '사용자'@'특정 IP 주소';

 

 

문제 1. apple 데이터베이스에 kdt.member 테이블을 복사하고 해당 테이블의 select 권한만 가능한 orange 계정을 만들어보자.

use apple;
create table apple.member(select * from kdt.member);
create user 'orange'@'localhost' identified by '2222';
grant select on apple.member to 'orange'@'localhost';
flush privileges;

 

orange로 로그인 후 테스트

use apple;
select * from member;
update member set point = 500 where userid='berry'; # Error Code : 1142. UPDATE command denied to user 'orange'@'localhost' for 'member'

 

사용자 목록 조회

use mysql;
select user, host from user;

 

사용자 제거

drop user '계정명'@'localhost'; # 추천!
delete from user where user = 계정명;

 

사용자 권한 조회하기

show grants for '계정명'@'localhost';

show grants for 'apple'@'localhost';
show grants for 'orange'@'localhost';
show grants for 'melon'@'localhost';

사용자 권한 제거하기

revoke 권한명 on 데이터베이스명.테이블명 from '계정명'@'localhost';

revoke all privileges on 데이터베이스명.* from '계정명'@'localhost';
revoke select on apple.member from 'orange'@'localhost';
728x90

'KDT > SQL' 카테고리의 다른 글

TCL(트랜잭션 제어어)  (0) 2023.03.21
뷰(View)  (0) 2023.03.21
MySQL 문자열 함수  (0) 2023.03.21
서브쿼리(Subquery)  (1) 2023.03.20
집합 연산자(UNION, UNION ALL, INTERSECT, EXCEPT)  (0) 2023.03.20