首頁  >  文章  >  資料庫  >  mysql中grant all privileges on怎麼賦給使用者遠端權限

mysql中grant all privileges on怎麼賦給使用者遠端權限

PHPz
PHPz轉載
2023-05-26 22:04:172354瀏覽

mysql grant all privileges on賦給使用者遠端權限

mysql中grant all privileges on賦給使用者遠端權限

  • 改表法。

當你的帳號不允許從遠端登陸,只能在localhost連線時。這時候只要在mysql伺服器上,更改mysql 資料庫裡的user 表裡的host 項,從localhost"改成%即可實現用戶遠端登入

在安裝mysql的機器上運行:

1. mysql -u root -p  

2. select host,user from user where user='root';

3. update user set host = '%' 其中 user= 'root' and host='localhost';  

4. select host, user from user where user='root';

  • 授權法

[root@aaa-server ~]# mysql -u root -p
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '123' with grant option;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
  • 授權法。

例如,你想要user使用mypwd從任何主機連接到mysql伺服器的話。

在安裝mysql的機器上運行:

1. GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'mypwd' WITH
      GRANT OPTION;  
2.FLUSH   PRIVILEGES;
模板:
grant all privileges on 库名.表名 to '用户名'@'IP地址' identified by '密码' with grant option;
flush privileges;
  • 如果你想允許用戶user從ip為192.168.1.4的主機連接到mysql伺服器,並使用mypwd作為密碼

在安裝mysql的機器上運行:

 GRANT ALL PRIVILEGES ON *.* TO 'user'@'192.168.1.3' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;   
 FLUSH   PRIVILEGES;

注意授權後必須FLUSH PRIVILEGES;否則無法立即生效。

高版本資料庫無法依照grant all privileges on *.* to "root"@"%" identified by "xxxx";去修改使用者權限

mysql> SELECT @@VERSION;
+-----------+
| @@VERSION |
+-----------+
| 8.0.14    |
+-----------+
1 row in set (0.00 sec)

高版本修改使用者權限方法:

# 先创建远程用户,再授权
mysql> create user 'root'@'%' identified by  'password';
Query OK, 0 rows affected (0.03 sec)
mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

再次查看發現有了root %

mysql>  select User,Host from user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)
————————————————

mysql授權語句說明grant all privileges、建立使用者、刪除使用者

mysql的賦權語句:

grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
  • all  privileges   ==》  表示所有的權限,增刪改核權限全部都有了

  • ##*.*               == ;的資料庫下面所有的表

  • root@%   ==》  所有資料庫下面所有的表,所有的權限,全部都給root用戶   % 表示root用戶可以在任何機器上面進行連線登入

  • The password used for remote login connection is "123456".

刷新權限清單:flush   privileges#

CREATE DATABASE 数据库名;
CREATE USER '用户名'@'%' IDENTIFIED BY '密码';    
GRANT all privileges ON 数据库名.* to '用户名'@'%' identified by '密码' WITH GRANT OPTION; 
flush privileges;

建立使用者:CREATE USER 'jack'@'localhost' IDENTIFIED BY 'test123';

檢視資料庫中已建立的使用者:select user,host from user;--user表在資料庫自帶的、名字為mysql的資料庫中

刪除使用者:delete from user where user = 'jack'; 

drop user ‘jack'@'%';

drop  user  會將該使用者的資訊全部刪掉,而delete  只會清除user表,其他的例如db表中的資訊還是存在。

清除快取:FLUSH PRIVILEGES

以上是mysql中grant all privileges on怎麼賦給使用者遠端權限的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除