Skip to main content

MySQL Configuration

Connect to MySQL databases for transactional data, structured queries, and OLTP systems.

Connection Parameters

Required Fields

  • Host: mysql.example.com
  • Port: 3306 (default)
  • Database: Database name
  • Username: Database user
  • Password: User password

Optional Fields

  • Use SSL: Enable for secure connections (recommended for production)

Configuration Example

When creating a MySQL data source, provide the following information:

FieldExample ValueNotes
Nameprod-mysqlUnique identifier
LabelProduction MySQLDisplay name
Hostmysql.example.comDatabase hostname
Port3306Default MySQL port
DatabasemyappDatabase name
Usernameapp_userDatabase user
Password••••••••Encrypted at rest
Use SSL✓ EnabledRecommended

Usage in Applications

Python Example

import os, json
import mysql.connector

# Parse STRONGLY_SERVICES environment variable
services = json.loads(os.getenv('STRONGLY_SERVICES', '{}'))
datasources = services.get('datasources', {})

# Get MySQL data source
mysql_config = datasources['prod-mysql']

# Connect using credentials
conn = mysql.connector.connect(
host=mysql_config['credentials']['host'],
port=mysql_config['credentials']['port'],
database=mysql_config['credentials']['database'],
user=mysql_config['credentials']['username'],
password=mysql_config['credentials']['password'],
ssl_disabled=not mysql_config['credentials'].get('ssl', False)
)

# Execute queries
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE active = 1")
users = cursor.fetchall()

Node.js Example

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

// Parse STRONGLY_SERVICES environment variable
const services = JSON.parse(process.env.STRONGLY_SERVICES || '{}');
const datasources = services.datasources || {};

// Get MySQL data source
const mysqlConfig = datasources['prod-mysql'];

// Create connection
const connection = await mysql.createConnection({
host: mysqlConfig.credentials.host,
port: mysqlConfig.credentials.port,
database: mysqlConfig.credentials.database,
user: mysqlConfig.credentials.username,
password: mysqlConfig.credentials.password,
ssl: mysqlConfig.credentials.ssl ? {} : false
});

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

PHP Example

<?php
// Parse STRONGLY_SERVICES environment variable
$services = json_decode(getenv('STRONGLY_SERVICES'), true);
$datasources = $services['datasources'] ?? [];

// Get MySQL data source
$mysqlConfig = $datasources['prod-mysql'];

// Connect using credentials
$mysqli = new mysqli(
$mysqlConfig['credentials']['host'],
$mysqlConfig['credentials']['username'],
$mysqlConfig['credentials']['password'],
$mysqlConfig['credentials']['database'],
$mysqlConfig['credentials']['port']
);

// Check connection
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
}

// Execute query
$result = $mysqli->query("SELECT * FROM users WHERE active = 1");
$users = $result->fetch_all(MYSQLI_ASSOC);

SSL/TLS Configuration

Enabling SSL

To enable SSL for encrypted connections:

  1. Check the Use SSL checkbox when configuring the data source
  2. Ensure your MySQL server is configured with SSL certificates
  3. Verify the MySQL server requires SSL: SHOW VARIABLES LIKE 'have_ssl';

SSL Certificate Verification

For production environments, you may want to verify SSL certificates:

# Python with certificate verification
import ssl

ssl_context = ssl.create_default_context()
ssl_context.check_hostname = True
ssl_context.verify_mode = ssl.CERT_REQUIRED

conn = mysql.connector.connect(
host=mysql_config['credentials']['host'],
port=mysql_config['credentials']['port'],
database=mysql_config['credentials']['database'],
user=mysql_config['credentials']['username'],
password=mysql_config['credentials']['password'],
ssl_disabled=False,
ssl_ca='/path/to/ca-cert.pem'
)

Common Issues

Connection Refused

  • Verify the host and port are correct
  • Check firewall rules allow connections
  • Ensure MySQL is listening on the correct interface (check bind-address in my.cnf)

Access Denied

  • Verify username and password are correct
  • Check user permissions: SHOW GRANTS FOR 'username'@'host';
  • Ensure the user is allowed to connect from the platform's IP address
  • Grant necessary privileges: GRANT ALL PRIVILEGES ON database.* TO 'username'@'host';

Database Not Found

  • Verify the database name is correct (case-sensitive on some systems)
  • Check if the database exists: SHOW DATABASES;
  • Create the database if needed: CREATE DATABASE myapp;

Too Many Connections

  • Check current connections: SHOW PROCESSLIST;
  • Increase max_connections in my.cnf if needed
  • Use connection pooling in your application

Best Practices

  1. Use SSL in Production: Always enable SSL for production databases
  2. Least Privilege: Create dedicated users with minimal required permissions
  3. Connection Pooling: Use connection pooling for better performance
  4. Character Encoding: Set proper character encoding (UTF-8/utf8mb4)
  5. Monitor Connections: Track connection usage and monitor for connection leaks
  6. Regular Testing: Periodically test connections to ensure they remain valid

Character Set Configuration

For proper Unicode support, ensure your database and tables use utf8mb4:

-- Create database with utf8mb4
CREATE DATABASE myapp
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

-- Convert existing database
ALTER DATABASE myapp
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

-- Create table with utf8mb4
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(255)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;