Recommendation: describes the security configuration of the MySQL database in detail _ MySQL

Source: Internet
Author: User
Tags mysql host
MySQL is a fully networked cross-platform relational database system and a distributed database management system with a client server architecture. It has the advantages of strong functions, ease of use, convenient management, fast operation speed, strong security and reliability. Users can use many languages to write programs that access the MySQL database, especially PHP, widely used

MySQL is a fully networked cross-platform relational database system and a distributed database management system with a client/server architecture. It has the advantages of strong functions, ease of use, convenient management, fast operation speed, strong security and reliability. Users can use many languages to write programs that access the MySQL database, especially PHP, it is widely used.

Because MySQL is a multi-platform database, its default configuration should be considered to be applicable in various circumstances, so further security reinforcement should be carried out in our own use environment. As a MySQL system administrator, we have the responsibility to maintain the data security and integrity of the MySQL database system.

The security configuration of the MySQL database must begin with two aspects: internal security of the system and external network security. In addition, we will briefly introduce some precautions and tips for programming.

2. internal system security:

First, we will briefly introduce the directory structure of the MySQL database. After MySQL is installed and the mysql_db_install script is run, the data directory and database initialization will be established. If we use the MySQL source code package and the installation directory is/usr/local/mysql, the data directory is usually/usr/local/mysql/var. The database system is composed of a series of databases, each containing a series of database tables. MySQL creates a database directory in the data directory with the database name. each database table uses the database table name as the file name, put the three files with the extension MYD, MYI, and frm in the Database Directory.

The MySQL authorization table provides flexible permission control for database access. However, if a local user has the permission to read database files, attackers only need to package and copy the database directories, copy it to the data directory of your local machine to access the stolen database. Therefore, the security of the MySQL host is the top priority. if the host is insecure and controlled by attackers, the security of MySQL cannot be discussed. The second is the security of data directories and data files, that is, permission settings.

From the perspective of some old binary distributions on the MySQL main site, the attribute of the data directory in version 3.21.xx is 775, which is very dangerous. any local user can read the data directory, therefore, database files are insecure. In version 3.22.xx, the attribute of the data directory is 770, which is also dangerous. local users in the same group can both read and write data, so data files are not secure. The attribute of the data directory of 3.23.xx is 700, which is better. only the user who starts the database can read and write the database files, ensuring the security of local data files.

If the user who starts the MySQL database is MysqSQL, the following directories and files are safe. pay attention to the data directory and the following attributes:

     shell>ls -l /usr/local/mysql total 40 drwxrwxr-x 2 root root 4096 Feb 27 20:07 bin drwxrwxr-x 3 root root 4096 Feb 27 20:07 include drwxrwxr-x 2 root root 4096 Feb 27 20:07 info drwxrwxr-x 3 root root 4096 Feb 27 20:07 lib drwxrwxr-x 2 root root 4096 Feb 27 20:07 libexec drwxrwxr-x 3 root root 4096 Feb 27 20:07 man drwxrwxr-x 6 root root 4096 Feb 27 20:07 mysql-test drwxrwxr-x 3 root root 4096 Feb 27 20:07 share drwxrwxr-x 7 root root 4096 Feb 27 20:07 sql-bench drwx------ 4 mysql mysql 4096 Feb 27 20:07 var shell>ls -l /usr/local/mysql/var total 8 drwx------ 2 mysql mysql 4096 Feb 27 20:08 mysql drwx------ 2 mysql mysql 4096 Feb 27 20:08 test shell>ls -l /usr/local/mysql/var/mysql total 104 -rw------- 1 mysql mysql 0 Feb 27 20:08 columns_priv.MYD -rw------- 1 mysql mysql 1024 Feb 27 20:08 columns_priv.MYI -rw------- 1 mysql mysql 8778 Feb 27 20:08 columns_priv.frm -rw------- 1 mysql mysql 302 Feb 27 20:08 db.MYD -rw------- 1 mysql mysql 3072 Feb 27 20:08 db.MYI -rw------- 1 mysql mysql 8982 Feb 27 20:08 db.frm -rw------- 1 mysql mysql 0 Feb 27 20:08 func.MYD -rw------- 1 mysql mysql 1024 Feb 27 20:08 func.MYI -rw------- 1 mysql mysql 8641 Feb 27 20:08 func.frm -rw------- 1 mysql mysql 0 Feb 27 20:08 host.MYD -rw------- 1 mysql mysql 1024 Feb 27 20:08 host.MYI -rw------- 1 mysql mysql 8958 Feb 27 20:08 host.frm -rw------- 1 mysql mysql 0 Feb 27 20:08 tables_priv.MYD -rw------- 1 mysql mysql 1024 Feb 27 20:08 tables_priv.MYI -rw------- 1 mysql mysql 8877 Feb 27 20:08 tables_priv.frm -rw------- 1 mysql mysql 428 Feb 27 20:08 user.MYD -rw------- 1 mysql mysql 2048 Feb 27 20:08 user.MYI -rw------- 1 mysql mysql 9148 Feb 27 20:08 user.frm

