tencent cloud

Feedback

Connecting SCF to Database

Last updated: 2022-01-23 18:08:52

    Overview

    You can quickly connect to your local or TencentDB databases by writing code in SCF. This document describes how to use an existing SDK to connect to a TencentDB for MySQL database in the SCF function code and perform operations such as insertion and query in the database. TDSQL-C and TDSQL for MySQL databases can also be connected, and you can perform relevant operations as needed.

    Note:

    You can also use Serverless Framework components to deploy databases and functions. For more information, see Serverless Application Center.

    Prerequisites

    • You have registered a Tencent Cloud account and completed identity verification.
    • Interconnect network environments:
      • For self-built databases (non-TencentDB databases), you need to enable public network access first before you can connect to them; otherwise, the connection may fail due to the lack of network connectivity.
      • For TencentDB databases, it is necessary to ensure that the function and database are in the same VPC.

    Directions

    You can follow the steps below to connect to and manage your TencentDB database in the function code.

    Creating VPC

    Note

    You can skip this step for self-built databases.

    Follow the steps below to create a VPC and subnet. For more information, see Building Up an IPv4 VPC.

    1. Log in to the VPC console.
    2. Select the region of the VPC at the top and click +Create.
    3. In the Create VPC pop-up window, enter the VPC information, initial subnet name, and region based on the following information as shown below:
    4. Click OK.

    Creating database instance

    Note

    You can skip this step for self-built databases.

    The following steps take TDSQL-C as an example to describe how to quickly create a MySQL database.

    Note:

    For other types of databases, see corresponding product documents:

    1. Log in to the TDSQL-C purchase page, select the deployment region, AZ, database specification, and other information, and click Buy Now.
    2. After the purchase is completed, you will be redirected to the cluster list. After the status of the cluster becomes Running, it can be used normally as shown below:
    3. Click the cluster ID to enter the cluster details page. You can modify configurations, manage accounts, set security groups, and perform other operations for your database cluster as shown below. For more information, see Managing TDSQL-C Cluster.

    Creating a function

    1. Log in to the SCF console and click Function Service on the left sidebar.
    2. Write your business code and connect to the database through an existing SDK or the SCF DB SDK for MySQL tool encapsulated by SCF by following the normal way of connecting to the database. Here, the Node.js function is used as an example. For other languages, see the function code samples below.
      Note

      To use an existing SDK, you need to install the dependency package first. For more information, see Dependency Installation.

      exports.main_handler = async (event, context, callback) => {
        var mysql      = require('mysql2');
        var connection = mysql.createConnection({
          host     : process.env.HOST,
          user     : process.env.USER,
          password : process.env.PASSWORD
        });
        connection.connect();
        connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
          if (error) throw error;
          console.log('The solution is: ', results[0].solution);
        });
        connection.end();
       }
      
    1. Enter the Function Configuration page of the function and configure the function as shown below:
      1. Add an environment variable and enter the information by referring to the table below:
        key value
        HOST Database address
        USER Database username
        PASSWORD Database password
      2. Enable VPC and select the same VPC and subnet as those of the database as shown below:
    2. After completing the configuration, save it and invoke your function to connect to and manage your database.

    Function code samples

    You can refer to the following code samples to create functions and configure corresponding environmental variables:

    In Python, you can use the built-in pymysql dependency package in the SCF environment to connect to the database. The sample code is as follows:

    # -- coding: utf8 --
    from os import getenv
    import pymysql
    from pymysql.err import OperationalError
    mysql_conn = None
    def __get_cursor():
    try:
    return mysql_conn.cursor()
    except OperationalError:
    mysql_conn.ping(reconnect=True)
    return mysql_conn.cursor()
    def main_handler(event, context):
    global mysql_conn
    if not mysql_conn:
    mysql_conn = pymysql.connect(
    host = getenv('DB_HOST', '<your db="" host="">'),
    user = getenv('DB_USER','<your db="" user="">'),
    password = getenv('DB_PASSWORD','<your db="" password="">'),
    db = getenv('DB_DATABASE','<your db="" database="">'),
    port = int(getenv('DB_PORT','<your db="" port="">')),
    charset = 'utf8mb4',
    autocommit = True
    )

    with __get_cursor() as cursor:
    cursor.execute('select * from employee')
    myresult = cursor.fetchall()
    print(myresult)
    for x in myresult:
    print(x)

    SCF DB SDK for MySQL

    For ease of use, the SCF team encapsulated the code related to connection pools in Node.js and Python as SCF DB SDK for MySQL. With this SDK, you can connect to MySQL, TDSQL-C, or TDSQL for MySQL databases and performs operations such as insertion and query.

    SCF DB SDK for MySQL has the following features:

    • It can automatically initialize the database client from environment variables.
    • It can maintain a persistent database connection globally and handle reconnection after disconnection.
    • The SCF team will continuously check issues to ensure that the database connection is available, so you don't need to pay attention to connection issues.

    The sample code is as follows:

    'use strict';
    const database = require('scf-nodejs-serverlessdb-sdk').database;
    exports.main_handler = async (event, context, callback) =&gt; {
    let pool = await database('TESTDB2').pool()
    pool.query('select * from coffee',(err,results)=&gt;{
    console.log('db2 callback query result:',results)
    })
    // no need to release pool

    console.log('db2 query result:',result)
    }
    Note:

    1. Python 3.6, Python 2.7, Node.js 12.16, and Node.js 10.15 have built-in SCF DB SDK for MySQL, so no additional installation is required.
    2. For other Node.js versions, please refer to Dependency Installation to install scf-nodejs-serverlessdb-sdk.
    3. For specific usage of the SDK for Node.js, see SCF DB SDK for MySQL.

    FAQs

    How do I manage database connections more efficiently under the operating mechanism of SCF?

    • Each SCF request actually runs on a container that can be reused for a period of time when there are continuous requests. A database connection is better to be established when the container is initialized, i.e., corresponding to the global part of the function code. After the database connection is established, it can be reused during the existence of the container and will be closed when the container is released. Please avoid frequent database connections and disconnections inside the entry function, as they affect the performance. To ensure the database connection availability, a connection check can be performed inside the entry function.
    • We recommend you use the database connection pool for container database connection management and set the minimum number of connections to 1.

    How do I perform database connection management in high-concurrency scenarios?

    In high function concurrency scenarios, the number of concurrent connections may exceed the maximum number of database connections. You can refer to the following solutions for handling:

    • Increase the maximum number of database connections.
    • Set the maximum dedicated concurrency quota for functions and limit the number of concurrent function connections to be less than the maximum number of database connections.
    • TencentDB for MySQL provides the database proxy feature. Requests arriving at the proxy address are all relayed through the proxy cluster to access the source and replica nodes of the database. Read/Write separation is implemented, so that read requests are forwarded to read-only instances, which lowers the load of the source database.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support