WordPress Database Security: What the Table Prefix Really Does, a Least-Privilege MySQL User, Keeping Port 3306 Closed and Hunting Injected Rows

By Information Security Asia · Updated November 30, 2026 · 9 min read

WordPress User Account Audit: Find Hidden Administrators, Revoke Application Passwords, End Sessions and Right-Size Every Role

Short answer: the WordPress database holds every account, password hash, post and setting on your site, so whoever can write to it controls the site. Securing it comes down to five things: give WordPress its own MySQL or MariaDB user with privileges on its own database only (never the root account, and never server-wide rights such as FILE or SUPER), keep the database server listening on localhost or a private network rather than the public internet, protect the credentials in wp-config.php, keep database dumps out of the web root, and know how to query the tables an attacker typically tampers with. Changing the default wp_ table prefix is a minor extra: it can defeat some automated SQL injection payloads, but it does not fix a vulnerable plugin and is not worth breaking a working site to change.

What an attacker can do with database access

Most WordPress compromises start with a vulnerable plugin, a stolen password or a weak hosting account, but the damage usually lands in the database. With write access, an attacker can create a new administrator in the users and usermeta tables, change the site URL so visitors are redirected, inject spam links or skimming code into posts and widgets, store a backdoor in an autoloaded option, or reset another user’s password hash. With read access alone, they can copy customer names, email addresses, order details and password hashes for offline cracking.

WordPress Database Security: What the Table Prefix Really Does, a Least-Privilege MySQL User, Keeping Port 3306 Closed and Hunting Injected Rows

That is why database hardening is about limiting blast radius. If a plugin on one site is exploited, the attacker should reach only that site’s tables, should not be able to read files on the server through MySQL, and should not find the same credentials reused on another site or in a backup file anyone can download.

The table prefix: what it does and does not do

WordPress stores its tables with a prefix set by the $table_prefix line in wp-config.php, wp_ by default. The official hardening guide notes that changing it can block at least some SQL injection attacks, because many published attack payloads assume table names such as wp_users. That is the full extent of the benefit. An attacker who can run arbitrary SQL can list the table names in seconds, and a well-written exploit reads the prefix first.

For a new installation, pick a short random prefix during setup; it costs nothing. For an existing site, changing the prefix means renaming every table and updating the option and usermeta keys that contain the prefix, such as the wp_user_roles option and wp_capabilities user meta. Miss one and administrators lose their roles. If you do it, take a full backup first and test on a staging copy. On a site that is already patched and monitored, your time is better spent on the controls below.

Give WordPress a least-privilege database user

The single most valuable change is making sure WordPress connects as a dedicated user that can touch only its own database. Many one-click installers and older tutorials connect as root or as a user with global privileges, which turns any SQL injection into full control of the database server, every other site on it, and possibly files on disk.

The WordPress hardening guide explains that for normal operation the database user needs only data read and write privileges, and that structure and administration privileges such as DROP, ALTER and GRANT could in principle be revoked. It also warns against doing so, because core, plugin and theme updates change the schema and will fail without them. The practical balance is to grant the full set of privileges WordPress needs, but only on its own database, and nothing at the global level.

Privilege Needed by WordPress? Scope to grant
SELECT, INSERT, UPDATE, DELETE Yes, for every page view and edit The site’s own database only
CREATE, ALTER, INDEX, DROP Yes, during installs and updates The site’s own database only
CREATE TEMPORARY TABLES, LOCK TABLES Used by some plugins and backup tools The site’s own database only
GRANT OPTION No Do not grant
FILE No Never; it lets SQL read and write server files
SUPER, PROCESS, RELOAD, SHUTDOWN No Never; these are server administration rights

The FILE privilege deserves special attention. The MySQL reference manual describes it as allowing an account to read any file on the host that is world-readable or readable by the MySQL server, and to create new files in any directory where the server can write. An attacker with SQL injection and FILE can read configuration files or drop a web shell. A typical grant for a site looks like this, run as an administrative account:

CREATE USER 'site1_wp'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP,
      CREATE TEMPORARY TABLES, LOCK TABLES
      ON site1_db.* TO 'site1_wp'@'localhost';
SHOW GRANTS FOR 'site1_wp'@'localhost';

Check the output of SHOW GRANTS. You want to see privileges on site1_db.* only, and a USAGE ON *.* line, which means no global rights. If you see ALL PRIVILEGES ON *.*, the site is running with far too much power.

One database and one user per site

If several WordPress sites share a server, give each its own database and its own user with a unique password. Sharing one user across sites means a single compromised plugin exposes every site. Hosting panels such as cPanel and aaPanel create a separate database and user per site by default when you use their installers, but sites that were migrated or cloned by hand often end up sharing credentials. Our guide to securing WordPress on aaPanel covers per-site isolation in more depth.

