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:

  1. Connect to your MySQL server using a client tool such as MySQL Workbench or the MySQL command-line client.

  2. Run the following SQL statement to create a new user with a unique username and password:

CREATE USER 'username'@'%' IDENTIFIED BY 'password';
  1. To grant read-only access to all databases, run the following SQL statement:
GRANT SELECT ON *.* TO 'username'@'%';
  1. 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'@'%';
  1. To grant read-only access to a specific table within a database, run the following SQL statement, replacing database_name and table_name with the names of the database and table, respectively:
GRANT SELECT ON database_name.table_name TO 'username'@'%';
  1. 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.