Connect to TiDB with Visual Studio Code

TiDB is a MySQL-compatible database, and Visual Studio Code (VS Code) is a lightweight but powerful source code editor. This tutorial uses the SQLTools extension which supports TiDB as an official driver.

In this tutorial, you can learn how to connect to your TiDB cluster using Visual Studio Code.

Prerequisites

To complete this tutorial, you need:

  • Visual Studio Code 1.72.0 or later versions.
  • SQLTools MySQL/MariaDB/TiDB extension for Visual Studio Code. To install it, you can use one of the following methods:
    • Click this link to launch VS Code and install the extension directly.
    • Navigate to VS Code Marketplace and click Install.
    • On the Extensions tab of your VS Code, search for mtxr.sqltools-driver-mysql to get the SQLTools MySQL/MariaDB/TiDB extension, and then click Install.
  • A TiDB cluster.

If you don't have a TiDB cluster, you can create one as follows:

Connect to TiDB

Connect to your TiDB cluster depending on the TiDB deployment option you have selected.

  • TiDB Serverless
  • TiDB Dedicated
  • TiDB Self-Hosted
  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.
    • Branch is set to main.
    • Connect With is set to VS Code.
    • Operating System matches your environment.
  4. Click Generate Password to create a random password.

  5. Launch VS Code and select the SQLTools extension on the navigation pane. Under the CONNECTIONS section, click Add New Connection and select TiDB as the database driver.

    VS Code SQLTools: add new connection

  6. In the setting pane, configure the following connection parameters:

    • Connection name: give this connection a meaningful name.

    • Connection group: (optional) give this group of connections a meaningful name. Connections with the same group name will be grouped together.

    • Connect using: select Server and Port.

    • Server Address: enter the HOST parameter from the TiDB Cloud connection dialog.

    • Port: enter the PORT parameter from the TiDB Cloud connection dialog.

    • Database: enter the database that you want to connect to.

    • Username: enter the USERNAME parameter from the TiDB Cloud connection dialog.

    • Password mode: select SQLTools Driver Credentials.

    • In the MySQL driver specific options area, configure the following parameters:

      • Authentication Protocol: select default.

      • SSL: select Enabled. TiDB Serverless requires a secure connection. In the SSL Options (node.TLSSocket) area, configure the Certificate Authority (CA) Certificate File field as the CA parameter from the TiDB Cloud connection dialog.

    VS Code SQLTools: configure connection settings for TiDB Serverless

  7. Click TEST CONNECTION to validate the connection to the TiDB Serverless cluster.

    1. In the pop-up window, click Allow.

    2. In the SQLTools Driver Credentials dialog, enter the password you created in step 4.

      VS Code SQLTools: enter password to connect to TiDB Serverless

  8. If the connection test is successful, you can see the Successfully connected! message. Click SAVE CONNECTION to save the connection configuration.

  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. In the connection dialog, select Public from the Connection Type drop-down list, and then click CA cert to download the CA certificate.

    If you have not configured the IP access list, click Configure IP Access List or follow the steps in Configure an IP Access List to configure it before your first connection.

    In addition to the Public connection type, TiDB Dedicated supports Private Endpoint and VPC Peering connection types. For more information, see Connect to Your TiDB Dedicated Cluster.

  4. Launch VS Code and select the SQLTools extension on the navigation pane. Under the CONNECTIONS section, click Add New Connection and select TiDB as the database driver.

    VS Code SQLTools: add new connection

  5. In the setting pane, configure the following connection parameters:

    • Connection name: give this connection a meaningful name.

    • Connection group: (optional) give this group of connections a meaningful name. Connections with the same group name will be grouped together.

    • Connect using: select Server and Port.

    • Server Address: enter the host parameter from the TiDB Cloud connection dialog.

    • Port: enter the port parameter from the TiDB Cloud connection dialog.

    • Database: enter the database that you want to connect to.

    • Username: enter the user parameter from the TiDB Cloud connection dialog.

    • Password mode: select SQLTools Driver Credentials.

    • In the MySQL driver specific options area, configure the following parameters:

      • Authentication Protocol: select default.
      • SSL: select Disabled.

    VS Code SQLTools: configure connection settings for TiDB Dedicated

  6. Click TEST CONNECTION to validate the connection to the TiDB Dedicated cluster.

    1. In the pop-up window, click Allow.
    2. In the SQLTools Driver Credentials dialog, enter the password of the TiDB Dedicated cluster.

    VS Code SQLTools: enter password to connect to TiDB Dedicated

  7. If the connection test is successful, you can see the Successfully connected! message. Click SAVE CONNECTION to save the connection configuration.

  1. Launch VS Code and select the SQLTools extension on the navigation pane. Under the CONNECTIONS section, click Add New Connection and select TiDB as the database driver.

    VS Code SQLTools: add new connection

  2. In the setting pane, configure the following connection parameters:

    • Connection name: give this connection a meaningful name.

    • Connection group: (optional) give this group of connections a meaningful name. Connections with the same group name will be grouped together.

    • Connect using: select Server and Port.

    • Server Address: enter the IP address or domain name of your TiDB Self-Hosted cluster.

    • Port: enter the port number of your TiDB Self-Hosted cluster.

    • Database: enter the database that you want to connect to.

    • Username: enter the username to use to connect to your TiDB Self-Hosted cluster.

    • Password mode:

      • If the password is empty, select Use empty password.
      • Otherwise, select SQLTools Driver Credentials.
    • In the MySQL driver specific options area, configure the following parameters:

      • Authentication Protocol: select default.
      • SSL: select Disabled.

    VS Code SQLTools: configure connection settings for TiDB Self-Hosted

  3. Click TEST CONNECTION to validate the connection to the TiDB Self-Hosted cluster.

    If the password is not empty, click Allow in the pop-up window, and then enter the password of the TiDB Self-Hosted cluster.

    VS Code SQLTools: enter password to connect to TiDB Self-Hosted

  4. If the connection test is successful, you can see the Successfully connected! message. Click SAVE CONNECTION to save the connection configuration.

Next steps

Need help?

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

Was this page helpful?