# perl MSSQL



## igor_l (Oct 3, 2012)

Hello, everybody

I have problem with connect between perl and mssql.
I am using FreTDS,unixODBC,perl

config files
1.freetds.conf

```
[FreeTDS]
Description     = MSSQL Driver
Driver          = /usr/local/lib/libtdsodbc.so.0
```
2.
	
	



```
[mssql]
Driver=FreeTDS
Trace=Yes
TraceFile=~/msssl.log
Server=x.x.x.x
Port=1433
Database=test
```

3.odbcinst.ini

```
[FreeTDS]
Description=MSSQL Driver
Driver=/usr/local/lib/libtdsodbc.so.0
UsageCount=2
```
4. perl script

```
$user = "sa";
$password = "123456";
$server = "mssql";
my $dbh = DBI->connect("DBI:Sybase:server=$server", $user, $passwd, {PrintError => 0});
my $sth;

unless ($dbh) {
    die "ERROR: Failed to connect to server ($server).\nERROR MESSAGE: $DBI::errstr";
} else {
    print "\n";
    print "Successful Connection.";
    print "\n\n";
}
```

When I try connect from shell to db 
`$ isql -v -s mssql sa 123456`

```
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[08S01][unixODBC][FreeTDS][SQL Server]Server is unavailable or does not exist.
[ISQL]ERROR: Could not SQLConnect
```
When I try connect from script I have error

```
ERROR: Failed to connect to server (mssql).
ERROR MESSAGE: OpenClient message: LAYER = (0) ORIGIN = (0) SEVERITY = (78) NUMBER = (41)
Server mssql, database
Message String: Server is unavailable or does not exist.
```

What's wrong? 

thanks for any help


----------



## SirDice (Oct 4, 2012)

Double check if you have the correct IP address or hostname for the server.

And as a friendly reminder, don't use the SA account in your scripts. Create a separate user account with just enough privileges to do what it should do. Using SA is a recipe for disaster.


----------



## igor_l (Oct 4, 2012)

I used SA account just for test, i now it's disaster  but thanks


----------



## igor_l (Oct 4, 2012)

Solved

Ok, I solve my problem, but instead of perl+freetds+odbc I am used php+mssql+freetds, I want to show you my solution maybe anybody use it.

1.php 5.3.17 + mssql (extension)

2.freetds with next freetds.conf

```
[global]
        try server login = yes
        try domain login = no
        cross domain login = no
        text size = 64512

        initial block size = 512

        swap broken dates = no
        swap broken money = no

[myhost]
       host = your ip
       port = your port (default for mssql 1433)
       instance = your_instance     (if use, i use because it's comfortable for me)
       tds version = 8.0
```
2. php script

```
<?php

$dbc = mssql_connect('myhost','test','123456');
if(!$dbc){
        echo "error";
}else
{
        echo "ok";
}

$q2 = mssql_query("backup database test to disk=N'\\\\your_IP\\incoming\\test.bak' with format");
//$q1 = mssql_query("select * from sys.server_principals");
//$row = mssql_fetch_array($q2);
//print_r($row);

echo mssql_get_last_message();
?>
```


----------

