Grant MySQL permissions on current database?

24
February 11, 2019, at 06:40 AM

I can grant permissions with a static database name in the GRANT statement.

GRANT SELECT,INSERT ON database_name.table_name TO 'username'@'localhost';

Is there a way to dynamically set the database name in a GRANT statement so that the grant is for whichever happens to be the current database?

USE database_name;
GRANT SELECT,INSERT ON <what goes here?>.table_name TO 'username'@'localhost';
Answer 1

You don't need to specify anything. If no database is specified, MySQL will assume the default database, which is set when you USE database_name. From the manual:

The USE db_name statement tells MySQL to use the db_name database as the default (current) database for subsequent statements.

And from the section on GRANT:

If you specify tbl_name rather than db_name.tbl_name, the statement applies to tbl_name in the default database.

So you can just use

GRANT SELECT,INSERT ON table_name TO 'username'@'localhost';
READ ALSO
MySql Login error - Domain name added automatically with the user name

MySql Login error - Domain name added automatically with the user name

I am on Windows 10 desktop with a Cloudera VM running on itFrom inside the coudera vm Linux OS, I can login to MySQL with:

38
Using junction tables in PHP and MySQL to categorize and include and exclude categories

Using junction tables in PHP and MySQL to categorize and include and exclude categories

I'm trying to analyze tweets using manually assigned categoriesEverything's stored in a MySQL database

32
pgloader mysql to Postgres command line exclude table

pgloader mysql to Postgres command line exclude table

I have the following command that works

62