talira

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

open sourceMITfree

mysql2 is a Ruby gem for connecting to MySQL and MariaDB, 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.

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 connections1

Alternatives

The same job

These do what mysql2 does.

  1. pg The Ruby interface to PostgreSQL
  2. MongoDB Ruby Driver MongoDB's official Ruby driver
  3. sqlite3-ruby Ruby bindings for the SQLite3 embedded database

Compare mysql2 with the top 3