Trouble With Connecting to MSSQL DB - php

I have an Ubuntu 12.04 server and I'm trying to establish a connection to a MSSQL database.
I've managed to connect using tsql and isql, but osql doesn't work and connecting with PHP using PDO also isn't working.. I will try to provide as much information as I can and if you need more just let me know and I will edit.
freetds.conf:
[MSSQL]
host = TPSACC
port = 54488
tds version = 8.0
odbc.ini:
[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 pass
+---------------------------------------+
| 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 pass
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"
In PHP I have:
$conn = new PDO ("dblib:host=TPSACC;dbname=PRODUCTION","$username","$pw");
..or..
$conn = new PDO ("dblib:host=TPSACC;port=54488;dbname=PRODUCTION","$username","$pw");
And they both return this error:
SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
I have been stuck here for a couple days and can't quite figure out why only half of the connections actually work..
Any and all help is really appreciated, thanks!
EDIT: It is different than that question, I explained in a comment but will repost here:
For that one the problem was that the port was changed from 1433. Mine was also changed and I fixed that, the port is now 54488 and since that change tsql and isql have been working. However, it still doesn't solve the osql and PHP issues.

osql is choking on something in your configuration. osql is a debugging utility which simply checks out your configuration, then passes along to unixODBC's isql to connect (http://linux.die.net/man/1/osql). Try this for your odbc.ini:
[MSSQL]
Driver = FreeTDS
Description = MS SQL connection to PRODUCTION database
Server = tpsacc.yourfulldomain.com
Port = 54488
TDS_Version = 7.2
Database = PRODUCTION
UserName = sa
Password = pass
Trace = No
Also, are you sure that's the correct location for libtdsodbc.so? When I install freetds-dev with Ubuntu 14 x64 (utopic), it installs to /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so; example odbcinst.ini:
[FreeTDS]
Description = v0.91 with protocol v7.2
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
And for good measure, the typical freetds.conf I use:
# A typical Microsoft server
[MSSQL]
host = tpsacc.yourfulldomain.com
port = 54488
tds version = 7.2
FreeTDS only supports up to TDS version 7.2 with most languages. While using "8.0" shouldn't break anything, using 7.2 is better for consistency. If you need to check for reference, I have a Vagrant box with a full configuration available here, with examples: https://github.com/FlipperPA/django-python3-vagrant/ Good luck!

Related

Doctrine on linux: unable to connect to sql server

I am trying to run a zend + doctrine web application from a Debian 9 host, using php 5.6
I've the need to connect to a sql server that is on different host (it's a windows machine, obviously)
I tested connection using isql command line and I am correctly able to login and to operate in the desired db
The problem is that the webabb do not return me any error but simply fails to login (i am not the developer of this webapp).
Configuration follows:
odbcinst.ini
[FreeTds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
UsageCount = 1
Trace = Yes
TraceFile = /tmp/odbcinst.log
freetds.ini
host ip is exact, I removed when pasting here
[flower1]
host = xxx.xxx.xxx.xxx
port = 1433
tds version = 7.3
odbc.ini
[test_flower1]
Description=Test for MSSQL Server on flower1
Driver=FreeTds
Database=test_flower1
ServerName=flower1
TDS_Version=7.3
application.ini
;; Database
doctrine.dsn = "odbc:Driver={Sql_server};Server=flower1;Database=test_flower1;Uid=<redacted but correct>;Pwd=<redacted but correct>;"
How can I debug the source of the problem?
I've not previously experience with zend/doctrine
The problem was due to this
doctrine.dsn = "odbc:Driver={Sql_server};Server=flower1;Database=test_flower1;Uid=<redacted but correct>;Pwd=<redacted but correct>;"
I solved simply adding the port !
doctrine.dsn = "odbc:Driver={Sql_server};Server=flower1;Port=1433;Database=test_flower1;Uid=<redacted but correct>;Pwd=<redacted but correct>;"

How to connect to sybase from PHP in UNIX using ODBC+FreeTDS?

I have tried to connect sybase from terminal using FreeTDS and it is working fine but I am not able to connect database using PHP.
I have done changes in below files.
/etc/odbc.ini file:
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
Server =
Port =
Database =
Username =
Password =
freetds.conf file
[sybase]
host=
port=
Tds version=5.0
It is working fine using Terminal
tsql -S SYBASE -U username -P password
But from PHP connection I am getting error:
[unixODBC][Driver Manager]Data source name not found, and no default driver specified
PHP file code:
$db = ADONewConnection('odbc');
$DSN ='UID=username;PWD=password;EngineName=dbServiceName;AutoStop=No;Integrated=No;Debug=No;DisableMultiRowFetch=No;CommLinks=SharedMemory,TCPIP{};Compress=No;Driver={FreeTDS}';
$db->Connect($DSN );
I know in some versions, you need to use the IP of the server rather than the name, like this in freetds.conf:
[myserver]
host = 10.10.10.10
port = 5000
tds version = 5.0
I also don't know if tds version is case sensitive, but I've always seen it in lowercase. Are you sure that PHP is reading from the same freetds.conf that the command line version is?

php dblib, Error: SQLSTATE[HY000] Unknown host machine name (severity 2)

I am using mac computer OSX 10.9. Freetds and unixODBC are already installed on my computer and added as extension to php , trying to connect to a remote MSSQL server. Below is my connection testing:
<?php
$dbh = new PDO('dblib:host=Hostname ;dbname=Dbname', 'user', 'pw');
if (!$dbh) {
die('Something went wrong while connecting to MSSQL');
}
?>
The error logs file show :
[error] [client 127.0.0.1] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] Unknown host machine name (severity 2)
What could be the problem ? It seems that my freetds and unixODBC are working fine if I use terminal to connect to the same database as below:
$ isql Hostname user pw
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
and
$ tsql -S Hostname -U user
Password:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>
here is my freetds.conf
[global]
# TDS protocol version
tds version = 8.0
[Hostname]
host = IP
port = 1433
tds version = 8.0
client charset = UTF-8 ##needed on MAC OS X
dump file = /tmp/freetds.log
and my odbc.ini
[Hostname]
#Driver=/usr/local/lib/libtdsodbc.so
Driver = /usr/local/Cellar/freetds/0.91_2/lib/libtdsodbc.so
Trace=No
Server=IP
Port=1433
TDS_Version=8.0
client charset = UTF-8
my phpinfo() shows that the extension has been added, there is dblib in PDO section and pdo_dblib section have driver Flavour enabled freetds.
So what is the problem? Any idea of what I should do ?
Any assistance will be highly appreciated.
here is my odbcinst.ini:
[freetdS]
Description = v0.63 with protocol v8.0
Driver = /usr/local/Cellar/freetds/0.91_2/lib/libtdsodbc.so
I actually solved this question by deleting both mssql.so, pdo_dblib.so in php extension folder, re-download php5.4 , phpize and build both the .so files again and put it back. Then it works.
It seems that the olde pdo_dblib.so file I made pointed to a different freetds.conf somewhere else.
Since it is not possible to debug this by proxy without real files information and you've opted to protect ip addresses and hostnames with dummy text. I am going with the PHP code. The hostname should be a FQDN or an ip address. Not the place holder text from the ini files.
<?php
$dbh = new PDO('dblib:host=[ ip address || example.com || localhost] ;dbname=Dbname', 'user', 'pw');
if (!$dbh)
{
die('Something went wrong while connecting to MSSQL');
}
?>

