brianmario/mysql2 — pull request #530 by michaelkruglos (2014-07-30 20:07:30+00:00)
Title: Added call to mysql_library_init during initialization of the gem

This call must be performed before trying to call mysql_init from multiple threads
Reference: http://dev.mysql.com/doc/refman/5.1/en/mysql-init.html
Minimal reproduction of the problem if mysql_library_init is not called
(username and password omitted)

require 'mysql2'

def connect
Mysql2::Client.new()
end

threads = [0,1].map {
Thread.new { connect }
}
threads.map(&:join)
puts "OK!"

The above code fails on version of the gem with error:
/home/michael/.rvm/gems/ruby-1.9.3-p484/gems/mysql2-0.3.16/lib/mysql2/client.rb:70:in `connect': Lost connection to MySQL server at 'reading initial communication packet', system error: 111 (Mysql2::Error)

After the fix, prints OK.