MySQL user full permission to own databases - mysql

Is there a way of giving a user full permissions to create new databases etc, but cannot access or see databases created by other users?
I need multiple users to be able to create their own databases as if super user, but not be able to effect other user databases.

Some thing like this may help.
GRANT ALL PRIVILEGES ON new_user\_% . * TO 'new_user'#'%';
this will grant new_user only access to database created by himself. You can do the same with others.
Hope this helps.

Related

What are the minimum privileges required to create MySQL databases, users, and grant permissions?

Using either MySQL 5.6 or 5.7, hosted in AWS RDS, or a Docker MySQL container, I'd like to create a least-privileged user, eg named creator, that can perform the following actions:
Create a new database.
Create a new user.
Grant the new user SELECT and INSERT permission for all tables in the new database.
I'd prefer if the creator user does not have access to existing databases that it was not responsible for creating.
Is this achievable?
My research so far suggests that such a creator user may require global SELECT and INSERT permissions across the MySQL instance but this seems excessive.
You may do this with a Stored Procedure and a workaround that it's simplier than SP and may be what you are looking for:
GRANT CREATE USER ON *.* TO '<user>'#'<host';
GRANT ALL PRIVILEGES ON `<user>_%`.* TO '<user>'#'<host>' WITH GRANT OPTION;
As you can see in this answer you need to pay attention to a few points:
User will only able to create databases that begins with his username + '_' (Using the given link examples if your username is aaa you can create database whose name are aaa_example, if aaa wants to create a database named bbb_example mysql will drop a permission denied error.
He will only has privileges on those databases created by him, but the ones created by his created users(If he grants that privilege).
Yet he might manage privileges in those databases owned by him.
Having that in mind, you may tweak this `<user>_%` to whatever fit the most of your needs.

MySQL Restrict user to register only one database

I am Stuck at a point where Requirement is :
I have created new user in mysql with insert,delete,update,select & create permission now i want if someone register database with this new user he should only able to register only one DATABASE from my list of databases ....
Restrict new user to register only one Database .... from my db list....
Thanks
Are you asking how you do this via script? In MySQL Admin you can grant rights by Database/Schema and a user can only connect to the database he has at least the right to SELECT.
If you want to do it without GUI you can do it by command line. From the docs (http://dev.mysql.com/doc/refman/5.1/en/grant.html):
GRANT SELECT, INSERT ON mydb.* TO 'someuser'#'somehost';
The command above grants SELECT and INSERT on the database "mydb" for the user "someuser" that tries to connect from "somehost" for all tables contained in the database. Of course you can set rights in more Detail, for single tables for example or to grant the right to connect from any host.
Or are you looking for a way to do this via phpmyadmin? Also there you can grant rights by database or overall but as I experienced you have to look closely sine do not see this right away and sometimes set the rights for all databases by accident.
Probably the best way is to take away all rights and then start from scratch so you do not miss to remove any (works for MySQL 4.1.2 and newer)
REVOKE ALL PRIVILEGES, GRANT OPTION FROM user
REVOKE INSERT ON SOMEDB.* FROM 'username'#'USER_IP';

Setting about users in phpmyadmin

I am using the phpmyadmin in wamp server. I have create a new user, and I don't want this user can see all the database. I only want this user can see and modify some of the database that I allow to this user.
Thanks a lot.
Pretty sure the best way to do this would be to create a new mysql user, and then login to phpmyadmin as that user.
How to create a new mysql user: http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
Once you have the user created, you can enable which databases that user can access as the mysql root user.
My guess is if you limit the user accounts access to specific databases in mysql using GRANT http://dev.mysql.com/doc/refman/5.1/en/grant.html, phpmyadmin will auto detect the logged in user and show him only those databases that he has privileges to use.
From within phpMyAdmin, find the Users tab on the main page. Find your user and click the "Edit Privileges" link.
From there, it sounds like you want to un-check all of the Global privileges. Then scroll down to "Database-specific privileges" and select the database you want them to access from dropdown at "Add privileges on the following database:".
That will take you to another page where you can set the permissions for that specific database; it sounds like you probably want to check them all. Click the Go button and give it a try!

MySQL Root User

I was trying to create a very simple MySQL event and realized the action was not running. I then realized that the user I was using did not have super privileges (or any other event-related privileges). I tried granting all privileges to this user via phpMyAdmin but that wasn't working (makes since because I was logged in as that user and not the root user). In fact, I'm not sure if my account has a root user. How do I find out who the root user is (username and password)?
I'm using a hosting provider that has cpanel and when I look at a list of my mysql users I only see the ones I created. Is there a way I can find out via phpMyAdmin if I'm logged in as another user?
Also, whenever I create a user in cpanel, I always select the checkbox to grant "all privileges" but I found out just now that super priviledge and a few other privileges are not on the list. As far as I can tell, I won't be able to create any mysql events since I can't grant super privileges to any of my users.
Addition:
When assigning a user to a database, the only privileges I can select from are
ALTER
ALTER ROUTINE
CREATE
CREATE ROUTINE
CREATE TEMPORARY TABLES
CREATE VIEW
DELETE
DROP
EXECUTE
INDEX
INSERT
LOCK TABLES
REFERENCES
SELECT
SHOW VIEW
TRIGGER
UPDATE
From what everyone is saying in their answers it looks like I'm using a shared database, the event schedule is either turned off and/or I am not allowed to grant users privileges dealing with events. Kind of sucks..
But for now I will use cron jobs.
Rule of thumb: Your application never, ever needs superuser privileges.
Not even to use the event system.
The only thing you need SUPER for in this instance is to enable or disable the scheduler (which is a server-wide setting). If you're on a shared database host, it will be up to the provider whether or not the scheduler is enabled, and whether you've been granted the EVENT privilege on your database(s).
If you're not on a shared host, check your my.ini for the event-scheduler line and make sure it's enabled. Then, make sure you've granted EVENT to your application userid. The EVENT privilege should be granted if you've used GRANT ALL PRIVILEGES on the database (as opposed to on individual tables).
You'll need to contact your hosting provider and ask for event-related privileges for your account. They most likely store all their user databases under the same MySQL server, so for obvious security reasons root access can't be granted.
select user() would tell you the login that the query's executing under.
However, you should NOT grant 'super user' privileges to any code that handles DB operations for public-facing code. For proper security, public-facing DB code SHOULD have the minimum privileges required to get the job done. Usually that would update/insert/delete/select only.
Granting things like drop/alter/create/grant/etc... is just asking for trouble.
I was trying to create a very simple MySQL event and realized the action was not running. I then realized that the user I was using did not have super privileges
While reading this you might first implement better checking and handling for exceptions since this should release some error in your system for sure. This is a complex task requiring extensive handling of errors to see whether this goes right otherwise you will likely find yourself searching for a bug in this part for hours.

Granting the create view privilege in mysql using cpanel/phpMyAdmin

I am using cpanel and I wanted to grant a user create view privileges. When I created the user and database in cpanel it did not have an option for that.
So I tried to do it in phpMyAdmin using :
GRANT CREATE VIEW ON (dbname).* TO '(db username)'#'localhost';
and I get the error:
#1044 - Access denied for user '(host username)'#'localhost' to database '(dbname)'
What version of cpanel are you using? I mine, I go to MySQL databases. All your tables are listed with users who have privileges on those tables and the users are links. When you click on one of the links, it takes you to a list of privileges that are checkboxes. Couldn't be simpler, all though I find it difficult to trust something that makes my privilege handling so simple!
Is it possible that you don't have the grants yourself to give grants?
I also had this same problem and after many searches when i look through the cpanel thoroughly i discovered something. To work with database we need to grant our user full privileges. Cpanel named this job differently. They call it "Add User To Database". Add your user with the database you desire. Then it'll work. Probably we miss it or we don't know what to do with it. So that we face this problem.
As best I can tell this isn't possible with some configurations.