I'm trying to connect to microsoft SQL server 2008 my application is running on a linux server and I have the dblib pdo driver installed. I used port forwarding to connect thru port 1433. I keep getting this message:
SQLSTATE[01002] Adaptive Server connection failed (severity 9);
PHP:
$connect = new PDO('dblib:host=ipaddres:1433;dbname=namedb','username','password');
I suggest to test DB connectivity w/ tsql in this way :
tsql -H server_address -p 1433 -U user -P pass -D dbname
in my case I found that simply "Reason: The password of the account has expired"
PDO gave me a strange error while tsql gave me the right error.
I got the same problem when rewriting a script from mssql_ functions to pdo.
I noticed that the old code did not have a mssql_select_db, but i used dbname in pdo.
When i removed the dbname parameter the connect worked.
Related
I have Centos7 installation with nginx/php5.6. MS SQL 2008 R2 is working on another server.
MSSQL definitely has allowed tcp connection, custom (non domain) user, firewall disabled, and I can connect to it using HeidiSQL and that setting from another Windows machine.
I can telnet to MSSQL from Centos (and I see Log error "Length specified in network packet payload did not match number of bytes read; the connection has been closed. Please contact the vendor of the client library." with centos ip address in MSSQL logs, so no errors here).
I couldn't connect to mssql from centos using php_mssql or sqlcmd;
During sqlcmd:
Microsoft (R) SQL Server Command Line Tool
Version 13.1.0007.0 Linux
[root#***]# /opt/mssql-tools/bin/sqlcmd -S *** -U ***
Password:
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Timeout error [258]. .
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Unable to complete login process due to delay in prelogin response.
During php interactive:
php > mssql_connect('***:***', '***','***');
PHP Warning: mssql_connect(): Unable to connect to server: *** in php shell code on line 1
I also tried tsql/freetds:
/etc/freetds.conf has section
[myserver]
host = ***
port = ***
client charset = UTF-8
tds version = 8.0 //also tried 7
[root#*]# tsql -S myserver -U ***
Password:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
5 //begins to count, i could't understand what does it mean
Also tried to clean it all and install php7.0 with sqlsrv extension - the same problem;
There are some network strange - Centos serv and MSSQL are in different networks, I couldnt ping mssql from that server, but can telnet, i'm using ip, not the host name during connection attempts, so don't think it's a big deal.
Now I am out of ideas where to dig, any suggestions, please?
It was caused by misconfiguration in the network, I don't understand exactly how it is possible - to make telnet connection, but fail to connect normally, but our IT guys repaired this problem in less then half an hour.
I am facing a weird problem here
we have a server A where the app files are stored
and B server with database
Tried to connect via command prompt from server A to B using the command
mysql -h xx.xx.xx.xx -u root -p password - and it worked
NOw i tried to create a php script in server A to connect to server B
the command is
$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password');
Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL
server on 'xx.xx.xx.xx' (13) Fatal error: Uncaught exception
'Exception' with message 'SQLSTATE[HY000] [2003] Can't connect to
MySQL server on 'xx.xx.xx.xx' (13)'
Unable to find a solution on this.
Can any help on this?
thank you
I got it working by running a command in the database server :)
setsebool httpd_can_network_connect_db=1
thanks for the replies yycdev
Try specifying the port in your connection string and ensure the database server is set to allow remote connections and the port is open on your firewall (both of these I suspect are already done as you are able to connect via the terminal but it never hurts to verify and check things).
Change your PDO connection and add the port=3306 or if you're using MAMP use port 8889
$this->db=new PDO('mysql:host=xx.xx.xx.xx;port=3306;dbname=databasename','root','password');
Another thing to check is if – SELinux is blocking network connections. Login as root and run
setsebool -P httpd_can_network_connect=1
I don't know much about that, but try to place the port in back of the script:
$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password',3306);
Environment:
Apache 2.2.9
PHP 5.4.37
freetds 0.91
SQL Server 2008
After last night's round of updates applied from Microsoft my mssql_connect stopped working.
tsql -LH x.x.x.x still shows the server and port correctly
tsql -H x.x.x.x -U user connects fine
I get nothing in the apache log files or the freetds log
Here is the code segment that is failing (critical info changed)
error_reporting(E_ALL); ini_set('display_errors',1);
$con = mssql_connect('server', 'user', 'pass');
var_dump($con);
if (!$con) {
if ( function_exists('error_get_last') ) {
var_dump(error_get_last());
}
die('Could not connect to the server!!'.mssql_get_last_message());
}
And here is the result:
Warning: mssql_connect(): Unable to connect to server: server in /home/....../public_html/states/index.php on line 25
bool(false)array(4) { ["type"]=> int(2) ["message"]=> string(55) "mssql_connect(): Unable to connect to server: server"["file"]=> string(63) "/home/....../public_html/states/index.php"["line"]=> int(25) } Could not connect to the server!!
Please help!
I'm unable to post a comment to ask this, but here is my thoughts.
Obviously it's not connecting to the server outside of the command line, so you'll want to double check its configuration (check the server port, specify the port, etc..)
Is this running from a Win or Unix type OS?
It is Linux. I found a work around for the moment. The problem appears to be on the Windows side where, although a tsql -LH command shows the IP and port, it is not actually answering queries on the port. You get connected to the server via TCP and that is it.
Because the SQL server is multi-homed, I added a secondary connection to the web app server that connects to it over the primary IP.
I'll chase down the IP binding and service issue later. Or maybe just convert it all to mariaDB or Postgres.
I'm facing this error while trying to connect to a remote SQL Server 2008.
SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53].
I use the following code to connect to the database.
$dsn = 'sqlsrv:server=SERVER_NAME:1433 ; database=testDB';
$user = 'user';
$password = 'password';
$dbConn = new PDO($dsn,$user,$password);
When I try to connect using sqlcmd from a command prompt I'm able to connect to the database. I tried connecting using the command :
SQLCMD -U user -P password -S SERVER_NAME,1433
What might be causing this issue? I also do not have SQL Configuration manager installed in the server. How can I fix/debug this issue?
I have installed FreeTDS 0.91, ODBC, on a Cpanel server running Centos 6.5x64. Everything appears to be running fine and I can connect to the remote MSSQL 2012 server using:
/usr/local/freetds/bin/tsql -S sqlserver -U test -P mypassword
and succesfully execute queries in the database.
I can also connect through:
isql -v sqlserverdatasource test mypasswordhere
But for some reason /usr/local/freetds/bin/tsql -LH server.ip.here
returns no information or errors which doesn't make much sense when it is proven I can connect with the other methods above.
So now when running a test script from a cpanel account on the machine I get:
Unknown host machine name (severity 2)
Here is the test script:
//Database connection function.
function getConnection() {
try {
//$dbconnect = new PDO("sqlserver:Server=server.ip.here,1433;Database=dbname", "user", "password");
$dbconnect = new PDO("dblib:host=server.ip.here,1433;dbname=dbname", 'user', 'password');
} catch (PDOException $e) {
echo "CONNECTION ERROR.<br>Error message:<br><br>" . $e->getMessage();
die();
}
if (!$dbconnect) {
die('Cant connect to database. Please try again later!');
}
else{
echo "i'm in!";
return $dbconnect;
}
}
The first commented line is the old one using sqlserv which I found did not work at all from what i can tell because of the x64 OS. I have also tried with "" around user and pass as well as no marks at all.
php -m does show PDO and pdo-dblib.
Any ideas where I can look next?
Update: This was fixed. I missed in freetds.conf:
[global]
# TDS protocol version
tds version = 8.0
It was originally set to 4.5 instead of 8.
The fix for me was with three steps:
First, I edited /etc/freetds/freetds.conf and changed the tds version like this:
tds version = 8.0
The second step was not entering port number. The port was already 1433, and not specifying it fixed the exact same issue on my case.
Lastly, to connect properly, I had to restart networking as #user1054844 mentioned as this:
/etc/init.d/networking restart
After all these steps, I was able to connect and work with the SQL Server database.
You actually did not need ODBC at all since your connect script is using pdo_dblib not odbc. You can just install FreeTDS than enable pdo_dblib via the compile time flag in rawopts and rebuild via EasyApache. Of course cPanel specifics for this are a bit different.
I just did this for a friend and decided to document it since it is hard to find accurate clear information for FreeTds and pdo_dblib on cPanel.
Guide is here: FreeTDS And pDO_dblib On cPanel