【备忘录】当WebAP服务器登录MariaDB的用户无法授予权限时的解决方法
首先
为了记录这个问题的解决办法,我撰写了以下内容:我尝试从WebAP服务器登录到DB服务器,并进行数据库和用户创建,然后尝试给用户授予对数据库的操作权限时出现了错误。请注意,我是一个不太熟悉DB服务器操作的初学者。
前提 tí) 相同
WebAP服务器的IP地址:10.0.10.221
数据库服务器的私有IP地址:10.0.10.228
在预先进行了以下命令的情况下,确保WebAP服务器可以以root用户的身份登录到DB服务器上:
事前,在DB服务器上执行以下命令。
GRANT ALL PRIVILEGES ON *.* TO root@10.0.10.221;
請問您有什麼問題呢?
从WebAP服务器登录到DB服务器,进行了数据库和用户的创建。
但是,当尝试为创建的用户授予数据库权限时,出现以下错误,无法成功授予权限。
GRANT ALL ON 20211017_db.* TO '20211017_user'@'10.0.10.221';
ERROR 1044 (42000): Access denied for user 'root'@'10.0.10.221' to database '20211017_db'
(参考)操作日志记录了DB创建和用户创建的操作
[root@ip-10-0-10-221 ~]# mysql -h 10.0.10.228 -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE 20211017_db DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> CREATE USER '20211017_user'@'10.0.10.221' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL ON 20211017_db.* TO '20211017_user'@'10.0.10.221';
ERROR 1044 (42000): Access denied for user 'root'@'10.0.10.221' to database '20211017_db'
调查
用户和数据库的名称输入有错误吗?
→没有。
因为双方服务器的MySQL版本不同造成的吗?
→由于版本相同,这种可能性不存在。
在DB服务器上授予的权限是否不足?
→如果将权限与本地主机(10.0.10.228)和10.0.10.221的root用户进行比较,会发现10.0.10.221缺少“with grant option”的权限,这可能是其中的原因。
MariaDB [(none)]> show grants for root@localhost;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> show grants for root@10.0.10.221;
+-----------------------------------------------------+
| Grants for root@10.0.10.221 |
+-----------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.0.10.221' |
因为这个原因
由于未在DB服务器(localhost)上授予WebAP服务器权限时执行「with grant option」命令。
换句话说,仅执行下面的命令无法在WebAP服务器的root用户上执行grant命令。
GRANT ALL PRIVILEGES ON *.* TO root@10.0.10.221;
进行实施
在本地主机上(localhost)的数据库服务器上,授予了具有”with grant option”选项的权限。
GRANT ALL PRIVILEGES ON *.* TO root@10.0.10.221 WITH GRANT OPTION;
在确认后发现已授予了”with grant option”的权限。
MariaDB [(none)]> show grants for root@10.0.10.221;
+-----------------------------------------------------------------------+
| Grants for root@10.0.10.221 |
+-----------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.0.10.221' WITH GRANT OPTION |
+-----------------------------------------------------------------------+
随后,在WebAP服务器上登录到数据库并尝试授予权限时,成功地进行了权限授予,没有出现任何错误代码。
[root@ip-10-0-10-221 ~]# mysql -h 10.0.10.228 -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT ALL ON 20211017_db.* TO '20211017_user'@'10.0.10.221';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show grants for 20211017_user@10.0.10.221;
+------------------------------------------------------------------------------------------------------------------------+
| Grants for 20211017_user@10.0.10.221 |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO '20211017_user'@'10.0.10.221' IDENTIFIED BY PASSWORD '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19' |
| GRANT ALL PRIVILEGES ON `20211017_db`.* TO '20211017_user'@'10.0.10.221' |
+------------------------------------------------------------------------------------------------------------------------+