MSSQL VIA FreeTDS, ODBC, and Cpanel Unknown host machine name (severity 2) - php

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

Related

PHP Sql Server PDOException:could not find driver

My server is a Windows 2008 server. PHP Version 7.2.7 is installed and running. Sql Server 11 (64 bit) is installed and is working (there is a couple asp.net apps running and already using that database)
I downloaded the PHP Sql Server Drivers from Microsofts website and placed the .dll files in the PHP ext directory.
In my PHP.ini I added:extension=php_pdo_sqlsrv_7_nts_x64
In my .php file I am using to test my db connection I have:
$SqlServer = "THISSERVER\SQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword");
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";}
I am getting:
PHP Fatal error: Uncaught PDOException: could not find driver in D:\Inetpub\wwwroot\TechStory2\DBtest.php:7 (Line 7 is the $SqlServerCon line).
What did I do wrong? and What do I need to do to get a connection to Sql Server?
I got it figured out. I had to install the ODBC Driver 17 for SQL Server (msodbcsql_17.2.0.1_x64.msi) on my server. The SQL Server Native Client 11.0 was installed but not the ODBC Driver for SQL Server.
For future reference for anyone else with this or a similar issue...
It can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=56567 (note: if you have a 32 bit server, you will want to install the msodbcsql_17.2.0.1_x86.msi - If you accidentally try to install the incorrect version, it will let you know during the installation). After the driver is installed, you need to reboot the server. It won't prompt you to restart, but you'll need to.
In my PHP.ini I have added extension=php_pdo_sqlsrv_72_nts.dll and extension=php_sqlsrv_72_nts_x64.dll They can be downloaded at https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017
More info can be found at https://learn.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017 and https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017
I can now establish a connection to Sql Server using either sqlsrv_connect or PDO.
PDO connection test:
$SqlServer = "THISSERVER\SQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword");
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";}
sqlsrv_connect connection test:
$SqlServer = "THISSERVER\SQLEXPRESS";
$DbConnInfo = array( "Database"=>"TheDatabase", "UID"=>"DbUName", "PWD"=>"DbPassword");
$SqlServerCon = sqlsrv_connect( $SqlServer, $DbConnInfo);
if( $SqlServerCon ) {echo "Connection established";}
else
{echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));}
As commentators said - adding .dll at the end of extension= config line should be good start with your issue.
I can also see that you're trying to load NTS version of extension (NTS stands for non thread safe). Are you sure that you're going to load right version of extension? Please check if you're running PHP as NTS runtime or not - you can check it with phpinfo();.

Timeout while connecting to mongo database with mongo php ext

Timeout while connecting to MongoDB database with mongo php ext under Ubuntu 12.04 and php 5.6
I'm using
latest Apache 2.4
Ubuntu 12.04
php 5.6 with mongo ext Version 1.6.14
MongoDB v3.4 running in an lxc container
And the problem is when I connect to the mongo database.
I'm using this connection string:
mongodb://myDbUser:passW0rT#0.0.0.0:27020/myDb
(password, database and addr have been changed).
I have verified that php mongo ext is installed, with this code:
echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n"; //prints loaded.
if (class_exists('\MongoClient')) {
echo 'is Mongo Client'."\n";//this is printed
}
else {
echo 'no Mongo Client'."\n";
}
try {
$client = new MongoClient('mongodb://myDbUser:passW0rT#0.0.0.0:27020/myDb', []);
} catch (Exception $ex) {
echo $ex->getMessage();//prints Failed to connect to: 0.0.0.0:27020: Connection timed out
}
What is more strange I can connect to this database using MongoClient and in addition to this I can connect to this database by my local server running on my computer (Win 10).
The problem appears also when I use security.authorization set to enabled and without difference set to disabled – then I use dsn mongodb://0.0.0.0:27020
Unfortunately server's response always gives timeout error when I enter web addr using www (domain which is on the server where mongo database is installed).
It could be one clue that server where mongo is installed is server which runs as container which means that I have to add port number to connect eg. via ssh.
I even changed the server from ngix to apache and problem didn't disappear.

Can't connect to SQL Anywhere 12 with PHP and ODBC on Ubuntu

