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:
- (Recommended) Follow Creating a TiDB Serverless cluster to create your own TiDB Cloud cluster.
- Follow Deploy a local test TiDB cluster or Deploy a production TiDB cluster to create a local cluster.
Connect to TiDB
Connect to your TiDB cluster depending on the TiDB deployment option you have selected.
- TiDB Serverless
- TiDB Dedicated
- TiDB Self-Hosted
Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.
Click Connect in the upper-right corner. A connection dialog is displayed.
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.
- Connection Type is set to
Click Generate Password to create a random password.
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.
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.
Click TEST CONNECTION to validate the connection to the TiDB Serverless cluster.
In the pop-up window, click Allow.
In the SQLTools Driver Credentials dialog, enter the password you created in step 4.
If the connection test is successful, you can see the Successfully connected! message. Click SAVE CONNECTION to save the connection configuration.
Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.
Click Connect in the upper-right corner. A connection dialog is displayed.
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.
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.
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.
Click TEST CONNECTION to validate the connection to the TiDB Dedicated cluster.
- In the pop-up window, click Allow.
- In the SQLTools Driver Credentials dialog, enter the password of the TiDB Dedicated cluster.
If the connection test is successful, you can see the Successfully connected! message. Click SAVE CONNECTION to save the connection configuration.
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.
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.
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.
If the connection test is successful, you can see the Successfully connected! message. Click SAVE CONNECTION to save the connection configuration.
Next steps
- Learn more usage of Visual Studio Code from the documentation of Visual Studio Code.
- Learn more usage of VS Code SQLTools extension from the documentation and GitHub repository of SQLTools.
- Learn the best practices for TiDB application development with the chapters in the Developer guide, such as Insert data, Update data, Delete data, Single table reading, Transactions, and SQL performance optimization.
- Learn through the professional TiDB developer courses and earn TiDB certifications after passing the exam.
Need help?
Ask questions on TiDB Community, or create a support ticket.