MySQL Users, Privileges and Backups
So far every example ran as an all-powerful administrator. Real servers have many accounts with carefully limited privileges, and their data is protected by regular backups. This lesson explains both. The commands here change server-wide state, so they are shown as ordinary code blocks rather than runnable examples.
Accounts
A MySQL account is a user name plus the host it connects from: 'app'@'10.0.0.%' and 'app'@'localhost' are two different accounts with their own passwords and privileges.
-- an account for the web application, allowed from the app servers' network
CREATE USER 'shop_app'@'10.0.0.%' IDENTIFIED BY 'a-long-random-password';
-- an account for an analyst, from anywhere ('%'), who must change the password
CREATE USER 'analyst'@'%' IDENTIFIED BY 'Temp#2025' PASSWORD EXPIRE;
ALTER USER 'analyst'@'%' IDENTIFIED BY 'new-password';
RENAME USER 'analyst'@'%' TO 'data_analyst'@'%';
DROP USER IF EXISTS 'old_report'@'localhost';Accounts are stored in the mysql system database. You can see them, without passwords, with:
SELECT user, host FROM mysql.user ORDER BY user;
SELECT CURRENT_USER(); -- the account you are logged in asPrivileges: GRANT and REVOKE
New accounts can't do anything. GRANT gives privileges at a chosen level:
| Level | Syntax | Example |
|---|---|---|
| global | ON *.* |
server administration |
| database | ON shop.* |
everything in shop |
| table | ON shop.orders |
one table |
| column | SELECT (col, ...) ON shop.t |
some columns of a table |
| routine | EXECUTE ON PROCEDURE shop.restock |
one stored procedure |
-- the application may read and write data in shop, but not change its structure
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'shop_app'@'10.0.0.%';
-- the analyst may read everything except employee salaries
GRANT SELECT ON shop.orders TO 'data_analyst'@'%';
GRANT SELECT ON shop.order_details TO 'data_analyst'@'%';
GRANT SELECT (employee_id, first_name, last_name, title) ON shop.employees TO 'data_analyst'@'%';
SHOW GRANTS FOR 'shop_app'@'10.0.0.%';
REVOKE DELETE ON shop.* FROM 'shop_app'@'10.0.0.%';Common privileges:
| Privilege | Allows |
|---|---|
SELECT, INSERT, UPDATE, DELETE |
reading and changing data |
CREATE, ALTER, DROP, INDEX |
changing structure |
EXECUTE |
calling stored procedures and functions |
CREATE VIEW, SHOW VIEW, TRIGGER |
working with views and triggers |
ALL PRIVILEGES |
everything at that level (except GRANT OPTION) |
GRANT OPTION |
passing your own privileges on to others |
Note: Old tutorials create users with
GRANT ... IDENTIFIED BY 'pw'. MySQL 8 removed that form: create the account withCREATE USERfirst, thenGRANT.FLUSH PRIVILEGESis only needed if someone edits themysql.*tables directly, never afterGRANT/REVOKE.
Roles
MySQL 8 (and MariaDB 10.0.5+) supports roles: named bundles of privileges that you grant to many users.
CREATE ROLE 'reporting';
GRANT SELECT ON shop.* TO 'reporting';
GRANT 'reporting' TO 'data_analyst'@'%', 'finance'@'%';
SET DEFAULT ROLE 'reporting' TO 'data_analyst'@'%'; -- MySQL: activate at loginViews as a security layer
Instead of column privileges, you can give a user access to a view that shows only what they may see:
CREATE VIEW shop.staff_directory AS
SELECT first_name, last_name, title FROM shop.employees;
GRANT SELECT ON shop.staff_directory TO 'intranet'@'%';Good practice
- Follow the principle of least privilege: an application that only reads and writes rows doesn't need
DROPorALTER. - Use a separate account per application and per person, so you can revoke one without affecting the others.
- Never let applications connect as
root. Restrict hosts ('10.0.0.%'rather than'%'). - Use parameterised queries in your application code, which closes the door on SQL injection.
Backups
A database without tested backups is a database waiting to be lost: disks fail, someone runs DELETE without WHERE, ransomware strikes. MySQL offers two families of backups.
| Logical backup | Physical backup | |
|---|---|---|
| What | SQL statements (CREATE TABLE, INSERT) |
copies of the data files |
| Tools | mysqldump, mysqlpump, MySQL Shell's util.dumpInstance() |
MySQL Enterprise Backup, Percona XtraBackup, mariadb-backup, file-system snapshots |
| Pros | readable, portable across versions and servers | fast for large databases |
| Cons | slow to restore for very large databases | tied to the same server version and platform |
mysqldump
mysqldump writes a database as an SQL script:
# one database, consistent snapshot without locking InnoDB tables
mysqldump -u backup_user -p --single-transaction --routines --triggers shop > shop_2025-06-30.sql
# compress on the fly
mysqldump -u backup_user -p --single-transaction shop | gzip > shop_2025-06-30.sql.gz
# only the structure, or only some tables
mysqldump -u backup_user -p --no-data shop > shop_schema.sql
mysqldump -u backup_user -p shop customers orders > shop_customers_orders.sql
# every database on the server
mysqldump -u backup_user -p --single-transaction --all-databases > full_backup.sql| Option | Why |
|---|---|
--single-transaction |
consistent snapshot of InnoDB tables without blocking writers |
--routines, --triggers, --events |
include stored programs (triggers are included by default) |
--no-data / --no-create-info |
structure only / data only |
--where="order_date >= '2025-01-01'" |
dump only some rows |
The dump file is plain SQL. Its beginning looks like this:
-- MySQL dump 10.13 Distrib 8.4.0, for Linux (x86_64)
/*!40101 SET NAMES utf8mb4 */;
DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
`category_id` int NOT NULL AUTO_INCREMENT,
...
INSERT INTO `categories` VALUES (1,'Beverages','Coffee, tea, juices and soft drinks'),...Restoring
Feed the script back to the mysql client:
mysql -u root -p -e "CREATE DATABASE shop_restored"
mysql -u root -p shop_restored < shop_2025-06-30.sql
gunzip < shop_2025-06-30.sql.gz | mysql -u root -p shop_restoredOr, from inside a mysql session: SOURCE /path/to/shop_2025-06-30.sql;.
Point-in-time recovery
A nightly dump loses everything since the last night. The binary log records every change after the dump; replaying it with mysqlbinlog up to just before the accident ("restore to 14:31, one minute before someone dropped the table") is called point-in-time recovery.
A backup checklist
- Automate backups (cron, systemd timers or your cloud provider's scheduled backups).
- Store copies off the server, ideally in another location, and encrypt them.
- Keep several generations (daily for a week, weekly for a month, …).
- Test restores regularly. A backup that has never been restored is only a hope.
- Use a dedicated backup account with just the privileges it needs (
SELECT,SHOW VIEW,TRIGGER,LOCK TABLES,PROCESS,EVENT).
Test yourself
Which two parts identify a MySQL account?
- User name and password
- User name and host
- Database and user name
- Host and port
What does GRANT SELECT ON shop.* TO 'bob'@'%'; allow?
- Bob can read every table in shop
- Bob can read all databases
- Bob can change data in shop
- Bob can create users
Why use mysqldump --single-transaction for InnoDB tables?
- It makes the dump smaller
- It takes a consistent snapshot without blocking other writes
- It includes stored procedures
- It encrypts the dump