I'm trying to get a PHP 5.3.10 installation on Ubuntu 12.04 to connect to a a remote SQL Anywhere 12 (Sybase?) server using ODBC (unixODBC). However, PHP's execution halts at odbc_connect().
PHP code:
$odbc = odbc_connect('DSN=TP189902;', 'username', 'password');
if ($odbc)
{
echo 'Connected';
}
else
{
echo 'Failed: '.odbc_error($odbc);
}
So regardless of whether or not it connects, it should be outputting one of the echos, but it doesn't. If I try using PHP's PDO library instead, I get the same result.
My unixODBC setup looks like the following. And this might be where my mistake is, because I've never setup ODBC on linux before and am not very familiar with it.
odbcinst.ini
[SQL Anywhere 12]
Description = SQL Anywhere 12
Driver = /opt/sqlanywhere12/lib64/libdbodbc12.so
Setup = /opt/sqlanywhere12/lib64/libdbodbc12.so
odbc.ini
[TP189902]
Description = TP189902
Uid = username
Pwd = password
Driver = SQL Anywhere 12
ServerName = 189902
CommLinks = tcpip(Host=1.2.3.4)
DatabaseName = DB189902
I've also tried a ton of alternatives, such as using the driver's path for the Driver value, using Host=1.2.3.4 instead of CommLinks, etc.
Also the command isql -v TP189902 username password doesn't output anything unless I give it a fake DSN so that it outputs and error.
I've also verified that libdbodbc12.so is the same architecture as isql and that it has all of it's dependencies.
On top of this, I have very similar setup on a Windows 7 machine running WAMP, that connects just fine (with both the ODBC and PDO library). I used the same DSN details on it.
Edit: I've also tried this to skip the DSN, but it gives the same result. It also works on the Windows box.
$odbc = odbc_connect('Driver={SQL Anywhere 12};Server=189902;CommLinks=tcpip(Host=1.2.3.4);', 'username', 'password');
I don't use PHP these days but here are some things I've spotted:
I would totally ignore php until you get isql working.
I'm assuming it is a typo that you say your system ini file is "odbcinstr.ini" - it should be "odbcinst.ini".
How do you know you are looking at the right odbc ini files - run odbcinst -j to check the locations unixODBC is using.
I know where that "[ODBC Data Sources]" section comes from (iodbc) but it not at all necessary for unixODBC - just delete the first 2 lines of your odbc.ini file.
your isql line is probably missing a username and password - it should be "isql -v TP189902 username password". I cannot for the life of me see why it would output nothing at all.
Ultimately the issue was getting the LD_LIBRARY_PATH set to /opt/sqlanywhere12/lib64 for Apache.
Setting it in /etc/environment got isql -v TP189902 and php connect.php working when called from any shell user, but not Apache.
To get Apache to see it, I had to edit /etc/init.d/apache2 and change
ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin"
to
ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/sqlanywhere12/lib64".
And then restart the Apache service.
Various other methods I found online to do this did not work.
One caveat is that with the path set, unixODBC still won't read my system DSN file for some reason. So to get Apache to access the DSN, I had to make a user DSN file (.odbc.ini) in /var/www, as that's the Apache user's (www-data) home folder.

How can I connect to Sql Server from a Mac with PHP PDO?

