ManOfHonor
ManOfHonor

Reputation: 21

CentOS 6.7 and Windows MSSQL connection - FreeTDS and PHP

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.

Upvotes: 1

Views: 3598

Answers (1)

ManOfHonor
ManOfHonor

Reputation: 21

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?

Upvotes: 1

Related Questions