How to Connect WordPress to MySQL Database Step by Step

Suppose you are a WordPress beginner. One of the most challenging things to understand is how to connect WordPress to MySQL database. For complete beginners who might not understand how WordPress works, in this quick tutorial, I want to introduce you to how you connect WordPress to MySQL database and explain how it works.

MySQL Command Prompt

To get the most out of this tutorial, you must have a basic understanding of how PHP and MySQL database connection works. To quickly jog your memory about the PHP MySQL database connection, this is the code we use in PHP to connect to the MySQL database.

  $servername = "localhost"; // database host
  $username = "username"; //database username
  $password = "password"; //databse password

// Create connection
  $conn = new mysqli($servername, $username, $password);

// Check connection
   if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
   }

//Response after the connection is successful

   echo "Connected successfully";

So ideally, WordPress files that you download from the WordPress repository represent the frontend application that requires a database in this context (MySQL) where all the information is stored. All the pages, posts, users, post-Meta, user Meta and admin options will be stored in this database.

In practice, when building a custom PHP application that uses the SQL databases, the code above is placed in a file that is commonly named ‘config.php,’ although you can give it any other name. To connect WordPress to MySQL database, we need this kind of file, as I will demonstrate in later steps.

But before we learn how to connect WordPress to MySQL database, we need first to understand why we need to connect WordPress to MySQL database.

Why Learn to Connect WordPress to MySQL Database 

First, one of the most common problems with WordPress sites is the error in establishing a database connection. In most cases, when you are developing a WordPress site, you will commonly come across this error.

To resolve this error, it is imperative you understand how WordPress works, especially how WordPress application is connected to the database.

How Does WordPress and MySQL Database Work?

Understanding this relationship between WordPress and MySQL database makes it easier for you to connect WordPress to MySQL databases.

As the image above illustrates, the WordPress frontend application phase relies on the database (MySQL) to store all the data published on a WordPress site.

When a user visits the website and clicks on a link to a page, the page content is retrieved from the database and served to see the content. This is purely the relationship between WordPress and MySQL database.

This back-and-forth relationship makes it easier to build a dynamic website since the data can be retrieved, edited, updated, and viewed. This is the basics of a CRUD (create, read, update, delete) operation that is the main idea behind every content management system.

How to Connect WordPress to MySQL Database Step by Step

In a nutshell, to connect WordPress to MySQL database, the following are the steps that you should take:

  1. Download the latest version of WordPress and copy it to your local or remote server or hosting server.
  2. Create the MySQL database and a user with the password to the MySQL database.
  3. Visit the browser where the unzipped WordPress files are located, choose a language, and then continue.
  4. Fill in your database details that include – database name, username, password.
  5. The localhost field should be localhost since it is relative to the location of the database. If it is in the same server, leave it as localhost default.
  6. The table prefix field should also be left as wp_ most of the time, but you can change it to your preferred prefix to improve your site security.
  7. Click on Submit, and you can now move to the next steps to complete WordPress installation.
  8. Open your wp-config file in the root path of WordPress installation. You should see the database details you entered in step 4 confirming that you have successfully connected WordPress to the MySQL database.

Download Latest Version of WordPress  

To install WordPress and connect to the MySQL database, you need to begin by downloading the latest version of WordPress from the official WordPress repository. When you have downloaded WordPress, copy it to your server local or remote to install WordPress.

How to Connect WordPress to MySQL Database

Unzip the content of the WordPress zipped folder and ensure you copy them to the main folder from the WordPress unzipped folder as shown below:

How to Connect WordPress to MySQL Database

How to Connect WordPress to MySQL Database

Now delete the empty folder and the originally downloaded WordPress zip folder shown in the image above. Now we are ready to connect WordPress to MySQL database.

The checkout the link where WordPress should be installed. If you are on localhost, go to the browser, then search 127.0.0.1/folder-where-you-unzipped-WordPressExample 127.0.0.1/njengah-tutorial

You will be redirected to the WordPress installation screen, as shown in the image below. You need to select the language you wish WordPress to be installed in, and when you select the language, click on the continue button as shown below:

How to Connect WordPress to MySQL Database

You will be presented with the next screen on how to connect WordPress to the MySql database, as shown in the image below:

How to Connect WordPress to MySQL Database

Create MySQL Database

As you can see from the screenshot above, you need to have the following information to connect WordPress to MySQL database.

  1. Database name
  2. Database username
  3. Database password
  4. Database host
  5. Table prefix (if you want to run more than one WordPress in a single database)

The following is the image of the empty database that has been created viewed from the PHPMyAdmin interface, ready to be connected to WordPress and populated with the WordPress database tables:

Connect WordPress to MySQL Database

Add MySQL Database Details to Installation Screen

When you have these details, click on let’s go and fill in your database details as shown in the image below:

Click on the Run Installation button to complete WordPress installation and connect WordPress to MySQL database.

How to Connect WordPress to MySQL Database

Fill in your website details so that you can complete the WordPress installation process:

At this step, you have successfully installed WordPress, and you can now log in to your newly installed WordPress.

How to Connect WordPress to MySQL Database

Open your wp-config file to View MySQL Database Details

For an aspiring WordPress developer, you need to understand how the database configuration details are added to the WordPress script to connect WordPress to the MySQL database.

To confirm, you should navigate to the root path where you installed WordPress and look for the wp-config.php file as shown in the image below:

How to Connect WordPress to MySQL Database

Open the file, and you should see all the details we added to step 4 confirming that your WordPress application has been successfully connected to the MySQL database.

In the future, when you want to troubleshoot the WordPress connection to the MySQL database, you should pay keen attention to this file since it is the one responsible for this connection.

Wrapping up

In this tutorial, we have comprehensively covered every step of connecting WordPress to MySQL database, and I am confident you can easily follow these steps. If you are a WordPress developer or want to get into WordPress development, understanding how WordPress relates to MySQL database is an important step.

Rate this post