If you search Google for this question, you will find a lot of incorrect, misleading, and outdated information. Surprisingly, there isn't a solid answer on Stack Overflow, so we should change that.
I am using the Mac port installation of Apache and PHP. I have installed php5-mssql, and I can see mssql on my phpinfo() page.
But I don't see it listed under PDO.
PDO support enabled
PDO drivers dblib, mysql, odbc, pgsql
Is mssql not associated with PDO? Is there another driver that can be used on a Mac to connect to a SqlServer database using PDO? Seems like this is something that should be possible.
Does this help you?
http://blog.nguyenvq.com/2010/05/16/freetds-unixodbc-rodbc-r/
I use FreeTDS to connect to Microsoft SQL servers from a Linux server and it looks like the person in the link above has used FreeTDS to connect from a Mac.
Here is my /etc/freetds/freetds.conf file (the only part I added was at the very end for the XYZ server):
[global]
# TDS protocol version
; tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# Define a connection to the MSSQL server.
[xyz]
host = xyz
port = 1433
tds version = 8.0
[Edit by the asker]
FreeTDS configuration is the first half of the answer. Once it's configured you should be able to run something like this from the command line and connect:
tsql -S xyz -U username -P password
Then you need to use dblib, not mssql, as the PDO driver:
$pdo = new PDO("dblib:host=$dbhost;dbname=$dbname",
"$dbuser","$dbpwd");
Where $dbhost is the name from the freetds.conf file
After looking at many threads, I've found that the best way to connect to MSSQL from Mac OS X with PHP 7 or older is to use dblib. (Just download the correct php version)
You can follow these instructions (ignoring the mssql.so extension) to connect very easily:
https://github.com/BellevueCollege/public-docs/blob/master/PHP/configure-mssql-pdodblib-mac.md
It worked perfect with OS X El Capitan, Bitnami with PHP 7.
Steps
1.- Install XCode
$ xcode-select ---install
2.- Install Homebrew
3.- Install autoconf using Homebrew.
$ brew install autoconf
4.- Install FreeTDS
$ brew install freetds
5.- Download your version of PHP Source and uncompress it.
6.- Build the PDO DBLIB extension (Example for PHP 5.5.14)
$ cd php-5.5.14/ext/pdo_dblib
$ phpize
$ ./configure --with-php-config=/usr/bin/php-config --with-pdo-dblib=/usr/local/
$ make
$ sudo cp modules/pdo_dblib.so /usr/lib/php/extensions/no-debug-non-zts-20121212
7.- Add the .so extensio to php.ini
extension=pdo_dblib.so
8.- Restart Apache
9.- Connect using the dblib dsn:
$pdo = new PDO("dblib:host=$dbhost;dbname=$dbname","$dbuser","$dbpwd");
dblib is the driver that need to be used with mssql on unix systems
No need for you to install anything else,
<?php
$dsn = 'dblib:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
$dbh = new PDO($dsn, $user, $password);
Thanks Esteban for the nice guide (https://stackoverflow.com/a/37707426) which succesfully helped me install the pdo_dblib driver.
However, I was having issues connecting to my Azure SQL database from OSX 10 with PHP (5.5) and FreeTDS using the provided dblib dsn. What finally fixed it for me was appending the Azure database (m53man42a) to my username.
My dblib PDO connection in PHP:
$conn = new PDO("dblib:host=azure-sql;dbname=my-database-name",
"username#m53man42a",
"my-secret-password");
My FreeTDS.conf:
[azure-sql]
host = m53man42a.database.windows.net
port = 1433
tds version = 8.0
client charset = UTF-8
text size = 20971520
Consider adding this as a bullet number 10 in your list... :D
Note that Microsoft have published a PHP7 extension for this, but if you're still on PHP5.x, that doesn't help you. I've succeeded in connecting using a different stack: freetds,odbc,pdo.
I'm using OS X 10.11.6 (El Capitan) with Macports, PHP5.6.
I've started by creating an Azure SQL Database called mydb on a server with a name of myserver.database.windows.net. It's important to remember to open the firewall to your client IP address, which you do on the server.
First step is to install freetds with the ODBC driver, and its PHP connector (change php56 to the correct version of your PHP):
sudo port install freetds +odbc
sudo port install php56-odbc
Next, you need to include some lines in your configuration files:
/opt/local/etc/odbcinst.ini
[FreeTDS]
Description = ODBC for FreeTDS
Driver = /opt/local/lib/libtdsodbc.so
Setup = /opt/local/lib/libtdsodbc.so
FileUsage = 1
This tells the odbc library where to find its odbc driver.
/opt/local/etc/freetds/freetds.conf
[myserver]
host = myserver.database.windows.net
port = 1433
tds version = 7.0
This tells the freetdc library where to find your server.
/opt/local/etc/odbc.ini
[myds]
Description = Test for SQL Server on Azure
Driver = FreeTDS
Trace = Yes
TraceFile = /var/log/sql.log
Database = mydb
Servername = myserver
UserName = myusername
Password = mypassword
Port = 1433
Protocol = 7.0
ReadOnly = No
RowVersioning = No
ShowSystemTables = No
ShowOidColumn = No
FakeOidIndex = No
This creates a data source called myds pointing at your database, enabling you to connect with the following PHP:
$conn = new PDO('odbc:myds', 'myusername', 'mypassword');
If any of this doesn't work for you, first check that the freetds installation is correct using:
tsql -S myserver -U myusername -P mypassword
And then check that the ODBC specifications are OK using:
isql -v myds myusername mypassword
Thanks to https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server, which does the equivalent job for Python and which pointed me in the right direction for all this.

Can not connect to SQL SERVER Using Latest Drivers for PHP

I have been trying to figure out which dll and how to use it to connect to sql server.
It was much easier using the old php_mssql.
I am using Xampp on WinXP Pro SP3. I have been unable to figure out how to connect, i have search the manual, and none of the command's work.
I get PDO Error Driver Not Found
extension-php_pdo_sqlsrv_54_ts.dll
extension=php_sqlsrv_54_ts.dll
I realized that I must use the SQLSERV 2.0 Drivers. But which dll is the correct one? And what syntax must I use to connect and run queries?
Thank you.
One way of doing this is using FreeTDS for Windows
I am assuming you have PHP >5.3
Download this http://download.moodle.org/download.php/dblib/php53/DBLIB_TS.zip
Add this line to your php.ini extension=php_dblib.dll
You will also need to make a file called freetds.conf in the root directory of your PHP installation.
It should look something like this:
[global]
host = xxx.xxx.xxx.xxx (host name or ip of the MSSQL server)
port = 1433
client charset = UTF-8
tds version = 8.0
text size = 20971520
Restart Apache and try running this script:
<?php
$link = mssql_connect('localhost', 'db_user', 'db_password');
if(!$link) {
echo'Could not connect';
die('Could not connect: ' . mssql_error());
}
echo'Successful connection';
mssql_close($link);
?>
hit me up on fb if this does not work ;)

Categories