Give user permission to drop and create specific databases - mysql

This question refers to MySQL and/or MariaDB specifically.
Is it possible to permit certain users to drop/create one (or more) specific databases, but not all databases? By this, I do not mean how to give users all privileges for certain specific databases with a query like:
GRANT ALL PRIVILEGES ON `example-db`.* TO 'example-user'#'localhost';
I've found some methods on how to do this for MSSQL here and a similar post for PostgreSQL.

grant privileges separately for a user account in MySQL.
When specifying the database name and table name, separate them with a. (period) and no spaces. This will give the root user fine-grain control over certain data.
Also, replace the PERMISSION_TYPE value with the kind of access you want to grant to your new user account.
Here are the most used commands in MySQL:
CREATE — enable users to create a database or table
SELECT — permit users to retrieve data
INSERT — let users add new entries in tables
UPDATE — allow users to modify existing entries in tables
DELETE — enable users to erase table entries
DROP — let users delete entire database tables
NOTE: Using the ALL PRIVILEGES permission type from before will allow
all of the permissions listed above.
To use any of these options, simply replace PERMISSION_TYPE with the appropriate keyword. To apply multiple privileges, separate them with a comma. For example, we can assign CREATE and SELECT to our non-root MySQL user account with this command:
GRANT CREATE, SELECT ON * . * TO 'user_name'#'localhost';
Sometimes, you might come across a situation where you need to revoke given privileges from a user. You can do so by entering:
REVOKE PERMISSION_TYPE ON database_name.table_name FROM ‘user_name’#‘localhost’;
For example, to withdraw all privileges for our non-root user we should use:
REVOKE ALL PRIVILEGES ON * . * FROM 'user_name'#'localhost';
Finally, you can entirely delete an existing user account by using the following command:
DROP USER ‘user_name’#‘localhost’;
Don't forget to FLUSH PRIVILEGES;
In order to find what privileges have already been granted to a MySQL user, you can use the SHOW GRANTS command:
SHOW GRANTS FOR 'user_name'#'localhost';

Let’s start by making a new user within the MySQL shell:
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
To provide a specific user with permission, you can use this framework:
GRANT type_of_permission ON database_name.table_name TO 'username'#'localhost'
Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.
FLUSH PRIVILEGES;

Related

MySQL User Privileges - Restrict Users to some tables

I have created a database with several tables, such as user, booking etc.
I have allocated privileges using the User tab within phpmyAdmin, wherein, Users are permitted View and Add privileges, Administrators have View, Add and Amend privileges and Managers have ALL privileges including Delete.
Using the MySQL database, is it possible to do some restrictions as below:
a) Restrict the User to View and Add to only certain tables.
b) Allow the User to view records solely for themselves and not those of any another user.
If so How?
You can give privileges to some users to some tables using mysql GRANT as below
GRANT SELECT, INSERT, UPDATE ON db.booking TO 'someuser'#'somehost';
For all privileges use ALL instead of SELECT, UPDATE etc.
GRANT ALL ON db.booking TO 'someuser'#'somehost';
For documentation please go through mysql GRANT

Allow MySQL user with limited database access to create more users with similar access

I know that I can create a user with the create user privileges like so:
create user primary_user identified by 'pass';
grant all on *.* to primary_user with grant option;
This will in turn allow me to create users whilst logged in with primary_user.
Now let's assume I have a subset of databases, all with prefix abc_, and that I want my primary_user to only be able to access these databases.
Then the grant query above would look like so:
grant all on `abc_%`.* to `primary_user` with grant option;
The problem now, however, is that when I log in with my primary_user and try to create a secondary_user, I get the following error:
ERROR 1227 (42000): Access denied; you need (at least one of) the
CREATE USER privilege(s) for this operation
TL;DR
Basically what I want is primary_user to only be able to access databases with the abc_ prefix, and to also be able to create secondary users that in turn also only have access to databases with an abc_ prefix.
Is this possible? Or is my only option to create the secondary users with a user account that has create user privileges on *.*?
As mentioned in MySQL Documentation:
To use CREATE USER, you must have the global CREATE USER privilege or
the INSERT privilege for the mysql database.
Thus, users with privileges in specific databases cannot create users. Ofcourse, since you have with grant option, your primary_user can grant all database level privileges to other already created users (for the abc_ databases).
Note #1: Here you can find an interesting table with the various privileges and the different levels that they can be granted.
Note #2: Be extra cautious when giving GRANT OPTIONS to non-admin users because they can modify the privileges of other users which can lead to chaos. The Open Web Application Security Project states:
[Grant privilege]... should be appropriately restricted to the DBA and Data (Table)
owners. Give specific permissions on an as needed basis and use
different logins for different purposes.

How do I view user accounts when logged into MySQL?

