Running a WordPress Site on a Remote Database

Category: Guides, WordPress
Last modified on November 26, 2024

Developing a website typically involves working on two versions simultaneously: dev (local) and production (server). If your site uses a database, it’s often convenient to use the same database for both versions during parallel development.

However, this setup presents a challenge: WordPress stores the site’s base URL in the database. In this article, I’ll walk you through configuring a remote database connection on your local site and address the base URL issue.

Database Connection

To set up a connection to a remote database, open the wp-config.php file in the root directory of WordPress and navigate to this part of the code:

... /** The name of the database for WordPress */ define( 'DB_NAME', 'database_name' ); /** Database username */ define( 'DB_USER', 'user_login' ); /** Database password */ define( 'DB_PASSWORD', 'user_password' ); /** Database hostname */ define( 'DB_HOST', '255.255.255.255' ); ...

Here, you’ll need to set four parameters:

  • DB_NAME – the name of your database;
  • DB_USER – the database username;
  • DB_PASSWORD – the user password for the database;
  • DB_HOST – the database host (usually set to localhost).

Usually, the database host is set to localhost, meaning WordPress uses the IP address of the current server connection. This works well if the database is accessed only on a single server. However, if a remote database connection is required, you’ll need to set a static IP for DB_HOST.

To find the IP of the server where your site is hosted, go to cPanel and check the sidebar on the main page under “General Information” at “Shared IP Address“. Alternatively, you can click on “Server Information” at the bottom of the page and find the IP address there. Use this IP as a string for the DB_HOST parameter. This configuration will work for both remote and local sites.

It’s also crucial to enable access to the database from a remote server. In cPanel, find the “Databases” section on the main page, and select “Remote MySQL®“. Here, you’ll likely see one record with “Access Hosts” set to your server’s IP, which allows connections only from this server. To enable access from any server, create a new entry with “Host” set to %. You can also specify your server’s static IP address to enhance the security of external connections.

Setting the Base URL

WordPress generates page URIs using a base URL stored in the wp_options table within the siteurl and home (front page URL) fields. If you set up a connection to a remote database from your local server, all links and resources (such as styles, scripts, and images) will direct to the production version of the site.

To resolve this, you only need to redefine these values in the wp-config.php file. Locate the section reserved for custom modifications, usually marked with comments, and add the following lines. The values of these parameters should correspond to your local site’s URL.

... /* Add any custom values between this line and the "stop editing" line. */ define( 'WP_SITEURL', 'https://local.yanmet.com' ); define( 'WP_HOME', 'https://local.yanmet.com' ); /* That's all, stop editing! Happy publishing. */ ...

Conclusion

Now, you know how to quickly and easily set up a remote database connection for your local server. This configuration enhances local development of an existing site or enables parallel development of a new site without the need to duplicate work or constantly export and import databases.

Related Posts

by the same categories