Creating a DSN-less connection to a MySQL database

A

Al Reynolds

Hello,

I hope someone will be able to help me with this.

I am trying to set up a connection to my MySQL
database using perl. I already have the database
set up and have used it with PHP without any problems.

When I try and execute the following code, I get an error:
(obviously I have substituted the names/passwords)

#!/usr/bin/perl
use DBI;
my $dbh =
DBI->connect('DBI:mysql:databasename:localhost','username','password');
$dbh->disconnect or warn "Disconnection error: $DBI::errstr\n";
exit;

I contacted the service provider and they replied:
"Using this code, are you trying to create a dsn-less
(adodb) connection, as opposed to dsn (odbc), because
only the former is supported on our hosting packages?"

I think I understand from what I have read so far that I
am currently trying to create a dsn connection, but I might
well be completely wrong about that! Does what the ISP
said make sense?

Can I use DBI to create a dsn-less (ADODB) connection,
or do I need to use something completely different?

IN ASP, I would need to use CreateObject('ADODB.Connection')
Is there something similar I can use in Perl?

The perl version is 5.00503
DB_File, CGI, DBI and DBD-Mysql modules are installed.

Server software is:
Apache/1.3.20 Sun Cobalt (Unix) Chili!Soft-ASP/3.6.2
mod_ssl/2.8.4 OpenSSL/0.9.6b PHP/4.1.2
mod_auth_pam_external/0.1 FrontPage/4.0.4.3 mod_perl/1.25

Thanks in advance,
Al Reynolds
 
J

James Willmore

I hope someone will be able to help me with this.

I am trying to set up a connection to my MySQL
database using perl. I already have the database
set up and have used it with PHP without any problems.

When I try and execute the following code, I get an error:
(obviously I have substituted the names/passwords)

#!/usr/bin/perl
use DBI;
my $dbh =
DBI->connect('DBI:mysql:databasename:localhost','username','passwor
d');$dbh->disconnect or warn "Disconnection error: $DBI::errstr\n";
exit;

Try to first, read the documentation for DBD::mysql and second, try to
connect using ...

DBI->connect('DBI:mysql:database=dbname;host=localhost',
'username',
'password')
or die "Connection error: ".$DBI::errstr."\n";

If you had read the documentation, you would have discovered that your
connection statement is wrong.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Never offend people with style when you can offend them with
substance. -- Sam Brown, "The Washington Post", January 26,
1977
 
A

Al Reynolds

James Willmore said:
Try to first, read the documentation for DBD::mysql and second, try to
connect using ...

DBI->connect('DBI:mysql:database=dbname;host=localhost',
'username',
'password')
or die "Connection error: ".$DBI::errstr."\n";

If you had read the documentation, you would have discovered that your
connection statement is wrong.

I read enough to decide that the using DBI would
generate a DSN connection*, which wasn't what I
wanted, which is why I gave up on the DBI route
to pursue the ADODB route. The code I used is
referred to in several perl-related resources and
tutorials on different web sites; I doubt that they
all made the same mistake. It isn't that uncommon
for a call to be possible in a variety of forms.

Anyway, thank you for the code snippet. If I am
trying to make a DBI connection again I will use it.

FWIW, I solved the problem using the older
mysql.pm module, which is less portable when
there is a possibility that the database type could
change later. It works fine just now though.

Cheers,
Al


* This may be wrong of course - I am still not quite clear
on the matter. There is surprisingly little info online about
the difference between DSN and DSN-less database
connections and how to set them up using perl.
 
J

James Willmore

Al Reynolds said:
I read enough to decide that the using DBI would
generate a DSN connection*, which wasn't what I
wanted, which is why I gave up on the DBI route
to pursue the ADODB route. The code I used is
referred to in several perl-related resources and
tutorials on different web sites; I doubt that they
all made the same mistake. It isn't that uncommon
for a call to be possible in a variety of forms.

Anyway, thank you for the code snippet. If I am
trying to make a DBI connection again I will use it.

FWIW, I solved the problem using the older
mysql.pm module, which is less portable when
there is a possibility that the database type could
change later. It works fine just now though.

Cheers,
Al


* This may be wrong of course - I am still not quite clear
on the matter. There is surprisingly little info online about
the difference between DSN and DSN-less database
connections and how to set them up using perl.

I had to review the ADODB manual to find out what you meant by
"DSN-less" database. In general, anything ADODB, DBI can do. There
is the DBD::ODBC module that you can use to connect to data sources
such as Access. You can also use DBD::proxy to create a client/server
type connection.

Basically - this is a term that ADODB authors came up with. You still
need to declare a data source using a "DSN-less" connection. So, the
term is mis-leading.

HTH

Jim
 
A

Al Reynolds

James Willmore said:
"Al Reynolds" <[email protected]> wrote in message

I had to review the ADODB manual to find out what you meant by
"DSN-less" database. In general, anything ADODB, DBI can do. There
is the DBD::ODBC module that you can use to connect to data sources
such as Access. You can also use DBD::proxy to create a client/server
type connection.

Basically - this is a term that ADODB authors came up with. You still
need to declare a data source using a "DSN-less" connection. So, the
term is mis-leading.

HTH

Cheers - it does.
Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top