How to create a Read Only user in MySQL
To create a read-only user in MySQL or MariaDB, you can use the following steps:
Connect to your MySQL server using a client tool such as MySQL Workbench or the MySQL command-line client.
Run the following SQL statement to create a new user with a unique username and password:
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
- To grant read-only access to all databases, run the following SQL statement:
GRANT SELECT ON *.* TO 'username'@'%';
- To grant read-only access to a specific database, run the following SQL statement, replacing
database_name
with the name of the database:
GRANT SELECT ON database_name.* TO 'username'@'%';
- To grant read-only access to a specific table within a database, run the following SQL statement, replacing
database_name
andtable_name
with the names of the database and table, respectively:
GRANT SELECT ON database_name.table_name TO 'username'@'%';
- Finally, run the
FLUSH PRIVILEGES
statement to apply the changes:
FLUSH PRIVILEGES;
This will create a read-only user that can connect to the MySQL server and perform SELECT queries on the specified databases and tables.