If the owner and attributes of these files are not the same, use the following two commands to correct them:

     shell>chown -R mysql.mysql /usr/local/mysql/var shell>chmod -R go-rwx /usr/local/mysql/var

Starting remote services with the root user has always been a security taboo, because if the service program encounters problems, remote attackers are very likely to gain full control of the host. MySQL has made minor changes since version 3.23.15. after installation by default, the service should be started by mysql users, and root users are not allowed to start the service. If you have to use the root user for startup, you must add the -- user = root parameter (./safe_mysqld -- user = root &). MySQL has the SQL statements of LOAD DATA INFILE and SELECT... INTO OUTFILE. if the root user starts the MySQL server, the database user has the write permission of the root user. However, MySQL still imposes some restrictions. for example, load data infile can only read globally readable files, and SELECT... into outfile cannot overwrite existing files.

Local log files cannot be ignored, including shell logs and MySQL logs. Some users log on to or back up the database locally for convenience, and sometimes directly include the database password in the command line parameters, such:

     shell>/usr/local/mysql/bin/mysqldump -uroot -ptest test>test.sql shell>/usr/local/mysql/bin/mysql -uroot -ptest

These commands are recorded by shell in History Files. for example, bash will write the. bash_history file in the user directory. if these files are accidentally read, the database password will be leaked. The SQL commands executed after you log on to the database are also recorded in the. mysql_history file in the user directory by MySQL. If the database user uses an SQL statement to modify the database password, the. mysql_history file will also leak. Therefore, do not add a password after-p during shell login and backup. Instead, enter the database password after prompt.

In addition, we should not allow these two files to record our operations, just in case.

     shell>rm .bash_history .mysql_history shell>ln -s /dev/null .bash_history shell>ln -s /dev/null .mysql_history

These two commands link these two files to/dev/null, so our operations will not be recorded in these two files.

3. external network security:

After the MySQL database is installed, the user table on the Unix platform is as follows:

     mysql> use mysql; Database changed mysql> select Host,User,Password,Select_priv,Grant_priv from user; +-----------+------+----------+-------------+------------+ | Host | User | Password | Select_priv | Grant_priv | +-----------+------+----------+-------------+------------+ | localhost | root | | Y | Y | | redhat | root | | Y | Y | | localhost | | | N | N | | redhat | | | N | N | +-----------+------+----------+-------------+------------+ 4 rows in set (0.00 sec)

The user table on Windows is as follows:

     mysql> use mysql; Database changed mysql> select Host,User,Password,Select_priv,Grant_priv from user; +-----------+------+----------+-------------+------------+ | Host | User | Password | Select_priv | Grant_priv | +-----------+------+----------+-------------+------------+ | localhost | root | | Y | Y | | % | root | | Y | Y | | localhost | | | Y | Y | | % | | | N | N | +-----------+------+----------+-------------+------------+ 4 rows in set (0.00 sec)

