Step 1: Log in to the MySQL/MariaDB Console
Open your terminal and log in as the root user, then run:
Code: Select all
mysql
Create the new user example_new_user with the password examplePassword. You can replace examplePassword with a stronger, randomly generated password if desired:
Code: Select all
CREATE USER 'example_new_user'@'localhost' IDENTIFIED BY 'examplePassword';
Grant the user SELECT privileges on the entire example_db database:
Code: Select all
GRANT SELECT ON example_db.* TO 'example_new_user'@'localhost';
To ensure all privilege changes take effect, flush the privileges:
Code: Select all
FLUSH PRIVILEGES;
Exit the MySQL/MariaDB console:
Code: Select all
EXIT;
To test that the new user has the correct read-only access, try connecting to the database using the new credentials:
Code: Select all
mysql -u example_new_user -p -D example_db
Conclusion
By following these steps, you have successfully created a new read-only database user for the example_db database. This setup is ideal for allowing web applications or external services to access your database securely without the risk of data modification.