웹프로그래밍 무작정따라하기/Database
[SQL] MySQL의 grant명령어로 사용자 권한 설정
RIMD
2023. 2. 28. 11:26
MySQL 접속
cmd창에서 다음 명령어로 mysql접속
> mysql -uroot -p
mysql 접속
MariaDB [(none)]> use mysql;
MariaDB 내부 table 확인
user 테이블을 통해 권한 설정할 예정
MariaDB [mysql]> show tables;
사용자 확인
MariaDB [mysql]> select user, host from mysql.user;
-- 아래와 같이 사용자 확인을 할 수 있음
(예시)
+-------------+-----------------------+
| User | Host |
+-------------+-----------------------+
| root | localhost |
| user | 192.168.0.% |
+-------------+-----------------------+
MySQL의 grant 명령어로 사용자 권한 설정
사용자 권한 설정
MariaDB [mysql]> grant all privileges on dbname.table to userid@host identified by 'password';
모든 db 및 테이블에 접근권한 설정
MariaDB [mysql]> grant all privileges on *.* to userid@host identified by 'password';
모든 db 및 테이블에 권한을 주고 로컬 및 원격에서도 접속가능하도록 설정
--host에 %을 주므로서 로컬, 원격 접속 모두 가능
MariaDB [mysql]> grant all privileges on *.* to userid@'%' identified by 'password';
특정 사용자에게 특정 DB의 모든 테이블에 select, insert 권한 부여하기
MariaDB [mysql]> grant select, insert on DB명.* to userid@host identified by 'passwrod';
특정 사용자에게 특정 DB의 특정 테이블의 정해진 컬럼에만 update 권한 부여하기
MariaDB [mysql]> grant update(컬럼1, 컬럼2, 컬럼3) on DB명.테이블명 to 'userid'@'host' identified by 'password';
ip주소가 192.168.0으로 시작하는 컴퓨터에서 접속가능한 사용자를 권한 부여하기
MariaDB [mysql]> grant all privileges on *.* to userid@'192.168.0.%' identified by 'password';
권한삭제
MariaDB [mysql]> revoke all on dbname.table from userid@host;
usage(삭제하면 로그인조차 안됨) 권한 부여, 삭제
-- usage 권한 부여
MariaDB [mysql]> grant usage on *.* to userid@host identified by 'userid';
-- usage 권한 삭제
MariaDB [mysql]> revoke usage on *.* to userid@host;
권한조회
MariaDB [mysql]> show grants for userid@host
변경 사항 권한 적용 (반드시 해야 적용이 됨!)
MariaDB [mysql]> flush privileges;
// 권한 조회
show grants for 'user계정'@'localhost';
// 권한 회수
revoke all privieges on 스키마.테이블명 from user계정@localhost;
ex) revoke all on '스키마'.* from 'user계정'@'localhost';
// 외부ip 주소의 사용자 삭제
drop user 'user계정'@'localhost';
// 사용자 비번 변경
alter user 'user계정'@'localhost' IDENTIFIED by 'newPassword';
// 적용
flush privileges;