◽ MariaDB | MySQL

[Mysql & MariaDB] root 삭제 했을 때 복구 ( empty user table )


아.. 사용자 정리한다고 root를 삭제해버렸다..

  이게 재설치를해도 어디에 남아있는지, 자꾸 이전 데이터를 참조하여 root가 복구 되지 않았다. OS재설치까지는 너무 오바인 것 같아서 찾아보니 방법이 있었다.

 

P.S. empty, 그러니깐 root이건 유저이건 다~ 지웠을 때, 참 난감한데 이에 대한 방법도 있다. 아래에 정리.


먼저 "systemctl stop mariadb"를 한다.

그런 다음 " systemctl set-environment MYSQLD_OPTS='--skip-grant-tables' " 옵션을 줘서 로그인 권한없이 들어가도록 만든다.

 

<추가>

"insert into user (Host, User, Password, ssl_cipher, x509_issuer, x509_subject, authentication_string) values('localhost','root', password('1234'),'','','','');" 

 

<수정>

"ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';"

 

10.3.24기준이다. 아래 버전에서는 host, user, password만 insert가 가능한데 버전이 올라갈수록 오른쪽에 하나씩 key값들이 추가가 되었나보다. 그래서 인터넷 글들이 다 되는 것만이 아니었다. 아래의 그림을 참조하면 계속 오류가 뜬 흔적을 볼 수 있다.

  

 

 

 

 

이렇게 root를 추가하게되어도 systemctl set-environment MYSQLD_OPTS=' '을 한 뒤에 들어가서 grant 작업을 하면 권한이 없다고 아래와 같은 참 난감한 상황이 나온다.

 

 

 

 

 

 

이러한 권한을 갖추기 위해서는 " Grant_priv='Y', Super_priv='Y' "이 매핑되어 있어야 한다.

아래와 같이 따라하면 된다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
MariaDB [(none)]> SELECT Host,User FROM mysql.user;
Empty set (0.00 sec)
 
MariaDB [(none)]> CREATE USER root@localhost IDENTIFIED BY '패스워드';
ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> CREATE USER root@localhost IDENTIFIED BY '패스워드';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO root@localhost;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> SELECT Host,User,Grant_priv,Super_priv FROM mysql.user;
+-----------+------+------------+------------+
| Host      | User | Grant_priv | Super_priv |
+-----------+------+------------+------------+
| localhost | root | Y          | Y          |
+-----------+------+------------+------------+
1 row in set (0.00 sec)
 
MariaDB [(none)]> exit
Bye
cs

 

 

 

 

root의 권한이다.

 


참고 :

https://sir.kr/g4_qa/132060

https://uiandwe.tistory.com/1181

https://uiandwe.tistory.com/648 

https://zetawiki.com/wiki/Mysql.user_empty


 

푸터바