Users, Permissions and Backups
💻 MySQLCreate least-privilege users and take backups you can actually restore.
A database is only production-ready when access is controlled and a restore has been tested.
- Create a user with limited grants.
- Take and restore a dump.
- Protect credentials.
Create a User
Give the application only the permissions it needs on one database. Grant nothing globally.
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT SELECT, INSERT, UPDATE, DELETE
ON school.*
TO 'app_user'@'localhost';
FLUSH PRIVILEGES;Backups
mysqldump writes a SQL file that recreates the schema and data. Test a restore on a separate database regularly.
mysqldump -u root -p school > school-backup.sql
mysql -u root -p school_restore < school-backup.sqlCredentials
Keep database passwords in environment variables, never in source code or a public repository. Rotate them if they leak.
Practice
- Create a read-only user for reporting.
- Dump a database and restore it under a new name.
- List the grants for a user.
Hands-on exercise
Write SQL that:
1. Creates a user reporter with a password.
2. Grants only SELECT on every table in school.
3. Applies the privileges.
Recap
Least-privilege users limit damage, tested backups make recovery possible, and credentials belong outside source code.
Example
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT SELECT, INSERT, UPDATE, DELETE
ON school.*
TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
Exercises
1. Creates a user reporter with a password.
2. Grants only SELECT on every table in school.
3. Applies the privileges.
Expected Output
A user that can read the school database but cannot change it.
Lesson Nav
MySQL Course
0/9 lessons completed
- 1 Connecting to MySQL Tutorial
- 2 Creating Databases and Tables Tutorial
- 3 Data Types and Constraints Tutorial
- 4 Reading and Writing Data Tutorial
- 5 Joins and Relationships in MySQL Tutorial
- 6 Functions, Dates and Text Tutorial
- 7 Views and Stored Procedures Tutorial
- 8 Users, Permissions and Backups Tutorial
- 9 Project: A Small School Database Project