MySQL用戶管理
Jun 21, 2016 am 09:09 AMmysql|用戶管理
MySQL管理員應(yīng)該知道如何設(shè)置MySQL用戶賬號,指出哪個(gè)用戶可以連接服務(wù)器,從哪里連接,連接后能做什么。MySQL 3.22.11開始引入兩條語句使得這項(xiàng)工作更容易做:GRANT語句創(chuàng)建MySQL用戶并指定其權(quán)限,而REVOKE語句刪除權(quán)限。兩條語句扮演了mysql數(shù)據(jù)庫的前端角色,并提供與直接操作這些表的內(nèi)容不同的另一種方法。CREATE和REVOKE語句影響4個(gè)表:授權(quán)表
內(nèi)容
user 能連接服務(wù)器的用戶以及他們擁有的任何全局權(quán)限
db 數(shù)據(jù)庫級權(quán)限
tables_priv 表級權(quán)限
columns_priv 列級權(quán)限
還有第5個(gè)授權(quán)表(host),但它不受GRANT和REVOKE的影響。
當(dāng)你對一個(gè)用戶發(fā)出一條GRANT語句時(shí),在user表中為該用戶創(chuàng)建一條記錄。如果語句指定任何全局權(quán)限(管理權(quán)限或適用于所有數(shù)據(jù)庫的權(quán)限),這些也記錄在user表中。如果你指定數(shù)據(jù)庫、表和列級權(quán)限,他們被分別記錄在db、tables_priv和columns_priv表中。
用GRANT和REVOKE比直接修改授權(quán)表更容易些,然而,建議你閱讀一下《MySQL安全性指南》。這些表異常重要,而且作為一名管理員,你應(yīng)該理解它們?nèi)绾纬紾RANT和REVOKE語句的功能水平。
在下面的章節(jié)中,我們將介紹如何設(shè)置MySQL用戶賬號并授權(quán)。我們也涉及如何撤權(quán)和從授權(quán)表中刪除用戶。
你可能也想考慮使用mysqlaccess和mysql_setpermission腳本,它是MySQL分發(fā)的一部分,它們是Perl腳本,提供GRANT語句的另一種選擇設(shè)置用戶賬號。mysql_setpermission需要安裝DBI支持。
1 創(chuàng)建用戶并授權(quán)
GRANT語句的語法看上去像這樣:
GRANT privileges (columns) ON what TO user IDENTIFIED BY "password" WITH GRANT OPTION
要使用該語句,你需要填寫下列部分:
privileges
授予用戶的權(quán)限,下表列出可用于GRANT語句的權(quán)限指定符:
權(quán)限指定符
權(quán)限允許的操作
ALTER 修改表和索引
CREATE 創(chuàng)建數(shù)據(jù)庫和表
DELETE 刪除表中已有的記錄
DROP 拋棄(刪除)數(shù)據(jù)庫和表
INDEX 創(chuàng)建或拋棄索引
INSERT 向表中插入新行
REFERENCE 未用
SELECT 檢索表中的記錄
UPDATE 修改現(xiàn)存表記錄
FILE 讀或?qū)懛?wù)器上的文件
PROCESS 查看服務(wù)器中執(zhí)行的線程信息或殺死線程
RELOAD 重載授權(quán)表或清空日志、主機(jī)緩存或表緩存。
SHUTDOWN 關(guān)閉服務(wù)器
ALL 所有;ALL PRIVILEGES同義詞
USAGE 特殊的“無權(quán)限”權(quán)限
上表顯示在第一組的權(quán)限指定符適用于數(shù)據(jù)庫、表和列,第二組數(shù)管理權(quán)限。一般,這些被相對嚴(yán)格地授權(quán),因?yàn)樗鼈冊试S用戶影響服務(wù)器的操作。第三組權(quán)限特殊,ALL意味著“所有權(quán)限”,UASGE意味著無權(quán)限,即創(chuàng)建用戶,但不授予權(quán)限。
columns
權(quán)限運(yùn)用的列,它是可選的,并且你只能設(shè)置列特定的權(quán)限。如果命令有多于一個(gè)列,應(yīng)該用逗號分開它們。
what
權(quán)限運(yùn)用的級別。權(quán)限可以是全局的(適用于所有數(shù)據(jù)庫和所有表)、特定數(shù)據(jù)庫(適用于一個(gè)數(shù)據(jù)庫中的所有表)或特定表的??梢酝ㄟ^指定一個(gè)columns字句是權(quán)限是列特定的。
user
權(quán)限授予的用戶,它由一個(gè)用戶名和主機(jī)名組成。在MySQL中,你不僅指定誰能連接,還有從哪里連接。這允許你讓兩個(gè)同名用戶從不同地方連接。MySQL讓你區(qū)分他們,并彼此獨(dú)立地賦予權(quán)限。
MySQL中的一個(gè)用戶名就是你連接服務(wù)器時(shí)指定的用戶名,該名字不必與你的Unix登錄名或Windows名聯(lián)系起來。缺省地,如果你不明確指定一個(gè)名字,客戶程序?qū)⑹褂媚愕牡卿浢鳛镸ySQL用戶名。這只是一個(gè)約定。你可以在授權(quán)表中將該名字改為nobody,然后以nobody連接執(zhí)行需要超級用戶權(quán)限的操作。
password
賦予用戶的口令,它是可選的。如果你對新用戶沒有指定IDENTIFIED BY子句,該用戶不賦給口令(不安全)。對現(xiàn)有用戶,任何你指定的口令將代替老口令。如果你不指定口令,老口令保持不變,當(dāng)你用IDENTIFIED BY時(shí),口令字符串用改用口令的字面含義,GRANT將為你編碼口令,不要象你用SET PASSWORD 那樣使用password()函數(shù)。
WITH GRANT OPTION子句是可選的。如果你包含它,用戶可以授予權(quán)限通過GRANT語句授權(quán)給其它用戶。你可以用該子句給與其它用戶授權(quán)的能力。
用戶名、口令、數(shù)據(jù)庫和表名在授權(quán)表記錄中是大小寫敏感的,主機(jī)名和列名不是。
一般地,你可以通過詢問幾個(gè)簡單的問題來識別GRANT語句的種類:
誰能連接,從那兒連接?
用戶應(yīng)該有什么級別的權(quán)限,他們適用于什么?
用戶應(yīng)該允許管理權(quán)限嗎?
下面就討論一些例子。
1.1 誰能連接,從那兒連接?
你可以允許一個(gè)用戶從特定的或一系列主機(jī)連接。有一個(gè)極端,如果你知道降職從一個(gè)主機(jī)連接,你可以將權(quán)限局限于單個(gè)主機(jī):
GRANT ALL ON samp_db.* TO boris@localhost IDENTIFIED BY "ruby"GRANT ALL ON samp_db.* TO fred@res.mars.com IDENTIFIED BY "quartz"
(samp_db.*意思是“samp_db數(shù)據(jù)庫的所有表)另一個(gè)極端是,你可能有一個(gè)經(jīng)常旅行并需要能從世界各地的主機(jī)連接的用戶max。在這種情況下,你可以允許他無論從哪里連接:
GRANT ALL ON samp_db.* TO max@% IDENTIFIED BY "diamond"
“%”字符起通配符作用,與LIKE模式匹配的含義相同。在上述語句中,它意味著“任何主機(jī)”。所以max和max@%等價(jià)。這是建立用戶最簡單的方法,但也是最不安全的。
取其中,你可以允許一個(gè)用戶從一個(gè)受限的主機(jī)集合訪問。例如,要允許mary從snake.net域的任何主機(jī)連接,用一個(gè)%.snake.net主機(jī)指定符:
GRANT ALL ON samp_db.* TO mary@.snake.net IDENTIFIED BY "quartz";
如果你喜歡,用戶標(biāo)識符的主機(jī)部分可以用IP地址而不是一個(gè)主機(jī)名來給定。你可以指定一個(gè)IP地址或一個(gè)包含模式字符的地址,而且,從MySQL 3.23,你還可以指定具有指出用于網(wǎng)絡(luò)號的位數(shù)的網(wǎng)絡(luò)掩碼的IP號:
GRANT ALL ON samp_db.* TO boris@192.168.128.3 IDENTIFIED BY "ruby" GRANT ALL ON samp_db.* TO fred@192.168.128.% IDENTIFIED BY "quartz" GRANT ALL ON samp_db.* TO rex@192.168.128.0/17 IDENTIFIED BY "ruby"
第一個(gè)例子指出用戶能從其連接的特定主機(jī),第二個(gè)指定對于C類子網(wǎng)192.168.128的IP模式,而第三條語句中,192.168.128.0/17指定一個(gè)17位網(wǎng)絡(luò)號并匹配具有192.168.128頭17位的IP地址?!?
如果MySQL抱怨你指定的用戶值,你可能需要使用引號(只將用戶名和主機(jī)名部分分開加引號)。
GRANT ALL ON samp_db.president TO "my friend"@"boa.snake.net"
1.2 用戶應(yīng)該有什么級別的權(quán)限和它們應(yīng)該適用于什么?
你可以授權(quán)不同級別的權(quán)限,全局權(quán)限是最強(qiáng)大的,因?yàn)樗鼈冞m用于任何數(shù)據(jù)庫。要使ethel成為可做任何事情的超級用戶,包括能授權(quán)給其它用戶,發(fā)出下列語句:
GRANT ALL ON *.* TO ethel@localhost IDENTIFIED BY "coffee" WITH GRANT OPTION
ON子句中的*.*意味著“所有數(shù)據(jù)庫、所有表”。從安全考慮,我們指定ethel只能從本地連接。限制一個(gè)超級用戶可以連接的主機(jī)通常是明智的,因?yàn)樗拗屏嗽噲D破解口令的主機(jī)。
有些權(quán)限(FILE、PROCESS、RELOAD和SHUTDOWN)是管理權(quán)限并且只能用"ON *.*"全局權(quán)限指定符授權(quán)。如果你愿意,你可以授權(quán)這些權(quán)限,而不授權(quán)數(shù)據(jù)庫權(quán)限。例如,下列語句設(shè)置一個(gè)flush用戶,他只能發(fā)出flush語句。這可能在你需要執(zhí)行諸如清空日志等的管理腳本中會有用:
GRANT RELOAD ON *.* TO flushl@localhost IDENTIFIED BY "flushpass"
一般地,你想授權(quán)管理權(quán)限,吝嗇點(diǎn),因?yàn)閾碛兴鼈兊挠脩艨梢杂绊懩愕姆?wù)器的操作。
數(shù)據(jù)庫級權(quán)限適用于一個(gè)特定數(shù)據(jù)庫中的所有表,它們可通過使用ON db_name.*子句授予:
GRANT ALL ON samp_db TO bill@racer.snake.net INDETIFIED BY "rock" GRANT SELECT ON samp_db TO ro_user@% INDETIFIED BY "rock"
第一條語句向bill授權(quán)samp_db數(shù)據(jù)庫中所有表的權(quán)限,第二條創(chuàng)建一個(gè)嚴(yán)格限制訪問的用戶ro_user(只讀用戶),只能訪問samp_db數(shù)據(jù)庫中的所有表,但只有讀取,即用戶只能發(fā)出SELECT語句。
你可以列出一系列同時(shí)授予的各個(gè)權(quán)限。例如,如果你想讓用戶能讀取并能修改現(xiàn)有數(shù)據(jù)庫的內(nèi)容,但不能創(chuàng)建新表或刪除表,如下授予這些權(quán)限:
GRANT SELECT,INSERT,DELETE,UPDATE ON samp_db TO bill@snake.net INDETIFIED BY "rock"
對于更精致的訪問控制,你可以在各個(gè)表上授權(quán),或甚至在表的每個(gè)列上。當(dāng)你想向用戶隱藏一個(gè)表的部分時(shí),或你想讓一個(gè)用戶只能修改特定的列時(shí),列特定權(quán)限非常有用。如:
GRANT SELECT ON samp_db.member TO bill@localhost INDETIFIED BY "rock"GRANT UPDATE (expiration) ON samp_db. member TO bill@localhost
第一條語句授予對整個(gè)member表的讀權(quán)限并設(shè)置了一個(gè)口令,第二條語句增加了UPDATE權(quán)限,當(dāng)只對expiration列。沒必要再指定口令,因?yàn)榈谝粭l語句已經(jīng)指定了。
如果你想對多個(gè)列授予權(quán)限,指定一個(gè)用逗號分開的列表。例如,對assistant用戶增加member表的地址字段的UPDATE權(quán)限,使用如下語句,新權(quán)限將加到用戶已有的權(quán)限中:
GRANT UPDATE (street,city,state,zip) ON samp_db TO assistant@localhost
通常,你不想授予任何比用戶確實(shí)需要的權(quán)限寬的權(quán)限。然而,當(dāng)你想讓用戶能創(chuàng)建一個(gè)臨時(shí)表以保存中間結(jié)果,但你又不想讓他們在一個(gè)包含他們不應(yīng)修改內(nèi)容的數(shù)據(jù)庫中這樣做時(shí),發(fā)生了要授予在一個(gè)數(shù)據(jù)庫上的相對寬松的權(quán)限。你可以通過建立一個(gè)分開的數(shù)據(jù)庫(如tmp)并授予開數(shù)據(jù)庫上的所有權(quán)限來進(jìn)行。例如,如果你想讓來自mars.net域中主機(jī)的任何用戶使用tmp數(shù)據(jù)庫,你可以發(fā)出這樣的GRANT語句:
GRANT ALL ON tmp.* TO ""@mars.net
在你做完之后,用戶可以創(chuàng)建并用tmp.tbl_name形式引用tmp中的表(在用戶指定符中的""創(chuàng)建一個(gè)匿名用戶,任何用戶均匹配空白用戶名)。
1.3 用戶應(yīng)該被允許管理權(quán)限嗎?
你可以允許一個(gè)數(shù)據(jù)庫的擁有者通過授予數(shù)據(jù)庫上的所有擁有者權(quán)限來控制數(shù)據(jù)庫的訪問,在授權(quán)時(shí),指定WITH GRANT OPTION。例如:如果你想讓alicia能從big.corp.com域的任何主機(jī)連接并具有sales數(shù)據(jù)庫中所有表的管理員權(quán)限,你可以用如下GRANT語句:
GRANT ALL ON sales.* TO alicia@%.big.corp.com INDETIFIED BY "applejuice" WITH GRANT OPTION
在效果上WITH GRANT OPTION子句允許你把訪問授權(quán)的權(quán)利授予另一個(gè)用戶。要注意,擁有GRANT權(quán)限的兩個(gè)用戶可以彼此授權(quán)。如果你只給予了第一個(gè)用戶SELECT權(quán)限,而另一個(gè)用戶有GRANT加上SELECT權(quán)限,那么第二個(gè)用戶可以是第一個(gè)用戶更“強(qiáng)大”。
2 撤權(quán)并刪除用戶
要取消一個(gè)用戶的權(quán)限,使用REVOKE語句。REVOKE的語法非常類似于GRANT語句,除了TO用FROM取代并且沒有INDETIFED BY和WITH GRANT OPTION子句:
REVOKE privileges (columns) ON what FROM user
user部分必須匹配原來GRANT語句的你想撤權(quán)的用戶的user部分。privileges部分不需匹配,你可以用GRANT語句授權(quán),然后用REVOKE語句只撤銷部分權(quán)限。
REVOKE語句只刪除權(quán)限,而不刪除用戶。即使你撤銷了所有權(quán)限,在user表中的用戶記錄依然保留,這意味著用戶仍然可以連接服務(wù)器。要完全刪除一個(gè)用戶,你必須用一條DELETE語句明確從user表中刪除用戶記錄:
%mysql -u root mysqlmysql>DELETE FROM user ->WHERE User="user_name" and Host="host_name";mysql>FLUSH PRIVILEGES;
DELETE語句刪除用戶記錄,而FLUSH語句告訴服務(wù)器重載授權(quán)表。(當(dāng)你使用GRANT和REVOKE語句時(shí),表自動重載,而你直接修改授權(quán)表時(shí)不是。)

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL query performance optimization needs to start from the core points, including rational use of indexes, optimization of SQL statements, table structure design and partitioning strategies, and utilization of cache and monitoring tools. 1. Use indexes reasonably: Create indexes on commonly used query fields, avoid full table scanning, pay attention to the combined index order, do not add indexes in low selective fields, and avoid redundant indexes. 2. Optimize SQL queries: Avoid SELECT*, do not use functions in WHERE, reduce subquery nesting, and optimize paging query methods. 3. Table structure design and partitioning: select paradigm or anti-paradigm according to read and write scenarios, select appropriate field types, clean data regularly, and consider horizontal tables to divide tables or partition by time. 4. Utilize cache and monitoring: Use Redis cache to reduce database pressure and enable slow query

