mysql: How to set read-only

There are two main ways to set read-only mode in MySQL: use global variables to set read-only permissions or lock the user. Both methods have their own advantages and can be chosen according to actual needs.

1. Use global variables to set read-only mode

We can set the entire database to read-only mode by setting the MySQL global variableread_only. This method is suitable for maintenance periods to ensure that there will be no new write operations.

Walk

  1. Log in to the MySQL server.
  2. Execute the following command to set read-only mode:
    SET GLOBAL read_only = ON;
    

To return to normal mode, use:

SET GLOBAL read_only = OFF;

Note: This method affects all users, including users with super privileges. If we want users with super privileges to still be able to write, we can use the super_read_only variable.

2. Set user permissions to read-only

Another way is to modify the user permissions to only grant read permissions. This approach is more granular and can be implemented for specific users or applications.

Walk

  1. Log in to the MySQL server.
  2. Find the user account that needs to be modified, and then update its permissions. For example, to set read-only permissions for user username:
    REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'host';
    GRANT SELECT ON *.* TO 'username'@'host';
    

In this method, username is the name of the user, and host is the host to which the user is connected. In this way, permissions can be controlled for specific users.

in conclusion

Which method to choose depends on our needs. If you need to temporarily make the entire database read-only, using global variables is a good approach. If you want to implement read-only permissions for a specific user, then modifying the user permissions would be a better option.

Guess you like

Origin blog.csdn.net/qq_14829643/article/details/134889242