# mysql2

> Fast MySQL and MariaDB client library for Ruby, binding to libmysqlclient

Source: https://talira.ai/catalog/mysql2-r38appf6
Every listing: https://talira.ai/catalog.md

- Kind: Open source
- Types: Library
- Categories: [libraries / databases / drivers](https://talira.ai/categories/libraries/databases/drivers), [languages / ruby](https://talira.ai/categories/languages/ruby)
- Licence: MIT[^2]
- Pricing: Free and open source[^2]
- Free tier: yes[^2]
- Backed by: [Brian Lopez](https://talira.ai/orgs/brian-lopez.md)

mysql2 is a Ruby gem for connecting to [MySQL](https://talira.ai/catalog/mysql-dy4scprm.md) and [MariaDB](https://talira.ai/catalog/mariadb-server-bxse0j6g.md), binding to libmysqlclient or MariaDB Connector/C. Its API is three classes: a client that connects and queries, results that iterate as arrays or hashes, and prepared statements. It forces UTF-8 or binary encoding on the connection, casts column values to Ruby types (or skips casting for speed), supports async queries, multiple result sets, streaming and TLS with peer verification, and is the driver behind Active Record's mysql2 adapter in [Ruby on Rails](https://talira.ai/catalog/ruby-on-rails-xkdcpj5k.md).

## Links

- GitHub: https://github.com/brianmario/mysql2
- Docs: https://www.rubydoc.info/gems/mysql2

## Problems it solves

- connect a Ruby app to a MySQL database
- run SQL queries against MariaDB from Ruby
- install the MySQL driver my Rails app needs

## Features

- Client, Result and Statement classes for connecting, querying and prepared statements
- Links against libmysqlclient or MariaDB Connector/C
- Results as arrays or hashes, with type casting or raw strings
- Async queries, multiple result sets and row streaming
- TLS with verification modes and certificate fingerprint pinning
- Active Record's mysql2 adapter uses it

## Limitations

- A client's connection can't be shared across fork(): call discard! in the child and open new connections[^1]

## Trust

Overall: 0.47 out of 1.
Signals: maintenance 0.49, adoption 0.67, momentum 0.33, community 0.89, backing 0.30, extensibility 0.00.

- last commit 40 days ago
- 6 commits in the last 90 days
- latest release 0.5.7 14 months ago
- 2.3k GitHub stars
- 169 contributors
- backed by Brian Lopez

## Related

- Works with [MySQL](https://talira.ai/catalog/mysql-dy4scprm.md)
- Works with [MariaDB Server](https://talira.ai/catalog/mariadb-server-bxse0j6g.md)
- Works with [Ruby on Rails](https://talira.ai/catalog/ruby-on-rails-xkdcpj5k.md)

## Alternatives

- [pg](https://talira.ai/catalog/pg-z86bax29.md): The Ruby interface to PostgreSQL
- [MongoDB Ruby Driver](https://talira.ai/catalog/mongodb-ruby-driver-9g75dqb6.md): MongoDB's official Ruby driver
- [sqlite3-ruby](https://talira.ai/catalog/sqlite3-ruby-7nwbmdjq.md): Ruby bindings for the SQLite3 embedded database

## Sources

[^1]: https://github.com/brianmario/mysql2 (checked 28 Nov 2026)
[^2]: https://github.com/brianmario/mysql2/blob/master/LICENSE (checked 28 Nov 2026)
