0

I wanted to grant privileges on some selected tables only. So far I could only gather the necessary queries but unable to execute those.

SELECT 
   CONCAT('GRANT SELECT ON drupal.', TABLE_NAME, ' to ''test'';') As queryStrings 
FROM 
    INFORMATION_SCHEMA.TABLES 
WHERE 
    TABLE_SCHEMA = 'drupal' AND TABLE_NAME LIKE 'cache%';

OUTPUT:

+----------------------------------------------------------------+
| queryStrings                                                   |
+----------------------------------------------------------------+
| GRANT SELECT ON drupal.cache_page to 'test';                   |
| GRANT SELECT ON drupal.cachetags to 'test';                    |
| GRANT SELECT ON drupal.cache_config to 'test';                 |
| GRANT SELECT ON drupal.cache_data to 'test';                   |
| GRANT SELECT ON drupal.cache_entity to 'test';                 |
| GRANT SELECT ON drupal.cache_discovery to 'test';              |
| GRANT SELECT ON drupal.cache_default to 'test';                |
| GRANT SELECT ON drupal.cache_render to 'test';                 |
| GRANT SELECT ON drupal.cache_container to 'test';              |
| GRANT SELECT ON drupal.cache_toolbar to 'test';                |
| GRANT SELECT ON drupal.cache_bootstrap to 'test';              |
| GRANT SELECT ON drupal.cache_menu to 'test';                   |
| GRANT SELECT ON drupal.cache_dynamic_page_cache to 'test';     |
+----------------------------------------------------------------+

Is there something like EXECUTE queryStrings; ?

I tried to store it in a variable like

SET @query = CONCAT('GRANT SELECT ON drupal.', TABLE_NAME, ' to ''test'';') 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = 'drupal' AND TABLE_NAME LIKE 'cache%');

But I am getting error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'drupal' AND TABLE_NAME LIKE' at line 1

4
  • Hi, It's 10.3.11-MariaDB
    – SkyRar
    Commented Jan 5, 2019 at 10:29
  • Have a look at dynamic queries
    – McNets
    Commented Jan 5, 2019 at 10:29
  • I am getting error You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'drupal' AND TABLE_NAME LIKE' at line 1 . Sorry I am a beginner here.
    – SkyRar
    Commented Jan 5, 2019 at 10:33
  • Honestly, given the likelihood that you won't be executing these every day, I'd just copy and paste the generated queries and run them manually. Commented Jan 7, 2019 at 18:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.