一般在為MySql創(chuàng)建用戶時建議使用GRANT前臺命令,當然如果對我們開發(fā)者而言,方法還有很多種,比如使用INSERT命令,甚至是直接修改mysql user數(shù)據(jù)表,但仍然建議按照MySQL規(guī)范去授權賬戶。因為它太容易忘記,特別整理方便參考。
成都創(chuàng)新互聯(lián)公司是專業(yè)的雙流網(wǎng)站建設公司,雙流接單;提供成都做網(wǎng)站、網(wǎng)站設計、外貿營銷網(wǎng)站建設,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行雙流網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
1、登錄MySQL
輸入mysql -u root和密碼即可登錄到Mysql。
2、選擇數(shù)據(jù)庫
語句如下:use mysql;
3、在mysql的user表中增加連接用戶
GRANT USAGE ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
其中:
“username”替換為將要授權的用戶名,比如clientusr;
“password”替換為clientusr設置的密碼;
4、可訪問數(shù)據(jù)表授權
創(chuàng)建好帳戶之后,就開始給上面的common user進行數(shù)據(jù)表授權,步驟3中增加的連接用戶默認權限都是“N”的,必須在db表中為該帳戶授權,允許其訪問專用數(shù)據(jù)庫,當然超級用戶就不說了。
使用下面語句:
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON dbx.* TO 'username'@'localhost' IDENTIFIED BY 'password';
本語句中的權限根據(jù)實際需要確定:
"dbx"替換為授權訪問的數(shù)據(jù)庫名,如果只給某張表授權:dbx.tablename
"username"是步驟2授權用戶名
"password"是步驟2授權用戶的設置密碼
這樣就為該用戶授予了對某數(shù)據(jù)表的SELECT, INSERT, UPDATE, DELETE, CAREATE, DROP權限。
5、生效授權,創(chuàng)建完畢
FLUSH PRIVILEGES;
備注:
1、不要直接使用INSERT語句添加user記錄,使用INSERT可能出現(xiàn):ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value錯誤。不過早期的MYSQL版本筆者倒沒出現(xiàn)這個錯誤,因為天緣一直都是直接修改user表或直接使用INSERT語句完成,后來升級MYSQL到5.1的時候,發(fā)現(xiàn)可能會出現(xiàn)這個錯誤。
2、上文3和4,也可使用一句話GRANT ALL ON tbx.* TO 'username' IDENTIFIED BY 'password',這句話會自動創(chuàng)建username并為之授權。更多授權權限可參考MYSQL官方網(wǎng)站。
慢慢看吧
mysql中可以給你一個用戶授予如select,insert,update,delete等其中的一個或者多個權限,主要使用grant命令,用法格式為:
grant 權限 on 數(shù)據(jù)庫對象 to 用戶
一、grant 普通數(shù)據(jù)用戶,查詢、插入、更新、刪除 數(shù)據(jù)庫中所有表數(shù)據(jù)的權利。
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@’%’
二、grant 數(shù)據(jù)庫開發(fā)人員,創(chuàng)建表、索引、視圖、存儲過程、函數(shù)。。。等權限。
grant 創(chuàng)建、修改、刪除 mysql 數(shù)據(jù)表結構權限。
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 存儲過程、函數(shù) 權限。
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.%’;
三、grant 普通 dba 管理某個 mysql 數(shù)據(jù)庫的權限。
grant all privileges on testdb to dba@’localhost’
其中,關鍵字 “privileges” 可以省略。
四、grant 高級 dba 管理 mysql 中所有數(shù)據(jù)庫的權限。
grant all on *.* to dba@’localhost’
五、mysql grant 權限,分別可以作用在多個層次上。
1. grant 作用在整個 mysql 服務器上:
grant select on *.* to dba@localhost; - dba 可以查詢 mysql 中所有數(shù)據(jù)庫中的表。
grant all on *.* to dba@localhost; - dba 可以管理 mysql 中的所有數(shù)據(jù)庫
2. grant 作用在單個數(shù)據(jù)庫上:
grant select on testdb.* to dba@localhost; - dba 可以查詢 testdb 中的表。
3. grant 作用在單個數(shù)據(jù)表上:
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 作用在存儲過程、函數(shù)上:
grant execute on procedure testdb.pr_add to ’dba’@’localhost’
grant execute on function testdb.fn_add to ’dba’@’localhost’
六、查看 mysql 用戶權限
查看當前用戶(自己)權限:
show grants;
查看其他 mysql 用戶權限:
show grants for dba@localhost;
七、撤銷已經(jīng)賦予給 mysql 用戶權限的權限。
revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;
八、mysql grant、revoke 用戶權限注意事項
1. grant, revoke 用戶權限后,該用戶只有重新連接 mysql 數(shù)據(jù)庫,權限才能生效。
2. 如果想讓授權的用戶,也可以將這些權限 grant 給其他用戶,需要選項 “grant option“
grant select on testdb.* to dba@localhost with grant option;
這個特性一般用不到。實際中,數(shù)據(jù)庫權限最好由 dba 來統(tǒng)一管理。
注意:修改完權限以后 一定要刷新服務,或者重啟服務
grant 權限 on 數(shù)據(jù)庫對象 to 用戶
一、grant 普通數(shù)據(jù)用戶,查詢、插入、更新、刪除 數(shù)據(jù)庫中所有表數(shù)據(jù)的權利。
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@'%'
或者
grant select, insert, update, delete on testdb.* to common_user@’%’
二、grant 數(shù)據(jù)庫開發(fā)人員,創(chuàng)建表、索引、視圖、存儲過程、函數(shù)。。。等權限。
grant 創(chuàng)建、修改、刪除 MySQL 數(shù)據(jù)表結構權限。
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 存儲過程、函數(shù) 權限。
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.%’;
三、grant 普通 DBA 管理某個 MySQL 數(shù)據(jù)庫的權限。
grant all privileges on testdb to dba@’localhost’
其中,關鍵字 “privileges” 可以省略。
四、grant 高級 DBA 管理 MySQL 中所有數(shù)據(jù)庫的權限。
grant all on *.* to dba@’localhost’
五、MySQL grant 權限,分別可以作用在多個層次上。
1. grant 作用在整個 MySQL 服務器上:
grant select on *.* to dba@localhost; — dba 可以查詢 MySQL 中所有數(shù)據(jù)庫中的表。
grant all on *.* to dba@localhost; — dba 可以管理 MySQL 中的所有數(shù)據(jù)庫
2. grant 作用在單個數(shù)據(jù)庫上:
grant select on testdb.* to dba@localhost; — dba 可以查詢 testdb 中的表。
3. grant 作用在單個數(shù)據(jù)表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;
這里在給一個用戶授權多張表時,可以多次執(zhí)行以上語句。例如:
grant select(user_id,username) on smp.users to mo_user@’%’ identified by ‘123345′;
grant select on smp.mo_sms to mo_user@’%’ identified by ‘123345′;
4. grant 作用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to dba@localhost;
5. grant 作用在存儲過程、函數(shù)上:
grant execute on procedure testdb.pr_add to ‘dba’@'localhost’
grant execute on function testdb.fn_add to ‘dba’@'localhost’
六、查看 MySQL 用戶權限
查看當前用戶(自己)權限:
show grants;
查看其他 MySQL 用戶權限:
show grants for zhangkh@localhost;
七、撤銷已經(jīng)賦予給 MySQL 用戶權限的權限。
revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;
八、MySQL grant、revoke 用戶權限注意事項
1. grant, revoke 用戶權限后,該用戶只有重新連接 MySQL 數(shù)據(jù)庫,權限才能生效。
2. 如果想讓授權的用戶,也可以將這些權限 grant 給其他用戶,需要選項 “grant option“
grant select on testdb.* to dba@localhost with grant option;
這個特性一般用不到。實際中,數(shù)據(jù)庫權限最好由 DBA 來統(tǒng)一管理。
mysql授權表共有5個表:user、db、host、tables_priv和columns_priv。
授權表的內容有如下用途:
user表
user表列出可以連接服務器的用戶及其口令,并且它指定他們有哪種全局(超級用戶)權限。在user表啟用的任何權限均是全局權限,并適用于所有數(shù)據(jù)庫。例如,如果你啟用了DELETE權限,在這里列出的用戶可以從任何表中刪除記錄,所以在你這樣做之前要認真考慮。
db表
db表列出數(shù)據(jù)庫,而用戶有權限訪問它們。在這里指定的權限適用于一個數(shù)據(jù)庫中的所有表。
host表
host表與db表結合使用在一個較好層次上控制特定主機對數(shù)據(jù)庫的訪問權限,這可能比單獨使用db好些。這個表不受GRANT和REVOKE語句的影響,所以,你可能發(fā)覺你根本不是用它。
tables_priv表
tables_priv表指定表級權限,在這里指定的一個權限適用于一個表的所有列。
columns_priv表
columns_priv表指定列級權限。這里指定的權限適用于一個表的特定列。
注:
對于GRANT USAGE ON,查看手冊有如下介紹和實例:
mysql GRANT USAGE ON *.* TO ‘zhangkh’@'localhost’;
一個賬戶有用戶名zhangkh,沒有密碼。該賬戶只用于從本機連接。未授予權限。通過GRANT語句中的USAGE權限,你可以創(chuàng)建賬戶而不授予任何權限。它可以將所有全局權限設為’N'。假定你將在以后將具體權限授予該賬戶。
第一:更改 “mysql” 數(shù)據(jù)庫里的 “user” 表里的 “host” 項,從”localhost”改稱'%'。 \x0d\x0a或者新加條記錄,“host” 項為要訪問的ip地址,并授權。重啟mysql服務。 \x0d\x0a第二:在系統(tǒng)防火墻添加例外端口:3306,并允許例外。 \x0d\x0a\x0d\x0a錯誤提示: \x0d\x0aERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server \x0d\x0a的解決方法: \x0d\x0a1。改表法。可能是你的帳號不允許從遠程登陸,只能在localhost。這個時候只要在localhost的那臺電腦,登入mysql后,更改 "mysql" 數(shù)據(jù)庫里的 "user" 表里的 "host" 項,從"localhost"改稱"%" \x0d\x0a1.mysql -u root -pvmware\x0d\x0amysqluse mysql;\x0d\x0amysqlupdate user set host = '%' where user = 'root';\x0d\x0amysqlselect host, user from user; \x0d\x0a\x0d\x0a2. 授權法。例如,你想myuser使用mypassword從任何主機連接到mysql服務器的話。 \x0d\x0a\x0d\x0aGRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; \x0d\x0a如果你想允許用戶myuser從ip為192.168.1.3的主機連接到mysql服務器,并使用mypassword作為密碼 \x0d\x0aGRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; \x0d\x0a\x0d\x0a3.在window自帶的防火墻里的例外添加3306端口 \x0d\x0a\x0d\x0a總結: \x0d\x0amysql -u root -p \x0d\x0amysqluse mysql; \x0d\x0amysqlselect 'host' from user where user='root'; \x0d\x0amysqlupdate user set host = '%' where user ='root'; \x0d\x0amysqlflush privileges; \x0d\x0amysqlselect 'host' from user where user='root'; \x0d\x0a第一句是以權限用戶root登錄 \x0d\x0a第二句:選擇mysql庫 \x0d\x0a第三句:查看mysql庫中的user表的host值(即可進行連接訪問的主機/IP名稱) \x0d\x0a第四句:修改host值(以通配符%的內容增加主機/IP地址),當然也可以直接增加IP地址 \x0d\x0a第五句:刷新MySQL的系統(tǒng)權限相關表 \x0d\x0a第六句:再重新查看user表時,有修改。。 \x0d\x0a重起mysql服務即可完成。
grant all privileges on *.* to jack@'localhost' identified by "jack" with grant option;
GRANT命令說明:
ALL PRIVILEGES 是表示所有權限,你也可以使用select、update等權限。
ON 用來指定權限針對哪些庫和表。
*.* 中前面的*號用來指定數(shù)據(jù)庫名,后面的*號用來指定表名。
TO 表示將權限賦予某個用戶。
jack@'localhost' 表示jack用戶,@后面接限制的主機,可以是IP、IP段、域名以及%,%表示任何地方。注意:這里%有的版本不包括本地,以前碰到過給某個用戶設置了%允許任何地方登錄,但是在本地登錄不了,這個和版本有關系,遇到這個問題再加一個localhost的用戶就可以了。
IDENTIFIED BY 指定用戶的登錄密碼。
WITH GRANT OPTION 這個選項表示該用戶可以將自己擁有的權限授權給別人。注意:經(jīng)常有人在創(chuàng)建操作用戶的時候不指定WITH GRANT OPTION選項導致后來該用戶不能使用GRANT命令創(chuàng)建用戶或者給其它用戶授權。
備注:可以使用GRANT重復給用戶添加權限,權限疊加,比如你先給用戶添加一個select權限,然后又給用戶添加一個insert權限,那么該用戶就同時擁有了select和insert權限。
首先打開phpMyadmin;
點擊用戶菜單;
在任意用戶菜單上點擊“編輯權限”;
修改密碼點擊執(zhí)行就OK了
權限意思可以對照下面翻譯:
數(shù)據(jù):
SELECT:允許讀取數(shù)據(jù)。
INSERT:允許插入和替換數(shù)據(jù)。
UPDATA:允許更改數(shù)據(jù)。
DELETE:允許刪除數(shù)據(jù)。
FILE:允許從文件中導入數(shù)據(jù)以及將數(shù)據(jù)導出至文件。
結構:
CREATE:允許創(chuàng)建新數(shù)據(jù)庫和表。
ALTER:允許修改現(xiàn)有表的結構。
INDEX:允許創(chuàng)建和刪除索引。
DROP:允許刪除數(shù)據(jù)庫和表。
CREATE TEMPORARY TABLES:允許創(chuàng)建暫時表。
CREATE VIEW:允許創(chuàng)建新的意見。
SHOW VIEW:顯示創(chuàng)建的看法。
CREATE ROUTINE:允許創(chuàng)建存儲過程。
ALTER ROUTINE:允許改變和下降存儲過程。
EXECUTE:允許許執(zhí)行存儲過程。
管理:
GRANT:允許添加用戶和權限,而不允許重新載入權限表。
SUPER:允許在達到最大允許數(shù)目時仍進行連接。
PROCESS:允許查看進程列表中的完整查詢。
RELOAD:允許重新載入服務器設置并刷新服務器的緩存。
SHUTDOWN:允許關閉服務器。
SHOW DATABASES:允許訪問完整的數(shù)據(jù)庫列表。
LOCK TABLES:允許鎖住當前線索的表。
REFERENCES:在此版本的 MySQL 中無效。
REPLICATION CLIENT:用戶有權詢問附屬者/控制者在哪里。
REPLICATION SLAVE:回復附屬者所需。
CREATE USER:允許創(chuàng)建,下降和重新命名的用戶帳戶。
分享標題:mysql視圖怎么授權,sqlserver創(chuàng)建用戶并授權視圖
本文路徑:http://www.muchs.cn/article20/phjjco.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供、電子商務、微信小程序、自適應網(wǎng)站、全網(wǎng)營銷推廣、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)