MySQL遠程連接、用戶授權


MySQL遠程連接

👉遠程連接

👉授權

👉常見權限表

相關庫:mysql

相關表:user

相關字段:select host,user from user;

創建用戶、授權

  • 創建用戶格式:create user 用戶名@ip地址 identified by '密碼';

  • 授權:grant all on *.* To 用戶名@'ip地址';

    grant select,create on 數據庫名.表名 To 用戶名@ip地址;

# 創建用戶
create user root@'192.168.11.%' identified  by '123456';
# 這樣root@192.168.11.% 這個網段的用戶可以登錄

create user hans@'192.168.11.161' identified by '123456';
# 這樣只允許hans用戶登錄

create user li@'%' identified by '123456';
# 所有li用戶都可登錄


# 刪除用戶
drop user root@'192.168.11.%';
# 授權
grant all on *.* To hans@'192.168.11.161';
grant select,create on oldboy_test.* To hans@'192.168.11.161';

# 查看授權
show grants for hans@'192.168.11.161';
# 刷新權限表
flush privileges;

MySQL添加用戶、刪除用戶、授權及撤銷權限

一.創建用戶:

mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));

#這樣就創建了一個名為:test 密碼為:1234 的用戶。

注意:此處的"localhost",是指該用戶只能在本地登錄,不能在另外一台機器上遠程登錄。如果想遠程登錄的話,將"localhost"改為"%",表示在任何一台電腦上都可以登錄。也可以指定某台機器(例如192.168.1.10),或某個網段(例如192.168.1.%)可以遠程登錄。

二.為用戶授權:

授權格式:grant 權限 on 數據庫.* to 用戶名@登錄主機 identified by "密碼"; 

2.1 首先為用戶創建一個數據庫(testDB):

mysql>create database testDB;

2.2 授權test用戶擁有testDB數據庫的所有權限(某個數據庫的所有權限):

 mysql>grant all privileges on testDB.* to test@localhost identified by '1234';

mysql>flush privileges;//刷新系統權限表,即時生效

2.3 如果想指定某庫的部分權限給某用戶本地操作,可以這樣來寫:

mysql>grant select,update on testDB.* to test@localhost identified by '1234';

mysql>flush privileges; 

#常用的權限有select,insert,update,delete,alter,create,drop等。可以查看mysql可授予用戶的執行權限了解更多內容。

2.4  授權test用戶擁有所有數據庫的某些權限的遠程操作:   

mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";

 #test用戶對所有數據庫都有select,delete,update,create,drop 權限。

2.5 查看用戶所授予的權限:

mysql> show grants for test@localhost;

三、刪除用戶:

mysql>Delete FROM user Where User='test' and Host='localhost';

mysql>flush privileges;

刪除賬戶及權限:>drop user 用戶名@'%';

        >drop user 用戶名@ localhost; 

四、修改指定用戶密碼:

mysql>update mysql.user set password=password('新密碼') where User="test" and Host="localhost";

mysql>flush privileges;

五、撤銷已經賦予用戶的權限:

revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:

mysql>grant all on *.* to dba@localhost;

mysql>revoke all on *.* from dba@localhost;

六、MySQL grant、revoke 用戶權限注意事項:

6.1 grant, revoke 用戶權限后,該用戶只有重新連接 MySQL 數據庫,權限才能生效。

6.2. 如果想讓授權的用戶,也可以將這些權限 grant 給其他用戶,需要選項 "grant option"

mysql>grant select on testdb.* to dba@localhost with grant option;

mysql>grant select on testdb.* to dba@localhost with grant option;

這個特性一般用不到。實際中,數據庫權限最好由 DBA 來統一管理。

 

補充:

mysql授權表共有5個表:user、db、host、tables_priv和columns_priv。

授權表的內容有如下用途:
user表
user表列出可以連接服務器的用戶及其口令,並且它指定他們有哪種全局(超級用戶)權限。在user表啟用的任何權限均是全局權限,並適用於所有數據庫。例如,如果你啟用了DELETE權限,在這里列出的用戶可以從任何表中刪除記錄,所以在你這樣做之前要認真考慮。

db表
db表列出數據庫,而用戶有權限訪問它們。在這里指定的權限適用於一個數據庫中的所有表。

host表
host表與db表結合使用在一個較好層次上控制特定主機對數據庫的訪問權限,這可能比單獨使用db好些。這個表不受GRANT和REVOKE語句的影響,所以,你可能發覺你根本不是用它。

tables_priv表
tables_priv表指定表級權限,在這里指定的一個權限適用於一個表的所有列。

columns_priv表
columns_priv表指定列級權限。這里指定的權限適用於一個表的特定列

 

 

 

MySQL可授予用戶的執行權限

(以下操作都是以root身份登陸進行grant授權,以root@localhost身份登陸執行各種命令。)


MySQL包含哪些權限,共29個。

權限 說明 舉例
usage 連接(登陸)權限,建立一個用戶,就會自動授予其usage權限(默認授予)。
   該權限只能用於數據庫登陸,不能執行任何操作;且usage權限不能被回收,也即REVOKE用戶並不能刪除用戶。
mysql>  grant usage on *.* to 'root′@'localhost' identified by '123';
file 擁有file權限才可以執行  select ..into outfile和load data infile…操作,但是不要把file, process,  super權限授予管理員以外的賬號,這樣存在嚴重的安全隱患。 mysql>  grant file on *.* to root@localhost;
   mysql> load data infile '/home/mysql/pet.txt' into table pet;
