1. 程式人生 > 實用技巧 >mysql 誤刪除所有使用者解決辦法

mysql 誤刪除所有使用者解決辦法

企業中誤刪除所有使用者故障案例

1.不小心刪除了所有使用者

#1.檢視使用者
mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | db02      |
| root | db02      |
|      | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.00 sec)

#2.誤刪除使用者
mysql> delete from mysql.user where 1=1;
Query OK, 6 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
Empty set (0.00 sec)

#3.刪除所有使用者後,仍然可以登入,重啟後就不能登入了
[root@db02 ~]# systemctl restart mysqld
[root@db02 ~]# mysql -uroot -p123
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

2.解決方案一:

1)停止資料庫

[root@db02 ~]# systemctl stop mysqld

2)跳過授權表和網路啟動資料庫

#1.只跳過授權表啟動
[root@db02 ~]# mysqld_safe --skip-grant-tables &

#2.跳過授權表和網路啟動
[root@db02 ~]# mysqladmin shutdown
[root@db02 ~]# mysqld_safe --skip-grant-tables --skip-networking &

3)插入一個使用者

#1.使用grant授權建立使用者失敗
mysql> grant all on *.* to root@'localhost' identified by '123';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

#2.使用create建立使用者失敗
mysql> create user root@'localhost';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

#3.使用insert語句插入新使用者
mysql> use mysql
mysql> insert into user(user,host,password) values('root','localhost','123');
ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
#原因:因為表中的欄位很多不能為空,預設值又是空,所以必須指定不為空的欄位
mysql> insert into user(user,host,password,ssl_cipher,x509_issuer,x509_subject) values('root','localhost',PASSWORD('123'),'','','');
Query OK, 1 row affected (0.00 sec)
#插入使用者可以成功,但是使用者許可權全是N,沒有任何許可權的使用者沒有用

#4.正確方法
mysql> insert into mysql.user values ('localhost','root',PASSWORD('123'),
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'Y',
'',
'',
'',
'',0,0,0,0,'mysql_native_password','','N');

#檢視插入使用者的許可權
mysql> select * from mysql.user\G

4)正常啟動資料庫

[root@db02 ~]# mysqladmin shutdown
[root@db02 ~]# systemctl start mysqld
[root@db02 ~]# mysql -uroot -p123

3.解決方案2:

1)停止資料庫

[root@db02 ~]# systemctl stop mysqld

2)跳過授權表和網路啟動資料庫

#1.只跳過授權表啟動
[root@db02 ~]# mysqld_safe --skip-grant-tables &

#2.跳過授權表和網路啟動
[root@db02 ~]# mysqladmin shutdown
[root@db02 ~]# mysqld_safe --skip-grant-tables --skip-networking &

3)插入一個使用者

#1.重新整理授權表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

#2.授權一個使用者
mysql> grant all on *.* to root@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

#3.檢視使用者許可權
mysql> select * from mysql.user\G

#4.管理員使用者中授權許可權為 N,所以要修改這個值
mysql> update mysql.user set Grant_priv='Y' where Host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

#5.或者再授權時直接加上引數包含 grant 許可權
mysql> grant all on *.* to root@'localhost' identified by '123' with grant option;
Query OK, 0 rows affected (0.00 sec)

4)正常啟動資料庫

[root@db02 ~]# mysqladmin shutdown
[root@db02 ~]# systemctl start mysqld
[root@db02 ~]# mysql -uroot -p123