Main Content

Checking the database connection manually

Archive - Originally posted on "The Horse's Mouth" - 2009-08-28 04:33:45 - Graham Ellis

For an initial database connectivity test from my own code, I use the mysql client program and 'check out' the commands manually through that. Noting all the parameters, I then check through a piece of code in my target language ... in this brief example from Wednesday, it was Perl:

use DBI;
 
# From my manual test:
# mysql -h192.168.200.84 -utrainee -pabc123
# use wellho
# select agid,locate,sid from sales
 
$db_handle = DBI ->
  connect("DBI:mysql:wellho:192.168.200.84","trainee","abc123")
    or die ("connection failed: $DBI::errstr\n");
 
$qu_handle = $db_handle -> prepare("select agid,locate,sid from sales");
$qu_handle -> execute;
 
while (@row = $qu_handle -> fetchrow()) {
  print "@row\n";
  }

One of the most common questions I get by email from people I have never heard of before relates to getting 3rd Party software running on their own systems. And one of the most common solutions that I suggest is checking out their database connection - changing sample MySQL accounts from user "hello" and password "world" to the name of an account that exists on their system ;-). A short test such as the one above, and done with the knowledge of what each step is doing, can identify these initial connection problems very quickly indeed.