728x90

mysql 8.x user 생성 및 변경, 삭제 처리 하기

 

> mysql 에 접속하시고 user 테이블로 이동 하셔서 

insert into user (Host,User,authentication_string,ssl_cipher,x509_issuer,x509_subject) values ('localhost','유저명', '', '','','');

 

insert into user (Host,User,authentication_string,ssl_cipher,x509_issuer,x509_subject) values ('localhost','유저명', '', '','','');

 

위와 같이 MySQL5.X버전의 경우와 같은 SQL문으로 user를 생성하지마세요.

 

유저가 생성되지 않습니다.

 

 

그럼 mysql에 user를 만들어 비밀번호를 반영하여 접속하는 방법을 설명하겠습니다.

먼저 MySQL에 root로 접속하신 후 mysql DB 이동해 주세요. 


0. MySQL에 root로 접속하여 mysql DB 로 이동해 주세요.

use mysql;

 

1. 사용자 정보 조회를 먼저 조회하세요.

SELECT user,authentication_string,plugin,host FROM mysql.user; 

SELECT user,authentication_string,plugin,host FROM mysql.user; 

그러면 위와 같이 뜨게 됩니다.

단, plugin 부분이 "caching_sha2_password" 로 되어서 암호화된 비밀번호의 형태가 다를 수 있습니다.

위의 경우에는 제가 "mysql_native_password" 로 변경한 것이기 때문입니다.

 

여전히 MySQL 클라이언트에서는 "caching_sha2_password" 플러그인이 반영되지 않아서 connect 가 안되기에 기존에 사용하던 "mysql_native_password" 로 변경하였기 때문입니다.

이 부분에 대해서는 아래에 변경하는 방법을 적어놨습니다.

 

2. 사용자 생성를 생성합니다.
create user '유저명'@'localhost' identified by '비밀번호'; 

create user '유저명'@'localhost' identified by '비밀번호'; 

 

3. 생성된 사용자를 확인합니다.
SELECT user,authentication_string,plugin,host FROM mysql.user; 

SELECT user,authentication_string,plugin,host FROM mysql.user; 

 

4. 생성된 사용자의 권한을 적용시켜 줍니다.
GRANT ALL PRIVILEGES ON *.* TO '유저명'@'localhost'; 
GRANT GRANT OPTION ON *.* TO '유저명'@'localhost'; 

GRANT ALL PRIVILEGES ON *.* TO '유저명'@'localhost'; 
GRANT GRANT OPTION ON *.* TO '유저명'@'localhost'; 

 

5. MySQL에 실시간 반영시켜 줍니다.
flush privileges;

flush privileges;

 

 

참고. MySQL에서 caching_sha2_password 을 mysql_native_password으로 플러그인을 변경하기

ALTER user '유저명'@'localhost' IDENTIFIED WITH mysql_native_password by '비밀번호';

ALTER user '유저명'@'localhost' IDENTIFIED WITH mysql_native_password by '비밀번호';

여전히 MySQL 클라이언트에서는 "caching_sha2_password" 플러그인이 반영되지 않아서 connect 가 안되기에 기존에 사용하던 "mysql_native_password" 로 변경하였기 때문입니다.

 


6. 사용자를 삭제할 수 있습니다.
drop user 유저명@localhost;

drop user 유저명@localhost;
flush privileges;

삭제하고 나면 꼭 "flush privileges;" 를 해주세요. MySQL에 제대로 반영을 시켜줘야 합니다.

그렇지 않으면 메모리상에 USER 정보가 남아 있어 동일한 "유저명" 으로 재생성할 수 없습니다.

예를 들어 아래와 같이 SQL문중 delete 문으로 유저를 삭제하고서 다시 동일한 유저를 생성하게 되면 에러가 뜨게 됩니다.

ERROR 1396 (HY000): Operation  관련 에러가 뜨게 됩니다.

mysql> delete from user where User='유저명';

Query OK, 1 row affected (0.00 sec)



mysql> create user '유저명'@'localhost' identified by '비밀번호'; 

ERROR 1396 (HY000): Operation CREATE USER failed for '유저명'@'localhost'

mysql> delete from user where User='유저명';

Query OK, 1 row affected (0.00 sec)

 

mysql> create user '유저명'@'localhost' identified by '비밀번호'; 

ERROR 1396 (HY000): Operation CREATE USER failed for '유저명'@'localhost'

 

아래는 실제 에러가 떠서 "Drop User " 명령문으로 삭제하고 CREATE USER 명령문으로 재 생성한 화면입니다.

+ Recent posts