Skip to main content

MySQL

Supports using MySQL in cloud functions.

Prerequisites

CloudBase has been provisioned.

Features

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

Applicable Scenarios

The cloud development database cannot meet the business requirements, so it is necessary to use MySQL.

Existing services that have been using MySQL wish to continue utilizing it when migrating to CloudBase.

Installer

! WeChat Mini Program developers should use [Other login methods] - [Official Account Login] to log in, then select the associated mini program account. QQ Mini Program developers can log in directly via the [CloudBase] button in the QQ Mini Program IDE, or through an associated Tencent Cloud account.

You can install and manage this capability via the CloudBase Console.

Using the Program

If your existing business code uses a MySQL connection pool, you may not need it in cloud functions, as each request is distributed to a different cloud function instance. Typically, one MySQL connection per cloud function instance is sufficient.

Based on the user's actual situation, we recommend using mysql and serverless-mysql.

By default, serverless-mysql is a wrapper for mysql that adds connection management capabilities, maintaining only a single connection to the MySQL Server. If you need to use a connection pool in cloud functions, it is recommended to use mysql.

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

Invoke Example

Usage in Cloud Functions:

"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

Operation Mechanism of Cloud Functions

Deep Dive into Cloud Functions - Operation Mechanism of Cloud Functions

Virtual Private Cloud (VPC)

In cloud functions, if developers need to access Tencent Cloud resources such as cloud databases, it is recommended to use a Virtual Private Cloud (VPC) to ensure data security and connection security. For information on VPC, how to set up a VPC, and how to add cloud functions to a VPC, refer to the relevant section in Creating a VPC for TencentDB for MySQL.

Cloud Function Concurrency & MySQL Connections

In the CloudBase Console - Environment Overview, you can view the maximum concurrency allowed for cloud functions in the current environment. The maximum concurrency is also the maximum number of instances for cloud functions.

Regarding MySQL connections, it is recommended to read the article MySQL Connection Handling and Scaling on the official MySQL blog.

Configuration of MySQL connection-related parameters can be performed in the TencentDB for MySQL console -> MySQL -> Database Management -> Parameter Settings.

When using MySQL in cloud functions, each cloud function instance establishes a connection with the MySQL Server. Therefore, the maximum number of connections between this cloud function and MySQL is the maximum connections per instance * the maximum actual concurrency. When configuring the max_connections parameter for MySQL, this value should be greater than the sum of the maximum connections of all cloud functions using this database.

Therefore, when using MySQL in cloud functions, it is recommended to centralize all code that reads from and writes to the same database (not just the same table) into a single cloud function. This approach offers two benefits.

  • Cloud functions have a relatively low probability of experiencing cold starts
  • MySQL has a relatively small maximum number of connections