Let's first look at the user table on the Unix platform. Here, redhat is only the name of the machine on our testing machine. Therefore, MySQL on the Unix platform only allows the local machine to connect to the database by default. However, the default root user password is empty, so it is imperative to add a password to the root user. There are three methods to add passwords to database users:

1) use the mysqladmin command at the shell prompt to change the root user password: shell> mysqladmin-uroot password test.

In this way, the password of the root user of the MySQL database is changed to test. (Test is just an example. we cannot use this weak password that is easy to guess)

2) use set password to change the password: mysql> set password for root @ localhost = password ('test ');.

The password of the root user is changed to test.

3) directly modify the root user password of the user table:

     mysql> use mysql; mysql> update user set password=password('test') where user='root'; mysql> flush privileges;

In this way, the password of the root user of the MySQL database is changed to test. The Last Command flush privileges indicates force refresh of the memory authorization table. Otherwise, the buffer password is used. in this case, illegal users can also log on with the root user and empty password, until the MySQL server is restarted.

We also see anonymous users with empty users. although they do not have any permissions on Unix platforms, we should delete them for security reasons: mysql> delete from user where user = '';.

The user table of Windows MySQL is very different. we can see that the Host field is % apart from localhost. Here % indicates that any Host is allowed to connect to the MySQL server. this is very insecure and creates a possible opportunity for attackers. we must delete the record whose Host field is %: mysql> delete from user where host = '% ';.

By default, the empty password of the root user must also be modified. The three methods are the same as those on the Unix platform.

We noticed that anonymous users whose Host field is localhost have all permissions! That is to say, a local user can log on to the MySQL database server with an empty username and password to obtain the highest permission! Therefore, anonymous users must be deleted!

Mysql> delete from user where user = '';

Do not forget to use flush privileges to forcibly refresh the memory authorization table after the user table operation.

There are too many insecure factors in the installed Windows version of MySQL by default. we must configure it further after installation!

Five MySQL authorization tables: user, db, host, tables_priv and columns_priv provide flexible security mechanisms. two statements GRANT and REVOKE are introduced from MySQL 3.22.11 to create and delete user permissions, you can easily restrict which user can connect to the server, where to connect to the server, and what operations can be performed after the connection. As a MySQL administrator, we must understand the meaning of the authorization table and how to use GRANT and REVOKE to create, authorize, REVOKE, and delete users.

The MySQL authorization mechanism before version 3.22.11 is not complete and is significantly different from the new version. we recommend that you upgrade to the latest version of MySQL. (The operation example in this book is MySQL 3.23.49.

Sample) Let's first understand the structure of the authorization table.

1) structure and content of the MySQL authorization table:

     mysql> desc user; +-----------------+-----------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-----------------+------+-----+---------+-------+ | Host | char(60) binary | | PRI | | | | User | char(16) binary | | PRI | | | | Password | char(16) binary | | | | | | Select_priv | enum('N','Y') | | | N | | | Insert_priv | enum('N','Y') | | | N | | | Update_priv | enum('N','Y') | | | N | | | Delete_priv | enum('N','Y') | | | N | | | Create_priv | enum('N','Y') | | | N | | | Drop_priv | enum('N','Y') | | | N | | | Reload_priv | enum('N','Y') | | | N | | | Shutdown_priv | enum('N','Y') | | | N | | | Process_priv | enum('N','Y') | | | N | | | File_priv | enum('N','Y') | | | N | | | Grant_priv | enum('N','Y') | | | N | | | References_priv | enum('N','Y') | | | N | | | Index_priv | enum('N','Y') | | | N | | | Alter_priv | enum('N','Y') | | | N | | +-----------------+-----------------+------+-----+---------+-------+ 17 rows in set (0.01 sec)

The user table is the most important of the five authorization tables. it lists the users that can connect to the server and their encryption passwords, and specifies which global (Superuser) permissions they have. All permissions enabled in the user table are global permissions and apply to all databases. Therefore, we cannot grant any user the permission to access the mysql. user table!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.