In this guide
1. Use strong credentials and one database user per site
- Give the database user a long, random password.
- Use a separate database and user for each site, so one compromised site can’t read the others.
- Don’t reuse the database password anywhere else.
2. Don’t give the WordPress user more rights than it needs
WordPress needs to read, write and change tables (for updates). It doesn’t need server-wide rights or access to other databases. On shared hosting your host handles this. On a VPS, don’t connect WordPress as root.
3. Keep the database private
- Most hosts only allow database connections from the server itself. Keep it that way, and don’t open the database port to the internet.
- Protect phpMyAdmin and similar tools: use your host’s control panel login, and don’t install standalone database tools in your website folder.
- Remove tools like Adminer if someone uploaded them for a one-off task and left them behind.
4. Never leave backups in public folders
The most common real-world database leak isn’t a hack. It’s a .sql export or a backup zip left in the website folder or in uploads, where anyone can download it. Store backups off-site, and make sure directory listing is off.
5. Protect wp-config.php
It contains your database credentials. Set strict file permissions (often 640 or 600, depending on your host), and make sure it can’t be downloaded. On Apache, add to your root .htaccess:
<Files "wp-config.php">
Require all denied
</Files>
6. Watch for malware in the database
Modern malware often stores its code in the database, especially in wp_options, so that it survives a file cleanup. Check now and then for unknown administrators and scripts in posts and options. My database malware page has safe, read-only queries to do it.
7. Back up regularly, and test a restore
Daily off-site backups with several weeks of history, and at least one test restore so you know it works.
From my cleanup work: the database is where cleanups most often fall short. The files get cleaned, but a few kilobytes of encoded code in wp_options stay behind and rewrite the malware on the next page load. Securing the database isn’t just about keeping attackers out. It’s also about knowing what’s inside.