Skip to main content

Overview

MySQL dialect that uses the mysql2 library. This dialect works with both MySQL and MariaDB databases. Source: src/dialect/mysql/mysql-dialect.ts:43

Installation

Install the required peer dependency:

Basic Usage

Configuration

MysqlDialectConfig

Source: src/dialect/mysql/mysql-dialect-config.ts:8
pool
MysqlPool | (() => Promise<MysqlPool>)
required
A mysql2 Pool instance or a function that returns one.If a function is provided, it’s called once when the first query is executed. This allows for lazy initialization of the connection pool.See mysql2 pool documentation for available pool options.
onCreateConnection
(connection: DatabaseConnection) => Promise<void>
Called once for each created connection. Useful for setting up connection-level configuration.
onReserveConnection
(connection: DatabaseConnection) => Promise<void>
Called every time a connection is acquired from the connection pool. Use this for per-query setup.

Connection Pooling

The MySQL dialect uses the mysql2 library’s built-in connection pooling. Here are recommended pool settings:

SSL/TLS Configuration

To connect using SSL:

Streaming Results

The MySQL dialect supports streaming large result sets:
You can also configure stream options:

Character Set and Collation

Configure character set and collation for your connection:

Timezone Handling

MySQL connections default to the server’s timezone. To use UTC:
Or set it per connection:

Multiple Statements

To execute multiple statements in a single query:
Enabling multiple statements can expose you to SQL injection vulnerabilities if you’re not careful with user input. Only enable this if you need it and understand the security implications.

MariaDB Compatibility

The MysqlDialect works with MariaDB databases. Simply point it to your MariaDB server:

MysqlPool

Source: src/dialect/mysql/mysql-dialect-config.ts:37 The subset of the mysql2 Pool interface that Kysely requires:

MysqlPoolConnection

Source: src/dialect/mysql/mysql-dialect-config.ts:44

MysqlOkPacket

Source: src/dialect/mysql/mysql-dialect-config.ts:66 Result type for INSERT, UPDATE, and DELETE queries: