Start a conversation

Connecting to MS SQL Server on Linux environment via FreeTDS

We will create a MS SQL Server connection without the need to compile PHP packages, we will use the FreeTDS which is available in apt repository.

 

Note: To connect to SQL Server 2008 use version 8.0 of the TDS.
For more details see the following article: http://www.freetds.org/userguide/logging.htm

 

Step 1: Installing FreeTDS.

On your Linux shell, type:

"sudo apt-get install php5-odbc tdsodbc unixodbc freetds-dev php5-sybase"

Note: all operations as root

Step 2: Installing the packages, we will create the file templates DNS

In Linux shell:

"vim /etc/freetds/tds.driver.template"

Step 3: Add the following content to the file

"[TDS]
MSSQL Description = FreeTDS Driver for Linux Debian 5 - "Lenny"
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so "


Step 4: We will now register the ODBC


"odbcinst-i-d-f /etc/freetds/tds.driver.template"


Step 5: We will register the base that will be accessed by ODBC. Remembering that each base requires a different ODBC, so if you have more than one, keep repeating the commands.

Create the file: "vim/etc/freetds/tds.dsn.template"

content:

"[DSN]

Description = ODBC Test - FreeTDS
Driver = TDS
Trace = No
Database = EIRAS
Server = 192.168.100.50
Port = 1433 "

NOTE: Remember to change the name "DSN" above. This name will be used in the application to identify the ODBC. Generally it has the same name as the database. (Database)

Step 6: Let the record ODBs and DSNs:

"odbcinst-i-d-f /etc/freetds/tds.driver.template
odbcinst-i-s-f /etc/freetds/tds.dsn.template "

| ODBC are registered to the users who create them (in our case, the root), but in this case, we will leave the record for use of the system, it will be used by apache. So we will copy the contents to the file global access.

"cat /root/.odbc.ini >>/etc/ odbc.ini"


Step 7: Let's make apache recognize the ODBC driver

Add the line:

"vim /etc/php5/apache2/php.ini

extension = odbc.so "

Step 8: Restart apache

"/etc/init.d/apache2 restart"


Step 9: Now your ODBC should work via PHP.

To test, type the command into the shell:

"Isql-v DSN sa password"

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Amanda Santos (Migrated deleted Agent)

  2. Posted
  3. Updated

Comments