使用PHP通过SSL连接到MySQL [英] Connecting to MySQL via SSL using PHP

查看:353
本文介绍了使用PHP通过SSL连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个脚本,该脚本将通过SSL连接到MySQL数据库.我一直在阅读有关该主题的大量文章,并遇到了许多设置方法.有些适用于我的情况,有些不适用于我的情况.这是我的情况:

I'm writing my first script that will connect via SSL to a MySQL database. I have been doing a lot of reading on the subject and have come across many options on how to set it up; some that apply to my situation and some that don't. Here is my situation:

MySQL服务器与脚本位于同一台计算机上.我的SSL证书已安装.通过使用以下代码的使用SSL的页面简单地连接到数据库是否安全?

The MySQL server is on the same machine as the script. My SSL Certificate is installed. Is it secure to simply connect to the database via a page that is using SSL with the following code?

Connecting via https://www.mysitehere.com/myscript.php

$con = mysql_connect("localhost", "username", "password", MYSQL_CLIENT_SSL);
if (!$con) {die('Could not connect: ' . mysql_error());}
mysql_select_db("my_database", $con);

推荐答案

MySQL服务器与脚本位于同一台计算机上.我的SSL 证书已安装.只需连接到 通过使用SSL并带有以下代码的页面访问数据库?

The MySQL server is on the same machine as the script. My SSL Certificate is installed. Is it secure to simply connect to the database via a page that is using SSL with the following code?

如果您控制服务器(VPS),并且PHP与MySQL在同一台计算机上(只需要允许来自同一台计算机的连接),那么将SSL用于MySQL是毫无意义的,只会导致开销.您应该只使用防火墙来保护MySQL ,就像您也应该使用memcached .

If you control the server(VPS) and PHP is on the same machine as MySQL(only need to allow connections from same machine) than it is pointless to use SSL for MySQL and will only result in overhead. You should just use firewall to protect MySQL like you should also do with memcached.

否则,您可以阅读MySQL的此部分以进行配置SSL .我认为所涉及的不只是$con = mysql_connect("localhost", "username", "password", MYSQL_CLIENT_SSL); .例如,您需要配置 SSL证书:

Otherwise you could read this section from MySQL to configure SSL. I think involves a little bit more than just $con = mysql_connect("localhost", "username", "password", MYSQL_CLIENT_SSL); . For example you need to configure your SSL Certificate:

mysqld --ssl-ca=ca-cert.pem \
       --ssl-cert=server-cert.pem \
       --ssl-key=server-key.pem

这篇关于使用PHP通过SSL连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