How to Secure Your WordPress Database from Hackers
Your WordPress database contains everything valuable about your site. Learn how to protect it from SQL injection, unauthorized access, and data theft.
Why Database Security is Critical
Your WordPress database stores everything that makes your website unique: posts, pages, user accounts, settings, customer data, and more. If hackers gain access to your database, they can steal sensitive information, modify your content, or completely destroy your website.
Database attacks are among the most damaging security incidents because they target your site's core data. Unlike file-based attacks that might be contained to specific directories, database compromises affect your entire website.
Common Database Security Threats
SQL Injection Attacks
SQL injection occurs when attackers insert malicious SQL code through input fields on your website. If your site doesn't properly sanitize user input, these queries can:
- Extract all data from your database
- Modify or delete existing records
- Create new admin accounts
- Execute administrative operations
Database Credential Theft
Hackers who access your wp-config.php file obtain your database credentials. With these credentials, they can connect directly to your database from anywhere in the world.
Database Backup Theft
Unprotected database backups stored on your server give attackers easy access to all your data without needing to exploit active vulnerabilities.
Securing Your Database Credentials
Protect wp-config.php
Your wp-config.php file contains your database host, name, username, and password. Protect it with these measures:
Move wp-config.php above the web root:
Move from: /public_html/wp-config.php
To: /wp-config.php (one directory up)
Add .htaccess protection:
<files wp-config.php>
order allow,deny
deny from all
</files>
Use Strong Database Passwords
Generate a complex database password with at least 20 characters including uppercase, lowercase, numbers, and symbols. Use a password manager to store it securely.
Create a Dedicated Database User
Don't use your hosting account's master database user. Create a dedicated user with only the permissions WordPress needs:
- SELECT, INSERT, UPDATE, DELETE
- CREATE, DROP, ALTER (for updates)
- INDEX (for optimization)
Change the Default Table Prefix
WordPress uses "wp_" as the default table prefix. This makes automated SQL injection attacks easier since hackers know exactly what to target. Change this to something unique during installation:
$table_prefix = 'x7y9z_';
For existing sites, changing the prefix requires updating all existing table names and several database records—use a plugin or make careful manual changes.
Preventing SQL Injection
Keep WordPress Updated
WordPress core includes protection against SQL injection, but vulnerabilities are occasionally discovered. Always run the latest version to benefit from security patches.
Use Trusted Plugins Only
Many SQL injection vulnerabilities come from poorly coded plugins. Only use plugins from reputable developers with good security track records.
Implement a Web Application Firewall
A WAF filters requests before they reach your site, blocking obvious SQL injection attempts. This provides protection even if your plugins have undiscovered vulnerabilities.
Validate and Sanitize All Input
If you write custom code, always use WordPress's built-in sanitization functions and prepared statements for database queries.
Secure Your Database Backups
Encrypt Backup Files
Database backups contain sensitive information. Always encrypt backup files before storing them anywhere, especially off-site locations.
Store Backups Securely
- Never store backups in publicly accessible directories
- Use secure cloud storage with encryption
- Implement strong access controls
- Regularly audit who has backup access
Delete Old Backups
Maintain a reasonable retention policy. Old backups sitting on your server create unnecessary risk. Keep recent backups and securely delete outdated ones.
Monitor Database Activity
Enable Database Logging
Enable query logging to track database activity. This helps identify suspicious queries that might indicate an attack in progress.
Set Up Alerts
Configure alerts for unusual database activity like:
- Large data exports
- New admin user creation
- Modified user passwords
- Changes to critical settings
Additional Protection Measures
Disable Remote Database Access
Unless specifically needed, configure your database to only accept connections from localhost. This prevents remote attackers from connecting even if they obtain credentials.
Use SSL for Database Connections
If your database is on a separate server, encrypt the connection using SSL to prevent credential interception.
Conclusion
Database security requires multiple layers of protection. By securing credentials, preventing SQL injection, protecting backups, and monitoring activity, you create a comprehensive defense that keeps your valuable data safe from attackers.
Written by Sarah Chen
WP Folder Shield Team