CentOS 6.7 and Windows MSSQL connection - FreeTDS and PHP - tsql

I searched for answers related to my question but not found what I'm looking for.
I have server with Linux CentOS 6.7. I need to connect to MS SQL Server 2005 on Windows Small Business Server 2003. In CentOS I installed FreeTDS and I managed to connect to SQL Server from terminal using this command:
# TDSVER=7.0 tsql -H ServerIPAdress -p 1433 -U username -P password
As far as I know this command bypass settings in freetds.conf.
Now I need to connect from PHP script. I'm using PDO extension and I tried this DSN string:
$db = new PDO("dblib:version=7.0;host=ServerIPAdress;dbname=Database;","username","password");
This leads me to first error:
Could not find driver
Ok, I understand that - my PHP 5.3.3 installation doesn't have installed PDO driver dblib.
My question: How can I install pdo_dblib driver in CentOS? Many tutorials and answers suggest to install through this command:
yum install php5-sybase
But that package doesn't exist:
No package php-sybase available.
If I check tsql configuration with command
tsql -C
I get these output:
Version: freetds v0.95
freetds.conf directory: /usr/local/etc
MS db-lib source compatibility: no
Sybase binary compatibility: no
Thread safety: yes
iconv library: yes
TDS version: 5.0
iODBC: no
unixodbc: yes
As I understand I need to configure FreeTDS:
./configure --enable-msdblib --with-pdo-dblib=/usr/local
But how can I configure FreeTDS after instalation? I tried to remove current version but I can't find way to do that. Also I tried to install another version but configuration doesn't change.
Any advice are appreciated.

When I run command # odbcinst -j I can see this output:
unixODBC 2.3.0
DRIVERS ...........: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLSETPOSIROW Size.: 8
Content of my freetds.conf:
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
tds version = 7.0
# 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
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 7.0
# A typical Microsoft server
[MSServer]
host = 192.168.1.55
port = 1433
tds version = 7.0
odbcinst.ini:
[FreeTDS]
Description = FreeTDS v7.0
Driver = /usr/local/lib/libtdsodbc.so
UsageCount = 1
odbc.ini:
[MSSQL]
Driver = FreeTDS
Description = MSSQL Server Connection
TDS_Version = 7.0
Trace = No
Server = MSServer
Port = 1433
Database = MyDatabase
Username = DOMAIN\MyUsername
Password = MyPassword
I can make connection with isql command when I specify username and password:
# isql -v MSSQL "DOMAIN\MyUsername" MyPassword
But if I omit username and password isql generate error:
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed
I suspect that my username make problems because I have to use DOMAIN name followed by backslash before username when I connect to SQL Server 2005.
UPDATE:
I succesfully made connection in PHP and now I can run queries. I added this line on top of my script: putenv("FREETDSCONF=/usr/local/etc/freetds.conf")
Why is this necessary?

Related

Oracle SQL Loader client unable to connect to server on LAN

