我有一台 Ubuntu 12.04 服务器,正在尝试建立与 MSSQL 数据库的连接。
我已设法使用tsql
和进行连接isql
,但osql
不起作用,并且使用 PDO 连接 PHP 也不起作用。我将尝试提供尽可能多的信息,如果您需要更多信息,请告诉我,我会进行编辑。
freetds配置文件:
[MSSQL]
host = TPSACC
port = 54488
tds version = 8.0
odbc配置文件:
[MSSQL]
Description = MS SQL connection to PRODUCTION database
Driver = FreeTDS
Database = PRODUCTION
Server = TPSACC
UserName = sa
Password = pass
Trace = No
TDS_Version = 8.0
Port = 54488
odbcinst.ini:
[FreeTDS]
Description = ODBC for Microsoft SQL
Driver = /usr/local/lib/libtdsodbc.so
UsageCount = 1
Threading = 2
~> isql MSSQL sa 通过
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
~> tsql -S MSSQL -U'sa'-P'pass'
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>
~> osql -S MSSQL -U sa -P 传递
checking shared odbc libraries linked to isql for default directories...
strings: '': No such file
trying /tmp/sql ... no
trying /tmp/sql ... no
trying /etc ... OK
checking odbc.ini files
reading /home/toolplas/.odbc.ini
[MSSQL] not found in /home/toolplas/.odbc.ini
reading /etc/odbc.ini
[MSSQL] found in /etc/odbc.ini
found this section:
[MSSQL]
Description = MS SQL connection to PRODUCTION database
Driver = FreeTDS
Database = PRODUCTION
Server = TPSACC
UserName = sa
Password = pass
Trace = No
TDS_Version = 8.0
Port = 54488
looking for driver for DSN [MSSQL] in /etc/odbc.ini
found driver line: " Driver = FreeTDS"
driver "FreeTDS" found for [MSSQL] in odbc.ini
found driver named "FreeTDS"
"FreeTDS" is not an executable file
looking for entry named [FreeTDS] in /etc/odbcinst.ini
found driver line: " Driver = /usr/local/lib/libtdsodbc.so"
found driver /usr/local/lib/libtdsodbc.so for [FreeTDS] in odbcinst.ini
/usr/local/lib/libtdsodbc.so is an executable file
"Server" found, not using freetds.conf
Server is "TPSACC"
osql: no IP address found for "TPSACC"
在 PHP 中我有:
$conn = new PDO ("dblib:host=TPSACC;dbname=PRODUCTION","$username","$pw");
..或者..
$conn = new PDO ("dblib:host=TPSACC;port=54488;dbname=PRODUCTION","$username","$pw");
它们都返回此错误:
SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
我在这里被困了好几天,仍然不明白为什么只有一半的连接能够真正起作用。
非常感谢所有帮助,谢谢!
答案1
PHP 模块安装了吗?
在您的网络服务器( Ubuntu 中的info.php
apache2 目录:)中创建一个文件,用于查看详细信息。用于获取 unixODBC 加载的配置文件,用于获取 FreeTDS 详细信息;检查是否为。/var/www/html
<?php phpinfo() ?>
odbcinst -j
tsql -C
MS db-lib source compatibility: yes
如果MS db-lib
为“否”,则需要使用 配置 freetds --enable-msdblib
。
我的./configure
台词:
./configure --with-tdsver=8.0 --with-unixodbc=/usr/local --enable-msdblib
然后sudo make && sudo make install
这是我的工作配置:
Freetds.conf:
[MSSQL]
host = 192.168.1.200
port = 1433
tds version = 8.0
client charset = UTF-8
odbc.ini:
[mssql]
Description = MSSQL Server
Driver = freetds #The odbcinst.ini driver name
ServerName = MSSQL # The Freetds.conf connection name
Database = database
TDS_Version = 8.0
odbcinst.ini
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbclibtdsS.so
UsageCount = 1