Home Database Mysql Tutorial Detailed explanation of permissions in MySQL

Detailed explanation of permissions in MySQL

Jan 13, 2018 pm 01:29 PM
mysql Detailed explanation

This article mainly introduces the permissions in MySQL, including the transactions that each permission can operate and some common command statements for operating permissions. Friends in need can refer to it. I hope it can help everyone.

1. Foreword

Many articles will say that the permissions of the database are based on the principle of least permissions. This sentence itself is not wrong, but it is an empty talk. Because of the minimum permissions, this thing is too abstract, and many times you don't know exactly what permissions it requires. Nowadays, many MySQL operations use root accounts. It's not that everyone doesn't know that using root permissions is too unsafe, but that many people don't know what kind of permissions should be given that are both safe and ensure normal operation. Therefore, this article is more about considering how we can simply configure a secure mysql in this case. Note: The test environment of this article is mysql-5.6.4
2. Introduction to Mysql permissions

There are 4 tables that control permissions in mysql, namely user table, db table, tables_priv Table, columns_priv table.

The verification process of mysql permission table is:

1. First, determine whether the connected ip, user name, and password exist from the three fields of Host, User, and Password in the user table. If it exists, it is verified.

2. After passing the identity authentication, assign permissions and verify in the order of user, db, tables_priv, columns_priv. That is, first check the global permission table user. If the corresponding permission in user is Y, then this user's permission on all databases is Y, and db, tables_priv, columns_priv will no longer be checked; if it is N, check this in the db table. The specific database corresponding to the user, and obtains the permission Y in the db; if the permission in the db is N, check the specific table corresponding to the database in tables_priv, and obtain the permission Y in the table, and so on.

3. What permissions does mysql have?

201557120429872.jpg (698×734)

201557120509083.jpg (702×342)

##4. Permission analysis at the database level (db table)

201557120536129.jpg (699×737)

##5. MySQL security configuration scheme

1 Limitation IP access to the mysql port

Windows can be restricted through Windows Firewall or ipsec, and Linux can be restricted through iptables.

2 Modify the port of mysql

Under Windows, you can modify the configuration file my.ini to achieve this. In Linux, you can modify the configuration file my.cnf to achieve this.

3 Set strong passwords for all users and strictly specify the access ip of the corresponding account

In mysql, the user's access ip can be specified in the user table

4 root privileges Account handling

It is recommended to set a strong password for the root account and specify that only local login is allowed

5 Log handling

If necessary, you can enable the query log, which will be recorded Login and query statements.

6 mysql process running account

It is forbidden to use local system to run mysql account under windows. You can consider using network service or creating a new account yourself, but you must give read access to the directory where the mysql program is located. Take permissions and read and write permissions of the data directory; Under Linux, create a new mysql account, and specify mysql to run as the mysql account during installation, and give read permissions to the directory where the program is located, and permissions to the directory where the data is located. Read and write permissions.

7 Disk permissions of the mysql running account

1) The mysql running account needs to be given read permissions to the directory where the program is located, as well as read and write permissions to the data directory

2) No Allows writing and executing permissions to be given to other directories, especially those with websites.

3) Cancel the execution permissions of the mysql running account for cmd, sh and other programs.

8 How to handle the mysql account used by the website

Create a new account and give the account all permissions in the database used. This not only ensures that the website can fully operate the corresponding database, but also ensures that the account will not affect security due to excessive permissions. An account given all permissions to a single database will not have super, process, file and other management permissions. Of course, if I can clearly know what permissions my website requires, it is better not to give more permissions, because many times the publisher does not know what permissions the website requires, so I only recommend the above configuration. And what I mean is general. Specifically, when there are only a few machines, and not many, I personally recommend giving only the necessary permissions. For details, please refer to the suggestions in the table above.

9 Delete useless database

The test database has default permissions for the newly created account

6. Analysis and prevention measures of mysql intrusion and privilege escalation
Generally speaking, there are several ways to escalate mysql privileges:

1 UDF privilege escalation

The key to this method is to import a dll file. Personally, I think that as long as the process account's write permissions on the directory are reasonably controlled, the dll file can be prevented from being imported; and if it is breached, as long as the process account's permissions are low enough , and it is impossible to perform high-risk operations, such as adding accounts, etc.

2 Write startup file

This method is the same as above, but it is still necessary to reasonably control the write permission of the process account to the directory.

3 When the root account is leaked

If the root account is not properly managed and the root account is invaded, the database information will definitely not be guaranteed. However, if the permissions of the process account and its permissions on the disk are controlled, the server can still ensure that it will not be compromised.

4 Ordinary account leaks (as mentioned above, accounts that only have all permissions for a certain library)

The ordinary account mentioned here refers to the account used by the website. I will give you a comparison A convenient suggestion is to simply give all permissions to a specific library. Account leakage includes injection and direct access to database account passwords after the web server is compromised.

At this time, the corresponding database data is not protected, but it will not threaten other databases. Moreover, the ordinary account here does not have file permissions and cannot export files to disk. Of course, the permissions of the process account will still be strictly controlled at this time.

What kind of permissions are given to ordinary accounts can be seen in the table above. It is really not possible to directly give all permissions to a library.
7. Common commands required for security configuration

1. Create a new user and grant permissions to the corresponding database


 grant select,insert,update,delete,create,drop privileges on database.* to user@localhost identified by 'passwd';
Copy after login


 grant all privileges on database.* to user@localhost identified by 'passwd';
Copy after login

2. Refresh permissions


flush privileges;
Copy after login

3. Display authorization


show grants;
Copy after login

4. Move Remove authorization


revoke delete on *.* from 'jack'@'localhost';
Copy after login

5. Delete user


drop user 'jack'@'localhost';
Copy after login

6. Change user name


rename user 'jack'@'%' to 'jim'@'%';
Copy after login

7. Change the user’s password


SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
Copy after login

8. Delete the database


##

drop database test;
Copy after login

9. Export files from the database

##

select * from a into outfile "~/abc.sql"
Copy after login

Related recommendations:

Detailed explanation of mysql permissions and security

Mysql permission system working principle

MySQL permission management

The above is the detailed content of Detailed explanation of permissions in MySQL. For more information, please follow other related articles on the PHP Chinese website!

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)

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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

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.

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.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

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

See all articles