Le mar 07/09/2004 à 11:24, Rob Keeling a écrit :
> Having googled extensively, I can`t seem to find a way to do the following.
> 
> I have a mysql table, with around 1200 usernames in it.
> 
> What I want to do is programmatically add each user, and create a database
> of the same name
> that that user has access to.
> 
> (This is for a school web server, we want to allow students to try out mysql
> querys with phpmyadm etc)
> 
> Any suggestions?

I recently tried Perl::DBI to create a lot of databases and found it
crashes after 100 databases because it doesn't close properly its
handler. So I would suggest not to do that.

Now I think the best way (not the cleanest) is to use a PHP script with
'exec' command to call:
 mysqladmin -u user --password=pass create tablename

I've done that in Perl so if you're more comfortable with Perl, go
ahead.

Also, I have a shell script creating databases by calling mysqladmin,
but it's based on a file containing all databases names (which you need
to output from mysql client first).


The best thing I would suggest is to do it in PHP or Perl in a loop
fetching all students name. You will also need to give the students
sufficient permissions on their own database so you will have to create
1200 mysql users, otherwise they will all have access to the other's
databases.

With PHP this would be something like:

$admin = 'adminuser'; # this is the username allowed to create databases
$pass = 'pass'; # this is his password

$query = "SELECT username from user";
$result = mysql_query($query);
foreach($row = mysql_fetch_array($result)){
        exec('mysqladmin -u '.$admin.' --password='.$pass.' create
'.$row['username']) 
                || die "Could not create database ".$row['username'].":
".mysql_last_error();
        exec(...something that will give the right to that user on that
database...)
                || die "Could not grant permissions to ".$row['username'].":
".mysql_last_error();
}

Cheers,

Yannick


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to