Failed to connect to database DB Error: extension not found Array, oh Sweet!

Today while running one of the php scripts which connects to external database server on Linux machine and connects to MSSQL using Pear extension i got the below error:

Failed to connect to database 'database_name' on 'x.x.x.x' DB Error: extension not found Array

First of all always start by confirming that you can access the destination server and the port is open, this can be done on Linux box using below command:

nmap -sS -O server_name or IP_Address

You expect to see output similar to following:

Screenoutput-1

Based on what Database is the destination and what port you should get something like 3306 for MySQL or 1433 for MS SQL like above, if the port is there and shows open like above example then you are good to go, otherwise you need to check firewall on both your server and the destination one.

Second are you using PHP Pear extension ? which was my case, then you need to confirm that you have php5-mysql or php-mssql package installed in the system ,depends on what SQL flavor you are connecting to,  to check for installed packages in CentOS or RedHAT you can use below command:

rpm -qa | grep -i php-mysql

If it's not there then you need to install it using either:

1- Yum

yum search php-mysql or yum php-mssql
yum install freetds "for MSSQL"
yum install freetds-devel.x86_64
yum install unixODBC unixODBC-devel
yum install package_name

2- RPM , by searching online for the required package rpm and install it using following method: (Search for the right rpm for your OS version) (Ex.: Don't install a php-mssql of CentOS 7 on CentOS 6)

rpm -i  downloaded_file_name.rpm

In some cases you will get errors asking for missing dependencies like below error example:

Screenoutput-2

For such case you need to google/yum the missing package and install it, most of the RPMs needed can be found in here.

Upgrade glibc 2.12 to 2.14in some cases to install above modules the glibc 2.14 will be needed, i don't recommend that just find the right packages for your OS.

php -m | grep -i mssql

Still not there ? maybe you need to reconfigure your php, so run the following from the php folder:

./config.nice  --with-mssql=shared,/usr
make
make install

Now be sure that the php.ini file includes the mssql.so extension , to know the location of the original php.ini file you can use below command:

php -i | grep -i php.ini
Configuration File (php.ini) Path => /usr/local/web/php/lib
Loaded Configuration File => /usr/local/web/php/lib/php.ini

Confirm that mssql or mysql is loaded by using:

php -i | grep -i mssql
mssql
MSSQL Support => enabled
mssql.allow_persistent => On => On
mssql.batchsize => 0 => 0
mssql.charset => no value => no value
mssql.compatability_mode => Off => Off
mssql.compatibility_mode => Off => Off
mssql.connect_timeout => 5 => 5
mssql.datetimeconvert => On => On
mssql.max_links => Unlimited => Unlimited
mssql.max_persistent => Unlimited => Unlimited
mssql.max_procs => Unlimited => Unlimited
mssql.min_error_severity => 10 => 10
mssql.min_message_severity => 10 => 10
mssql.secure_connection => Off => Off
mssql.textlimit => Server default => Server default
mssql.textsize => Server default => Server default
mssql.timeout => 60 => 60

Third , if still no luck then check if you have the required pear extension, for example if you are trying to connect to MSSQL then you need the required Pear extension, to list installed packages you can use:

pear list

To install the MSSQL Pear then use below command:

pear install MDB2_Driver_mssql

If other then it's always helpful to google "install pear mysql" or whatever the needed pear extension.

How to connect or test MSSQL connection from Linux Command Line ?

There are several ways to do so but for me i am using the following:

First install FreeTDS and UnixODBC:

yum search unixODBC
yum install unixODBC
yum install freetds

Then you can use "tsql" , below is an example and you can use also -h for help:

tsql -H  ServerIP_HERE  -U userName  -P passWord  -p 1433

FreeTDS(May 14, 2011)                                    

NAME
       tsql - utility to test FreeTDS connections and queries

SYNOPSIS
       tsql   { -S servername [-I interface] | -H hostname -p port }
              -U username [-P password] [-o options]

       tsql   -C

DESCRIPTION
       tsql  is  a diagnostic tool provided as part of FreeTDS. It uses the TDS protocol directly to con-
       nect to Sybase or Microsoft SQL Servers, and allows the user to issue queries that test the  capa-
       bilities of FreeTDS.

       tsql  is  *not* a replacement for a complete isql, such as sqsh (www.sqsh.org).  It is designed to
       rely on the lowest level FreeTDS library, tdslib, as a way to isolate potential bugs in the proto-
       col implementation.

Below is a very simple php script which can test a MSSQL Connection from Linux server to external server to see if it's successful or not:

[code]
<?php $server = '10.198.16.17'; $link = mssql_connect($server, 'dd_ods_ro', 'd4r.Qod5'); if (!$link) { die('Something went wrong while connecting to MSSQL'); } else { echo "connected successfully\n"; } ?>[/code]

Hope any worked for you.

Connect to MSSQL on Linux ?! Can i ?

sqllinux

How to connect or test MSSQL connection from Linux Command Line ?

There are several ways to do so but for me i am using the following:

First install FreeTDS and UnixODBC:

yum search unixODBC
yum install unixODBC
yum install freetds

Then you can use "tsql" , below is an example and you can use also -h for help:

tsql -H  ServerIP_HERE  -U userName  -P passWord  -p 1433

FreeTDS(May 14, 2011)                                    

NAME
       tsql - utility to test FreeTDS connections and queries

SYNOPSIS
       tsql   { -S servername [-I interface] | -H hostname -p port }
              -U username [-P password] [-o options]

       tsql   -C

DESCRIPTION
       tsql  is  a diagnostic tool provided as part of FreeTDS. It uses the TDS protocol directly to con-
       nect to Sybase or Microsoft SQL Servers, and allows the user to issue queries that test the  capa-
       bilities of FreeTDS.

       tsql  is  *not* a replacement for a complete isql, such as sqsh (www.sqsh.org).  It is designed to
       rely on the lowest level FreeTDS library, tdslib, as a way to isolate potential bugs in the proto-
       col implementation.

Below is a very simple php script which can test a MSSQL Connection from Linux server to external server to see if it's successful or not:

[code]<?php // Change ip.ip.ip.ip below with the destination server ip $server = 'ip.ip.ip.ip'; // Connect to MSSQL , change user with username and pass with the password $link = mssql_connect($server, 'user', 'pass'); if (!$link) { die('Something went wrong while connecting to MSSQL'); } else { echo "connected successfully\n"; } ?>[/code]