mysql grant
本文实例,运行于 MySQL 5.0 及以上版本。 MySQL 赋予用户权限命令的简单式可概括为: grant 权限 on 数据库对象 to 用户 一、grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。 grant select on testdb. * to common_user@ ' % ' gra
本文实例,运行于 MySQL 5.0 及以上版本。
MySQL 赋予用户权限命令的简单格式可概括为:
<p><span>grant</span><span> 权限 </span><span>on</span><span> 数据库对象 </span><span>to</span><span> 用户</span></p>
一、grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。
<p><span>grant</span><span>select</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> common_user@</span><span>'</span><span>%</span><span>'</span><span><br></span><span>grant</span><span>insert</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> common_user@</span><span>'</span><span>%</span><span>'</span><span><br></span><span>grant</span><span>update</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> common_user@</span><span>'</span><span>%</span><span>'</span><span><br></span><span>grant</span><span>delete</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> common_user@</span><span>'</span><span>%</span><span>'</span></p>
或者,用一条 MySQL 命令来替代:
<p><span>grant</span><span>select</span><span>, </span><span>insert</span><span>, </span><span>update</span><span>, </span><span>delete</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> common_user@</span><span>'</span><span>%</span><span>'</span></p>
二、grant 数据库开发人员,创建表、索引、视图、存储过程、函数。。。等权限。
grant 创建、修改、删除 MySQL 数据表结构权限。
<p><span>grant</span><span>create</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>;<br></span><span>grant</span><span>alter</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>;<br></span><span>grant</span><span>drop</span><span> on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>;</span></p>
grant 操作 MySQL 外键权限。
<p><span>grant</span><span>references</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>;</span></p>
grant 操作 MySQL 临时表权限。
<p><span>grant</span><span>create</span><span>temporary</span><span> tables </span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>;</span></p>
grant 操作 MySQL 索引权限。
<p><span>grant</span><span>index</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>;</span></p>
grant 操作 MySQL 视图、查看视图源代码 权限。
<p><span>grant</span><span>create</span><span>view</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>;<br></span><span>grant</span><span> show </span><span> view</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>;</span></p>
grant 操作 MySQL 存储过程、函数 权限。
<p><span>grant</span><span>create</span><span> routine </span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>; </span><span>--</span><span> now, can show procedure status</span><span><br></span><span>grant</span><span>alter</span><span> routine </span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>; </span><span>--</span><span> now, you can drop a procedure</span><span><br></span><span>grant</span><span>execute</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> developer@</span><span>'</span><span>192.168.0.%</span><span>'</span><span>;</span></p>
三、grant 普通 DBA 管理某个 MySQL 数据库的权限。
<p><span>grant</span><span>all</span><span>privileges</span><span>on</span><span> testdb </span><span>to</span><span> dba@</span><span>'</span><span>localhost</span><span>'</span></p>
其中,关键字 “privileges” 可以省略。
四、grant 高级 DBA 管理 MySQL 中所有数据库的权限。
<p><span>grant</span><span>all</span><span>on</span><span>*</span><span>.</span><span>*</span><span>to</span><span> dba@</span><span>'</span><span>localhost</span><span>'</span></p>
五、MySQL grant 权限,分别可以作用在多个层次上。
1. grant 作用在整个 MySQL 服务器上:
<p><span>grant</span><span>select</span><span>on</span><span>*</span><span>.</span><span>*</span><span>to</span><span> dba</span><span>@localhost</span><span>; </span><span>--</span><span> dba 可以查询 MySQL 中所有数据库中的表。</span><span><br></span><span>grant</span><span>all</span><span>on</span><span>*</span><span>.</span><span>*</span><span>to</span><span> dba</span><span>@localhost</span><span>; </span><span>--</span><span> dba 可以管理 MySQL 中的所有数据库</span></p>
2. grant 作用在单个数据库上:
<p><span>grant</span><span>select</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> dba</span><span>@localhost</span><span>; </span><span>--</span><span> dba 可以查询 testdb 中的表。</span></p>
3. grant 作用在单个数据表上:
<p><span>grant</span><span>select</span><span>, </span><span>insert</span><span>, </span><span>update</span><span>, </span><span>delete</span><span>on</span><span> testdb.orders </span><span>to</span><span> dba</span><span>@localhost</span><span>;</span></p>
这里在给一个用户授权多张表时,可以多次执行以上语句。例如:
<p><span>grant</span><span>select</span><span>(</span><span>user_id</span><span>,username) </span><span>on</span><span> smp.users </span><span>to</span><span> mo_user@</span><span>'</span><span>%</span><span>'</span><span> identified </span><span>by</span><span>'</span><span>123345</span><span>'</span><span>;<br></span><span>grant</span><span>select</span><span>on</span><span> smp.mo_sms </span><span>to</span><span> mo_user@</span><span>'</span><span>%</span><span>'</span><span> identified </span><span>by</span><span>'</span><span>123345</span><span>'</span><span>;</span></p>
4. grant 作用在表中的列上:
<p><span>grant</span><span>select</span><span>(id, se, rank) </span><span>on</span><span> testdb.apache_log </span><span>to</span><span> dba</span><span>@localhost</span><span>;</span></p>
5. grant 作用在存储过程、函数上:
<p><span>grant</span><span>execute</span><span>on</span><span>procedure</span><span> testdb.pr_add </span><span>to</span><span>'</span><span>dba</span><span>'</span><span>@</span><span>'</span><span>localhost</span><span>'</span><span><br></span><span>grant</span><span>execute</span><span>on</span><span>function</span><span> testdb.fn_add </span><span>to</span><span>'</span><span>dba</span><span>'</span><span>@</span><span>'</span><span>localhost</span><span>'</span></p>
六、查看 MySQL 用户权限
查看当前用户(自己)权限:
<p><span>show grants;</span></p>
查看其他 MySQL 用户权限:
<p><span>show grants </span><span>for</span><span> dba</span><span>@localhost</span><span>;</span></p>
七、撤销已经赋予给 MySQL 用户权限的权限。
revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:
<p><span>grant</span><span>all</span><span>on</span><span>*</span><span>.</span><span>*</span><span>to</span><span> dba</span><span>@localhost</span><span>;<br></span><span>revoke</span><span>all</span><span>on</span><span>*</span><span>.</span><span>*</span><span>from</span><span> dba</span><span>@localhost</span><span>;</span></p>
八、MySQL grant、revoke 用户权限注意事项
1. grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。
2. 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option“
<p><span>grant</span><span>select</span><span>on</span><span> testdb.</span><span>*</span><span>to</span><span> dba</span><span>@localhost</span><span>with</span><span>grant</span><span>option</span><span>;</span></p>
这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。
*************************************************************************************************
遇到 SELECT command denied to user '用户名'@'主机名' for table '表名' 这种错误,解决方法是需要把吧后面的表名授权,即是要你授权核心数据库也要。
我遇到的是SELECT command denied to user 'my'@'%' for table 'proc',是调用存储过程的时候出现,原以为只要把指定的数据库授权就行了,什么存储过程、函数等都不用再管了,谁知道也要把数据库mysql的proc表授权
*************************************************************************************************
参考:http://zhidao.baidu.com/question/19633785.html
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表指定列级权限。这里指定的权限适用于一个表的特定列。

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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 is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Building an SQL database involves 10 steps: selecting DBMS; installing DBMS; creating a database; creating a table; inserting data; retrieving data; updating data; deleting data; managing users; backing up the database.
