How To Grant Access Multiple Tables to User in MySQL

In this case, you have to grant select to user with multiple table.

You can easily add manually, but imagine if you have to add more than 10 tables, manually…

Here is the easy way to “grant select” multiple tables to user

  1. Create table list on some file, I put in /tmp/data_tables

    table_user
    table_contact
    product
    tabel_consumer
    
    List Tables Examples

    List Tables Examples

  2. Then run command

cat /tmp/data_tables | xargs -I {} mysql -u root --password=PasswordMySQL -Ne "GRANT SELECT ON YourDatabase.{} TO 'DatabaseUser'@'%';"
Grant Access to Multiple Tables Success

Grant Access to Multiple Tables Success

  1. To check if it working
show grants for 'DatabaseUser'@'%';
List Tables Examples

List Tables Examples

  1. To revoke access with multiple tables to user
cat /tmp/data_tables | xargs -I {} mysql -u root --password=PasswordMySQL -Ne "revoke select ON YourDatabase.{} from 'DatabaseUser'@'%';"

Source:

DBA Stackexchange - How to grant access for multiple tables to


Jika kamu merasa tulisan ini bermanfaat & membantu kamu, kamu bisa berdonasi lewat saweria

If you feel this website help you, you can donate at saweria