WordPress Database Security: What the Table Prefix Really Does, a Least-Privilege MySQL User, Keeping Port 3306 Closed and Hunting Injected Rows

Keep the database off the public internet

On a single server, MySQL or MariaDB should listen only on localhost. Check the bind-address setting in the server configuration (commonly 127.0.0.1) and confirm from another machine that port 3306 does not answer. If the database lives on a separate server, put it on a private network and allow connections only from the web server’s address, both in the firewall and in the user definition, for example 'site1_wp'@'10.0.0.5' rather than 'site1_wp'@'%'.

Database management tools are a common weak point. A phpMyAdmin or Adminer installation left at a predictable URL invites password guessing. Restrict it to your own IP address, protect it with an extra layer of authentication, or remove it and use an SSH tunnel when you need it. Delete stray copies of Adminer that someone uploaded into a site folder during a migration; a single PHP file gives a full database console to anyone who finds it.

Protect the credentials in wp-config.php

WordPress has to store the database password in plain text in wp-config.php, so that file’s protection is your database’s protection. Set restrictive permissions, block web access to it, and watch for backup copies such as wp-config.php.bak or wp-config.old, which a web server may serve as plain text. Our wp-config.php hardening guide walks through each of these. After any compromise, assume the database password was read: change it in MySQL and in wp-config.php, and rotate the authentication keys and salts at the same time so existing login cookies stop working.

Keep database dumps out of reach

Backups are essential, but a .sql or .sql.gz file sitting in the web root is one of the easiest data leaks to exploit. Attackers routinely request names such as backup.sql, db.sql and site.sql.zip. Write dumps to a directory outside the document root, copy them off the server, restrict their permissions, and delete local copies once the off-site copy is verified. If a backup plugin stores archives inside wp-content, make sure the folder blocks direct downloads and that the file names are not guessable.

Hunting for injected rows

When a site misbehaves, a few targeted queries show whether the database itself was modified. Replace the wp_ prefix with yours, and run these against a copy if you can.

-- Site URL and home: should match your real domain
SELECT option_name, option_value FROM wp_options
WHERE option_name IN ('siteurl','home');

-- Accounts with administrator capability
SELECT u.ID, u.user_login, u.user_email, u.user_registered
FROM wp_users u JOIN wp_usermeta m ON m.user_id = u.ID
WHERE m.meta_key = 'wp_capabilities' AND m.meta_value LIKE '%administrator%';

-- Posts and options containing script tags or encoded payloads
SELECT ID, post_title FROM wp_posts
WHERE post_content LIKE '%<script%' OR post_content LIKE '%eval(%';
SELECT option_name FROM wp_options
WHERE option_value LIKE '%base64_decode%' OR option_value LIKE '%<script%';

Treat results as leads, not verdicts. Analytics snippets and page builders legitimately store scripts, so compare suspicious rows with a clean backup. Large autoloaded options with random names, administrator accounts nobody recognizes, and posts whose content changed without a matching revision are the strongest signs. Database injections often accompany file-based backdoors, so pair this with the checks in our guide to where WordPress backdoors hide, and if the damage is widespread, follow the steps for restoring WordPress after a hack rather than editing rows one at a time.

Prevent SQL injection at the source

Database hardening limits damage; it does not stop the injection itself. SQL injection in WordPress almost always comes from a plugin or theme that builds a query from user input without preparing it. Keep plugins updated, remove ones you no longer use, and prefer actively maintained code. If you write custom code, use $wpdb->prepare() for every query that includes variable data, which is the WordPress implementation of the parameterized queries that OWASP recommends as the primary defense. A web application firewall adds a useful layer, but it is not a substitute for patched code.

Frequently asked questions

Should I change the WordPress table prefix?

Choose a random prefix on new installs because it is free. On an existing site it offers little protection and carries real risk of breaking user roles, so do it only with a tested backup and a staging copy.

Can WordPress run with only SELECT, INSERT, UPDATE and DELETE?

Day-to-day page views and edits can, but core, plugin and theme updates that change tables will fail. Grant the schema privileges on the site’s own database and withhold global privileges such as FILE and SUPER instead.

Is it safe to allow remote MySQL connections?

Only over a private network or tunnel, from specific addresses, with a user restricted to that host. Never leave port 3306 open to the whole internet.

What should I change after a database breach?

Change the database user’s password and update wp-config.php, rotate the authentication keys and salts, reset passwords for every administrator, remove unknown accounts, and check options and posts for injected code.

Sources

Keep reading