简体   繁体   中英

php cli mssql_connect - Unable to connect: SQL server is unavailable

Im trying to run a shell with cakephp but I'm not able to do it due to a SQL connection error, I'm always getting this

Warning:mssql_connect(): Unable to connect to server: XXXXX I'm sure the config for the data source is correct as I'm able to connect using the website side of the app.

Have anyone stumble with something like this?

BTW the webserver and the mssql server are not the same, they are on different hosts.

Environment Info ( I agree is a mess... ):

  • Web server
    • Windows server 2003 R2
    • PHP-5.2.4
    • IBM http server 6.1
    • Cakephp 1.2
  • DB Server
    • Windows server 2003 R2
    • Microsoft SQL server 2005

I have isolated the problem and found is not cakephp related, I created a script with just this line of code

mssql_connect('XXXX', 'YYYY', 'ZZZZ', true);

Giving me the same error. This should be something different between CLI and how php is run from the HTTP server.

Thank you!

Make sure you specify the port to connect to (usually 1433). If that still doesn't work its possible you need to play with named pipes settings. More information can be found in the comments on the PHP site: http://ca3.php.net/function.mssql-connect

I would also suggest you look into using the Microsoft SQL Serv drivers for PHP, they are much better maintained than the mssql ones. http://www.microsoft.com/download/en/details.aspx?id=20098 Here is an article explaining the differences: http://blogs.msdn.com/b/brian_swan/archive/2010/03/08/mssql-vs-sqlsrv-what-s-the-difference-part-1.aspx If you do go the sqlsrv route (and I really suggest you do) here are the connection parameters: http://php.net/manual/en/function.sqlsrv-connect.php Use example 3.

Here are the steps I followed to narrow and then fix the problem:

  • Check which php.ini is your cli using by running the next command: php -i
  • Isolate the problem, I was thinking it was something with cakephp, but at the end every problem is related to a single line in php, my problem was around this line of code mssql_connect('XXXX', 'YYYY', 'ZZZZ', true);
  • Based on the info provided here I found out that the next dll was outdated or not compatible with Windows Server 2003, just get ntwdblib.dll ver. 2000.80.2039.0, use this exact version as others like ver.2000.80.194.0 didn't worked for me.

As @charles mentioned if you are not working already in a project already deployed use sqlsrv drivers as those are maintained currently and the mssql are basically getting deprecated.

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM