Path: /root/bin/addSitedb.sh Size: 1034 bytes
Automatically add a mysql user and database with pwgen password. It is meant to be used to create a user with his own database. Within that database the user should have all rights of dropping, creating and manipulating tables. Please note that the permissions are probably to loose for most uses.
#!/bin/sh
#
#You need a ~/.my.cnf to contain the password for the root login to get this
# script working nicely.
#
user=`echo -n ${1}|tr '. ' __`
db=${user}
pass=`pwgen -s 20 1`
#Must have a useranme
if [ -z "${user}" ]; then
echo 'Need a username.'
exit
fi
#Maximum username of 16
if [ "${user:0:16}" != "${user}" ]; then
echo 'Username to large'
exit
fi
echo 'Creating user : '${user}
echo ' with password: '${pass}
echo ' with db: '${db}
echo
#TODO: drop FLUSH and move CREATE USER into a "identified by" statement.
# Which will make it two clean statements of CREATE and GRANT
# Also, the list given may also be equal to ALL PRIVILEGES ON (check)
mysql -u root <<EOF
CREATE USER '${user}'@'localhost' IDENTIFIED BY '${pass}';
CREATE DATABASE ${db};
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EXECUTE, INDEX, INSERT, LOCK TABLES, SELECT, UPDATE ON ${db}.* TO '${user}'@'localhost';
FLUSH PRIVILEGES;
EOF
#echo 'Kill this user with DROP USER '${user}
Copyright (C) 2010 A. Bram Neijt
This file is part of the logfish.net configuration collection.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You can find a copy of the GNU General Public License
online at <http://www.gnu.org/licenses/>.