您需要设置几个配置文件。 /etc/odbc.ini、/etc/odbcinst.ini 和 /etc/freetds/freetds.conf(这些位置对 Ubuntu 12.04 有效,并且可能对大多数 *nixes 是正确的)。
您需要安装 unixodbc 和 freetds(不确定 CentOS 上的软件包名称是什么)。在 Ubuntu 中,这将是 apt-get install unixodbc tdsodbc。
有关安装这些的帮助,请查看此问题Can't Install FreeTDS via Yum Package Manager
/etc/odbc.ini(此文件可能为空)
# Define a connection to a Microsoft SQL server
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssql]
Description = MSSQL Server
Driver = freetds
Database = XXXXXX
ServerName = MSSQL
TDS_Version = 7.1
/etc/odbcinst.ini
# Define where to find the driver for the Free TDS connections.
# Make sure you use the right driver (32-bit or 64-bit).
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
#Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
/etc/freetds/freetds.conf(或者您可以在 /etc/freetds.conf 中找到它)
# The basics for defining a DSN (Data Source Name)
# [data_source_name]
# host = <hostname or IP address>
# port = <port number to connect to - probably 1433>
# tds version = <TDS version to use - probably 8.0>
# Define a connection to the Microsoft SQL Server
[mssql]
host = XXXXXX
port = 1433
tds version = 7.1
您可能需要根据您的 MSSQL 版本更改上面的 tds version = 7.1 行。
进行这些更改后,您必须重新启动 apache。
在您的 PHP 代码中,您将像这样创建 PDO 对象:
$pdo = new PDO("dblib:host=mssql;dbname=$dbname", "$dbuser","$dbpwd");
请注意,您的用户名可能需要采用以下格式:domain\username。
此外,如果您在页面中执行 phpinfo() 并搜索“freetds”,您就会知道它有效,这将显示一个 mssql 部分,其中 freetds 列为库版本。