I use this PHP script to to add a user with the permissions you specified.
this script also randomize a password and dump it into a file in the user home dir.
you will need to fix it a bit because it depends on some other files, but it should be easy enough to just take the Sql statements and use them.
#! /usr/bin/php
<?php
error_reporting(E_ALL);
define('ADMIN','true');
require_once 'utils/utils.php';
if (count($argv) < 2)
{
echo "usage : php -f ". $argv[0] . " user\n";
return;
}
$user = $argv[1];
$pass = createRandomPassword(10);
global $conn;
if(false === $conn->query("CREATE USER '$user'@'localhost' IDENTIFIED BY '$pass'")) die("mysql error\n");
if(false === $conn->query("GRANT USAGE ON * . * TO '$user'@'localhost' IDENTIFIED BY '$pass'")) die("mysql error\n");
if(false === $conn->query("GRANT ALL PRIVILEGES ON `$user\_%` . * TO '$user'@'localhost'")) die("mysql error\n");
echo "Created mysql user $user/$pass\n";
savePasswordFile($user,$pass);
function savePasswordFile($user,$pass)
{
$passfile = "/home/$user/passwords.txt";
$f = fopen($passfile,"a");
if (!$f) die("Error opening passwords file");
fputs($f, "Mysql login:\nuser : $user\npass : $pass\n\nYou may create databases named ".$user."_*, for example ".$user."_blog\n");
chown($passfile,"$user");
chgrp($passfile,"$user");
chmod($passfile,0700);
fclose($f);
}
function createRandomPassword($length)
{
$chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ023456789-!@#$%^&*";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= $length) {
$num = rand() % strlen($chars);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
?>