WordPress, the most well-liked CMS, runs on MySQL, the most well-liked database on the market. Spending a while to make sure your MySQL set up and WordPress database configuration set up is satisfactorily hardened towards widespread assault vectors can assist you scale back dangers. This is very true in case you are managing your MySQL server your self.

It is price noting that many WordPress installations use MariaDB, which is a fork of MySQL. As each work very equally, we’ll use MySQL to imply each MySQL and MariaDB. Regardless of which RDMS taste you’re working, hardening your MySQL can assist you decrease the dangers of assaults from hackers. However, this doesn’t exchange different safety measures, comparable to putting in an online utility firewall, guaranteeing you may have the newest model of plugins, themes, and WordPress, and hardening WordPress.

Heads up, this text is focused at MySQL 8.0 working on Linux (Ubuntu). While the ideas will translate to different working techniques and MySQL/MariaDB variations, the instructions and file paths utilized in these examples might differ. Before making any adjustments to a manufacturing system, it’s extremely suggested to check any adjustments in a staging or pre-production surroundings.

In this text, which is primarily geared toward these managing their very own MYSQL, we provide a number of suggestions and tutorials on the way to safe MySQL. Even so, the in depth record of finest practices introduced on this article is price a learn for anybody managing WordPress web sites. Securing your MySQL server is a crucial step in sustaining a safe WordPress, and defending your self from various kinds of brute power assaults, malware injection, and different kinds of assaults.

Table of contents

Consider utilizing a Database as a Service (DBaaS)

Database as a Service is properly price contemplating when you’re not internet hosting WordPress on a managed plan. It replaces the standard mannequin of putting in MySQL domestically with a service you hook up with. This may go well with your use case in case you are working your WordPress site with a internet hosting supplier that provides managed database companies. Available choices typically embody Amazon RDS, DigitalOcean Managed MySQL, and Linode Managed MySQL). At face worth, these companies could be costlier than working MySQL your self. However, they do all of the heavy lifting of working production-grade databases. Most companies embody safety finest practices presets, ongoing safety patches and upkeep, and backups.

Using a Database as a Service (DBaaS) is without doubt one of the finest choices when it comes to safety and reliability. While this isn’t necessary, it’s nonetheless a good-to-have. However, in case you are seeking to handle MySQL your self, the next is a set of hardening suggestions to remember.

Keep MySQL up-to-date

Just because it’s essential to make sure you’re working the newest model of WordPress, it’s essential to maintain MySQL up-to-date. Like most different software program, updates to the MySQL server are launched periodically. These updates tackle bugs, mitigate vulnerabilities, and supply new options. You ought to preserve MySQL up-to-date with the newest safety patches to cut back the dangers of working software program with recognized vulnerabilities. Bear in thoughts that when up to date, you may be required to restart the ‘mysql daemon.’ This is a course of which will incur some downtime. As at all times, plan forward.

Run MySQL on a devoted machine

Many WordPress installations run MySQL, PHP, and an online server (comparable to Nginx or Apache HTTP Server) on the identical machine. This will not be optimum – each when it comes to efficiency and safety. MySQL ought to ideally run on a devoted server to cut back the blast radius of an assault. If an attacker manages to compromise and escalate privileges on the internet server, it will be a lot tougher for that attacker to maneuver laterally and likewise compromise the MySQL server.

Bind MySQL to an IP tackle

You can configure MySQL to solely settle for TCP/IP connections from a particular IPv4 or IPv6 interface. All it’s essential do is ready the bind-address configuration choice to a particular IP tackle. This gives extra controls and restrictions on how consumer purposes (in our case, WordPress) can hook up with MySQL. By default, this setting is ready to *, that means that out-of-the-box MySQL will pay attention on all interfaces.

If not configured to hearken to a particular IP, all IPs can be utilized to hook up with MySQL. This setting is very essential to set in case you are working MySQL on the identical machine as an online server that you’re exposing to the Internet (on this case, it’s best to set the bind-address to so MySQL solely listens on localhost).