I had a long running bash script/MySQL code to LOAD FILE into MySQL database.
I needed to convert this to Windows environment and Oracle DB. Wrote a PowerShell program and one of the lines in code uses sql loader to load files into oracle DB. The DB is in another system in LAN and I am connecting from a windows server system where I have installed the oracle full client (not just instant client) package.
Before using SQL Loader, I made sure that I am able to connect to DB from the system. The following SQL plus command works
Before using SQL Loader, I made sure that I am able to connect to DB from the system.
The following SQL plus command works
sqlplus abc/oracleabc#'"(description=(address=(host=192.168.22.44)(protocol=tcp)(port=1521))(connect_data=(sid=orcl)))"'
However I am not able to make SQL Loader work in PowerShell script.
I tried multiple command formats and connection strings
Try-1 :
sqlldr.exe abc/oracleabc#'"(description=(address=(host=192.168.22.44)(protocol=tcp)(port=1521))(connect_data=(sid=orcl)))"' control=$ctl_file_name direct='true'
Result :
LRM-00116: syntax error at 'address' following '('
Try-2:
sqlldr.exe 'abc/oracleabc#(description=(address=(host=192.168.22.44)(protocol=tcp)(port=1521))(connect_data=(sid=orcl)))' control=$ctl_file_name
Result :
LRM-00116: syntax error at 'address' following '('
Try-3
sqlldr.exe abc/oracleabc#"(DESCRIPTION\=(ADDRESS_LIST\=(ADDRESS\=(PROTOCOL\=TCP)(HOST\=192.168.22.44)(Port\=1521)))(CONNECT_DATA\=(SERVICE_NAME\=orcl)))" control=$ctl_file_name
Result:
SQL*Loader-704: Internal error: ulconnect:
OCIServerAttach [0]
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
Try-4
sqlldr.exe userid=abc/oracleabc#ORCL control=$ctl_file_name
Result:
SQL*Loader-704: Internal error: ulconnect:
OCIServerAttach [0]
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
Environmental variable ORACLE_HOME is defined in the system and points to client installation director D:\app\client\abc\product\19.0.0\client_1
tnanames.ora File :
# tnsnames.ora Network Configuration File:
D:\app\client\abc\product\19.0.0\client_1\NETWORK\ADMIN\tnsnames.ora
# Generated by Oracle configuration tools.
ORCL =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.22.44)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
sqlnet.ora File
# sqlnet.ora Network Configuration File:
D:\app\client\abc\product\19.0.0\client_1\NETWORK\ADMIN\sqlnet.ora
# Generated by Oracle configuration tools.
# This file is actually generated by netca. But if customers choose to
# install "Software Only", this file wont exist and without the native
# authentication, they will not be able to connect to the database on NT.
SQLNET.AUTHENTICATION_SERVICES= (NTS)
NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
TNS Ping:
C:\Users\abc>tnsping orcL
TNS Ping Utility for 64-bit Windows: Version 19.0.0.0.0 - Production on 04-MAY-2022
08:14:07
Used parameter files:
D:\app\client\abc\product\19.0.0\client_1\network\admin\sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST =
192.168.22.44)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))
OK (20 msec)
Need help in identifying the connection parameter/format issue related to SQL Loader.

How to configure a ODBC connection to teiid on rhel8? GSSAPI negotiation fails

I have a default Teiid 12.2 installation on RHEL 8.
Now I'm trying to configure an ODBC connection that would be used by PHP.
This always results in an error "[unixODBC]received invalid response to GSSAPI negotiation: R"
This is my ODBC configuration
[TEIID12]
Driver = PostgreSQL
Trace = No
Description = PostgreSQL Data Source
Servername = servername
Port = 35432
Protocol = 7.4-1
UserName = someusername
Password = xxxx
Database = vdb
ReadOnly = no
ServerType = Postgres
ConnSettings =
UseServerSidePrepare=1
Debug=0
Fetch = 10000
A regular isql command also fails with the same information
isql -v TEIID12 someusername xxxx
Results in:
[08001][unixODBC]received invalid response to GSSAPI negotiation: R
[ISQL]ERROR: Could not SQLConnect
Additional information:
The same configuration used to work on a different Linux Distro (Ubuntu) but on this machine, kerberos was not configured. So I assume that this is influincing some sort of "preference".
The standalone-teiid.xml configuration has <ssl mode="disabled" /> for the odbc transport. And yet the GSS API errors occur.
In combination with the later, is it possible that the postgresql ODBC driver is requiring GSS to be used? Is there a setting in odbc.ini where this can be disabled?
On Ubuntu the driver version was 10.01, on RHEL it is 10.03
A solution that worked was to set the environment variable PGGSSENCMODE=disable in /etc/profile.

trying to connect to an azure SQL database with python / pyodbc and a raspberry pi

with following code I try to connect to an azure database :
server = 'tcp:myserver.database.windows.net'
database = 'DBFREE'
username = 'user'
password = 'password'
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
but getting following error :
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
I used following commands to install pyodbc :
sudo apt-get install python-pyodbc
sudo apt-get install unixodbc-dev
pip3 install pyodbc
thanx in advance
As mentioned in my comment I recently walked this path and could never get the drivers from Microsoft to work on my Rasberry PIs. I speculate it has something to do with the ARM architecture. So even though Rasberry PI OS is based on Debian and Microsoft supplies the drivers, I could never get them to work.
I ended up using FreeTDS
First, install using the following commands:
sudo apt-get install freetds-dev freetds-bin unixodbc-dev tdsodbc
sudo pip install pyodbc sqlalchemy
When that is done, open the following file in a text editor:
/etc/odbcinst.ini
Add the following:
[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Setup=/usr/lib/arm-linux-gnueabihf/odbc/libtdsS.so
Then in the python connection string use FreeTDS as the driver and add ;TDS_Version=8.0 to the end of the connection string:
server = 'servername.database.windows.net'
database = 'database'
username = 'user'
password = 'password'
driver = 'FreeTDS'
pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password + ';TDS_Version=8.0')
Please ref the pyodbc here: Quickstart: Use Python to query a database in Azure SQL Database or Azure SQL Managed Instance
import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
You didn't set the ODBC driver version and the server don't have prefix tcp: .

