Mac: Connecting to msSQL Server on Mac

Filed in mac | php 3 Comments

This is for ppl who are mac lover and mysql lover but due to work requirement have to conenct to mssql server on their mac.

if you had installed PHP Apache Module from Entropy.ch the following instruction is for you, else, you may try my earlier post on installing msSQL extension for php in *nix/Centos Server, largely used by WHM/cPanel.

entropy’s php for the version i install for mac is still in beta. but it comes with freeTDS installed, i am not too sure about the earlier versions. so to conenct to mssql server you jus have to configure your freetds.conf file which should be located in, the path,

/usr/local/php5/etc/freetds.conf

you may had name “php5” with something else instead.

vi this file or open with a text editor and insert the following line at the end.

[MSHOSTNAME]
host = MSHOSTNAME
port = 1433
tds version = 8.0

note that MSHOSTNAME for both case mus be the same, and is something like 192.168.0.100 or mssql.somehost.com etc. it should be the hostname you are connecting to probably in your php script.

ie: $link = mssql_connect(‘MSHOSTNAME’,’username’,’password’) or die(‘Damn! Unable to connect to msSQL Server!’);

tds version depends on the mssql server version you are connecting to, gennerally 8.0, and port is standard 1433 unless your remote mssql server connects on weird port number.

save, and you are done!

, , ,

How To: Install mssql extension for php on unix

Filed in php | software | unix 72 Comments

woh! okie.. this installation is gng to be long a tough.. took me a few days of google crawling and testing before i finally get it to work.

okie, why will u need to install mssql extension on php? possibly becos u need to connect to a ms sql server of ur vendor or something like that. for integration i hope. cos unix works best w php and mysql, ie, installation, it is almost already thr. correct me if i m wrong.

so wad you need?
if your server is jus fresh out of the box or say u had installed whm/cpanel there will be quite a far bit of stuff u will need.

1. Txt2man
2. unixODBC
3. freeTDS
4. PHP mssql.so

if this point if you are wondering if i m using any rpm for installation?
sorry,i m not, cos my server centos installed w whm/cpanel seems to be poorly equipped or configured for rpm installation, too many dependencies required. so jus download all the binary and upload them onto ur server.

Setup 1: Install unixODBC package

Install unixODBC first. Download the software in your home directory or anywhere you like and uncompress and untar the software.

You can do this by using following command

# tar -xvzf unixODBC-2.2.12.tar.gz

Once it is done, go to the unixODBCxxxx dir and run the configure command.

# ./configure -prefix=/usr/local -enable-gui=no

Note: If you dont use enable-gui option and dont have Qt package, you will get the error. So make sure you use it if no Qt is installed.

After configure is successfully completed, run the make and then make install commands.

# make

Install it

# make install

Step 1.1: Installing Txt2man
this step is optional if your server already have txt2man installed.
txt2man is required by freeTDS to man it documentation, i do find this portion pretty useless though.
else jus download the package, and run the make

# make

Install it

# make install

Step 2: Install freeTDS package

Download the freeTDS package and untar and uncompress it.

# tar -xvzf freetds-stable.tgz

Run the configure.

# ./configure -with-tdsver=8.0 -with-unixODBC=/usr/local

Run the make

# make

Install it

# make install

Step 3: Configuration freeTDS
Start configuration with freeTDS. Look for the freetds.conf file and add the entry for the MS SQL server.

[MSHOSTNAME]
host = MSHOSTNAME
port = 1433
tds version = 8.0

Sql server standard port is the 1433. The tds version for MS SQL server 2000 is 8. You can try 8.0 with new version as well.
Important: the host need to be the same as the declaration. ie
if you have [mssql.hostname.com], ur host will be = mssql.hostname.com

Step 4: Configuration – unixODBC

The unixODBC need two main configuration files called odbcinst.ini and odbc.ini. These files should be in the /etc/ dir.

I. The first file odbcinst.ini contain the definition of ODBC driver.

[TDS]
Description = FreeTDS driver
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
Trace = Yes
TraceFile = /tmp/freetds.log
FileUsage = 1

Note: Before making above entries, make sure libtdsodbc.so exists in the /usr/local/lib dir.
In the above configuration, we have define TDS as driver.

II. The second file is odbc.ini. This file has information about your MS SQL database.

[MSHOSTNAME]
Driver = TDS
Description = MS SQL Test
Trace = Yes
TraceFile = /tmp/mstest.log
Servername = 192.168.1.100
Database = testdb
Port = 1433

You can notice above that the freeTDS driver is the name defined in odbcinst.ini while servername is the one defined in freetds.conf.

Step 5: Test MS SQL connectivity from Linux box
Check if you can connect to MS SQL database using unixODBC tool called isql.

# isql -v mstest satest satest
+—————————————+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+—————————————+
SQL> select * from “sysObjects”;

If you see the results, you are connected.

Step 6: Getting your mssql.so file
Update: I came back to follow this to create an .so file and realise, mmm, this instruction wasn’t too clear. So here it goes. From PHP.net Download page, download the source code, then if you unzip, you will find a folder “ext” and “mssql” in the “ext” folder. tat’s the folder i m referring to, ‘cd’ to this folder and then run phpize as below mentioned.

Add: for the benefits of those who just need the .so file, you can download the mssql.so from here. however, i don’t assure, compatibility with you php version. is built with php 5.2.10 binaries.

php.net is not distrubuting the binary version of php package for *nix server, so you will have to create the .so extension file yourself. download the source file for php and upload the folder /ext/mssql/ into your server
then

# cd /uploaded_path/mssql/
# phpize
# ./configure
# make
# make install

this commands will automatically create a copy of mssql.so in the installed extension folder.
you can check your extension folder from

<?php
phpinfo();
?>

Step 7: Enabling the extension

# echo “extension=mssql.so” > /usr/local/lib/php.ini

or you can use a texteditor and add that value manually into php.ini

Step 8: Restarting Apache Server!
dependin on version installed and location

# /etc/init.d/httpd restart

and you are done!
do drop me an email should you face any problem at any step!
i know the pain in gettin this extension installed on unix. and considering rpm fails to work in the first place.
i will say, this is the best way to get it installed, cos u have full control over it. and didn’t fail me, of cos, after hell lots of googling around.

Source:
dpacool.com
unixodbc.org
Scott’s Blog
PHP PECL

, , , , , , , , , , ,

TOP