mysql中grant all privileges on賦給用戶遠程權限方式
目錄
- mysql grant all privileges on賦給用戶遠程權限
- mysql授權語句說明grant all privileges、創建用戶、刪除用戶
- 總結
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 = '%' where 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用戶可以在任意機器上面進行連接登錄
- identified by '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
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持。
