gmacfadden has asked for the wisdom of the Perl Monks concerning the following question:

In preparation to seeking help from the Perl Monks, scholarship on this problem includes, but is not limited to, the following: a careful study of my self-study textbook (MySQL and Perl for the Web by Paul DuBois), and numerous hyperlinks in Google....all without success. I'm learning Perl and in following the textbook that I'm using, I'm trying to employ an easier way to connect to my mySQL server (the SQL database is on my web hosted site) using DBI's "connect_with_option_file" method and .my.cnf option file. In other words, in lieu of using the following instruction (which works perfectly well) in each of my cgi source programs:
$dbh = DBI->connect("DBI:mysql:gmacfadd_webdb:localhost","gmacfadd +_webdev2","webdevpass2", {PrintError => 0, RaiseError => 1});
I've put the following connection code information in a library function module called WebDB.pm and placed it in the directory home/gmacfadd/public_html/cgi-bin/dubois on my hosted site:
#! /usr/bin/perl -wT package WebDB; use strict; use DBI; my $host_name = "localhost"; my $db_name = "gmacfadd_webdb"; my $dsn = "DBI:mysql:database=$db_name;host=$host_name"; # Connect to MySQL server using hardwired name and password sub connect { return (DBI->connect ($dsn, "gmacfadd_webdev2", "webdevpass2", {PrintError => 0, RaiseError =>1})); } sub connect_with_option_file { $dsn .= ";mysql_read_default_file=$ENV{HOME}/.my.cnf"; return (DBI->connect ($dsn, undef, undef, {PrintError => 0, RaiseError =>1})); } 1; #return true
In accordance with the above code, the hardwired connection methods using name and password also work fine. It is the with connect_with_otion_file that I'm having difficulty. I have duly created the .my.cnf file in my root directory (C:/.my.cnf) on my Windows XP system, specifiying the following client program connection parameters:
[client] user="gmacfadd_webdev2" password="webdevpass2"
I have created and confirmed that the aforementioned user and password are existing in mySQL. When I then try to run the CGI containing the following:
#! /usr/bin/perl -wT # intro7b.cgi - use WebDB to connect use strict; # FORCE ALL VARIABLES TO BE DECLARED BEFORE + USE use DBI; # IMPORT THE DATABASE INTERFACE METHODS use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use lib qw(/home/gmacfadd/public_html/cgi-bin/dubois); use WebDB; my ($dbh, $sth, $count, $user); # DECLARE VARIABLES $dbh = WebDB::connect_with_option_file();
I receive the following error message: DBI connect('database=gmacfadd_webdb;host=localhost; mysql_read_default_file=/.my.cnf','',...) failed: Access denied for user: 'gmacfadd@localhost' (Using password: NO) at /home/gmacfadd/public_html/cgi-bin/dubois/WebDB.pm line 20 Can you please help me correct my problem?

Replies are listed 'Best First'.
Re: connect_with_option_file
by graff (Chancellor) on Jul 26, 2005 at 01:21 UTC
    It looks like you cannot count on using "$ENV{HOME}" to specify the path to your ".my.cnf" file. Figure out what the absolute path to the intended file is (e.g. it might be "/home/whoever_you_are/.my.cnf"). Remember that the CGI script runs as whatever "user" owns the web server process, and it's likely that "$ENV{HOME}" is undefined for that user.
      Thx for replying graff. I implemented your suggestion as follows:
      sub connect_with_option_file { $dsn .= ";mysql_read_default_file=home/gmacfadd/public_html/cg +i-bin/dubois/.my.cnf"; return (DBI->connect ($dsn, undef, undef, {PrintError => 0, RaiseError =>1})); }
      I also moved the option file (.my.cnf) to the following directory on my hosting server: home/gmacfadd/public_html/cgi-bin/dubois When I executed, here is the error I received:

      Software error: DBI connect('database=gmacfadd_webdb;host=localhost;mysql_read_default_file=home/gmacfadd/public_html/cgi-bin/dubois/.my.cnf','',...) failed: Access denied for user: 'gmacfadd@localhost' (Using password: NO) at /home/gmacfadd/public_html/cgi-bin/dubois/WebDB.pm line 20

      The fact the server thinks I'm gmacfadd@localhost instead of gmacfadd_webdev2 is puzzling...possibly meaning that it can't find my option file - but it is there!

        Try putting an initial slash in that path string, to make it absolute:
        $dsn .= ";mysql_read_default_file=/home/gmacfadd/public_html/cgi-bin/d +ubois/.my.cnf";
        (update: and if that doesn't work, be sure to check permissions along every step of that path; it needs to be readable by both the web server and the mysql daemon (which typically runs with username "mysql"), so rwxr-xr-x on each directory in the path, and rw-r--r-- on the config file itself.)
Re: connect_with_option_file
by salva (Canon) on Jul 27, 2005 at 15:31 UTC
    you can use Config::Find to find the configuration file, it will look in all the common places for it, for instance...
    use Config::Find; my $cfg_fn = Config::Find->new(name=>'foo');
    will look for it on
    ~/.foo ~/.foo.conf ~/.foo.cfg $FOO_HOME/etc/foo.conf $FOO_HOME/etc/foo.cfg /etc/foo.conf /etc/foo.cfg