For instance, if you’d like the MySQL server to solely settle for connections on a particular IPv4 tackle, you may add an entry much like the instance beneath. You ought to enter this below the [mysqld] choice group in your server’s /and so forth/mysql/mysql.conf.d/mysqld.cnf configuration file.


Note that when you set this, you’ll need to reconfigure WordPress to hook up with the database utilizing this IP tackle (except it’s doing so already) since connections on different server host addresses wouldn’t be permitted.

Many WordPress installations embody web-based front-end graphical administration instruments. Common examples embody Cpanel, phpMyAdmin, or Adminer. These instruments make it simpler to handle MySQL and different facets of the underlying infrastructure. While a web-based graphical interface can assist you handle your MySQL databases, these interfaces can enhance the assault floor by including one other vector. Furthermore, there’s a danger that they’ll be found and abused by attackers to run harmful or malicious SQL queries towards your database. Attacks might even lead to a full takeover of your WordPress web site.

The solely secure server is the one which’s switched off and unplugged – nonetheless, danger could be managed. Uninstalling non-critical techniques is one choice; nonetheless, these will also be locked down and restricted to reduce the danger.

It is feasible to limit entry to those instruments in a wide range of methods. You can set up phpMyAdmin for WordPress remotely, thus minimizing danger to the net server. Alternatively, you may also wish to think about using instruments comparable to MySQL Workbench or Beekeeper Studio on your native machine and hook up with your database server over an SSH tunnel.

Run the MySQL daemon utilizing a devoted person

As with different companies working on a server, you may run the MySQL daemon below a devoted person. When you run MySQL utilizing a devoted person, you may exactly outline what permissions that person is given inside the system. Running MySQL below a devoted person additionally follows the precept of least privilege since this reduces the blast radius of a MySQL vulnerability. It additionally decreases the potential for a misconfiguration being taken benefit of since a restricted person might be unable to entry sources unrelated to MySQL (comparable to working system configurations and secrets and techniques).

The excellent news is that installations through package deal managers (comparable to apt or yum) care for this step robotically when putting in MySQL. A fast strategy to confirm if MySQL is working below a devoted person is to run the next on the machine working the MySQL daemon.

ps -ef | egrep “^mysql.*$”

If MySQL is working utilizing a devoted person, it’s best to anticipate to see at the least one line from ps’s output returned.

Use the mysql_secure_installation script

The mysql-server package deal comes with a shell script utility known as mysql_secure_installation. You can use this script to arrange a safe start line for the MySQL server. As such, it’s best to run it after a recent set up of MySQL. This utility helps you:

  • Set a password for root accounts
  • Remove root accounts which might be accessible from outdoors localhost
  • Remove nameless person accounts
  • Remove the check database (which, by default, could be accessed by nameless customers)

To invoke mysql_secure_installation, run the next command:

sudo mysql_secure_installation

Once the setup course of begins, you may be introduced with a number of prompts asking you whether or not you wish to allow the validate password plugin, which is used to check the energy of passwords you decide for MySQL customers. It is really useful that you just allow this plugin.

After you allow the validate password plugin, the script will ask you to specify a password validation coverage. Here, it’s best to select a robust password coverage. You will subsequently be requested to reset the basis person’s password.

Next, the script will immediate you to take away nameless MySQL customers. This is essential to cut back any likelihood of attackers getting access to the database server by leveraging an nameless MySQL person.

The subsequent immediate will ask you if you need to disable logins utilizing the basis person when authenticating remotely to the MySQL server. Remote authentication utilizing the basis person is harmful and barely required. Instead, it’s best to both SSH onto the MySQL and use the MySQL consumer on the server to authenticate as the basis person or, ideally, use an SSH tunnel to ahead the distant MySQL port to your native machine and join utilizing an area consumer.

Next, you’ll be requested to delete the default databases (in the event that they exist) that MySQL ships with. This is the really useful apply for manufacturing MySQL servers.

Delete default database

Finally, you’ll be requested if you wish to reload the privileges tables for all adjustments which have been utilized to take impact.

Create a devoted WordPress database person

