Home  >  Article  >  Database  >  【Must Know】Five important security tips you should take to strengthen MySQL

【Must Know】Five important security tips you should take to strengthen MySQL

青灯夜游
青灯夜游forward
2022-09-20 11:00:072001browse

【Must Know】Five important security tips you should take to strengthen MySQL

The database has long been an important part of your balanced architecture, and is arguably the most important part. Today, the pressure has been toward your largely disposable and stateless infrastructure, which puts a greater burden on your database to be both reliable and secure since all the other servers will inevitably be on the server along with the rest of your data. State information is stored in the database.

Your database is every attacker’s prize. As attacks become more sophisticated and networks become more hostile, taking extra steps to harden your database is more important than ever.

MySQL is the most popular and favorite database among developers and administrators due to its speed and overall ease of use. Unfortunately, this ease of use comes at the expense of security. Even though MySQL can be configured with strict security controls, your normal default MySQL configuration may not use them. In this article, I'll cover five important steps you should take to harden your MySQL database.

Step 1: Set a strong password

For all database users, it is important to use strong passwords. Given that most people don't manually log into a database often, use a password manager or the command-line tool pwgen to create a random 20-character password for your database account. This is still important even if you use additional MySQL access controls to limit where specific accounts can log in (such as strictly limiting accounts to localhost).

The most important MySQL account to set a password for is the root user. By default, on many systems, this user has no password. In particular, Red Hat-based systems do not set a password when installing MySQL; while Debian-based systems prompt you for a password during an interactive installation, a non-interactive installation (as you might do using Configuration Manager ) will skip it. Additionally, you can still skip setting a password during an interactive installation.

You might think that letting the root user not enter a password is not a big security risk. After all, the user is set to "root@localhost", which you might think means you have to root the computer before you can become that user. Unfortunately, this means that any user who can trigger the MySQL client from localhost can log in as the MySQL root user using the following command:

*$ mysql — user root*

So if you don't set a password for the root user, anyone who can get a local shell on your MySQL machine will now have full control of your database.

To fix this vulnerability, use the mysqladmin command to set a password for the root user:

$ sudo mysqladmin password

Unfortunately, MySQL runs as root Run background tasks as user. Once you set a password, these tasks break unless you take the extra step of hardcoding the password into the /root/.my.cnf file:

[mysqladmin]
user = rootpassword = yourpassword

However, this means you must set the password in plain text The form is stored on the host machine. But you can at least use Unix file permissions to restrict access to the file to only the root user:

sudo chown root:root /root/.my.cnf
sudo chmod 0600 /root/.my.cnf

Step Two: Delete Anonymous Users

Anonymous accounts are both A MySQL account with no username and no password. You don't want an attacker to have any kind of access to your database without a password, so look in the output of this command for any MySQL users logged with a blank username:

> SELECT Host, User FROM mysql.user;
+ — — — — — — — — — — — — + — — — -+
| Host | User |
+ — — — — — — — — — — — — + — — — -+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | |
| localhost | root |
+ — — — — — — — — — — — — + — — — -+
4 rows in set (0.00 sec)

at these roots There is an anonymous user ( localhost ) among the users, which is empty in the User column. You can clear specific anonymous users using the following command:

> drop user ""@"localhost";
> flush privileges;

If you find any other anonymous users, make sure to delete them.

Step 3: Follow the principle of least privilege

The principle of least privilege is a security principle that can be summarized as follows:

Just for An account provides the access necessary to perform the job and no more.

This principle can be applied to MySQL in a variety of ways. First, when using the GRANT command to add database permissions to a specific user, be sure to restrict only the permissions that the user needs to access the database:

> grant all privileges on mydb.* to someuser@"localhost" identified by 'astrongpassword';
> flush privileges;

If the user only needs to access a specific table (for example, the users table), use Replace mydb.users or any name of your table with mydb.* (grant permission to all tables).

Many people grant users full access to the database; but if your database user only needs to read the data and not change the data, you will need to take the extra step of granting read-only access to the database:

> grant select privileges on mydb.* to someuser@"localhost" identified by 'astrongpassword';
> flush privileges;

Finally, many database users will not access the database from localhost, usually the administrator will create them, like this:

> grant all privileges on mydb.* to someuser@"%"  identified by 'astrongpassword';
> flush privileges;

This will allow "someuser" to access the database from any network. However, if you have a well-defined set of internal IPs, or - even better - have VLANS set up so that all your application servers are on different subnets from other hosts, then you can take advantage of this to limit "someuser" , so that the account can only access the database from a specific network:

> grant all privileges on mydb.* to someuser@10.0.1.0/255.255.255.0 identified by 'astrongpassword';
> flush privileges;

第四步:启用 TLS

设置强密码仅只有攻击者可以在网络上读取你的密码或者其他敏感数据的情况下才能达到此目的。因此,使用 TLS 保护你的所有网络流量比以往任何时候都更加重要。

MySQL 也不例外。

幸运的是,在 MySQL 中启用 TLS 比较简单。一旦你有了你的主机的有效证书,只需要在你的主 my.cnf 文件的 [mysqld]部分添加以下几行 :

[mysqld]
ssl-ca=/path/to/ca.crt
ssl-cert=/path/to/server.crt
ssl-key=/path/to/server.key

为了额外的安全性,还可以添加 ssl-cipher 配置选项,其中包含一个被认可的密码列表,而不是只接受默认的密码列表,这可能包括较弱的 TLS 密码。我推荐使用  Mozilla Security/Server Side TLS page 所推荐的现代或者中级密码套件。

一旦服务器端设置了 TLS ,你可以限制客户端必须采用 TLS 进行连接,通过在 GRANT 语句中添加 REQUIRE SSL :

> grant all privileges on mydb.* to someuser@10.0.1.0/255.255.255.0 identified by 'astrongpassword' REQUIRE SSL;

> flush privileges;

第五步:加密数据库密钥

虽然现在很多人都知道使用单向散列(理想情况下是像 bcrypt 这样慢速散列 ),保护用户数据库存储的密码有多重要,但通常没过多考虑使用加密来保护数据库上其他的敏感数据。事实上,许多管理员会告诉你他们的数据库是加密的,因为磁盘本身是加密的。这实际上会影响你的数据库加固,不是因为磁盘加固有缺陷或糟糕的做法,而是因为它会给你一种错误的信任感。

磁盘加密保护你的数据库数据,以防止有人从你的服务器窃取磁盘(或者你买了二手磁盘后忘记擦除磁盘),但是磁盘加密并不能在数据库本身运行时保护你,因为驱动器需要处于解密状态才能被读取。

要保护数据库中的数据,你需要采取额外的措施,在存储敏感字段之前对它们进行加密。这样如果攻击者找到了某种方法来转存完整的数据库,你的敏感字段仍然会受到保护。

有许多加密数据库中字段的方法,而且 MySQL 支持本地加密命令。无论你采取哪种加密方法,我都建议避免你需要将解密密钥存储在数据库本身的加密方法。

理想情况下,你会把解密的密钥存储在应用服务器上,作为本地GPG密钥(如果你使用GPG进行加密)或者将其存储为应用程序服务器上的环境变量。这样即使攻击者可能找到一种方法来破坏应用程序服务器的服务器,他也必须将攻击转换为本地shell访问,以此来获取你的解密密钥。

MySQL 加固原则:掌握最小权限原则

有很多方法来锁定你的MySQL服务器。确切地说,你如何实施这些步骤取决于你如何设置自己的数据库,以及它在网络中的位置。

虽然前面的五个步骤将有助于保护你的数据库,但我认为更需要掌握的最重要的整体步骤是最小权限原则。你的数据库可能存储来一些非常有用的数据,如果你确保用户和应用程序只具有执行其工作的所需的最小访问权限,那么你将限制攻击者能够做什么,如果黑客找到来危害该用户或者应用程序的方法。

【相关推荐:mysql视频教程

The above is the detailed content of 【Must Know】Five important security tips you should take to strengthen MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete