Perl DBI connection works but not with Catalyst - perl

I am using Perl, and I am able to connect to a local PostgreSQL 9.3 server using
this code
#!/usr/bin/perl
use DBI;
use strict;
my $dbh = DBI->connect("dbi:Pg:dbname=postgres;host=127.0.0.1;port=5432", "postgres", "postgres", { RaiseError => 1 }) or die $DBI::errstr;
$dbh->disconnect();
print "Done\n";
Now, following the Catalyst documentation about PostgreSQL I try to generate a Catalyst model.
I use this shell command
script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
create=static components=TimeStamp,EncodedColumn \
'dbi:Pg:dbname=postgres,host=127.0.0.1,port=5432' 'postgres' 'postgres' '{ AutoCommit => 1 }'
But I get the following error:
DBIx::Class::Storage::DBI::catch {...} ():
DBI Connection failed:
DBI connect('dbname=postgres,host=127.0.0.1,port=5432','postgres',...) failed:
FATAL: database "postgres,host=127.0.0.1,port=5432" does not exist at
/usr/local/share/perl/5.18.2/DBIx/Class/Storage/DBI.pm line 1487. at
/usr/local/share/perl/5.18.2/Catalyst/Helper/Model/DBIC/Schema.pm line 637

Your first DBI connection test has a properly configured connection string using semi-colons ';' - your second one is using commas ',' - which are not valid property separators.
Change from;
'dbi:Pg:dbname=postgres,host=127.0.0.1,port=5432'...
To;
'dbi:Pg:dbname=postgres;host=127.0.0.1;port=5432'...

Related

Informix error in Perl: ISAM: 107: ISAM error: record is locked. at test_db_connectivity.pl line 14

I'm trying to connect to our database in our production environment. I am getting the 107 ISAM error: record is locked . I have checked various articles on what it could be, but no luck. I double checked the database details with our DBA and the information is correct.
FYI - Also the first error previous to the "record is locked" error I received was SQL: -931: Cannot locate web1_tcp service/tcp service in /etc/services. at test_db_connectivity.pl line 13. . I worked with our SA to get that entered and that is no longer appearing. Can anyone offer any guidance on the ISAM 107 error? Is my connection string wrong? There shouldn't be a lock because I am not even opening a connection to the database.
test_db_connectivity.pl:
#/usr/bin/perl -w
use DBI;
use strict;
use DBI qw(:sql_types);
use DBD::Informix qw(:ix_types);
my $driver = "Informix";
my $database = "web1";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "user";
my $password = "password";
print "I got this far!\n";
my $dbh = DBI->connect($dsn, $userid, $password, {RaiseError => 1}) or die $DBI::errstr;
print "Opened db successfully\n";
$dbh->disconnect();
Some of the variables set:
export INFORMIXDIR=/opt/informix
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INFORMIXDIR/lib:$INFORMIXDIR/lib/cli:$INFORMIXDIR/lib/esql:$INFORMIXDIR/lib/tools
export INFORMIXSERVER=web1
INFORMIX SQLHOSTS FILE:
#dbservername nettype hostname servicename options
web1 onsoctcp 10.199.***.* web1dev1_tcp
etc/services FILE:
web1dev1_tcp 1533/tcp # Port assigned to database
virtual-places 1533/tcp # Virtual Places Software
web1dev1_tcp 1533/udp # Port assigned to database
On Linux, there is a system error 107:
107 (ENOTCONN): Transport endpoint is not connected
It is hard to distinguish that from a C-ISAM error 107 (the number is the same). However, in context, I think the ENOTCONN is far more likely to be the problem than
ISAM ERROR 107: record is locked.
I observe that you're using a notation in $dsn that DBD::Informix doesn't test for:
my $dsn = "DBI:$driver:dbname=$database";
The dbname= portion of that is incorrect. You should use:
my $dsn = "DBI:$driver:$database";
Note that the DBI spec (when I last looked) said that what follows the driver name is driver-specific. You could submit an enhancement request.
However, on my working system, when I use the dbname=stores notation, the error I get is:
DBI connect('dbname=stores', '',...) failed: SQL: -354: Incorrect database or cursor name format. at dbname-dsn.pl line 8.
That's quite different. You say you got:
SQL: -931: Cannot locate web1_tcp service/tcp service in /etc/services. at test_db_connectivity.pl line 13
The error message is quite explicit. You need an entry in /etc/services such as:
web1_tcp 9088/tcp
At the moment, you do not have such an entry, and therefore the connection is not even attempted, much less successful.

