Friday, September 23, 2011

Database Configuration for MSSQL

If you are connecting to a MSSQL database, first ensure that your php-mssql package is installed. A simple command can show you this:
$ rpm -qa | grep php-mssql
The output should look something like this:
php-mssql-5.3.8-4.el6.remi.x86_64
Next check that freetds is installed.
$ rpm -qa | grep freetds
The output should look something like this:
freetds-0.82-6.el6.x86_64
You should now be able to connect to your MSSQL database directly from the command line. Make sure to replace the server, user and password with valid information.
$ /usr/bin/tsql -S [mssql.servername.or.ip] -U [ValidUser]
locale is "en-US.UTF-8"
locale charset is "UTF-8"
Password: [password]
1>
Enter "quit" to exit your successful connection. If the tsql command doesn't return the 1> prompt, verify that you can get to your MSSQL server with the following command:
ssh [mssql.servername.or.ip] -p 1433
Also verify that your username and password are valid.
At this point you can set your database configuration to connect to the database.
    var $default = array(
        'driver' => 'mssql',
        'persistent' => false,
        'host' => 'mssql.server.or.ip',
        'login' => 'username',
        'password' => 'password',
        'database' => 'database',
        'prefix' => ''
    );
Through much trial and error I've discovered that if you connect directly to the server in this manner some commands, such as mssql_bind, mssql_execute, and mssql_init don't work. Now you could setup and execute stored procedure commands through mssql_query this isn't as secure.
Instead, set up a freetds configuration for the server, connect with that instead, and the commands will now work fine.
Edit /etc/freetds.conf and add the following block:
[ServerIdentifier]
        host = [mssql.servername.or.ip]
        port = 1433
        tds version = 8.0
Then use the ServerIdentifier you chose instead of the server name or ip in the database configuration host variable and you're all set.

No comments:

Post a Comment