您需要设置多个配置文件。和(这些位置对 Ubuntu 12.04 有效,并且可能对于大多数 *nixes 是正确的)。/etc/odbc.ini
/etc/odbcinst.ini
/etc/freetds/freetds.conf
你需要安装 和 (不确定 CentOS 上的软件包名称是什么)。在 Ubuntu 中,这将是 .unixodbc
freetds
apt-get install unixodbc tdsodbc
有关安装这些的帮助,请查看此问题 无法通过 Yum 包管理器安装 FreeTDS
/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
另外,如果您在页面中执行并搜索“freetds”,则会知道它是有效的,这将显示一个mssql部分,其中freetds被列为库版本。phpinfo()