CTEs are a feature introduced by MySQL8.0 to improve the readability and maintenance of complex queries. 1. CTE is a temporary result set, which is only valid in the current query, has a clear structure, and supports duplicate references; 2. Compared with subqueries, CTE is more readable, reusable and supports recursion; 3. Recursive CTE can process hierarchical data, such as organizational structure, which needs to include initial query and recursion parts; 4. Use suggestions include avoiding abuse, naming specifications, paying attention to performance and debugging methods.

The security of remote access to MySQL can be guaranteed by restricting permissions, encrypting communications, and regular audits. 1. Set a strong password and enable SSL encryption. Force-ssl-mode=REQUIRED when connecting to the client; 2. Restrict access to IP and user rights, create a dedicated account and grant the minimum necessary permissions, and disable root remote login; 3. Configure firewall rules, close unnecessary ports, and use springboard machines or SSH tunnels to enhance access control; 4. Enable logging and regularly audit connection behavior, use monitoring tools to detect abnormal activities in a timely manner to ensure database security.

WhensettingupMySQLtables,choosingtherightdatatypesiscrucialforefficiencyandscalability.1)Understandthedataeachcolumnwillstore—numbers,text,dates,orflags—andchooseaccordingly.2)UseCHARforfixed-lengthdatalikecountrycodesandVARCHARforvariable-lengthdata