Security finest practices dictate segregating customers and privileges by duties or roles. This implies that each utility that makes use of the database ought to have its personal devoted person with the minimal quantity of MySQL database permissions required to hold out its job. As such, you’ll guarantee person privileges don’t go over and above what’s required.

This apply ought to prolong to deployments working a number of WordPress web sites — every WordPress web site ought to have its personal devoted database and MySQL person. This ensures that at any time, just one person has entry to at least one database at a time, and customers can’t entry different databases, avoiding unauthorized entry and knowledge breaches.

The following SQL assertion (substitute <host> and <password> and <database> to suit your wants) can be utilized to create a devoted person for your WordPress web site and grant privileges for common use. Keep in thoughts that some WordPress plugins, themes, and WordPress updates might often want extra privileges to function accurately (see the official WordPress steering on this for extra info)

Ensure that local_infile is disabled

The LOAD DATA assertion means that you can load knowledge recordsdata into database tables. Under particular situations, this may be abused to learn recordsdata from the MySQL server. As such, except you may have a particular use case for this in your WordPress site, it’s best to disable this characteristic.

If MySQL and the net server are working on the identical machine, it might permit an attacker to make use of the LOAD DATA LOCAL assertion to learn arbitrary recordsdata that the net server course of has learn entry to. This assumes that an attacker has the power to run arbitrary SQL statements towards MySQL. Such often is the case with an SQL injection vulnerability or via the set up of a malicious WordPress plugin. This is but one more reason to maintain your net server and database servers separate.

By default, local_infile is disabled in MySQL 8.0 (it was once enabled by default in earlier variations of MySQL). To stop the MySQL server from accepting LOAD DATA LOCAL statements, make sure that the mysqld daemon is began with local_infile disabled.

Disable MySQL command historical past

On Linux, the MySQL consumer logs statements executed interactively are saved to a historical past file (sometimes positioned in $HOME/.mysql_history). The MySQL command historical past ought to ideally be disabled since this reduces the probability of exposing delicate info, comparable to passwords, encryption keys, or different secrets and techniques.

To confirm that .mysql_history recordsdata don’t exist on the system, run the next instructions:

discover /dwelling -name “.mysql_history”
discover /root -name “.mysql_history”

If the above instructions return any output, take away any .mysql_history recordsdata. Additionally, you may set $HOME/.mysql_history as a symlink to /dev/null as follows:

ln -s /dev/null $HOME/.mysql_history

Ensure that mysqld will not be began with the –skip-grant-tables argument

Should the MySQL’s root password get misplaced, whereas not the popular technique, some MySQL directors might resort to setting MySQL to start out with the –skip-grant-tables argument. When beginning MySQL with this parameter, it can keep away from checking its grant tables when a consumer connects or runs a question, successfully permitting anybody, anyplace (supplied they will attain the database over the community), to do something on the database server.

To make sure that –skip-grant-tables will not be enabled, open your server’s /and so forth/mysql/mysql.conf.d/mysqld.cnf configuration file and look for skip-grant-tables. The worth ought to both not be set, or set to skip-grant-tables = FALSE.

Back up your database

Backing up your WordPress database is completely essential to have the ability to get better promptly from a catastrophe or an assault. While there’s a myriad of the way to again up your WordPress database – from WordPress backup plugins and companies to homegrown scripts that take a database dump periodically — the next are just a few salient suggestions to remember.

Take frequent backups

Taking common backups is fairly apparent and self-explanatory — the extra often you are taking database backups, the simpler it will likely be to get better from a knowledge loss incident. While the frequency of backups will rely on the kind of WordPress site you might be working, as a rule of thumb, taking a backup each day serves most use instances properly.

Verify the integrity of your backups often

Your backups are solely helpful in the event that they work. and you’d possible desire to not discover out while you’re in the midst of an incident making an attempt to get better knowledge. The easy remediation to that is to often confirm that your backups really work by doing check restores every now and then. A great way to do that is to set a calendar occasion each few months to undergo a restore process to make sure your backups are nonetheless working as anticipated. Additionally, documenting database restoration steps can also be a good suggestion — the much less guesswork when responding to an incident, the higher.

