在CentOS系统下使用mssql_connect()时,一直出现“Unable to connect server”错误提示。
背景: centOS系统的web服务器需要连接 MSSQL 的数据库服务器。mssql数据库使用的是MSSSQL2000,(MSSQL2008也试过,问题同样)。
centOS系统使用yum install方式安装了Freetds和php-mssql模块,使用phpinfo()查看php-mssql已经安装成功。在centOS下使用tsql测试连接mssql账号密码,都正确。但是php文件中使用mssql_connect()函数连接时,总出现“Unable to connect server”的故障提示。
程序代码:
<?php
$conn = mssql_connect('192.168.0.112', 'sa', '****'); //此处出现错误,加不加端口号:1433,故障依旧
if (!$conn) {
die('Unable to connect! error: ' . mssql_get_last_message());
}
?> 展开
你必须保证两台服务器可以正常通许。用telnet来测试。
你必须保证该用户名可登陆到mssql。
在centOS下使用tsql 测试连接mssql 账号密码,一切正常,帐号密码无误。
这是php.net的事例
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = 'KALLESPC\SQLEXPRESS';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
这是别人的一些测试方法
There is certainly a php-mssql package available in the extras repository so just a `yum install php-mssql` should sort that out - it pulls in freetds as a dependency too. If you already have those installed but it doesn't seem to be connecting, the one possibility is that you are being blocked by SELinux. To check that run
# getsebool -a | grep httpd_can_network_connect
httpd_can_network_connect --> on
httpd_can_network_connect_db --> on
and to enable them if they are not on, do
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1