Connect to TiDB Cloud Serverless with WordPress

TiDB is a MySQL-compatible database, TiDB Cloud Serverless is a fully managed TiDB offering, and WordPress is a free, open-source content management system (CMS) that lets users create and manage websites. WordPress is written in PHP and uses a MySQL database.

In this tutorial, you can learn how to use TiDB Cloud Serverless to run WordPress for free.

Prerequisites

To complete this tutorial, you need:

Run WordPress with TiDB Cloud Serverless

This section demonstrates how to run WordPress with TiDB Cloud Serverless.

Step 1: Clone the WordPress sample repository

Run the following commands in your terminal window to clone the sample code repository:

git clone https://github.com/Icemap/wordpress-tidb-docker.git cd wordpress-tidb-docker

Step 2: Install dependencies

  1. The sample repository requires Docker and Docker Compose to start WordPress. If you have them installed, you can skip this step. It is highly recommended to run your WordPress in a Linux environment (such as Ubuntu). Run the following command to install Docker and Docker Compose:

    sudo sh install.sh
  2. The sample repository includes the TiDB Compatibility Plugin as a submodule. Run the following command to update the submodule:

    git submodule update --init --recursive

Step 3: Configure connection information

Configure the WordPress database connection to TiDB Cloud Serverless.

  1. Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.

  2. Click Connect in the upper-right corner. A connection dialog is displayed.

  3. Ensure the configurations in the connection dialog match your operating environment.

    • Connection Type is set to Public.
    • Connect With is set to WordPress.
    • Operating System is set to Debian/Ubuntu/Arch.
    • Database is set to the database you want to use—for example, test.
  4. Click Generate Password to create a random password.

  5. Run the following command to copy .env.example and rename it to .env:

    cp .env.example .env
  6. Copy and paste the corresponding connection string into the .env file. The example result is as follows:

    TIDB_HOST='{HOST}' # e.g. gateway01.ap-northeast-1.prod.aws.tidbcloud.com TIDB_PORT='4000' TIDB_USER='{USERNAME}' # e.g. xxxxxx.root TIDB_PASSWORD='{PASSWORD}' TIDB_DB_NAME='test'

    Be sure to replace the placeholders {} with the connection parameters obtained from the connection dialog. By default, your TiDB Cloud Serverless comes with a test database. If you have already created another database in your TiDB Cloud Serverless cluster, you can replace test with your database name.

  7. Save the .env file.

Step 4: Start WordPress with TiDB Cloud Serverless

  1. Execute the following command to run WordPress as a Docker container:

    docker compose up -d
  2. Set up your WordPress site by visiting localhost if you start the container on your local machine or http://<your_instance_ip> if the WordPress is running on a remote machine.

Step 5: Confirm the database connection

  1. Close the connection dialog for your cluster on the TiDB Cloud console, and open the SQL Editor page.
  2. Under the Schemas tab on the left, click the database you connected to Wordpress.
  3. Confirm that you now see the Wordpress tables (such as wp_posts and wp_comments) in the list of tables for that database.

Need help?

Ask questions on TiDB Community, or create a support ticket.

Was this page helpful?