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

Hi Monks,
Here is my problem. If I try to establish a connection with a MySql database,
and for some reason it cannot connect I get an error message like:
DBI connect(':','root',...) failed: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) at ./install.bin line 100

What I need is, for example, first to check if a connection is possible, so I can make sure this message will not uppear and I can try to connect to an other mysql server!
Thanks a lot monks Luca

Replies are listed 'Best First'.
Re: how to test a connection with mysql db
by Zaxo (Archbishop) on Apr 15, 2005 at 21:37 UTC

    DBI->connect() will return undef if the connection failed, so you can call first one, then another on failure by,

    my $dbh = DBI->connect(@pref_args) || DBI->connect(@alt_args) or die $DBI::errstr;
    You will want to avoid setting RaiseError or PrintError in the connect calls. Set those you want as soon as convenient after connecting.

    Your error message suggests that your connection may be failing due to bad arguments. I'm not positive of that, but it's worth checking.

    The $dbh->ping method is handy for checking if a connection is still alive, once made.

    After Compline,
    Zaxo

      You will want to avoid setting RaiseError or PrintError in the connect calls.

      Actually, RaiseError defaults to 0 so you can just leave it off, but PrintError defaults to 1 so if you don't want messages printed, you'll need to explicitly set PrintError to 0 in the call to connect().

Re: how to test a connection with mysql db
by moot (Chaplain) on Apr 15, 2005 at 21:30 UTC
    Why can't you use the inability to connect to the first database as the check itself? Seems to me if you can't connect to server A (and you will know this through DBI's error handling), you would just try to connect to server B.
      If I try to connect, and it fails, because this server is off line, my perl-script generates a message (to screen!!) that it failed. I don't want this message to screen.
        Pass PrintError => 0 as an option in your connect string. From perldoc DBI:
        The "\%attr" parameter can be used to alter the default settings of "PrintError", "RaiseError", "Auto­ Commit", and other attributes. For example: $dbh = DBI->connect($data_source, $user, $pass, { PrintError => 0, AutoCommit => 0 });

        Update: Fixed bogus entity; added 'perldoc'.

Re: how to test a connection with mysql db
by wazoox (Prior) on Apr 15, 2005 at 21:44 UTC
    Hello, here are some things to check :

    1) are you sure the mysql server is running? try to connect using the 'mysql' command.

    2) double-check your parameters, hostname, port, username and password.

    3) try with a simple snippet of code.

    4) if it still doesn't work, post your code here.

    here's the simplest exemple:
    my $dbh = DBI->connect("DBI:mysql:database='database':host=localhost:p +ort=3307,'root','password') or die "can't connect: $!";
      But what about: how do I know if my connection is still OK ?
      Is there a command to test the connection ? or what is the best way to test this ?

      Thanks
      Luca
        You shouldn't have to worry that much about the connection; simply test the return value of each DBI statement, if it fails, then it MAY be because the connection's lost.
        $dbh->something() or .... # put your error management code here