can't ssh:unable to establish master SSH connection: bad password or master process exited unexpectedly

I have problem with perl modules, i 'm trying to connect to the remote server using the OpenSSH module from CPAN and i have given the username and password correctly but when i run my CGI from browser i see the following error message "
can't ssh:unable to establish master SSH connection: bad password or
master process exited unexpectedly
in need of immediate reply's and awaiting,
Thanks in Advance.
Here is my code
#!/usr/bin/perl -w
use CGI;
use Data::Dumper;
use strict;
use CGI::Carp qw(fatalsToBrowser);
use Net::OpenSSH;
my $query = CGI->new();
#my $mach_name=$query->param('mach_name');
my $mach_name= '****.cce.***.com';
my $user='root';
my $passwd='*******';
my $ssh = Net::OpenSSH->new("$mach_name",user => "$user" , passwd => "$passwd
", master_opts => [-o => "strictHostKeyChecking=no"]);
$ssh->error and die "can't ssh:" . $ssh->error;
my $mem_info =$ssh->capture("ioscan -m lun");
print "$mem_info";
print "Content-type: text/html\n\n";
print "<html><head><title>test page</title></head>\n";
print "<body><p>Dicovering Machine Please wait....</p>\n";
print "<p>$mach_name</p>";
print "<p><b>Swap Memory :$mem_info </b> </p>";
print "</body></html>";
The same program if i run through the command line it is getting the o/p but through the browser i'm seeing the above error i have given the passwd and username correctly
My problem was solved when I've increased the timeout from 10 seconds to 15 seconds, because the device is using an older SSH daemon version, but my linux server that made the client connection is upgraded and of course it probes first the newer ciphers.
my $ssh = Net::OpenSSH->new(
$host,
port => $port,
user => $user,
password => $pass,
timeout => 15, # <= this was 10 and now is 15
kill_ssh_on_timeout => 1,
strict_mode => 0,
master_opts => [-o => "StrictHostKeyChecking=no", '-vvv'], #<= -vvv helped much
ctl_dir =>'/tmp/libnet-openssh-perl',
Looks like some permissions issue. Run ssh in verbose mode:
my $ssh = Net::OpenSSH->new("$mach_name",
user => "$user",
passwd => "$passwd",
master_opts => '-vvv',
master_stderr_fh => \*LOG);
If that doesn't give you enough information about the cause of the problem, then, you can use truss to see what's happening at the OS level.
Also check .ssh/known_hosts, make sure .ssh directory is writeable by Apache. .libnet-openssh-perl directory should be writeable by Apache.
For example, my Apache user is apache with a home directory of /var/www
I have /var/www/.ssh owned by apache, and /var/www/.libnet-openssh-perl owned by apache. I ssh to devices using my own account, then copy my known_hosts file to /var/www/.ssh/known_hosts

Not able to connect to .accdb file using perl

I am trying to connect to access 2007 database using perl. Below is the code that I tried executing
use DBI;
$path='T:\a\abc.accdb';
$datasource = "driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=$path";
$dbh = DBI->connect("dbi:ODBC:$datasource", '', '') || die "Error connecting: $!";
$query="select name from receiver";
$exe=$dbh->prepare($query) or die "cannot prepare";
$exe->execute or die "cannot execute";
$a=$exe->fetchall_arrayref();
foreach $aa(#{$a})
{
print #$aa;
}
but the execution fails with the below error
DBI connect('driver={Microsoft Access Driver(*.mdb,*.accdb)};DBQ=T:\a\abc.accdb','',...)failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at test.pl line 5.
Error connecting: at test.pl line 5.
can someone please guide me how to resolve this issue.
Your driver name is missing a space. ODBC is very fussy about that. Try
$datasource = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=$path";
Also, NAME is a reserved word in Access SQL so you may need to use
$query="select [name] from receiver";
My experience with DBI:ODBC is somewhat dated but I would suggest the following:
Check the driver names in the ODBC datasource setup and make sure
that you have the spelling right. (spaces and all) - Maybe set up a
DSN and use that instead - more reliable
Check that the file exists (I don't think that is the problem from
the error message)
Capitalization on the Driver/Dbq (not likely but we are dealing with
MS)

Error while connecting to Sybase DB

I have a perl script which connects to a Sybase db server (alias for server - MYDATABASESERVER). My code is:
exec perl -w -x
#!perl
use Sybase::DBlib;
use Mail::Sendmail;
use Env qw(DSQUERY DBNAME DBUSER DBPASSWD);
$dbh = &execRemoteSQL($sql_text);
sub execRemoteSQL
{
my ($sqlText) = #_;
my ( $ret, $retS );
local ($dbh) = undef;
$dbh = new Sybase::DBlib $DBUSER, $DBPASSWD, $DSQUERY;
if ( !(defined $dbh) )
{
print STDERR "execRemoteSQL(): Failed To Create DB Handle for :\n";
print STDERR " SERVER = $DSQUERY\n";
print STDERR " DATABSE = $DBNAME\n";
exit(-1);
}
.
.
.
When i provide the server name as MYDATABASESERVER (value of DSQUERY), I get the error in the If statement, but it connects properly to the server with name as MYDB.
Wanted to know if there is any constraint on the server name length or is it due to something else.
Here is the error messsage i am getting:
DB-Library error:
Unknown host machine name.
execRemoteSQL(): Failed To Create DB Handle for :
SERVER = MYDATABASESERVER
DATABSE = my_db
The server alias is defined properly because i am able to connect to the db using isql.
I don't think the length of the server name is too long here.
What is your exact error message?
Maybe the MYDATABASESERVER server-alias name is not defined properly in the Sybase-specific "interfaces" config file. It resides in dir $SYBASE/$SYBASE_OCS. What does it say about MYDATABASESERVER (should be 2 lines)?
In any case:
Don't use DBLib, it is really old and deprecated. I think the maintainer of the Sybperl/DBLib and DBD::Sybase modules, only still supports the Sybase::DBLib code to make life easier for client programmers who have to deal with old programs, and to keep legacy applications compatible with new releases of Sybase ASE.
You should use DBI and DBD::Sybase instead. 99% of all perl/sybase code on the internet uses DBI.
Can you try the following syntax instead? :
$dbh = Sybase::DBlib->new( $DBUSER, $DBPASSWD, $DSQUERY );

How do I connect with Perl to SQL Server?

I have a user id, password, database name and datasource details. I want to connect with Perl to a MSSQL server. I just used the following snippet, but I am getting an error.
#!/usr/bin/perl -w
use strict;
use DBI;
my $data_source = q/dbi:ODBC:192.168.3.137/;
my $user = q/bharani/;
my $password = q/123456/;
# Connect to the data source and get a handle for that connection.
my $dbh = DBI->connect($data_source, $user, $password)
or die "Can't connect to $data_source: $DBI::errstr";
My error is:
DBI connect('192.168.3.137','bharani',...) failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at my sqlconnect.pl line 14
Can't connect to dbi:ODBC:192.168.3.137: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at mysqlconnect.pl line 14.
The SQL server runs on another system, I am just trying to connect with above details. Please tell me, should I crease DSN in my system, or is anything missing in my program?
Everything following 'dbi:ODBC:' in your connection string is passed to the ODBC driver. For MSSQL, try this connection string:
DBI->connect("dbi:ODBC:Driver={SQL Server};Server=192.168.3.137;UID=$user;PWD=$password")
You can find some more alternatives on connectionstrings.com
I am on Ubuntu (20.04) and followed the instructions to install SLQ Server in a docker container:
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15&pivots=cs1-bash
I created the TestDB and the table Inventory as per the tutorial.
I installed the ODBC driver following
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
The driver library file was /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1
With that, the following script successfully connects to the DB and returns the content of the table:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $user = 'SA';
my $password = '<YourNewStrong#Passw0rd>';
my $dbh = DBI->connect("dbi:ODBC:Driver={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1};Server=localhost;Database=TestDB;UID=$user;PWD=$password");
my $sth = $dbh->prepare("SELECT * FROM Inventory");
$sth->execute();
while ( my #row = $sth->fetchrow_array ) {
print "#row\n";
}