Unable to connect to MS-SQL with ISQL

First post on StackExchange - please go easy :)
I have setup ODBC in Centos 6 in order to perform ms-sql queries from my Asterisk installation.
My Config files are:
/etc/odbc.ini
[asterisk-connector]
Description = MS SQL connection to 'asterisk' database
Driver = /usr/lib64/libtdsodbc.so
Setup = /usr/lib64/libtdsS.so
Servername = SQL2
Port = 1433
Username = MyUsername
Password = MyPassword
TDS_Version = 7.0
/etc/odbcinst.ini
[odbc-test]
Description = TDS connection
Driver = /usr/lib64/libtdsodbc.so
Setup = /usr/lib64/libtdsS.so
UsageCount = 1
FileUsage = 1
/etc/asterisk/res_odbc.conf
[asterisk-connector]
enabled => yes
dsn => asterisk-connector
username => MyUsername
password => MyPassword
pooling => no
limit =>
pre-connect => yes
I am able to connect via ISQL when I pass in the password and username:
[root#TestVM etc]# isql -v asterisk-connector MyUsername MyPassword
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
..but I should be able to connect without the username / password. All that returns is:
[root#TestVM etc]# isql -v asterisk-connector
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed
[ISQL]ERROR: Could not SQLConnect
It is as if ISQL cannot read the username and password from the config files.
I need to be able to perform MS-SQL lookups from within the Asterisk dialplan, but for that to happen I must be able to call ISQL with just the data source name and can't pass in the authentication parameters.
All the guides I've read online state that I should be able to connect with just the
isql -v asterisk-connector
command, but that's not happening for me.
I've been pulling my hair out for a few days on this, so any help or pointers in the right direction would be much appreciated.
Thanks in advance.
Edit:
I have turned on logging, and may have a clue. The username and password definitely aren't being passed in. Look:
[ODBC][27557][1455205133.129690][SQLConnect.c][3614]
Entry:
Connection = 0xac3080
Server Name = [asterisk-connector][length = 18 (SQL_NTS)]
User Name = [NULL]
Authentication = [NULL]
UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'
DIAG [01000] [FreeTDS][SQL Server]Adaptive Server connection failed
DIAG [S1000] [FreeTDS][SQL Server]Unable to connect to data source
So User Name and Authentication here are [NULL]. It's obviously not picking up the username / password in odbc.ini or res_odbc.conf, but the question is why. I'll keep investigating :)
Edit2:
The OSQL utility returns:
[root#TestVM etc]# osql -S SQL2 -U MyUsername -P MyPassword
checking shared odbc libraries linked to isql for default directories...
strings: '': No such file
trying /tmp/sqlH ... no
trying /tmp/sqlL ... no
trying /etc ... OK
checking odbc.ini files
reading /root/.odbc.ini
[SQL2] not found in /root/.odbc.ini
reading /etc/odbc.ini
[SQL2] found in /etc/odbc.ini
found this section:
looking for driver for DSN [SQL2] in /etc/odbc.ini
no driver mentioned for [SQL2] in odbc.ini
looking for driver for DSN [default] in /etc/odbc.ini
osql: error: no driver found for [SQL2] in odbc.ini
I would replace "Username" with "UID" and "Password" with "PWD" in your odbc.ini.... from FreeTDS Manual - Chapter 4 - Preparing ODBC:
The original ODBC solution to this conundrum employed the odbc.ini file. odbc.ini stored information about a server, known generically as a Data Source Name (DSN). ODBC applications connected to the server by calling the function SQLConnect(DSN, UID, PWD), where DSN is the Data Source Name entry in odbc.ini, UID is the username, and PWD the password. Any and all information about the DSN was kept in odbc.ini. And all was right with the world.
The ODBC 3.0 specification introduced a new function: SQLDriverConnect. The connection attributes are provided as a single argument, a string of concatenated name-value pairs. SQLDriverConnect subsumed the functionality of SQLConnect, in that the name-value pair string allowed the caller to pass — in addition the the original DSN, UID, and PWD — any other parameters the driver could accept. Moreover, the application can specify which driver to use. In effect, it became possible to specify the entire set of DSN properties as parameters to SQLDriverConnect, obviating the need for odbc.ini. This led to the use of the so-called DSN-less configuration, a setup with no odbc.ini.
Ok, so I solved it (pretty much). The password and username in my odbc files were being ignored. Because I was calling the DB queries from Asterisk, I was using a file called res_odbc.ini too. This contained my username and password also, and when I run the query from Asterisk, it conencts and returns the correct result.
In case it helps, here is my final working configuration.
odbc.ini
[asterisk-connector]
Description = MS SQL connection to asterisk database
driver = /usr/lib64/libtdsodbc.so
servername = SQL2
Port = 1433
User = MyUsername
Password = MyPassword
odbcinst.ini
[FreeTDS]
Description = TDS connection
Driver = /usr/lib64/libtdsodbc.so
UsageCount = 1
[ODBC]
trace = Yes
TraceFile = /tmp/sql.log
ForceTrace = Yes
freetds.conf
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[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
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 5.0
# A typical Microsoft server
[SQL2]
host = 192.168.1.59
port = 1433
tds version = 8.0
res_odbc.conf
[asterisk-connector]
enabled = yes
dsn = asterisk-connector
username = MyUsername
password = MyPassword
pooling = no
limit = 1
pre-connect = yes
Remember if you are using Centos 64 bit to modify the driver path to lib64. Most of the guides online have the wrong (for 64 bit) paths.
Good luck - it's a headache :)
I contacted the Nick Gorham the developer of unixODBC about this exact issue and he confirmed that isql is not reading the username/password from the config file
Hi Nick,
I think unixODBC is a great project but I was surprised to see that it
is insecure (or at least I don’t know how to use it properly).
When I connect to the database using the isql I have to type in the
password. On a shared server this is insecure because the
$ ps –aux
Command shows the password in clear.
Is there a fix for that? Can I put the password in a file readable
only by my user?
Thank you for your help.
The answer:
Hi,
It depends on the driver. Some can read the user and password from the
odbc.ini or ~/.odbc.ini file so you can store the password there.
isql is only designed as a simple test app, there is nothing stopping
you from modifying ilsq to pull the user and password from a file of
your choice, decrypting it if needed.
I was having a slightly different issue, but my google search lead me here. When trying to connect through isql, I was getting Login failed for user '' even though I had specified a user in my odbc.ini file
[SQLSERVER_SAMPLE]
Driver=ODBC Driver 17 for SQL Server
Server=SERVER
Database=DATABASE
Trusted_Connection=no
UID=USER
PWD=PASSWORD
I tried both UID and User, but both gave the same error. After reading #Andrei Sura's solution, I figured out that the username and password were being ignored.
My solution was to run isql -v SQLSERVER_SAMPLE USER PASSWORD even though the username and password were specified in the odbc.ini file - and it connected.

Error 20012 (severity 2):Server name not found in configuration files

Here is my code details
Step 1.
/etc/freetds/freettds.conf
[mssql]
host = 192.168.1.10
port = 1433
tds version =8.0
client charset = UTF-8
Step 2
/etc/odbcinst.ini
[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
Step 3
/etc/odbc.ini
[mssqltest]
Description = MS SQL connection to mssqltest database
Driver = FreeTDS
Database = cakephp_results
Server = mssql
UserName = sa
Password = sa
Trace = Yes
Port = 1433
I tried to connect sql by command line with the following command:
$ tsql -S mssqltest -U sa -P sa
Error 20012 (severity 2):
Server name not found in configuration files.
locale is "en_IN"
locale charset is "UTF-8"
using default charset "UTF-8"
Error 20013 (severity 2):
Unknown host machine name.
There was a problem connecting to the server
You should copy /etc/freetds.conf to your home folder with dot before its name like this:
cp /etc/freetds.conf ~/.freetds.conf
Looks like it searches for config per user, I got the same problem recently.
Try creating freetds.conf file in /usr/local/etc
Creating a ~/.freetds.conf should work for you. It works fine for me