On this page
Overview
mysql2 is a native-extension Ruby gem for connecting to MySQL-compatible servers. Mysql2::Client, Mysql2::Result, and Mysql2::Statement cover connections, queries, prepared statements, and result iteration. The extension links against MySQL libmysqlclient, Connector/C, or compatible clients such as MariaDB.
Features and best fit
Based on official documentation; not hands-on tested · Content checked:
Key features
Handle connections, queries, and prepared statements through a small API
Mysql2::Client represents a connection, Mysql2::Result represents query output, and Mysql2::Statement represents a prepared statement, avoiding a direct one-to-one exposure of the underlying C API.
Sources: [1]
Configure UTF-8, TLS, and connection options through the client library
Connections use UTF-8 or binary and can configure TLS modes, MySQL option files, sockets, hosts, and other client settings. Available TLS behavior also depends on the linked MySQL or MariaDB client library and server.
Sources: [1]
Stream large results and use asynchronous queries where appropriate
:stream => true can fetch rows on demand, while :async => true sends a query so the application can monitor socket readiness before collecting the result.
Sources: [1]
Best fit
Fits Rails and Ruby applications backed by MySQL-compatible databases
Active Record can use mysql2 as its adapter, while plain Ruby applications can use the Client API directly for MySQL or MariaDB database access.
Sources: [1]
Before adoption
Native builds require a compatible client library and headers
Installation links against MySQL libmysqlclient, Connector/C, or a compatible implementation such as MariaDB. Platform development packages such as libmysqlclient-dev or libmariadb-dev may be required.
Sources: [1]
Account for streaming and asynchronous-query limits
A streaming result must be fully consumed before another query uses the same connection. The README also notes that asynchronous queries are not supported on Windows, so concurrency design may require connection pooling.
Sources: [1]
Official sources
- [1]mysql2 0.5.7 README(2026-10-03)
- [2]mysql2 0.5.7 gemspec(2026-10-03)
- [3]mysql2 0.5.7 release(2026-10-03)
- [4]mysql2 MIT license(2026-10-03)
Supplemental curator note
Compatibility spans both the Ruby API and the MySQL or MariaDB client library used at build and runtime. Pin development headers and runtime libraries consistently in containers and CI to keep native-extension builds reproducible.
Try it in 3 steps
- 1
Install mysql2 0.5.7
Build the pinned stable release in an environment with compatible MySQL or MariaDB client libraries and development headers.
gem install mysql2 -v 0.5.7 - 2
Load the native extension and verify the version
Confirm the gem and native extension load successfully without connecting to a database.
ruby -rmysql2 -e 'puts Mysql2::VERSION' - 3
Inspect the default query options
Load the Client API and inspect defaults such as
:async,:as, and:symbolize_keys. Configure host, credentials, and TLS for the actual environment.ruby -rmysql2 -e 'p Mysql2::Client.default_query_options'
Growth
Growth trends · Last 30 days
2,276 Stars
Trend data is still being collected.
Development activity
Last 90 days · weekly
- Commits (last 30 days)
- 1
- Open PRs
- 33
Development activity is still being collected.
Built with
Categories and tags
Categories
GitHub data
GitHub dataView detailed GitHub data
Related information
Write a related articleShare a guide or use case for this OSS in Markdown. Articles are published after administrator approval.
Explore next
- DuckDB41,885 Stars
2 shared tag(s) · 1 shared category(s)
Run analytical SQL directly over CSV, Parquet, and DataFrames inside the application process without a database server
C++ - MyBatis20,447 Stars
2 shared tag(s) · 1 shared category(s)
keep SQL explicit while mapping query results into Java objects
Java - Flyway10,119 Stars
2 shared tag(s) · 1 shared category(s)
manage database schema changes as versioned migration code with tracked history
Java - Redash28,831 Stars
2 shared tag(s)
query multiple data sources and turn results into visualizations, dashboards, sharing, and alerts
Python - Supabase111,027 Stars
1 shared tag(s) · 2 shared category(s)
Build on Postgres with integrated Auth, REST/GraphQL APIs, Realtime, Storage, and Functions
TypeScript - drawDB39,827 Stars
1 shared tag(s) · 2 shared category(s)
design database schemas in the browser and turn them into SQL and migrations
JavaScript
Report incorrect information
Tell us if any listing information is incorrect or outdated.