SQLx’s MySQL driver is written in pure Rust and speaks the MySQL client/server protocol directly. It supports TLS, the full range of MySQL built-in types, JSON columns, and works with both MySQL (5.7+) and MariaDB (10.2+). The driver is runtime-agnostic and can be used withDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt
Use this file to discover all available pages before exploring further.
tokio or async-std.
Feature flag
Enable the driver by adding themysql feature to your Cargo.toml. You must also choose a runtime and, optionally, a TLS backend.
RSA password authentication (
caching_sha2_password / sha256_password) without TLS requires the separate mysql-rsa feature. Enable it only if you must connect without TLS to a server that requires RSA auth. Prefer TLS instead.Connecting
Connection URL
MySqlConnectOptions
For programmatic configuration, useMySqlConnectOptions:
Key options
| Option | Default | Description |
|---|---|---|
host | localhost | Hostname or Unix socket path |
port | 3306 | TCP port |
username | root | Login user |
password | None | Password |
database | None | Target database |
ssl_mode | Preferred | TLS negotiation mode (see below) |
charset | utf8mb4 | Connection character set |
collation | Derived from charset | Connection collation |
timezone | +00:00 | Session timezone (set to None to use server default) |
statement_cache_capacity | 100 | LRU prepared-statement cache size per connection |
TLS and MySqlSslMode
MySqlSslMode controls TLS negotiation. Pass it to .ssl_mode() on MySqlConnectOptions or set ssl-mode= in the URL.
| Variant | Behaviour |
|---|---|
Disabled | Never use TLS |
Preferred | Try TLS first; fall back to plain (default) |
Required | Require TLS; do not verify the server certificate |
VerifyCa | Require TLS; verify the server certificate is signed by a trusted CA |
VerifyIdentity | Like VerifyCa, plus hostname verification |
Using
rustls requires TLS 1.2 or later. MySQL has supported TLS 1.2 since version 5.6.46. If you see HandshakeFailure, switch to the tls-native-tls feature for broader version support.Connection pooling
MySqlPool (an alias for Pool<MySql>) manages a pool of connections.
- URL
- MySqlPoolOptions
Querying
MySQL uses? as the bind parameter placeholder (unlike PostgreSQL’s $1):
JSON support
Enable thejson feature to map JSON columns to serde_json::Value:
Type mapping
| MySQL / MariaDB type | Rust type |
|---|---|
TINYINT(1) / BOOL | bool |
TINYINT | i8 |
SMALLINT | i16 |
INT / INTEGER | i32 |
BIGINT | i64 |
TINYINT UNSIGNED | u8 |
SMALLINT UNSIGNED | u16 |
INT UNSIGNED | u32 |
BIGINT UNSIGNED | u64 |
FLOAT | f32 |
DOUBLE | f64 |
VARCHAR / TEXT / CHAR | String / &str |
VARBINARY / BLOB / BINARY | Vec<u8> / &[u8] |
JSON | serde_json::Value (feature: json) |
DECIMAL / NUMERIC | bigdecimal::BigDecimal (feature: bigdecimal) |
DATETIME | chrono::NaiveDateTime (feature: chrono) |
DATE | chrono::NaiveDate (feature: chrono) |
TIME | chrono::NaiveTime (feature: chrono) |
TIMESTAMP | chrono::DateTime<Utc> (feature: chrono) |
UUID (as CHAR(36)) | uuid::Uuid (feature: uuid) |
MariaDB compatibility
The MySQL driver is compatible with MariaDB 10.2 and later. MariaDB’s wire protocol is a fork of MySQL’s, and most features work transparently. A few notes:- MariaDB uses its own authentication plugins. If you encounter auth errors, ensure your server version is in SQLx’s supported range (check the CI configuration for tested versions).
- The
NO_ENGINE_SUBSTITUTIONSQL mode is enabled by default. Disable it with.no_engine_substitution(false)if your MariaDB instance does not support it. - For
sql_modedetails, see the MariaDB documentation.