看到很多网友提出关于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)

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

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.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

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


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

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor