Home Database Mysql Tutorial MySQL的存取权限系统

MySQL的存取权限系统

Jun 07, 2016 pm 04:17 PM
mysql access Permissions system

看到很多网友提出关于MySQL登录不上服务器的问题,包括有的是在PHP中调用MySQL时发生的不能登录MySQL数据库服务器的问题,以为是PHP出了问题。其实是MySQL权限的问题。 MySQL的权限系统在MySQL的手册中是很长的一章,我把它打印出来足足印了20多页!这里就将

  看到很多网友提出关于MySQL登录不上服务器的问题,包括有的是在PHP中调用MySQL时发生的不能登录MySQL数据库服务器的问题,以为是PHP出了问题。其实是MySQL权限的问题。

  MySQL的权限系统在MySQL的手册中是很长的一章,我把它打印出来足足印了20多页!这里就将我对它的理解简要地写出来,希望能对刚刚接触MySQL的同志有点帮助;有说得不对的地方,也请同志们指出。

  在我了解了MySQL的权限机制后,不由得不赞叹它的严密与巧妙;也许所有的数据库系统都是如此罢,只是别的大型数据库把权限做得不需超级管理员亲自干预数据表而已。

  MySQL的权限保存在名为mysql的数据库中,有user、db、host、tables_priv、columns_priv 等五个表。

  首先,限制用户的登录的,只有 user 表,其中最常用的是 user、host、password这三个字段。其他的select_priv、update_priv、…… 这些字段分别表示该用户是否有select、update、……等权限,这些字段设置为'Y'表示该用户拥有对应的权限,'N'表示用户没有对应的权限。注意,,这里指定的权限是全局的,一旦你在user表中给了一个用户select或update权限,他就对这台服务器上任何数据库、任何表拥有上述权限!其中当然包括mysql数据库!!这意味着他可以通过更改user表里的数据非法地获取更大的权限!!!这是非常可怕的,所以建议除了给root用户外,不要在user表中分配权限。特别的,你可以建立一个几乎跟root拥有同样权限的用户(密码可不要告诉别人哟!)在忘记了root密码时就用得到了。在添加一个用户时,如果不指定权限字段的值,它们的默认值都是'N',——也就是这个用户什么权限也没有——你可以放心的在user表中添加用户,因为即使他能登录进来,却什么也干不了。

  还有一个应该注意的问题就是user表中的password字段。试想,既然root用户可以浏览mysql数据库,可以看到user表的password字段,是不是就是看到了其他用户的密码呢?不是的!user表的password字段保存的是用password()函数加密了的用户密码,当用户登录时,服务器将收到的用户输入的密码用password()函数加密,加密得到的字串与user表password字段如能匹配,则认为密码正确。password()没有逆运算,所以任何人无法从一个加密的字串得到密码的明文。同时,如果你手工修改user表,别忘了在更新password字段时一定要用password()函数加密。例如,你要允许一位名为 bill 的用户登录你的服务器,你给他设定一个 12345 的密码,则应该:

  insert into user (user,host,password) values ('bill','%',password('12345'));

  如果你直接

  insert into user (user,host,password) values ('bill','%','12345');

  的话,恐怕他登不上你的服务器,也会到奥索网上发帖子问的。

  注意,每当手工操作了跟权限有关的数据表以后,要执行一条 flush privileges 命令才能使其生效。

  下面的问题就是如何给用户分配权限了。如果你要给一个用户开一个数据库,并把有关这个数据库的所有或部分权限开放给他,这就用到了db表。db表的意义在于,当一个用户请求一个查询时,检测该用户对于他的查询所针对的数据库是否拥有进行该查询操作的权限,有,则允许查询;没有,则进一步咨询tables_priv表。db表的最常用字段是 user、db、和那一大堆有关权限的字段。user,不用说了,就是那个用户的用户名,跟user表中的对应;db,就是要分给他的数据库名。然后就把要给他的权限相对应的权限字段设为'Y'。同样,这些字段的值在不指定的时候默认是'N'。你也可以用GRANT/REVOKE命令给用户分配权限,可以是这样的:

  grant select,update,insert,delete,creater,alter,drop,index on bill.* to bill;

  这样就把针对bill这个数据库的几乎所有的权限给了用户bill。这里没有给的只是grant权限,关于这个权限,建议不要轻易给人,因为用户有了grant权限,也就可以将权限分配给其他用户。值得庆幸的是,拥有grant权限的用户能而且只能将他自己已经拥有的权限分配给别人。

  使用GRANT/REVOKE命令更改了权限后,不须执行 flush privileges 命令就可以使更改生效。

  上面讨论的是给一个用户完全开放一个数据库的问题,如果只想给一个用户一个特定的表的权限,就是 tables_priv 表发挥作用的时候啦。这里的关键性字段是user、db、table_name 。显而易见的,要给一个用户指定一个特定的数据库中特定表的权限,三个关键要素就是:哪个用户(user)、哪个数据库(db)、哪个表(table_name)。它的机理跟db表是类似的,我不必再重复。唯一不同的是这里用了一个SET类型的字段 table_priv 来指定用户对这个表权限,SET的成员有 SELECT, UPDATE, INSERT, DELETE, ALTER, CREATE, DROP, GRANT, INDEX, REFERENCE 等,你可以选择其中任意一个或几个分配给用户。

  在上述提及的几个权限表中以及未提及的host表中,都有一个host字段,它用来区分来自不同主机的、用户名可能相同的人,或者是给同一个用户从不同的主机连接时给予不同的权限。这种用法不很常用,但为了安全起见,建议root用户,如果不需要从远程连接,请将他的host设为 localhost,其他的则可以设为 % ,即任何主机。

  对我有用(3) 对我没用(3)

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 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)

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.

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'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

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.

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

How to view sql database error How to view sql database error Apr 10, 2025 pm 12:09 PM

The methods for viewing SQL database errors are: 1. View error messages directly; 2. Use SHOW ERRORS and SHOW WARNINGS commands; 3. Access the error log; 4. Use error codes to find the cause of the error; 5. Check the database connection and query syntax; 6. Use debugging tools.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

See all articles