I want to see the names of all the users for MySQL. When I try to google this question most results are all concerned with users in a database as opposed to MySQL users. I checked out the MySQL documentation but it is very verbose and does not really help much. How do I view the account names of MySQL users, change permissions, and create new users?
Normally very simple... Log in as root, and do:
select * from mysql.user;
And if you googled on mysql show users you would have gotten this as first hit.
Furthermore, rights to specific databases are held in mysql.db and host limitations are in mysql.host
Display only User and Host
SELECT User, Host FROM mysql.user
Display users and privileges (pretty way)
SELECT * FROM mysql.user\G;
Create user
CREATE USER 'jeffrey'#'localhost' IDENTIFIED BY 'mypass';
Grant Privileges (Set)
-- Grant all privileges - Specific Database, all tables
GRANT ALL ON db1.* TO 'jeffrey'#'localhost';
-- Grant only SELECT privilege - Specific Database, specific table
GRANT SELECT ON db2.invoice TO 'jeffrey'#'localhost';
-- Grant USAGE privilege All databases, all tables
GRANT USAGE ON *.* TO 'jeffrey'#'localhost' WITH MAX_QUERIES_PER_HOUR 90;
List Of Privileges:
CREATE
DROP
GRANT OPTION
LOCK TABLES
REFERENCES
EVENT
ALTER
DELETE
INDEX
INSERT
SELECT
UPDATE
CREATE TEMPORARY TABLES
TRIGGER
CREATE VIEW
SHOW VIEW
ALTER ROUTINE
CREATE ROUTINE
EXECUTE
FILE
CREATE USER
PROCESS
RELOAD
REPLICATION CLIENT
REPLICATION SLAVE
SHOW DATABASES
SHUTDOWN
SUPER
ALL [PRIVILEGES]
USAGE
Performing the following query will provide all your MySQL users:
SELECT user FROM mysql.user;
You may need to login as admin to perform the above query. If that is the case login as admin from terminal by using the following command:
sudo mysql -p
Additionally, you can also create new users as follows:
create user '<user name>'#'<host name>';
Example
create user 'tony'#'localhost';
Depending on the scope of your project you will need to provide this new user with various permissions. To do that use the following syntax:
grant <permission type> on <database name> to '<user name>'#'<host name>';
Example
grant all on uncovery.* to 'tony'#'localhost';
If you are looking to do this with a GUI just to get yourself started with MySQL download MySQL workbench.
https://www.mysql.com/products/workbench/
Once this is completed launch the application and add your new connection by clicking the little + sign and filling out the parameters.
Defaults:
Hostname: 127.0.0.1 (if hosted on the machine you are running Workbench)
Username: root
Password: <blank> (if you set the root password enter that here)
Click test connection and make changes until the connection is successful and connect.
You can then click the server drop-down on the top and select Users and Privileges. This will give you a nice easy GUI to play around with things and get familiar with the available options.
Some additional documentation can be found here:
https://dev.mysql.com/doc/workbench/en/wb-mysql-connections-navigator-management-users-and-privileges.html

New user with just USAGE previleges was able to drop a table (MySQL)

I logged into MySQL from root user.
Ran the command:-
create user new;
Then I logged in as user new. Ran the following commands:-
use test;
show grants;
The Output was:-
GRANT USAGE ON *.* TO ''#'LOCALHOST'
As I understand, the new user does not have the drop table permissions or any permissions for that matter (except USAGE) on this table.
However when I run the following:-
DROP TABLE test_table;
The query is successful. Could you please explain why is this happening? How do I disable this user from dropping tables and revoke other permissions from this user.
Thanks in advance.
According to the manual, there is more you need to consider:
SHOW GRANTS displays only the privileges granted explicitly to the
named account. Other privileges might be available to the account, but
they are not displayed. For example, if an anonymous account exists,
the named account might be able to use its privileges, but SHOW GRANTS
will not display them.
http://dev.mysql.com/doc/refman/5.1/en/create-user.html mentions CREATE USER creates a new row in the mysql.user table and assigns the account no privileges.
What do you get when you run SHOW GRANTS FOR CURRENT_USER with user new?

MySql grant user permission

I want to create a new user in MySql. I do not want that new user to do much with my existing databases [I just want to grant Select privilege to him], but he can do anything and everything with a new database which he creates.
Firstly, is there a way to grant permission as per the database owner? If it is possible, then that is the ideal thing I am looking for. And if not, then how do I restrict a particular user from accessing [only Select privilege] some specific database only, allowing him to do anything he wants with the remaining ones?
From the MySQL grant documentation:
CREATE USER 'jeffrey'#'localhost' IDENTIFIED BY 'mypass';
GRANT SELECT ON *.* TO 'jeffrey'#'localhost';
GRANT ALL ON db1.* TO 'jeffrey'#'localhost';
The first command creates the user. The second grants select on all databases and tables. The third command grants all access to all tables in db1.
Is there anything else specific you are looking to do?
To provide a specific user with a permission, you can use this framework:
GRANT [type of permission] ON [database name].[table name] TO ‘[username]’#'localhost’;
GRANT ALL PRIVILEGES ON * . * TO 'newuser'#'localhost';
The asterisks in this command refer to the database and table (respectively) that they can access—this specific command allows to the user to read, edit, execute and perform all tasks across all the databases and tables.
Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.
FLUSH PRIVILEGES;
For more about permission you can read this article
https://www.digitalocean.com/community/articles/how-to-create-a-new-user-and-grant-permissions-in-mysql
For the list of permissions, see the MySQL Manual page Privileges Provided by MySQL.
Open mysql command prompt.
To create a new user when host is localhost then use this command
CREATE user 'test_user'#'localhost' identified by 'some_password';
for any host use %, like this
CREATE user 'test_user'#'%' identified by 'some_password';
Once the user is created, you need to Grant some access. Use following command for this.
GRANT SELECT,INSERT,UPDATE
ON database_name.table_name
TO 'test_user'#'localhost';
After successful execution of above query, test_user can select, insert and update in table_name (name of table) of database_name (name of database).
grant privilege is given in data base like this
grant privilege on object to user
object is any data base table or relation and user might be the whom the privilege is provided to him.
Example
grant select,insert,update,on object name to user name
grant select on employee to john with grant option;
revoke delete on employee from john.