使用 SSL 的 PHP 到 MySQL Amazon RDS,确认 SSL

发布于 2024-12-08 07:49:07 字数 246 浏览 5 评论 0原文

我发现很多关于使用 MYSQL_CLIENT_SSL 标志与 mysql_connect() 调用、与设置和 SSH 隧道相冲突的信息... MYSQL_CLIENT_SSL 可以接受吗?官方文档似乎表明是的,但是堆栈和其他地方的很多帖子都说隧道更好,但没有解释。

如果我使用 MYSQL_CLIENT_SSL,如何验证我是否确实获得了加密连接?我已将它添加到我的 mysql_connect() 调用中,并且它不会抛出任何错误,我认为它正在工作,但我如何确定?

I'm finding lots of conflicting information regarding usage of the MYSQL_CLIENT_SSL flag with a mysql_connect() call, vs setting up and SSH tunnel... Is MYSQL_CLIENT_SSL acceptable? the official documentation seems to indicate yes, but lots of posts on stack and elsewhere say that a tunnel is better but don't explain.

How can I verify that I'm actually getting an encrypted connection if I use MYSQL_CLIENT_SSL? I have added it to my mysql_connect() call and it doesn't throw any errors, I assume it's working, but how can I be sure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀞厅☆埖开 2024-12-15 07:49:07

您可以使用以下命令进行检查:

$connection = mysql_connect([host], [username], [password], false, MYSQL_CLIENT_SSL)
    or die(mysql_error());
$res = mysql_query("SHOW STATUS LIKE 'ssl_cipher';", $connection);
print_r(mysql_fetch_row($res));

输出应如下所示

Array
(
    [0] => Ssl_cipher
    [1] => xxx-xxx-xxxxxx-xxx
)

You can check it using this:

$connection = mysql_connect([host], [username], [password], false, MYSQL_CLIENT_SSL)
    or die(mysql_error());
$res = mysql_query("SHOW STATUS LIKE 'ssl_cipher';", $connection);
print_r(mysql_fetch_row($res));

The output should look like this

Array
(
    [0] => Ssl_cipher
    [1] => xxx-xxx-xxxxxx-xxx
)
ま柒月 2024-12-15 07:49:07

我知道这是一个老问题,但我也遇到了这个问题并解决了它,所以我想我会为后代分享我的答案。

Amazon 的文档非常有帮助。首先,您需要从 Amazon 下载 mysql-ssl-ca-cert.pem 文件(请参阅链接)。然后尝试使用该文件从终端进行连接。

mysql --host=mydb.c83ks9ckdk39.us-east-1.rds.amazonaws.com --user=myuser -p --ssl_ca=mysql-ssl-ca-cert.pem

Amazon 表示,您可以使用此 grant 语句限制与 SSL 的连接,因此请在连接时运行此语句。

GRANT USAGE ON *.* TO 'myuser'@'%' REQUIRE SSL

现在断开连接并尝试在不使用“--ssl_ca=mysql-ssl-ca-cert.pem”标志的情况下再次连接。如果您被拒绝,那么您就知道该用户现在需要 SSL 连接。现在你只需要正确设置 php 即可。像这样的事情:

$link = mysqli_init();
mysqli_options($link, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$link->ssl_set(NULL,NULL,"mysql-ssl-ca-cert.pem",NULL,NULL); 
$ok = $link->real_connect($MYSQL['host'], $MYSQL['user'], $MYSQL['pass'], $MYSQL['db'], 3306, NULL, MYSQLI_CLIENT_SSL);

如果您可以连接,那么您已通过 SSL 连接,并且您可以运行 Tom 的查询来确认是否需要。

$rs = $link->query("SHOW STATUS LIKE 'ssl_cipher'");
print_r($rs->fetch_assoc());

I know it's an old question, but I had this problem too and solved it so thought I would share my answer for posterity.

Amazon's docs are pretty helpful. First you need to download the mysql-ssl-ca-cert.pem file from Amazon (see the link). Then try to connect from the terminal using that file.

mysql --host=mydb.c83ks9ckdk39.us-east-1.rds.amazonaws.com --user=myuser -p --ssl_ca=mysql-ssl-ca-cert.pem

Amazon says that you can restrict a connection to SSL by using this grant statement, so run this statement while connected.

GRANT USAGE ON *.* TO 'myuser'@'%' REQUIRE SSL

Now disconnect and try to connect again without the "--ssl_ca=mysql-ssl-ca-cert.pem" flag. If you are denied, then you know that SSL connections are now required for this user. Now you just need to setup php correctly. Something like this:

$link = mysqli_init();
mysqli_options($link, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$link->ssl_set(NULL,NULL,"mysql-ssl-ca-cert.pem",NULL,NULL); 
$ok = $link->real_connect($MYSQL['host'], $MYSQL['user'], $MYSQL['pass'], $MYSQL['db'], 3306, NULL, MYSQLI_CLIENT_SSL);

If you can connect then you are connected with SSL and you can run Tom's query to confirm if you want.

$rs = $link->query("SHOW STATUS LIKE 'ssl_cipher'");
print_r($rs->fetch_assoc());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文