普通视图

发现新文章,点击刷新页面。
昨天以前首页

MySQL 如何启用 SSL 加密连接

2024年1月31日 00:00

MySQL 是一种流行的开源数据库管理系统,广泛应用于各种 Web 应用程序和数据存储中。然而,传统的 MySQL 连接在传输数据时并未加密,可能存在安全风险。启用 SSL 加密连接可确保数据在传输过程中的安全性和完整性。本文详细说明如何在 MySQL 中启用 SSL 加密连接。

准备工作

SSL 是一种被广泛使用的网络协议,用于在客户端和服务器之间建立安全通信通道。通过使用 SSL 可以确保传输的数据不被第三方窃听或篡改。启用 SSL 加密连接第一步是准备 SSL 证书。SSL 证书由可信证书颁发机构签发,用于验证服务器的身份。您可以从 CA 购买 SSL 证书,也可使用 OpenSSL 等工具自行生成。

在开始启用 SSL 之前,需要确保 MySQL 服务器和客户端已经安装了 SSL 模块。如果您使用的是 MySQL 二进制包,那么可能已经安装了 SSL 模块。如果您使用的是源码包,则需手动编译并安装 SSL 模块。

安装证书

将 SSL 证书安装到 MySQL 服务器上。通常,您需要将 SSL 证书和私钥文件复制到 MySQL 服务器的指定目录,如 /etc/mysql/ssl

编辑 MySQL 服务器配置文件,以启用 SSL 并指定 SSL 证书和私钥文件的路径。

配置示例

以下是一个 SSL 示例配置:

1
2
3
4
[mysqld]
ssl-ca=/etc/mysql/ssl/ca.pem
ssl-cert=/etc/mysql/ssl/server-cert.pem
ssl-key=/etc/mysql/ssl/server-key.pem

以下是 SSL 配置参数说明:

参数说明
ssl-caCA 证书的路径
ssl-cert服务器证书的路径
ssl-key服务器私钥的路径

重启测试

保存 MySQL 配置文件后,重启 MySQL 服务以应用新的配置。在 Linux 系统中可以使用以下命令重启 MySQL 服务:

1
sudo systemctl restart mysql

在配置完成后,您可以使用 mysql 命令行工具测试 SSL 连接。要启用 SSL 连接,您可以使用--ssl-ca, --ssl-cert--ssl-key 选项。以下是一个测试 SSL 连接示例:

1
mysql --host=localhost --port=3306 --user=root --password=your_password --ssl-ca=ca.pem --ssl-cert=server-cert.pem --ssl-key=server-key.pem

验证连接

测试 SSL 连接无误后,您可以在 MySQL 服务器中启用 SSL。要启用 SSL,您需要在 my.ini 配置文件中添加以下参数:

1
2
require_secure_transport=ON
ssl=ON

要验证 SSL 连接是否成功启用,您可在 MySQL 命令行工具中输入以下命令:

1
SHOW STATUS LIKE 'Ssl_cipher';

结果总结

如果看到类似以下输出,则表示 SSL 连接已经启用:

1
2
3
4
5
+--------------------+--------------------------------------------------------------+
| Variable_name | Value |
+--------------------+--------------------------------------------------------------+
| Ssl_cipher | DHE-RSA-AES256-GCM-SHA384 |
+--------------------+--------------------------------------------------------------+

启用 MySQL 的 SSL 加密连接可有效提高数据传输的安全性。我们建议您在生产环境中始终启用 SSL,以确保数据安全性。最后分享一个 OpenSSL 创建公私秘钥的示例:

1
2
openssl genrsa -out rsa_private.key 2048
openssl rsa -in rsa_private.key -pubout -out rsa_public.key

姐姐,你也不想让别人知道你的秘密吧? — 浅谈 Python 代码加密

2024年8月23日 11:13

像 python 这种非编译型的语言,在代码加密上有这先天性的弱势,虽然java 之类的编译成 jar 依然比较容易反编译回来,但是毕竟也算是提升了那么一点点门槛,再加上混淆神马的,基本就能避免一些入门级的破解了。

但是对于 python 这种,如果发布不想直接让别人看代码,最简单的办法就是打包成二进制。通常的做法就是 py2exe.

官网地址:https://www.py2exe.org

py2exe

py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.Spice

Development is hosted on GitHub. You can find the mailing listsvn, and downloads for Python 2 there. Downloads for Python 3 are on PyPI.

py2exe was originally developed by Thomas Heller who still makes contributions. Jimmy Retzlaff, Mark Hammond, and Alberto Sottile have also made contributions. Code contributions are always welcome from the community and many people provide invaluable help on the mailing list and the Wiki.

py2exe is used by BitTorrentSpamBayes, and thousands more – py2exe averages over 5,000 downloads per month.

In an effort to limit Wiki spam, this front page is not editable. Feel free to edit other pages with content relevant to py2exe. You will need an account to edit (but not to read) and your account information will be used only for the purposes of administering this Wiki.

The old py2exe web site is still available until that information has found its way into this wiki.

之前发布的各种美女爬虫基本都是通过 py2exe 打包的,虽然体积比较大,但是整体来说效果还算不错。

但是对于 web 框架,例如 flask django 之类的该怎么打包?这个就稍显麻烦一些了。

搜索一下,也能找到一些工具,例如 https://github.com/amchii/encryptpy 这个东西底层还是通过 cython 来实现的,如果不想使用这个工具,那么直接使用 cython 也是可以的,至于原理,本质上是直接把 py代码编译成了二进制文件。

下面直接用 cython 来实现:

pip install cython

编写编译脚本,叫什么无所谓,这里我的名称是cython_build.py:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize(["application/settings.py",
                           "PowerManagement/models.py",
                           "PowerManagement/views/meter.py",
                           "PowerManagement/views/meter_remote.py",
                           "PowerManagement/views/substation_picture.py",
                           "PowerManagement/views/circuit.py",
                           ])
)

建议将上面的代码放在项目的根目录下,要处理的 modules 使用相对路径来实现。

通过下面的命令编译 py 文件:

python3 cython_build.py build_ext --inplace

但是上面的代码有个问题,那就是–inplace 并没有吧所有的 so文件放到原来的目录下,编译之后,一些文件放到了项目根目录下:

扩展名为 so 的文件就是编译生成的二进制文件,此时如果直接运行项目会提示各种组件找不到,还需要将处理后的文件复制到原来的目录下:

mv *.so PowerManagement/views/

最后一步就是删除原来的 py 文件:

cd "PowerManagement/views/"
rm  *.py

到这里整个编译流程就算完成了,可以尝试重新启动服务了。

毕竟姐姐,你也不想你的代码被人随便给抄走吧?

❌
❌