Skip to main content

MySQL

Cloud Functions support using MySQL.

Prerequisites

Have activated Cloud Development.

Features

The service provides a high-performance distributed data storage solution professionally built on the open-source MySQL database, enabling users to set up, operate, and scale relational databases more easily in the cloud.

Applicable Scenarios

The database in Cloud Development is insufficient to meet business requirements, which requires the use of MySQL.

Existing services that use MySQL can be migrated to Cloud Development while continuing to utilize MySQL.

Installer

! WeChat Mini Program developers, please use [Other Login Methods] - [WeChat Official Account Login] to log in, then select the associated Mini Program account to log in; QQ Mini Program developers can directly log in via the [Cloud Development] button in the QQ Mini Program Developer IDE, or log in via the associated Tencent Cloud account.

You can install and manage this capability via the Tencent Cloud Development Console.

Using the Program

If your existing business code uses a MySQL connection pool, it may not be necessary in Cloud Functions since each request is distributed to a different Cloud Functions instance. Typically, a single MySQL connection per Cloud Functions instance is sufficient.

Depending on the user's actual situation, it is recommended to use mysql and serverless-mysql.

By default, serverless-mysql is a wrapper for mysql that adds connection management features, maintaining only one connection to the MySQL Server. If a connection pool is required in Cloud Functions, using mysql is recommended.

When using serverless-mysql, pay attention to the configuration of MySQL's max_user_connections and max_connections.

Invocation Example

Using in Cloud Function

"use strict";
const mysql = require("serverless-mysql")({
config: {
host: process.env.HOST,
port: process.env.PORT,
database: process.env.DATABASE,
// Fill in the actual username and password.
user: "xxx",
password: "xxx"
}
});
exports.main = async (event, context, callback) => {
let res;
try {
res = await mysql.query("SELECT * FROM mysql_test");
} catch (e) {
console.error(e);
}
return {
res,
code: 200
};
};

Other

Operating Mechanism of Cloud Function

In-depth Understanding of Cloud Function - Operating Mechanism of Cloud Function

Virtual Private Cloud (VPC)

In cloud functions, if developers need to access resources such as Tencent Cloud's databases, it is recommended to use a private network to ensure data security and connection security. For details on private networks, including how to establish a private network and add cloud functions to it, refer to the relevant section in Creating a Private Network for Database MySQL.

Cloud Function Concurrency & MySQL Connections

In the Cloud Development Console - Environment Overview, you can view the maximum concurrency allowed for cloud functions in the current environment. The maximum concurrency also represents the maximum instances for cloud functions.

It is recommended to read the MySQL official blog post MySQL Connection Handling and Scaling regarding MySQL connections.

The relevant parameters for MySQL connections can be configured in the Database TencentDB console -> MySQL -> Database Management -> Parameter Settings.

In Cloud Functions, each instance establishes a connection to the MySQL Server. Therefore, the maximum number of connections between Cloud Functions and MySQL is calculated as: maximum connections per instance * actual maximum concurrency. When configuring MySQL's max_connections, this parameter should be greater than the sum of the maximum connections from all Cloud Functions using this database.

Therefore, when using MySQL in Cloud Functions, it is recommended to consolidate all code that accesses the same database (not just the same table) within a single cloud function. This approach offers two benefits:

  • The probability of cold starts occurring in Cloud Functions is relatively low
  • The maximum number of MySQL connections is relatively small