Connecting to MySQL Instance

Last updated: 2023-09-01 15:22:48

This document describes how to connect to an initialized TencentDB for MySQL instance over the private or public network.

Prerequisites

You have created a TencentDB for MySQL instance. For more information, see Creating MySQL Instance.
Prepare a database account and authorize the IP addresses allowed to access MySQL. For more information, see Creating Account and Modifying Authorized Host Addresses. You can also use the root account directly.
You have configured security group rules for the CVM instance and the TencentDB for MySQL instance to allow specific IPs or IP ranges to access the TencentDB for MySQL instance. For more information, see TencentDB Security Group Management.

Connection Methods

Note
To connect to a TencentDB for MySQL instance, no matter whether over the private or public network, you must open its port. You can log in to the TencentDB for MySQL console, click an instance ID in the instance list, and view its port number on the instance details page.


TencentDB for MySQL uses private network port 3306 by default and supports customizing the port. If the default port is changed, the new port should be opened in the security group.
The TencentDB for MySQL public port is automatically assigned by the system and cannot be customized. After the public network access is enabled, it will be controlled by the ACL of the security group. When configuring the security policy, you need to open the private port 3306.
The security group rules displayed on the Security Group page in the TencentDB for MySQL console take effect for private and public (if enabled) network addresses of the TencentDB for MySQL instance.
TencentDB for MySQL can be connected in the following methods:
Private network connection: A CVM instance can be used to connect to the private network address of a TencentDB instance. This method utilizes the high-speed private network of Tencent Cloud and features low delay.
The CVM and TencentDB instances must be under the same account and in the same VPC in the same region, or both in the classic network.
The private network address is provided by default and can be viewed in the instance list or on the instance details page in the TencentDB for MySQL console.
Note
For CVM and TencentDB instances in different VPCs (under the same or different accounts in the same or different regions), please refer to Cloud Connect Network for private network connection methods.
Public network connection: If you cannot access the private network, you can connect to your TencentDB for MySQL instance at its public network address. The public network address needs to be manually enabled. It can be viewed on the instance details page in the TencentDB for MySQL console and can be disabled if no longer needed. To enable public network access, you also need to properly configure the security group as instructed in TencentDB Security Group Management.
The public network address can be enabled for source instances in Guangzhou, Shanghai, Beijing, Chengdu, Chongqing, Nanjing, Hong Kong (China), Singapore, Seoul, Tokyo, Silicon Valley, Virginia, and Frankfurt regions. The latest information about the regions where the public network address can be enabled for read-only instances can be found in the console.
Enabling the public network address will expose your database services to the public network, which may lead to database intrusions or attacks. We recommend that you use the private network to connect to the database.
Public network connection to TencentDB is suitable for development or auxiliary management of databases but not for business access in the production environment, as potentially uncontrollable factors may lead to unavailability of the public network connection, such as DDoS attacks and bursts of high-traffic access.
The following describes how to log in to a TencentDB for MySQL instance from Windows and Linux CVM instances over the private and public networks.

Connecting from a Windows CVM instance

1. Log in to a Windows CVM instance. For more information, see Customizing Windows CVM Configurations.
2. Download a standard SQL client.
Note
We recommend that you download MySQL Workbench. Click here and download an installer based on your operating system.

3. Login, Sign Up, and No thanks, just start my download. will appear on the page. Select No thanks, just start my download. to download quickly.

4. Install MySQL Workbench on this CVM instance.
Note
Microsoft .NET Framework 4.5 and Visual C++ Redistributable for Visual Studio 2015 are required for the installation.
You can click Download Prerequisites in the MySQL Workbench installation wizard to enter the corresponding page to download and install them. Then, install MySQL Workbench.

5. Open MySQL Workbench, select Database > Connect to Database, enter your MySQL database instance's private (or public) network address, username, and password, and click OK to log in.
Hostname: enter the private (or public) network address, which can be viewed with the port on the instance details page in the TencentDB for MySQL console. For public network address, check whether it has been enabled as instructed in Enabling Public Network Address.
Port: private (or public) network port
Username: The default username is root. For public network connections, it is recommended to create a separate account for easier connection control and management.
Password: The password corresponding to Username. If you forgot the password, reset it as instructed in Resetting Password.

6. After successful login, the following page will appear, where you can view the modes and objects of the MySQL database, create tables, and perform operations such as data insertion and query.


Connecting from a Linux CVM Instance

1. Log in to the Linux CVM instance. For more information, see Quickly Configuring Linux CVM.
2. Taking a CVM instance on CentOS 7.2 (64-bit) as an example, run the following command to install the MySQL client.
yum install mysql
If Complete! is displayed, it means the MySQL client is installed successfully.

3. Perform the corresponding operation based on the connection method:
When connecting via private network:
3.1 Run the following command to log in to the TencentDB for MySQL instance:
mysql -h hostname -u username -p
hostname: Replace it with the private network address of the target TencentDB for MySQL instance, which can be viewed on the instance details page in the TencentDB for MySQL console.
Note
The default port number of MySQL is 3306.
If the port number is 3306, you only need to replace hostname with the IP address. For example, if the private network address is 10.16.0.11:3306, set hostname to 10.16.0.11.
If the port number is not 3306, you need to specify the port in the connection command in the format of mysql -h hostname -P port -u username -p, such as mysql -h 10.16.0.11 -P 5308 -u username -p.
username: Replace it with the default username root.
3.2 After the prompt Enter password:, enter the password corresponding to the root account of the MySQL instance. If you forget the password, you can refer to Reset Password to modify it. In this example, the prompt MySQL [(none)]> indicates a successful login to MySQL.

When connecting via public network:
3.3 Run the following command to log in to the TencentDB for MySQL instance:
mysql -h hostname -P port -u username -p
hostname: Replace it with the public network address of the target TencentDB for MySQL instance, which can be viewed together with the port on the instance details page in the TencentDB for MySQL console. If the public network address has not been enabled, enable it as instructed in Enabling Public Network Address.
port: Replace it with the public network port number.
username: Replace with the public network connection username. For public network connections, it is recommended to create a separate account for easier connection control and management.
3.4 When prompted with Enter password:, enter the password corresponding to the public network connection username. If you have forgotten the password, you can refer to Reset Password to modify it. In this example, the hostname is 59281c4exxx.myqcloud.com, and the public network port number is 15311.

4. At the MySQL [(none)]> prompt, you can send SQL statements to the target MySQL server. For specific command-line instructions, see MySQL Client Commands. In the following example, the show databases; command is used:




Appendix 1. Troubleshooting connection errors

If you encounter connection errors, we recommend that you use One-Click Connectivity Checker to troubleshoot the problem first and then find the corresponding solution in Instance Connection Failure according to the check report.

Appendix 2. Network Connectivity Verification Method

We recommend that you troubleshoot and locate network connectivity problems quickly with the telnet command. For more information, see Prohibition of Ping Command.
If the verification with telnet found that the network access of the TencentDB instance was normal, but an error was reported when you tried to log in to it via the command line in the CVM instance, see Connection.

Appendix 3. Enabling public network access

1. Log in to the TencentDB for MySQL console. In the instance list, click an instance ID or Manage in the Operation column to enter the instance details page.
2. In the Basic Info section, click Enable next to Public Network Address.
Note
If the Basic Info section displays the public IP and port, the public network address has been enabled.

3. In the pop-up dialog box, click OK.
Note
Once enabled successfully, the public network address can be found in the basic info section.
The public network access can be disabled using the switch. When it is enabled again, the public network address corresponding to the domain name remains the same.