1. The first choice for the Laravel MySQL Vue/React combination in the PHP development question and answer community is the first choice for Laravel MySQL Vue/React combination, due to its maturity in the ecosystem and high development efficiency; 2. High performance requires dependence on cache (Redis), database optimization, CDN and asynchronous queues; 3. Security must be done with input filtering, CSRF protection, HTTPS, password encryption and permission control; 4. Money optional advertising, member subscription, rewards, commissions, knowledge payment and other models, the core is to match community tone and user needs.

CTE is a temporary result set in MySQL used to simplify complex queries. It can be referenced multiple times in the current query, improving code readability and maintenance. For example, when looking for the latest orders for each user in the orders table, you can first obtain the latest order date for each user through the CTE, and then associate it with the original table to obtain the complete record. Compared with subqueries, the CTE structure is clearer and the logic is easier to debug. Usage tips include explicit alias, concatenating multiple CTEs, and processing tree data with recursive CTEs. Mastering CTE can make SQL more elegant and efficient.

Temporary tables are tables with limited scope, and memory tables are tables with different storage methods. Temporary tables are visible in the current session and are automatically deleted after the connection is disconnected. Various storage engines can be used, which are suitable for saving intermediate results and avoiding repeated calculations; 1. Temporary tables support indexing, and multiple sessions can create tables with the same name without affecting each other; 2. The memory table uses the MEMORY engine, and the data is stored in memory, and the restart is lost, which is suitable for cache small data sets with high frequency access; 3. The memory table supports hash indexing, and does not support BLOB and TEXT types, so you need to pay attention to memory usage; 4. The life cycle of the temporary table is limited to the current session, and the memory table is shared by all connections. When choosing, it should be decided based on whether the data is private, whether high-speed access is required and whether it can tolerate loss.

The steps for setting MySQL semi-synchronous replication are as follows: 1. Confirm the version supports and load the plug-in; 2. Turn on and enable semi-synchronous mode; 3. Check the status and operation status; 4. Pay attention to timeout settings, multi-slave library configuration and master-slave switching processing. It is necessary to ensure that MySQL 5.5 and above versions are installed, rpl_semi_sync_master and rpl_semi_sync_slave plugins, enable corresponding parameters in the master and slave library, and configure automatic loading in my.cnf, restart the service after the settings are completed, check the status through SHOWSTATUS, reasonably adjust the timeout time and monitor the plug-in operation.
