Home Database Mysql Tutorial MySQL 权限管理相关

MySQL 权限管理相关

Jun 07, 2016 pm 03:52 PM
mysql Permissions Related manage

MySQL 权限管理相关 本文通过理论联系实际操作,对MySQL权限相关的知识点做出梳理,并在实际应用中慢慢补充完善。 一、MySQL用户有哪些权限 以下部分copy自网上,点击浏览该博文 MYSQL到底都有哪些权限呢?从官网复制一个表来看看: 权限 权限级别 权限说明

MySQL 权限管理相关


本文通过理论联系实际操作,对MySQL权限相关的知识点做出梳理,并在实际应用中慢慢补充完善。


一、MySQL用户有哪些权限

以下部分copy自网上,点击浏览该博文

MYSQL到底都有哪些权限呢?从官网复制一个表来看看:

权限

权限级别

权限说明

CREATE

数据库、表或索引

创建数据库、表或索引权限

DROP

数据库或表

删除数据库或表权限

GRANT OPTION

数据库、表或保存的程序

赋予权限选项

REFERENCES

数据库或表

 

ALTER

更改表,比如添加字段、索引等

DELETE

删除数据权限

INDEX

索引权限

INSERT

插入权限

SELECT

查询权限

UPDATE

更新权限

CREATE VIEW

视图

创建视图权限

SHOW VIEW

视图

查看视图权限

ALTER ROUTINE

存储过程

更改存储过程权限

CREATE ROUTINE

存储过程

创建存储过程权限

EXECUTE

存储过程

执行存储过程权限

FILE

服务器主机上的文件访问

文件访问权限

CREATE TEMPORARY TABLES

服务器管理

创建临时表权限

LOCK TABLES

服务器管理

锁表权限

CREATE USER

服务器管理

创建用户权限

PROCESS

服务器管理

查看进程权限

RELOAD

 

 

服务器管理

执行flush-hosts, flush-logs, flush-privileges, flush-status, flush-tables, flush-threads, refresh, reload等命令的权限

REPLICATION CLIENT

服务器管理

复制权限

REPLICATION SLAVE

服务器管理

复制权限

SHOW DATABASES

服务器管理

查看数据库权限

SHUTDOWN

服务器管理

关闭数据库权限

SUPER

服务器管理

执行kill线程权限

 MySQL的权限如何分布,就是针对表可以设置什么权限,针对列可以设置什么权限等等,这个可以从官方文档中的一个表来说明:

权限分布

可能的设置的权限

表权限

'Select', 'Insert', 'Update', 'Delete', 'Create', 'Drop', 'Grant', 'References', 'Index', 'Alter'

列权限

'Select', 'Insert', 'Update', 'References'

过程权限

'Execute', 'Alter Routine', 'Grant'


二、MySQL权限经验原则:

    权限控制主要是出于安全因素,因此需要遵循一下几个经验原则:

    1、只授予能满足需要的最小权限,防止用户干坏事。比如用户只是需要查询,那就只给select权限就可以了,不要给用户赋予update、insert或者delete权限。

    2、创建用户的时候限制用户的登录主机,一般是限制成指定IP或者内网IP段。

    3、初始化数据库的时候删除没有密码的用户。安装完数据库的时候会自动创建一些用户,这些用户默认没有密码。

    4、为每个用户设置满足密码复杂度的密码。

    5、定期清理不需要的用户。回收权限或者删除用户。


三、权限增、删、查、改操作

添加:

权限的添加用grant (文档)命令来添加,具体格式如下:


GRANT
    <span><code>priv_type</code></span> [(<span><code>column_list</code></span>)]
      [, <span><code>priv_type</code></span> [(<span><code>column_list</code></span>)]] ...
    ON [<span><code>object_type</code></span>] <span><code>priv_level</code></span>
    TO <span><code>user_specification</code></span> [, <span><code>user_specification</code></span>] ...
    [REQUIRE {NONE | <span><code>ssl_option</code></span> [[AND] <span><code>ssl_option</code></span>] ...}]
    [WITH <span><code>with_option</code></span> ...]

<span><code>object_type</code></span>:
    TABLE
  | FUNCTION
  | PROCEDURE

<span><code>priv_level</code></span>:
    *
  | *.*
  | <span><code>db_name</code></span>.*
  | <span><code>db_name.tbl_name</code></span>
  | <span><code>tbl_name</code></span>
  | <span><code>db_name</code></span>.<span><code>routine_name</code>

<code>user_specification</code></span>:
    <span><code>user</code></span> [IDENTIFIED BY [PASSWORD] '<span><code>password</code></span>']

<span><code>ssl_option</code></span>:
    SSL
  | X509
  | CIPHER '<span><code>cipher</code></span>'
  | ISSUER '<span><code>issuer</code></span>'
  | SUBJECT '<span><code>subject</code></span>'

<span><code>with_option</code></span>:
    GRANT OPTION
  | MAX_QUERIES_PER_HOUR <span><code>count</code></span>
  | MAX_UPDATES_PER_HOUR <span><code>count</code></span>
  | MAX_CONNECTIONS_PER_HOUR <span><code>count</code></span>
  | MAX_USER_CONNECTIONS <span><code>count</code></span>
Copy after login


PS:

with_option 是对所授权限的一些限制或管理,例如 with grant option 表示被授权的用户拥有对其他用户授予同样权限的能力

删除:

权限的删除用revoke (官方文档)命令来添加,具体格式如下:

REVOKE
    priv_type [(column_list)]
      [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    FROM user [, user] ...

REVOKE ALL PRIVILEGES, GRANT OPTION
    FROM user [, user] ...
Copy after login


例子

假如你要删除用户jeffrey@localhost 对所有数据库的插入权限,请用下边命令

REVOKE INSERT ON *.* FROM 'jeffrey'@'localhost';
Copy after login

假如你还要随便删除其授权的权限,可用:

REVOKE grant option ON *.* FROM 'jeffrey'@'localhost'; 

当然,你也可以向赋予权限那样用all privilege删除所有权限(ps:all privilege 不包含 grant 权限)

REVOKE all privileges ON *.* FROM 'jeffrey'@'localhost'; 

查看:

show grants (官方文档)命令由于查看用户的权限

SHOW GRANTS [FOR <span><code>user</code></span>]
Copy after login

当 for user被缺省时,显示所有查询用户可见用户的权限:

<span style="font-size:14px;">mysql> show grants;
+-------------------------------------------------------------------------------
---------------------------------------------------------+
| Grants for root@localhost
                                                         |
+-------------------------------------------------------------------------------
---------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*81F
5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION
                                                         |
+-------------------------------------------------------------------------------
---------------------------------------------------------+
2 rows in set (0.00 sec)</span>
Copy after login
查看root用户权限:

<span>SHOW GRANTS FOR 'ROOT'@'LOCALHOST'</span>
Copy after login

四、实际应用及一些坑

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

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.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

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: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

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.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

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.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

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: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

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 Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

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

How to build a SQL database How to build a SQL database Apr 09, 2025 pm 04:24 PM

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.

See all articles