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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles