Database Logins

As mentioned before, database administrators have to deal with some of the same issues system administrators contend with, like maintaining logins and accounts. For instance, at my day job we teach database programming classes. Each student who takes a class gets a login on our Sybase server and her or his very own (albeit small) database on that server to play with. Here’s a simplified version of the code we use to create these databases and logins:

use DBI; # USAGE: syaccreate <username> $admin = 'sa'; print "Enter passwd for $admin: "; chomp($pw = <STDIN>); $user=$ARGV[0]; # generate a *bogus* password based on user name reversed # and padded to at least 6 chars with dashes $genpass = reverse join('',reverse split(//,$user)); $genpass .= "-" x (6-length($genpass)); # here's a list of the SQL commands we will execute in order # we: 1) create the database on the USER_DISK device, # with the log on USER_LOG # 2) add a login to the server for the user, # making the new database the default # 3) switch to the newly created database # 4) change its owner to be this user @commands = ("create database $user on USER_DISK=5 log on USER_LOG=5", "sp_addlogin $user,\"$genpass\",$user", "use $user", "sp_changedbowner $user"); # connect to the server $dbh = DBI->connect('dbi:Sybase:',$admin,$pw); die "Unable to connect: $DBI::errstr\n" unless (defined $dbh); # loop over the command array, execute each command in turn for (@commands) { $dbh->do($_) or die "Unable to $_: " . ...

Get Perl for System Administration now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.