Connecting to MS SQL database with PHP: Data source name not found, and no default driver specified

I'm stuck with this problem for a while and I just can't get any further, I did a lot of searches but nothing works... I'm trying to connect to a Microsoft SQL Database with php using odbc.
Everything is set up, as follows (the values between "" are correct in the file):
/etc/odbc.ini:
[CRMCONNECT]
Description = "CRMConnect"
Driver = FreeTDS
Trace = No
Servername = CRMSERVER
Database = "dbname"
UserName = "username"
Password = "password"
[Default]
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
/etc/odbcinst.ini:
[FreeTDS]
Description = tdsodbc
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout = 5
CPReuse = 5
FileUsage = 1
/etc/freetds/freetds.conf:
[CRMSERVER]
host = xxx.xxx.xxx.xxx
port = 1433
tds version = 8.0
I doublechecked the host many times and it is correct. I also tried tds version 7.0, but no luck.
I can succesfully connect to the server with isql:
root#crmart-web004:/# isql -v CRMCONNECT "user" "pass"
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
But with php I just can't get it working, I get the following error:
[unixODBC][Driver Manager]Data source name not found, and no default driver specified
My connectionstring:
$connection = odbc_connect("Driver={CRMCONNECT};Server=xxx.xxx.xxx.xxx;Database=dbname;","username","password");
All parameters are doublechecked and are correct.
How come I can successfully connect with isql but it fails in php?
My php version:
PHP Version 5.4.4-14+deb7u5
odbcinst configuration:
unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
I hope someone has an idea what could be wrong.
Thanks in advance
Regards
UPDATE:
I changed my connectionstring to:
$connection = odbc_connect("CRMCONNECT;Database=dbname;","user","pass");
which is resulting in another error:
[unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed
I'll be looking into that, thanks again vinodadhikary
Regards.
UPDATE 2:
My connection string was wrong, it should have been:
$connection = odbc_connect("CRMCONNECT","user","pass");
Thanks vinodadhikary!
Regards and happy holidays.
Since you already have CRMCONNECT DSN defined, you could use the following connection method:
$connection = odbc_connect("CRMCONNECT","username","password");
Also in your connection string you have Driver={CRMCONNECT};. CRMCONNECT as you've defined is not a driver, it is a Data Source Name. The driver in your case would be FreeTDS
I had the same error in Laravel 5.5 with PHP 7.1.9 on Debian 7.11. Fixed it by removing curly braces from driver's name in DSN:
Failed: "odbc:Driver={fail};Server=host;Database=db;"
Worked: "odbc:Driver=success;Server=host;Database=db;"
Another working option was moving data source configuration from this string into odbc.ini and then referencing it: "odbc:odbc_ini_data_source_name"
The funny thing is that the same data source with Driver={SQL Server} (with curly braces) worked correctly under Windows 10.
The same applies for PDO. $conn = new PDO("odbc:CRMCONNECT"); and be sure to set your default driver in odbc.ini
[Default]
Driver = IBM i Access ODBC Driver 64-bit in our case

PDO: How to access Microsoft Access file on Linux?

I have managed to read data of Microsoft Access file (.accdb) on Windows using PDO but I'm having problem getting to work on Linux (CentOS). I can see modules is installed:
[root#rapid host]# php -m | grep PDO
PDO
PDO_ODBC
[root#rapid host]# php -m | grep odbc
odbc
code:
<?php
try{
$dbhAccess = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=/root/access/data.accdb;Uid=Admin");
}
catch(PDOException $e){
echo $e->getMessage();
exit();
}
I get an erro when I execute (CLI) PHP file:
[root#rapid host]# php access.php
SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
you should install the MDB driver.
I have no way to try it now, but I think that MDB Tools (specifically the ODBC driver) can do what you' re interested to.
I've never worked with a Microsoft Access database but I do regularly connect to DB2 (on IBM AS/400's) and MS SQL servers from Linux (Ubuntu) servers. Your error seems to indicate you don't have a MS Access driver installed - the only one I'm aware of is: http://www.easysoft.com/products/data_access/odbc-access-driver/index.html
Your error message also says the "Data source name not found" - in order for me to connect to DB2 or MSSQL I have to add some information to /etc/odbc.ini and /etc/odbcinst.ini.
/etc/odbcinst.ini - this is where you describe where to find the drivers for ODBC. Here's an example of what I use for DB2 and MSSQL:
[iseries]
Description = iSeries Access for Linux ODBC Driver
Driver = /usr/lib/libcwbodbc.so
Setup = /usr/lib/libcwbodbcs.so
NOTE1 = If using unixODBC 2.2.11 or later and you want the 32 and 64-bit ODBC drivers to share DSN's,
NOTE2 = the following Driver64/Setup64 keywords will provide that support.
Driver64 = /usr/lib/lib64/libcwbodbc.so
Setup64 = /usr/lib/lib64/libcwbodbcs.so
Threading = 2
DontDLClose = 1
UsageCount = 1
# Define where to find the driver for the Free TDS connections.
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
[ODBC]
Trace = no
TraceFile = /tmp/odbc.log
In that last section ([ODBC]) I currently have Trace = no - if you change that to Trace = yes you will get some helpful debugging information in the /tmp/odbc.log file.
/etc/odbc.ini - this is where you define your data sources. Here's an example of what I use for DB2 and MSSQL:
[primary]
Description = primary
Driver = iseries
System = XXX.XXX.XXX.XXX
UserID = XXXXXXXXXX
Password = XXXXXXXXXX
Naming = 0
DefaultLibraries = QGPL
Database = MYLIB
ConnectionType = 0
CommitMode = 2
ExtendedDynamic = 0
DefaultPkgLibrary = QGPL
DefaultPackage = A/DEFAULT(IBM),2,0,1,0,512
AllowDataCompression = 1
LibraryView = 0
AllowUnsupportedChar = 0
ForceTranslation = 0
Trace = 0
# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssql]
Description = MSSQL Server
Driver = freetds
Database = MyDatabase
ServerName = mssql
TDS_Version = 8.0
I've seen a number of questions here on StackOverflow talking about using MSFT Access databases from a Linux machine - and there doesn't seem to ever be a happy ending. If there is any way you can port the data to a different, better supported, database system (like MySQL) I think you'll save yourself some headaches. Good luck!
You can not use {Microsoft Access Driver (*.mdb, *.accdb)} as part of your data source because Microsoft do not make an MS Access ODBC driver for Linux. There are 2 ODBC drivers for MS Access as far as I know of. The MDB Tools and the Easysoft ODBC-Access Driver.
If you install either driver you can either use a DSN which you setup in your odbc.ini file or as you prefer a DSN-Less connection. Here is an example of the an Easysoft DSN-Less connection to an MS Access database
PDO("odbc:Driver={Driver=Easysoft ODBC-ACCESS};Dbq=/root/access/data.accdb");
More information on connecting and getting data back using PDO-ODBC can be found on Easysoft PHP guide There is a section about half way down that talks about PDO-ODBC.
import pandas_access as mdb
db_filename = 'my_db.mdb'
# Listing the tables
for tbl in mdb.list_tables(db_filename):
print(tbl)
# Read a small table
df = mdb.read_table(db_filename, "MyTable")
On Ubuntu you may need to run:
sudo apt install mdbtools
For WINDOWS this is the solution.
After investing a lot of time I have the solution.
The 32-bit version of "AccessDatabaseEngine" must be installed.
Download the 32-bit Access Engine on: https://www.microsoft.com/es-es/download/confirmation.aspx?id=13255
And run it with the command line: AccessDatabaseEngine.exe /passive
After that, it should work.

Categories