Skip to main content

Direct Connection Service

MySQL database provides connection string method, supporting external services to directly connect to CloudBase MySQL database, meeting developers' data access needs in various scenarios.

By enabling direct connection service, you can obtain the database connection address and directly connect to MySQL database for data operations in CloudRun environment or local development environment. Direct connection service provides the following two connection methods:

  • Internal Address: Only accessible in CloudRun environment, providing high-speed and stable database connection
  • External Address: Accessible in any network environment, suitable for local development and debugging scenarios

⚠️ Note: External connection address is only for development and debugging, please use internal connection for production environment business access to ensure performance and security.

Enable Direct Connection Service

Operation Steps

  1. Visit CloudBase Platform/MySQL Database/Database Settings

MySQL Database-Database Settings-Direct Connection Service Page

  1. In the "Direct Connection Service" module, click the "Enable" button to activate the direct connection function

  2. After enabling, the system will automatically generate internal address and external address

Connection Information Description

Connection TypeAccess RangeUse CaseDescription
Internal AddressCloudRun environment onlyProduction environment business accessProvides high-performance, low-latency database connection
External AddressAny network environmentLocal development, remote debuggingConvenient for development and debugging, can be optionally disabled

Using Connection String

After obtaining the connection address, you can use the standard MySQL connection string format in your application code:

mysql://<username>:<password>@<host>:<port>/<database>

Parameter Description

ParameterDescription
usernameDatabase username (needs to be created in "Account Management" module)
passwordDatabase password
hostDatabase connection address (internal address or external address)
portDatabase port
databaseDatabase name

💡 Tip: If you haven't created a database account yet, please go to the "Account Management" module to create an account and password first

Code Examples

const mysql = require('mysql2/promise');

// Connect using internal address (recommended for production environment)
const connection = await mysql.createConnection({
host: 'your-internal-host.mysql.tencentcdb.com',
port: 3306,
user: 'your-username',
password: 'your-password',
database: 'your-database'
});

// Execute query
const [rows] = await connection.execute('SELECT * FROM users WHERE id = ?', [1]);
console.log(rows);

// Close connection
await connection.end();