super 這個權限允許用戶終止任何查詢;修改全局變量的SET語句;使用CHANGE  MASTER,PURGE MASTER LOGS。 mysql>  grant super on *.* to root@localhost;
   mysql> purge master logs before 'mysql-bin.000006′;
select 必須有select的權限,才可以使用select  table mysql>  grant select on pyt.* to 'root′@'localhost';
   mysql> select * from shop;
insert 必須有insert的權限,才可以使用insert  into ….. values…. mysql>  grant insert on pyt.* to 'root′@'localhost';
   mysql> insert into shop(name) values('aa');
update 必須有update的權限,才可以使用update  table mysql>  update shop set price=3.5 where article=0001 and dealer='A';
delete 必須有delete的權限,才可以使用delete  from ….where….(刪除表中的記錄) mysql>  grant delete on pyt.* to 'root′@'localhost';
   mysql> delete from table where id=1;
alter 必須有alter的權限,才可以使用alter  table mysql>  alter table shop modify dealer char(15);
alter routine 必須具有alter  routine的權限,才可以使用{alter |drop} {procedure|function} mysql>grant  alter routine on pyt.* to 'root′@' localhost ‘;
   mysql> drop procedure pro_shop;
   Query OK, 0 rows affected (0.00 sec)
create 必須有create的權限,才可以使用create  table mysql>  grant create on pyt.* to 'root′@'localhost';
drop 必須有drop的權限,才可以刪除庫、表、索引、視圖等 mysql>  drop database db_name; 
   mysql> drop table tab_name;
   mysql> drop view vi_name; 
   mysql> drop index in_name;
create routine 必須具有create  routine的權限,才可以使用{create |alter|drop} {procedure|function} mysql>  grant create routine on pyt.* to 'root′@'localhost';
   當授予create routine時,自動授予EXECUTE, ALTER ROUTINE權限給它的創建者:
create temporary tables (注意這里是tables,不是table) 必須有create  temporary tables的權限,才可以使用create temporary tables.
   mysql> grant create temporary tables on pyt.* to  'root′@'localhost';
   [mysql@mydev ~]$ mysql -h localhost -u root -p pyt
   mysql> create temporary table tt1(id int);
create view 必須有create  view的權限,才可以使用create view mysql>  grant create view on pyt.* to 'root′@'localhost';
   mysql> create view v_shop as select price from shop;
create user 要使用CREATE  USER,必須擁有mysql數據庫的全局CREATE USER權限,或擁有INSERT權限。 mysql>  grant create user on *.* to 'root′@'localhost';
   或:mysql> grant insert on *.* to root@localhost;
show database 通過show  database只能看到你擁有的某些權限的數據庫,除非你擁有全局SHOW DATABASES權限。
   對於root@localhost用戶來說,沒有對mysql數據庫的權限,所以以此身份登陸查詢時,無法看到mysql數據庫:
mysql>  show databases;
show view 必須擁有show  view權限,才能執行show create view mysql>  show create view name;
index 必須擁有index權限,才能執行[create  |drop] index mysql>  grant index on pyt.* to root@localhost;
   mysql> create index ix_shop on shop(article);
   mysql> drop index ix_shop on shop;
excute 執行存在的Functions,Procedures mysql>  call pro_shoroot(0001,@a);
event event的使用頻率較低建議使用root用戶進行創建和維護。
   要使event起作用,MySQL的常量GLOBAL event_scheduler必須為on或者是1
mysql>  show global variables like 'event_scheduler';
lock tables 必須擁有lock  tables權限,才可以使用lock tables mysql>  grant lock tables on pyt.* to root@localhost;
   mysql> lock tables a1 read;
   mysql> unlock tables;
references 有了REFERENCES權限,用戶就可以將其它表的一個字段作為某一個表的外鍵約束。  
reload 必須擁有reload權限,才可以執行flush  [tables | logs | privileges] mysql>  grant reload on pyt.* to root@localhost;
   ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
   mysql> grant reload on *.* to 'root′@'localhost';
   Query OK, 0 rows affected (0.00 sec)
   mysql> flush tables;
replication client 擁有此權限可以查詢master  server、slave server狀態。 mysql>  grant Replication client on *.* to root@localhost;
   或:mysql> grant super on *.* to root@localhost;
   mysql> show master status;
replication slave 擁有此權限可以查看從服務器,從主服務器讀取二進制日志。 mysql>  grant replication slave on *.* to root@localhost;
   mysql> show slave hosts;
   Empty set (0.00 sec)
   mysql>show binlog events;
Shutdown 關閉mysql權限 [mysql@mydev  ~]$ mysqladmin shutdown
grant option 擁有grant  option,就可以將自己擁有的權限授予其他用戶(僅限於自己已經擁有的權限) mysql>  grant Grant option on pyt.* to root@localhost;
   mysql> grant select on pyt.* to p2@localhost;
process 通過這個權限,用戶可以執行SHOW  PROCESSLIST和KILL命令。默認情況下,每個用戶都可以執行SHOW PROCESSLIST命令,但是只能查詢本用戶的進程。 mysql>  show processlist;
all privileges 所有權限。with  grant option 可以連帶授權 mysql>  grant all privileges on pyt.* to root@localhost with grant option;

 

另外:

  • 管理權限(如 super, process, file等)不能夠指定某個數據庫,on后面必須跟 *.*

  • 有人會問truncate權限呢,其實truncate權限就是create+drop,這點需要注意


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM