In Rails 8.1, how do the mysql2 and trilogy adapters differ for a MySQL or MariaDB app, and what changes when you switch?
answer
- built in since Rails 7.1
- no libmysqlclient to compile against
- prepared_statements: true raises on trilogy
- same AbstractMysqlAdapter underneath
- adapter: line and the Gemfile
basics
~20 sBoth talk to MySQL and MariaDB through the same AbstractMysqlAdapter, so SQL, strict mode and schema behaviour match. mysql2 wraps the C libmysqlclient; trilogy is a self-contained client, so builds need no MySQL dev headers, and it refuses prepared_statements: true.
solid answer
~40 s`mysql2` binds the MySQL C client library, so the build needs `libmysqlclient` headers (the generated Dockerfile installs `default-libmysqlclient-dev` for it). `trilogy`, a built-in Rails adapter since 7.1, is a MySQL-protocol client written in C with no dependency beyond the C library and OpenSSL, so the generator lists no build package for it. Both subclass `AbstractMysqlAdapter`, which owns `strict:`, `variables:`, `encoding:`, schema statements and error translation, so models and migrations behave the same. Differences are at the driver edge: Trilogy raises `ArgumentError` if `prepared_statements: true` is set (both default to false), and it does not support `LOAD DATA LOCAL INFILE`. Switching is a Gemfile change (`gem "trilogy"`) plus `adapter: trilogy` in database.yml, then running the test suite and any raw SQL against it.
go deeper
Recall that mysql2 and trilogy are both Rails adapters for MySQL and that switching means the gem and the adapter: line.
Explain the shared AbstractMysqlAdapter, the libmysqlclient dependency and the prepared-statements difference.
De-risk a driver switch: grep for Mysql2 constants and raw connections, run the suite, canary the deploy, check bulk loads.
Weigh build simplicity and driver maintenance against migration effort when standardising drivers across services.
## Two drivers, one adapter family Rails talks to MySQL-compatible servers through **adapters**, and since Rails 7.1 there are two built in: | | `mysql2` | `trilogy` | |---|---|---| | Gem | `mysql2` (`~> 0.5` in the generator) | `trilogy` (`~> 2.7` in the generator) | | Client underneath | the MySQL C client library, `libmysqlclient` | Trilogy's own C client, no `libmysqlclient` | | Build packages in the generated Dockerfile | `default-libmysqlclient-dev` | none | | Runtime package | `default-mysql-client` | `default-mysql-client` | | `prepared_statements: true` | allowed (default false) | raises `ArgumentError` | | `LOAD DATA LOCAL INFILE` | available through the C client | not supported by Trilogy | | `rails new -d` values | `mysql`, `mariadb-mysql` | `trilogy`, `mariadb-trilogy` | Both adapter classes, `Mysql2Adapter` and `TrilogyAdapter`, inherit from `AbstractMysqlAdapter`. That parent holds almost everything an application notices: how `database.yml` keys such as `strict:`, `variables:`, `encoding:` and `collation:` become one session `SET` statement, how migrations are turned into SQL, how MySQL error codes become `ActiveRecord::RecordNotUnique`, `ValueTooLong` or `Deadlocked`, and how `mariadb?` detects a MariaDB server from its version string. The child classes mostly handle connecting, running queries and reading results. ## Why teams pick Trilogy - **Simpler builds.** Without `libmysqlclient` there are no development headers to install and no mismatch between the headers a gem was compiled against and the library present at run time, a classic source of segfaults and failed deploys with `mysql2`. - **Fewer moving parts.** Trilogy depends only on POSIX, the C standard library and OpenSSL, and its Ruby binding is maintained in the Trilogy repository itself. ## What to check before switching 1. **Prepared statements.** If `database.yml` sets `prepared_statements: true`, Trilogy refuses to start: "Trilogy currently doesn't support prepared statements." Both adapters default to false, so most apps never set it. 2. **Driver-specific code.** Anything that reaches the raw connection (`with_connection { |c| c.raw_connection }`) gets a `Trilogy` object instead of a `Mysql2::Client`, with a different API. Search for `Mysql2::` in the codebase, including `rescue Mysql2::Error` clauses that should rescue Active Record's own error classes instead. 3. **Bulk loads.** Imports that use `LOAD DATA LOCAL INFILE` need another path, such as batched inserts. 4. **Socket versus host.** The Trilogy adapter drops `host` when `socket` is set so a UNIX socket in `DATABASE_URL` wins; check connection settings in each environment. The switch itself: ```ruby # Gemfile gem "trilogy", "~> 2.7" # replaces gem "mysql2" ``` ```yaml default: &default adapter: trilogy encoding: utf8mb4 ``` ## MariaDB with either adapter MariaDB speaks the same protocol, so both adapters connect to it, and `rails new` offers `mariadb-mysql` and `mariadb-trilogy` to generate the matching config. Inside the adapter, `mariadb?` switches feature checks. Two that matter to a hotel-reservation app: - MariaDB 10.5 and later supports `INSERT ... RETURNING`, so `insert_all` with `returning:` works there but raises `ArgumentError` on MySQL; - the adapter reports no native JSON type for MariaDB, where JSON is stored as text. ## Rolling the switch out 1. Change the Gemfile and `adapter:` on a branch and run the whole test suite against a real MySQL server, not a stand-in. 2. Grep for driver constants and raw-connection calls, and replace driver error classes with Active Record's (`ActiveRecord::RecordNotUnique`, `ActiveRecord::ConnectionNotEstablished`). 3. Deploy to one server or a canary first and compare error rates and query latency. 4. Keep the old gem out of the Gemfile once the switch is done, so nobody reaches for its API again. ## How to answer in an interview Lead with the shared parent: changing the driver does not change Active Record's behaviour. Then name the concrete differences (build dependency, prepared statements, raw connection API) and how you would de-risk the change: a branch with the new adapter, the full test suite, and a canary deploy watching error rates.
- A Rails app switched from mysql2 to trilogy and now fails at boot with an ArgumentError about prepared statements; what happened?Its database.yml sets `prepared_statements: true`. The Trilogy adapter checks for it in its initializer and raises "Trilogy currently doesn't support prepared statements". Remove the key: both MySQL adapters default to false, and Rails falls back to quoting values into the SQL.
- Why does switching the MySQL driver not change strict mode, collation or error classes in Rails?`Mysql2Adapter` and `TrilogyAdapter` both inherit from `AbstractMysqlAdapter`, which builds the session SET statement from `strict:`, `variables:`, `encoding:` and `collation:`, and translates MySQL error numbers into Active Record exceptions. The driver change sits below that layer.
- Thinks trilogy needs libmysqlclient development headers to build
- Expects Active Record to behave differently on trilogy for the same SQL
- Keeps prepared_statements: true when moving to trilogy
- Rescues Mysql2::Error in app code instead of Active Record errors
- Believes MariaDB needs a separate adapter class in Rails
Assumes Rails 8.1.4 with trilogy 2.13 and mysql2 0.5; Trilogy has been a built-in Active Record adapter since Rails 7.1.