WordPress Security

Least Privilege for the WordPress Database User

Least Privilege for the WordPress Database User

When I audit WordPress servers, one of the first things I check is the database connection in wp-config.php. Which user does WordPress connect as, and what can that user do? Surprisingly often, the answer is “everything”. I’ve found sites connecting as root, sites sharing a single database user across a dozen client installations and users with permission to read every database on the server and connect from anywhere on the internet. The permissions given to the WordPress database user rarely get a second thought.

None of that matters while everything is going well. It matters enormously after something goes wrong. If an attacker exploits a vulnerability that gives them database access, or reads wp-config.php through a file disclosure bug, the permissions of the WordPress database user decide how far the damage spreads. A properly limited user contains the incident to one site. An all-powerful one hands over the whole server.

This article explains what least privilege means for the WordPress database user, how to set it up on a self-managed server and the practical compromises involved.

The principle

Least privilege means giving each account exactly the permissions it needs to do its job, and nothing more. For WordPress, that translates into a few rules:

  • Each site gets its own database.
  • Each site gets its own database user.
  • That user can only access its own database.
  • That user can only connect from where WordPress actually runs, usually localhost.
  • That user only has the privileges WordPress needs.

Why sharing a user is dangerous

On servers hosting several sites, it’s tempting to create one database user and grant it access to all the databases. It makes setup quicker. But consider what happens when one of those sites is compromised. The attacker reads that site’s wp-config.php, finds the credentials and now has full access to every other site’s database. They can add administrator accounts to all of them, inject malware into post content or extract customer data from every store on the server.

One user per site turns a single compromised site into a single problem rather than a server-wide disaster.

Never use root

The MySQL root account can create and drop databases, manage users, read every table and, depending on configuration, read and write files on the server through functions like LOAD DATA INFILE and SELECT INTO OUTFILE. WordPress needs none of that. If WordPress connects as root and is compromised, the attacker gains control of the entire database server, and possibly a route to the operating system.

If you find a site using root, fix it immediately.

Creating a proper user

On MySQL 8 or MariaDB, create a database and a user for a site like this:

Notice the scope: wp_example.*, meaning only this one database, and ‘localhost’, meaning connections are only accepted from the same machine.

Use a long, random password generated by a password manager. Nobody needs to type it; it lives in wp-config.php.

Which privileges WordPress actually needs

Day to day, WordPress needs SELECT, INSERT, UPDATE and DELETE. Those cover reading and writing content, options, users and everything else during normal operation.

Updates are where it gets more complicated. WordPress core updates sometimes change the database schema, using ALTER and occasionally CREATE. Plugins create their own tables on activation and modify them during updates, which needs CREATE, ALTER, INDEX and sometimes DROP. WooCommerce, form plugins, security plugins and many others maintain custom tables.

If you remove those privileges, updates fail, sometimes partially, which can leave a plugin in a broken state. So the grant list above includes the structural privileges WordPress and typical plugins expect.

What WordPress does not need:

  • GRANT OPTION, the ability to give privileges to others
  • FILE, the ability to read and write server files
  • SUPER, PROCESS, SHUTDOWN, RELOAD and other administrative privileges
  • Access to any other database
  • CREATE USER

Those are the privileges that turn a site compromise into a server compromise, and they’re the ones to remove without hesitation.

Stricter setups: separate users for runtime and updates

Some security-focused setups go further, using two database users:

  • A runtime user with only SELECT, INSERT, UPDATE and DELETE, used by the live site
  • A maintenance user with structural privileges, used only during updates

Switching between them requires changing credentials during maintenance, usually through WP-CLI with a different configuration or an environment variable. It’s a real improvement in containment, because an attacker who compromises the running site can’t drop tables or alter schemas. But it adds operational complexity, and if anyone updates a plugin through the dashboard with the runtime user active, the update may fail halfway.

For most sites, a single well-scoped user is a sensible balance. The split approach suits high-value sites with a disciplined deployment process where updates never happen through the dashboard.

Restrict where the user can connect from

The host part of the user, ‘localhost’ in the example, matters as much as the privileges. A user defined as ‘wp_example’@’%’ can connect from any IP address. If the database server is reachable from the internet, anyone who learns the password can connect directly, bypassing WordPress entirely.

If the database runs on the same server as WordPress, use localhost. If it runs on a separate server, specify the application server’s private IP address:

And make sure the database server isn’t listening on a public interface. In MySQL’s configuration, bind-address should be 127.0.0.1 for a local-only server or a private network address. A firewall rule blocking port 3306 from the internet adds another layer.

Check what’s listening:

<code>sudo ss -tlnpgrep 3306</code>

If you see 0.0.0.0:3306 and the database doesn’t need remote connections, change the bind address and restart MySQL.

Auditing existing users

To see what users exist and where they can connect from:

To see what a particular user can do:

Look for:

  • Users with host ‘%’ that don’t need it
  • Users with ON *.* grants, which apply to every database
  • Users with ALL PRIVILEGES where specific grants would do
  • Old users for sites that no longer exist
  • Anonymous users with an empty username, which older installations sometimes created

Remove what’s unnecessary and tighten what’s too broad.

Fixing an over-privileged user

If a site currently uses an over-privileged user, the safest way to fix it is to create a new, properly scoped user and switch wp-config.php to it, rather than modifying the existing user while the site uses it:

  1. Create the new user with the correct grants.
  2. Update DB_USER and DB_PASSWORD in wp-config.php.
  3. Load the site and the admin area to confirm it works.
  4. Test a plugin update on staging or carefully on production.
  5. Drop the old user, or revoke its excess privileges if something else depends on it.

This also gives you a fresh password, which is wise if the old one may have been shared around.

Managed hosting and control panels

On managed WordPress hosting and most control panels like cPanel or Plesk, databases and users are created through the interface, and the host usually applies sensible scoping automatically. You can still check that each site has its own database and user, and that you haven’t granted a single user access to several databases for convenience.

If your host provides phpMyAdmin or a similar tool, make sure access to it is protected, because it’s another route into every database the panel controls.

Database credentials and backups

Least privilege also applies to the tools around WordPress. Backup scripts that dump the database need SELECT, LOCK TABLES and sometimes SHOW VIEW and EVENT, but they don’t need write access. A separate read-only backup user means that even if backup credentials leak, nobody can modify your data with them.

Keep the WordPress database user as small as WordPress allows, give every other tool its own account and you’ll have contained most of the damage before an incident ever happens.

Document the setup

Record each site’s database name, user and host scope in your server documentation, along with where the password is stored. When a site is decommissioned, drop both its database and its user. Orphaned users with valid passwords and broad grants are exactly the kind of forgotten access that turns up during incident investigations.

Frequently asked questions

Will limiting privileges break plugin installation?

Not with the grant list above, which includes the structural privileges plugins need. Removing CREATE and ALTER entirely would break many plugin installs and updates.

Can I see which user my site uses without logging in to the server?

Not from the WordPress admin by default. Check DB_USER in wp-config.php, or ask your host.

Is the database password in wp-config.php safe?

It’s protected by file permissions and the web server never serves the file’s contents directly. But any vulnerability that reads files can expose it, which is exactly why least privilege matters.

Should each site on a multisite network have its own database user?

A multisite network shares one database, so it uses one user. Least privilege applies at the network level.