How to Create a Read-Only MySQL User for Your WordPress Backups
Most WordPress sites back up their database with the same MySQL user the site itself uses. That user can read, write, and change anything, so a backup script that gets hijacked or runs a bad command can wipe the whole database. A read-only MySQL user for WordPress backups fixes that problem at the source. It can read every table it needs for a dump and nothing else. This guide shows you how to create one with GRANT SELECT only, test it with mysqldump, wire it into your backup script, and remove it when you no longer need it.
Why a Backup Job Should Never Use Your WordPress Admin User
Your WordPress database user, the one listed in wp-config.php, has full rights over its database. It can insert rows, update posts, drop tables, and delete the database itself. WordPress needs those rights to run. A backup job does not. It only needs to read.
That gap matters because backup jobs run on a schedule, often at night, and often from a script that stores credentials in plain text. If someone gets hold of those credentials, or if a script bug turns a read into a write, the damage is limited only by the rights attached to the user. A read-only user turns a worst-case scenario into a failed command. The attacker or the bug can read data, which is already bad, but cannot drop a table or delete a row.
There is a second reason: separation makes mistakes easier to spot. When the backup user and the site user are different accounts, a failed backup tells you exactly which part broke. You can check the backup user’s rights without touching the credentials your live site depends on. For more on locking down the file that holds those credentials, see our guide to securing wp-config.php and WordPress file permissions.
What GRANT SELECT Actually Lets the User Do
SELECT is the only privilege this user needs. It lets MySQL read rows from tables, which is exactly what mysqldump does. Everything else stays locked out.
| Privilege | Read-only backup user | What it would allow |
|---|---|---|
| SELECT | Yes | Read tables and dump them |
| INSERT | No | Add rows to tables |
| UPDATE | No | Change existing rows |
| DELETE | No | Remove rows |
| DROP | No | Delete tables or the database |
| LOCK TABLES | Optional | Lock tables during a consistent dump |
One privilege deserves a note. LOCK TABLES helps mysqldump take a consistent snapshot on MyISAM tables, but modern WordPress installs run InnoDB, where --single-transaction gives you a consistent dump without locking. Skip LOCK TABLES unless you know your tables need it. Keep the grant list short: fewer rights mean a smaller blast radius. A mysql grant select only setup is the simplest version of this idea: one right, one database, nothing extra.
If you use MariaDB, the same statements work. The mysql.user table and GRANT syntax are the same for this task, so follow the steps below on either server.
Create a Read-Only MySQL User for WordPress Backups
Log in to MySQL as root or as a user with grant rights, then run the statements below. Replace wp_backup with any username you like, strong_password_here with a long random password, and wordpress_db with your actual database name. You can find the database name in wp-config.php next to DB_NAME.
CREATE USER 'wp_backup'@'localhost' IDENTIFIED BY 'strong_password_here';
GRANT SELECT ON wordpress_db.* TO 'wp_backup'@'localhost';
FLUSH PRIVILEGES;
Three things to get right here. First, the host part: 'localhost' means the user can only connect from the same server. If your backup script runs on another machine, use that machine’s IP or hostname instead, and tighten your firewall so only that address can reach port 3306. Second, the scope: wordpress_db.* limits the user to one database. Never grant on *.* for a backup account. Third, the password: generate it with a password manager or openssl rand -base64 24, and store it in a file only root can read, not inside a world-readable script.
Check the result before you move on:
SHOW GRANTS FOR 'wp_backup'@'localhost';
You should see exactly one grant line for SELECT on your database, plus the USAGE line MySQL adds by default. If you see ALL PRIVILEGES or grants on *.*, stop and fix the grant. A backup user with full rights is the mistake this whole guide exists to prevent.
Test the Dump with mysqldump
Never trust a backup user until you have watched it produce a real dump. Run this from the server shell as the mysqldump read-only user, and point the output at a test file:
mysqldump -u wp_backup -p --single-transaction wordpress_db > /tmp/test-backup.sql
ls -lh /tmp/test-backup.sql
head -20 /tmp/test-backup.sql
The dump should complete without errors and the file should hold real table data, not just headers. Open the head of the file and look for CREATE TABLE and INSERT lines from your own tables, such as wp_posts and wp_options. If mysqldump complains about access denied, check the password, the host part, and that the database name matches DB_NAME exactly.
Now confirm the user really is read-only. Try a write as that user and watch it fail:
mysql -u wp_backup -p -e "DELETE FROM wordpress_db.wp_options LIMIT 1;"
MySQL should refuse with an access denied error. That failure is the whole point: the user can read everything and change nothing. If the delete goes through, your grant is wrong and you need to revisit the previous section.
Once the dump checks out, you can build it into a regular routine. Our guide to backing up MySQL databases automatically with mysqldump and cron walks through the scheduling side, and the daily WordPress database backup script shows a complete script you can adapt. In both cases, swap in the read-only user and keep the site credentials out of the backup job entirely.
Wire the New User into Your Backup Script
Most backup scripts read credentials from a small config file or from variables at the top of the script. Change only the user and password lines to the new read-only account. Leave the database name, host, and dump options alone. This wordpress backup database user needs no other rights, so the script stays the same apart from those two lines.
Store the credentials in a separate file with tight permissions rather than hard-coding them in the script:
# /root/.my-backup.cnf
[client]
user = wp_backup
password = strong_password_here
host = localhost
chmod 600 /root/.my-backup.cnf
mysqldump --defaults-extra-file=/root/.my-backup.cnf --single-transaction wordpress_db > /backups/wp-$(date +%F).sql
With chmod 600, only root can read the file. The script itself can then live in a shared location without leaking credentials, and the --defaults-extra-file flag keeps the password out of the process list, where -p on the command line would show it to anyone running ps.
Run the script by hand once after the change and confirm the dump file is fresh and non-empty. Then let cron take over. A backup that only works when you watch it is not a backup, and a dump is only as good as its last restore test, so check the file after the first scheduled run too. Our walkthrough on testing backup restores without breaking production shows how to verify a dump restores cleanly on a spare database before you ever need it for real.
Revoke or Remove the User When You Are Done
Temporary users have a habit of becoming permanent. If you created this account for a one-off migration or a short backup test, remove it when the job ends. To revoke mysql user privileges you have two options, depending on whether you might need the account again:
-- Take away the read right but keep the account
REVOKE SELECT ON wordpress_db.* FROM 'wp_backup'@'localhost';
FLUSH PRIVILEGES;
-- Or delete the account entirely
DROP USER 'wp_backup'@'localhost';
FLUSH PRIVILEGES;
DROP USER is the cleaner choice for a one-off account: it removes the user and all its grants in one step. Use REVOKE when the account will come back, for example a monthly backup task where you re-grant rights before each run and revoke them after.
After either command, confirm the user is gone or powerless:
SELECT user, host FROM mysql.user WHERE user = 'wp_backup';
SHOW GRANTS FOR 'wp_backup'@'localhost';
The first query should return no rows after a drop. The second should error if the user no longer exists, or show only USAGE after a revoke. Either result means the account can no longer read your database.
A read-only backup user is a small change with an outsized payoff. It takes five minutes to set up, it costs nothing, and it draws a hard line between reading your data and destroying it. Set it up once, test the dump, and your backups stop being a second way into your database.