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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

Enable root permissions with one click (quickly obtain root permissions) Enable root permissions with one click (quickly obtain root permissions) Jun 02, 2024 pm 05:32 PM

It allows users to perform more in-depth operations and customization of the system. Root permission is an administrator permission in the Android system. Obtaining root privileges usually requires a series of tedious steps, which may not be very friendly to ordinary users, however. By enabling root permissions with one click, this article will introduce a simple and effective method to help users easily obtain system permissions. Understand the importance and risks of root permissions and have greater freedom. Root permissions allow users to fully control the mobile phone system. Strengthen security controls, customize themes, and users can delete pre-installed applications. For example, accidentally deleting system files causing system crashes, excessive use of root privileges, and inadvertent installation of malware are also risky, however. Before using root privileges

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

See all articles