Skip to main content

MySQL

The MySQL query runner allows you to connect to and query MySQL and MariaDB databases. It also includes a specialized runner for Amazon RDS MySQL instances.

Configuration

The MySQL query runner supports a comprehensive configuration to connect to MySQL databases:

{
"host": "127.0.0.1",
"port": 3306,
"user": "username",
"passwd": "password",
"db": "database_name",
"connect_timeout": 60,
"charset": "utf8",
"use_unicode": true,
"autocommit": true,
"use_ssl": false,
"ssl_mode": "preferred",
"ssl_cacert": "/path/to/ca.pem",
"ssl_cert": "/path/to/client-cert.pem",
"ssl_key": "/path/to/client-key.pem"
}

Configuration Schema

ParameterTypeRequiredDescription
hostStringNoMySQL server hostname or IP address (default: "127.0.0.1")
portNumberNoMySQL server port (default: 3306)
userStringNoUsername for authentication
passwdStringNoPassword for authentication
dbStringYesDatabase name to connect to
connect_timeoutNumberNoConnection timeout in seconds (default: 60)
charsetStringNoCharacter set (default: "utf8")
use_unicodeBooleanNoWhether to use Unicode (default: true)
autocommitBooleanNoWhether to autocommit transactions (default: true)
use_sslBooleanNoWhether to use SSL for connection
ssl_modeStringNoSSL mode (options: "disabled", "preferred", "required", "verify-ca", "verify-identity")
ssl_cacertStringNoPath to CA certificate file
ssl_certStringNoPath to client certificate file
ssl_keyStringNoPath to client key file

Amazon RDS MySQL Configuration

For Amazon RDS MySQL instances, you can use the rds_mysql type, which has a simplified configuration:

{
"host": "myinstance.abcdefghijkl.us-west-2.rds.amazonaws.com",
"port": 3306,
"user": "username",
"passwd": "password",
"db": "database_name",
"use_ssl": true
}

RDS MySQL Configuration Schema

ParameterTypeRequiredDescription
hostStringYesRDS instance endpoint
portNumberNoMySQL server port (default: 3306)
userStringYesUsername for authentication
passwdStringYesPassword for authentication
dbStringYesDatabase name to connect to
use_sslBooleanNoWhether to use SSL for connection

Example Usage

from legion_query_runner.query_runner import QueryRunner

# Initialize with MySQL configuration
mysql_config = {
"host": "mysql.example.com",
"port": 3306,
"user": "username",
"passwd": "password",
"db": "mydatabase"
}

# Create a QueryRunner instance
runner = QueryRunner('mysql', mysql_config)

# Test connection
runner.test_connection()

# Execute a query
results = runner.run_query("SELECT * FROM users LIMIT 10")

Type Mapping

The MySQL query runner maps MySQL data types to standardized types:

MySQL Type CodeLegion Type
0, 4, 5, 246FLOAT
1, 2, 3, 8, 9, 16INTEGER
7, 12DATETIME
10DATE
15, 253, 254STRING

Schema Retrieval

The MySQL query runner extracts schema information from the INFORMATION_SCHEMA views:

# Get schema for all tables
schema = runner.get_schema()

# Get columns for a specific table
columns = runner.get_table_columns("users")

# Get column types for a specific table
column_types = runner.get_table_types("users")

Implementation Details

The MySQL query runner uses the MySQLdb (also known as mysqlclient) library to connect to MySQL databases. This library must be installed for the query runner to be enabled.

To install the required dependency:

pip install mysqlclient

The query runner also includes support for:

  • Thread management for query execution
  • Query cancellation
  • Cross-database queries
  • SSL connections
  • Connection pooling