PHP 使用 SSL 连接到 MS SQL [英] PHP connect to MS SQL with SSL
问题描述
我要实现的目标很简单.我想通过安全连接从 PHP 脚本连接到外部 MS SQL 数据库.然而,事实证明这是有问题的,到目前为止,我花了三个小时的时间进行研究,我不知所措.
What I mean to achieve is very simple. I want to connect to an external MS SQL database from a PHP script over a secure connection. This has however proven problematic and, with three hours put in to research so far, I am at a loss.
客户端的平台是Ubuntu,这意味着我不能使用SQLSRV.安全连接已在不同的客户端上进行了测试,并且运行良好.我目前正在使用 PDO 和 DBlib 连接到数据库,也可以正常工作.
The platform for the client is Ubuntu, which means I can not use SQLSRV. The secure connection has been tested with different clients and it works fine. I am currently using PDO and DBlib to connect to the database, which also works fine.
我找不到任何强制建立安全连接的方法.我试过很多其他的驱动,都无济于事.
I was not able to find any method to force a secure connection. I have tried multiple other drivers, to no avail.
我有哪些选择?
我留下了以下 FreeTDS 日志...
I am left with the following FreeTDS logs...
config.c:543: Got a match.
config.c:565: host = 'XXXXXXXXXX'
config.c:595: Found host entry XXXXXXXXXX.
config.c:599: IP addr is XXXXXXXXXX.
config.c:565: port = '1433'
config.c:565: encryption = 'require'
config.c:565: check certificate hostname = 'no'
config.c:629: UNRECOGNIZED option 'check certificate hostname' ... ignoring.
config.c:565: ca file = 'XXXXXXXXXX.pem'
config.c:629: UNRECOGNIZED option 'ca file' ... ignoring.
推荐答案
如果你想使用 PDO,你可以设置 PDO ODBC.您需要设置配置文件 /etc/odbc.ini
、/etc/odbcinst.ini
和 /etc/freetds/freetds.conf
.
If you want to use PDO, you could set up PDO ODBC. You will need to setup the configuration files /etc/odbc.ini
, /etc/odbcinst.ini
and /etc/freetds/freetds.conf
.
您还必须安装 unixodbc 和 freetds:apt-get install unixodbc tdsodbc
.
You'll also have to install unixodbc and freetds: apt-get install unixodbc tdsodbc
.
您可以在此处查看更多信息:通过 PDO ODBC 将 PHP 连接到 MSSQL
You can see more info here: Connect PHP to MSSQL via PDO ODBC
要在 ODBC 中强制使用 SSL,请添加 Encrypt
关键字并将其在连接字符串中设置为 true.并设置您的 SQL Server 以使用 SSL:https://support.microsoft.com/en-us/kb/316898
To enforce SSL in ODBC, add the Encrypt
keyword and set it to true in your connection string. And setup your SQL Server to use SSL: https://support.microsoft.com/en-us/kb/316898
EDIT 2:根据 OP,按照以下规范将 encryption=require
和 check certificate hostname
添加到 freetds.config:http://www.freetds.org/userguide/freetdsconf.htm 以及以上步骤将解决问题
EDIT 2: According to the OP, adding encryption=require
and check certificate hostname
to freetds.config as per the following specification: http://www.freetds.org/userguide/freetdsconf.htm along with the above steps will fix the problem
这篇关于PHP 使用 SSL 连接到 MS SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!