Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the difference between mysql and mysql2 gem

I used to work with the mysql gem, but often nowadays I hear about the mysql2 gem. Also, it is included in the Gemfile by default in Rails 3.0.x.

What is the difference and advantages of using the mysql2 gem?

like image 718
fl00r Avatar asked Mar 23 '11 20:03

fl00r


People also ask

What is MySQL gem?

The mysql2 Ruby gem provides an API for connecting to MySQL, performing queries, and iterating through results; it is intended to support MySQL 5.6, 5.7, and 8.0.

What is the use of mysql2?

MySQL has turned out to be one of the most popular open-source databases used by organizations for web development. It is the central component of LAMP, which is a software stack model that facilitates building web applications and websites.

What is mysql2?

MySQL2 project is a continuation of MySQL-Native. Protocol parser code was rewritten from scratch and api changed to match popular mysqljs/mysql. MySQL2 team is working together with mysqljs/mysql team to factor out shared code and move it under mysqljs organisation.


2 Answers

Here's a quote from the mysql2 gem page:

Yeah… but why?

Someone: Dude, the Mysql gem works fiiiiiine.

Me: It sure does, but it only hands you nil and strings for field values. Leaving you to convert them into proper Ruby types in Ruby-land - which is slow as balls.

Someone: OK fine, but do_mysql can already give me back values with Ruby objects mapped to MySQL types.

Me: Yep, but its API is considerably more complex and can be ~2x slower.

like image 184
ALoR Avatar answered Oct 04 '22 14:10

ALoR


mysql2 is very much faster than the mysql gem. Also, apart from being effective, it also has more features and is more fun, just like Ruby on Rails.

like image 38
Nikhil Avatar answered Oct 04 '22 12:10

Nikhil