Let MySQL users create databases, but allow access to only their own databases

后端 未结 4 1483
一向
一向 2020-11-29 23:09

I want to have multiple a MySQL users to be able to issue commands like

CREATE DATABASE dbTest;

But I also want each of t

相关标签:
4条回答
  • 2020-11-29 23:19

    Create a stored procedure that is defined by the admin user and invokes with the admin user privileges by using SQL SECURITY DEFINER. In the stored procedure,

    • Create the database.
    • Set the privileges on the database so only the current user has access.
    • Execute FLUSH PRIVILEGES to reload the privileges from the grant tables.

    Use USER() to get the current user login details.

    Find out more about SQL SECURITY DEFINER.

    0 讨论(0)
  • 2020-11-29 23:20

    You can use

    GRANT ALL PRIVILEGES ON `testuser_%` . * TO 'testuser'@'%';
    

    to grant the user testuser privileges on all databases with names beginning with testuser_.

    EDIT: I'm not sure if this user is now also allowed to create databases.

    Yes, this allows the testuser to create databases limited to names starting with testuser_

    0 讨论(0)
  • 2020-11-29 23:26

    You can use

    GRANT ALL PRIVILEGES ON `testuser\_%` .  * TO 'testuser'@'%';
    

    to grant the user testuser privileges on all databases with names beginning with testuser_.

    This allows the testuser to create databases limited to names starting with testuser_

    0 讨论(0)
  • 2020-11-29 23:43

    It is impossible to do this using permissions only .

    The workaround as suggested in another answer: GRANT ALL PRIVILEGES ONtestuser_%. * TO 'testuser'@'%'; has the problem that the users must then be very careful in naming their databases.

    For example if user aaa creates database bbb_xyz, it can then be accessed exclusively by user bbb but not by user aaa.

    0 讨论(0)
提交回复
热议问题