Store your backups securely

Never preserve backups of your WordPress site on your net or database server (particularly on your net server). Backups are an excellent place for attackers to go dumpster diving. Storing your backups in a safe offsite location is extremely advisable. If you take periodic database dumps, contemplate storing your database dumps on an object storage service. These can embody Amazon S3, Cloudflare R2, DigitalOcean Spaces, Linode Object Storage, and so forth. Taking this route is usually a nice, cost-effective strategy to retailer your database backups. However, do be additional cautious that you don’t make the storage bucket you might be utilizing publicly accessible.

Enable and imposing TLS connections

Unless you might be working MySQL on the identical machine as your net server (which, as we already lined above, will not be a really perfect safety apply), it’s extremely really useful to encrypt knowledge between WordPress and MySQL utilizing Transport Layer Security (TLS certificates), previously known as Secure Socket Layer (SSL certificates).

By default, once you set up MySQL, it can generate a self-signed certificates for you robotically. You can confirm this by working the next (alternatively, you need to use the mysql_ssl_rsa_setup script to generate new certificates).

You might want to copy over ca.pem from the above record (for instance, through SCP) to the server working your WordPress web site. Once you add the ca.pem file to your WordPress server, you’ll need to maneuver the certificates over to the working system’s certificates belief retailer and replace the certificates belief retailer as follows.

Heads up, the file title of the CA certificates should finish with a .crt file extension (e.g. mysql-ca.crt is legitimate, however mysql-ca.pem.crt, or mysql-ca.pem are invalid).

sudo mv ca.pem /usr/native/share/ca-certificates/mysql-ca.crt
sudo update-ca-certificates

Next, it’s essential configure WordPress to make use of TLS when connecting to MySQL by including the next to your wp-config.php file of your WordPress set up.


Once you replace wp-config.php, WordPress will provoke connections to your MySQL server utilizing TLS.

Next, it’s endorsed that you just implement TLS connections to your MySQL server utilizing the require_secure_transport system variable by including the next to your /and so forth/mysql/mysql.conf.d/mysqld.cnf file.

require_secure_transport = ON

Finally, restart MySQL for adjustments to take impact.

systemctl restart mysql

Change the desk prefix

By default, all WordPress tables are created with the ‘wp_’ prefix. This could make it simpler for attackers to reach sure assaults, comparable to SQL injection, since they’d know the names of the database tables. While this alone will not be going to guard you, it’s a simple train, really useful by many as a finest WordPress safety apply.

You can change the database prefix in the course of the set up course of or at any level thereafter, though the latter is barely extra advanced. Either means, you’ll find on-line tutorials on altering WordPress database prefix.

How to implement adjustments

Hopefully, this text has supplied you with an summary of MySQL safety hardening within the context of working a WordPress web site. While there are not any silver bullets in web site safety, with some effort, taking a layered, defense-in-depth method to safety will make attacking your web site considerably tougher for attackers.
While this information presents various hardening methods for MySQL, MySQL is only one element of the WordPress ecosystem. As such, you must also contemplate different facets of WordPress safety lined in our WordPress safety hardening information. This, coupled with confirmed safety measures comparable to WordPress two issue authentication, will aid you make sure you’re as secure as you could be.

If it appears like loads to soak up, bear in mind that you would be able to (and possibly ought to) apply the assorted hardening methods lined on this information regularly.

Keeping your WordPress safe

Bear in thoughts that attackers are oftentimes after tender targets since they don’t must put as a lot effort into exploiting weakly secured web sites. Being one step forward of the following WordPress web site’s safety posture makes you a much less engaging goal.

The publish Hardening MySQL for your WordPress site appeared first on WP White Security.

*** This is a Security Bloggers Network syndicated weblog from WP White Security authored by Mark Grima. Read the unique publish at: https://www.wpwhitesecurity.com/wordpress-mysql-hardening/


Please enter your comment!
Please enter your name here