2. DBMS이야기/02. MySQL

MySQL에서 새 user를 생성하고 permission을 부여하는 방법

OSSW(Open Source System SoftWare 2014. 8. 4. 11:47

How to Create a New User

 사용자를 생성하고, 권한을 사용자 별로 부여를 해서 데이터베이스 접근에 제약을 줄 수 있다.

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

위 구문을 사용하면 새로 생성되는 유저에게 어떤 데이터베이스에 대해서도 작업을 수행할 권한이 주어지지 않는다. 만약 새로운 유저가 로그인을 한다면, MySQL 쉘에 접근할 수 없을 것이다.


새로운 유저에게 필요한 권한을 부여하는 작업이 필요하다.

RANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';

위 구문의 *는 newuser가 액세스 할 수 있는 데이터베이스와 테이블을 가리킨다. 

새로운 사용자 권한부여 설정을 완료하였으면 항상 모든 권한을 다시 로드해야 한다.

FLUSH PRIVILEGES;

위 구문을 실행해야 변경 사항이 적용된다.



How to Grant Different User Permissions

다음은 사용자들이 가질 수 있는 가능한 권한들이다.

  • ALL PRIVILEGES- as we saw previously, this would allow a MySQL user all access to a designated database (or if no database is selected, across the system)
  • CREATE- allows them to create new tables or databases
  • DROP- allows them to them to delete tables or databases
  • DELETE- allows them to delete rows from tables
  • INSERT- allows them to insert rows into tables
  • SELECT- allows them to use the Select command to read through databases
  • UPDATE- allow them to update table rows
  • GRANT OPTION- allows them to grant or remove other users' privileges

특정 유저에게 권한을 제공하기 위해서, 아래 프레임 워키를 사용할 수 있다


 GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’;
어떤 데이터베이스 베이스나 테이블에 접근하기 위해서 database이름이나 table이름에 *를 넣어야 한다.
update를 하거나 권한을 수정할 때마다 반드시 Flush Privileges 커맨드를 사용해야 한다.
권한 허가를 취소해야 하는 경우는 아래의 구문을 사용한다.
 REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’;
database를 DROP으로 지울 수 있는 것 처럼 user 를 한번에 DROP하는 것도 가능하다

 DROP USER ‘demo’@‘localhost’;
새 유저를 테스트 하기 위해 로그아웃 하는 법은 다음과 같다
 quit 
mysql -u [username]-p

by lee ji eun