Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine mysql2 server version using mysql2 Ruby gem

I'm using Rails and the mysql2 gem. Is there a way to get the mysqld server version as running the command:

$ mysqld --version
mysqld  Ver 5.5.29 for osx10.8 on i386 (Source distribution)

I do not wish to execute a shell command because the database server might be running on another server.

like image 986
Abdo Avatar asked Feb 21 '14 13:02

Abdo


2 Answers

You can get the version info in rails via ActiveRecord::Base.connection. I'm doing it in my rails console here. I'm using an old version (2.2) of rails so the syntax might be different in yours.

irb(main):001:0> ActiveRecord::Base.connection.select_rows(
                   "SHOW VARIABLES LIKE '%version%'"
                 )
=> [
    ["innodb_version", "5.5.34"],
    ["protocol_version", "10"],
    ["slave_type_conversions", ""],
    ["version", "5.5.34-0ubuntu0.12.04.1"],
    ["version_comment", "(Ubuntu)"],
    ["version_compile_machine", "x86_64"],
    ["version_compile_os", "debian-linux-gnu"]
   ]

Once you've got this you can pull out the info you want, eg:

version = ActiveRecord::Base.connection
                            .select_rows("SHOW VARIABLES LIKE 'version'")
                            .last.last
=> "5.5.34-0ubuntu0.12.04.1"
like image 77
Max Williams Avatar answered Sep 28 '22 07:09

Max Williams


def mysql_version
  mysql_version_sql = 'SHOW VARIABLES WHERE Variable_name = "version"'
  ActiveRecord::Base.connection.select_rows(mysql_version_sql)[0][1]
end

mysql_version #=> "5.5.35-0+wheezy1"
like image 26
mdesantis Avatar answered Sep 28 '22 07:09

mdesantis