Home  >  Article  >  Database  >  How to enable SSL connection protection database in MySQL

How to enable SSL connection protection database in MySQL

PHPz
PHPzOriginal
2023-09-09 19:43:481261browse

如何在 MySQL 中启用 SSL 连接保护数据库

How to enable SSL connection protection in MySQL

With the increasing importance of data security, many organizations and developers have begun to pay attention to how to enable SSL connection protection in database connections. SSL encryption protocol to protect data transmission. In a MySQL database, enabling SSL connections ensures that data is properly encrypted and authenticated during transmission. This article describes how to enable SSL connections in MySQL to protect your database and provides corresponding code examples.

Step one: Prepare SSL certificate and key

When enabling SSL connection, you need to prepare SSL certificate and key. Here are some basic preparation steps:

  1. Generate SSL certificate and key:
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server-key.pem -out server-cert.pem

This command will generate a self-signed SSL certificate and private key. Please make sure to keep these files securely.

  1. Copy the certificate and key files to the secure directory of the MySQL server:
$ sudo cp server-cert.pem /etc/mysql/ssl/
$ sudo cp server-key.pem /etc/mysql/ssl/

Please modify the corresponding directory according to your actual situation.

Step 2: Modify the MySQL configuration file

Next, you need to modify the MySQL configuration file to enable SSL connections.

  1. Open the MySQL configuration file:
$ sudo vi /etc/mysql/my.cnf
  1. Add the following configuration in the file:
[mysqld]
ssl-ca=/etc/mysql/ssl/server-cert.pem
ssl-cert=/etc/mysql/ssl/server-cert.pem
ssl-key=/etc/mysql/ssl/server-key.pem

These configuration items specify Path to SSL certificate and key. Please modify it according to your actual situation.

  1. Save and close the file.

Step 3: Restart the MySQL service

After completing the above configuration, you need to restart the MySQL service to make the configuration take effect:

$ sudo systemctl restart mysql

Step 4: Test the SSL connection

Now, you can test whether the SSL connection is successful.

  1. Connect to MySQL server:
$ mysql -h localhost -u username -p

Please replace username with your MySQL username.

  1. Check whether the connection has SSL enabled:
mysql> SHOW STATUS LIKE 'Ssl_cipher';

If the returned result contains RSA, it means that the SSL connection has been successfully enabled.

At this point, you have successfully enabled SSL connection in MySQL to protect the database.

Summary

Enabling SSL connections during data transfer is an effective way to ensure data security. This article describes how to enable SSL connections in MySQL and provides corresponding code examples. By following the steps above, you can enable SSL connections in your MySQL database to protect your data.

Note: The code examples in the article are for Linux-based MySQL configurations. For other operating systems, follow the guidelines for the appropriate platform.

The above is the detailed content of How to enable SSL connection protection database in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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