2009-08-05
[Perl] Waiting for a DB connection to become available
While working on Hadoop, I found that connecting to a DB to get or insert data presented a problem as you often need more connections then what's available. My solution right now is to "wait" for a connection to become available - without using any fancy queuing mechanisms:
And that's it. When the while loop finally exists, you have a DB handle to use. In the above example we retry for more then an hour - which should be sufficient for most scenarios.
Hope some one else find it useful as well.
# Connect to DB
my $cont = 0;
my $tries = 0;
my $dbh1;
while( not $cont ) {
$tries++;
eval {
$dbh1 = DBI->connect( "dbi:mysql:database=$dbdb;host=$dbhost;port=$dbport", "$dbuser", "$dbpass" );
};
if( $@ ) { sleep( 1 ); } else { $cont++; }
if( $tries > 3600 ) {
# We have been trying for an hour - give up
print "GIVE_UP\t1\n";
exit;
}
}
And that's it. When the while loop finally exists, you have a DB handle to use. In the above example we retry for more then an hour - which should be sufficient for most scenarios.
Hope some one else find it useful as well.
Labels: database, hadoop, mysql, perl
2008-07-07
SSH and Virtual User Authentication
I was searching around for a solution on how to authenticate users via OpenSSH on a Linux system, if the users live in a DB. It appears that not a lot of information is available on the topic on some of the more obvious search terms, but after some digging the solution was actually straight forward.
Of course I'm running Debian stable, so the following solution should work for all Debian based systems. However - try at your own risk :-)
Step 1: Install all dependencies:
Step 2 : Prep your database:
Step 3 : Modify your /etc/nsswitch.conf file by replacing the following lines:
Step 4 : Restart SSH and TEST
Also refer to the libnss-mysql-bg documentation online.
Of course I'm running Debian stable, so the following solution should work for all Debian based systems. However - try at your own risk :-)
Step 1: Install all dependencies:
# apt-get install mysql-server libpam-mysql libnss-mysql-bg
Step 2 : Prep your database:
# mysql -uroot < /usr/share/doc/libnss-mysql-bg/examples/linux/sample_database.sql
Step 3 : Modify your /etc/nsswitch.conf file by replacing the following lines:
#passwd: compat
#group: compat
#shadow: compat
passwd: files mysql
group: files mysql
shadow: files mysql
Step 4 : Restart SSH and TEST
Also refer to the libnss-mysql-bg documentation online.
Labels: authentication, linux, mysql