im trying to write simple shell script to create new mysql user and database for this user. Since i dont want to do if using mysql root account, ive created a new user, lets call him 'creator'. I gave him INSERT, SELECT, UPDATE, CREATE, CREATE USER, GRANT privilegs, but i cant grant access to created database for the new user.

Script looks something like this:

    myuser=creator
    mypass=xxxx
    dbuser=newuser
    dbname=newdb

    mysql -u$myuser -p$mypass -rs -e "CREATE USER '$dbuser'@'localhost' IDENTIFIED BY '$dbpass';";
    mysql -u$myuser -p$mypass -rs -e "GRANT USAGE ON * . * TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpass' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;";
    mysql -u$myuser -p$mypass -rs -e "CREATE DATABASE IF NOT EXISTS $dbname ;";
    mysql -u$myuser -p$mypass -rs -e "GRANT ALL PRIVILEGES ON $dbname . * TO '$dbuser'@'localhost';";
    mysql -u$myuser -p$mypass -rs -e "FLUSH PRIVILEGES ;";

I get ERROR 1044 (42000) at line 1: Access denied for user 'creator'@'localhost' to database 'newdb'. When i login as 'creator' i get same erros which is odd since 'creator' has GRANT privilege.

So, what privilege is 'creator' missing? I want him to have as minimal privileges as possible.

link|improve this question
feedback

1 Answer

At the moment, based on the comments below, I think the issue is that the creator user doesn't have the permissions it's trying to grant to the newuser account.

According to the documentation,

You cannot grant another user a privilege which you yourself do not have

So I understand your desire to have the creator account have as few privileges as possible, but that's not how the MySQL privilege model works; to GRANT ALL on newdb.* to newuser, creator will also need ALL on newdb.*, as well as GRANT privilege.

link|improve this answer
Im pretty sure im logged in correctly. Tried both shell and phpmyadmin.. – psztucz Nov 16 '10 at 10:46
show grants produces:GRANT SELECT, INSERT, UPDATE, CREATE, RELOAD, ALTER, SHOW DATABASES, CREATE VIEW, CREATE USER ON . TO 'creator'@'localhost' IDENTIFIED BY PASSWORD '*2EA417AC557EDC669453727C84FF5B4AC8C58529' WITH GRANT OPTION – psztucz Nov 16 '10 at 10:47
OK, then from shell, could you confirm that you can do a mysql -u creator -pMYPASSWORD and confirm that you can connect correctly. – MadHatter Nov 16 '10 at 10:47
v1:~# mysql -ucreator -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 901128 Server version: 5.0.51a-24+lenny3 (Debian) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> GRANT ALL PRIVILEGES ON newdb . * TO 'newuser'@'localhost' -> ; ERROR 1044 (42000): Access denied for user 'creator'@'localhost' to database 'newdb' – psztucz Nov 16 '10 at 11:08
could you try, as mysql root, GRANT ALL ON newdb.* to 'creator'@'localhost'; , then see if that changes the output of (a) SHOW GRANTS FOR 'creator'@'localhost' and (b) the GRANT ALL you reference in the above comment? – MadHatter Nov 16 '10 at 11:34
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.