首頁  >  文章  >  後端開發  >  使用者管理和權限和設定——mysql

使用者管理和權限和設定——mysql

小云云
小云云原創
2017-11-16 13:27:082226瀏覽

MySQL是世界上最受歡迎的資料庫管理系統之一。書中從介紹簡單的資料檢索開始,逐步深入一些複雜的內容,包括聯結的使用、子查詢、正規表示式和基於全文本的搜尋、預存程序、遊標、觸發器、表格約束,等等。透過重點突出的章節,條理清晰、系統而扼要地講述了讀者應該掌握的知識,使他們不經意間立刻功力大增。本節內容主要講述了mysql 使用者管理和權限設定的相關指令整理。

使用者管理

mysql>use mysql;

查看

mysql>select host,user,password from user ;

建立

mysql>create user zx_root;

修改

mysql>rename user feng to newuser; //mysql 5之后可以使用,之前需要使用update 更新user表

刪除

mysql>drop user newuser;   //mysql5之前删除用户时必须先使用revoke 删除用户权限,然后删除用户,mysql5之后drop 命令可以删除用户的同时删除用户的相关权限

#更改密碼

mysql>set password for zx_root =password('xxxxxx');
mysql>update  mysql.user  set  password=password('xxxx')  where user='otheruser'

 
查看使用者權限

mysql>show grants for zx_root;

賦予權限

mysql>grant select on dmc_db.*  to zx_root;

回收權限

mysql>revoke  select on dmc_db.*  from  zx_root;  //如果权限不存在会报错

上面的指令也可使用多個權限同時賦予和回收,權限之間使用逗號分隔

mysql>grant select,update,delete  ,insert  on dmc_db.*  to  zx_root;

如果想立即看到結果使用

flush  privileges ;

命令更新 
 
設定權限時必須給出訊息

1,要授予的權限

2,被授予存取權限的資料庫或表格

3,使用者名稱

grant和revoke可以在幾個層級控制存取權

1,整個伺服器,使用grant ALL  和revoke  ALL

2,整個資料庫,使用on  database.*

#3,特性表,使用on  database.table

4,特定的列

5,特定的預存程序
 
user表中host列的值的意義

%符合所有主機

localhost      localhost不會解析成IP位址,直接透過UNIXsocket連線

127.0.0.1      會透過TCP/IP協定連接,且只能在本機存取

#::1            ::1是相容支援ipv6的,表示同ipv4的127.0.0.1

grant 一般資料用戶,查詢、插入、更新、刪除資料庫中所有資料表資料的權利。

grant select on testdb.* to common_user@'%'
grant insert on testdb.* to common_user@'%'
grant update on testdb.* to common_user@'%'
grant delete on testdb.* to common_user@'%'

或者,用一條 MySQL 指令來取代:

grant select, insert, update, delete on testdb.* to common_user@'%'

9>.grant 資料庫開發人員,建立表格、索引、檢視、預存程序、函數。 。 。等權限。

grant 建立、修改、刪除 MySQL 資料表結構權限。

grant create on testdb.* to developer@'192.168.0.%';
grant alter on testdb.* to developer@'192.168.0.%';
grant drop on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 外鍵權限。

grant references on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 臨時表權限。

grant create temporary tables on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 索引權限。

grant index on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 檢視、檢視檢視原始碼 權限。

grant create view on testdb.* to developer@'192.168.0.%';
grant show view on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 預存程序、函數 權限。

grant create routine on testdb.* to developer@'192.168.0.%'; -- now, can show procedure status
grant alter routine on testdb.* to developer@'192.168.0.%'; -- now, you can drop a procedure
grant execute on testdb.* to developer@'192.168.0.%';

10>.grant 一般 DBA 管理某個 MySQL 資料庫的權限。

grant all privileges on testdb to dba@'localhost'

其中,關鍵字 “privileges” 可以省略。

11>.grant 高階 DBA 管理 MySQL 中所有資料庫的權限。

grant all on *.* to dba@'localhost'

12>.MySQL grant 權限,分別可以作用在多個層次上。

1. grant 作用在整個MySQL 伺服器上:

grant select on *.* to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。
grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库

2. grant 作用在單一資料庫上:

grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。

3. grant 作用在單一資料表上:

grant select, insert, update, delete on testdb.orders to dba@localhost;

4. grant 作用在表格中的欄位上:

grant select(id, se, rank) on testdb.apache_log to dba@localhost;

5. grant 作用在預存程序、函數上:

grant execute on procedure testdb.pr_add to 'dba'@'localhost'
grant execute on function testdb.fn_add to 'dba'@'localhost'

注意:修改完權限以後一定要刷新服務,或重新啟動服務,刷新服務用:FLUSH PRIVILEGES。

相關推薦:

MySQL查詢時間的相關知識

PHP和MySQL之間的關係

php、apache、mysql三者之間的關係


#

以上是使用者管理和權限和設定——mysql的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn