{"id":249,"date":"2009-04-03T23:52:52","date_gmt":"2009-04-03T18:22:52","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/?p=249"},"modified":"2009-04-03T23:52:52","modified_gmt":"2009-04-03T18:22:52","slug":"error-connection-mysql-ruby-rails-linux-solution","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2009\/04\/error-connection-mysql-ruby-rails-linux-solution\/","title":{"rendered":"Error Connecting to MySQL using Ruby on Rails in Linux &#8211; and its Solution"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.bin-co.com\/blog\/wp-content\/uploads\/2008\/05\/ruby_logo.gif\" alt=\"Ruby Logo\" title=\"Ruby Logo\" width=\"105\" height=\"109\" class=\"alignnone size-full wp-image-105 intro\" align=\"right\" \/><\/p>\n<p class=\"intro\">I am currently working on a new Rails project. Its been a while since I worked with rails &#8211; so I updated Rails using the command &#8216;<code>sudo gem update --system<\/code>&#8216;. I started the inbuilt server and tried to open a page &#8211; and to my surprise, I found that <strong class=\"highlight\">Rails is having trouble connecting to the MySQL<\/strong> server. It was apparent that the <strong class=\"highlight\">mysql gem was missing<\/strong>. So I tried to install it using the command &#8216;<code>sudo gem install mysql<\/code>&#8216;. That failed too &#8211; this is the error I got&#8230;<\/p>\n<pre>Building native extensions.  This could take a while...\nERROR:  Error installing mysql:\n        ERROR: Failed to build gem native extension.\n\n\/usr\/bin\/ruby extconf.rb install mysql\nchecking for mysql_query() in -lmysqlclient... no\nchecking for main() in -lm... yes\nchecking for mysql_query() in -lmysqlclient... no\nchecking for main() in -lz... yes\nchecking for mysql_query() in -lmysqlclient... no\nchecking for main() in -lsocket... no\nchecking for mysql_query() in -lmysqlclient... no\nchecking for main() in -lnsl... yes\nchecking for mysql_query() in -lmysqlclient... no\n*** extconf.rb failed ***\nCould not create Makefile due to some reason, probably lack of\nnecessary libraries and\/or headers.  Check the mkmf.log file for more\ndetails.  You may need configuration options.\n\nProvided configuration options:\n        --with-opt-dir\n        --without-opt-dir\n        --with-opt-include\n        --without-opt-include=${opt-dir}\/include\n        --with-opt-lib\n        --without-opt-lib=${opt-dir}\/lib\n        --with-make-prog\n        --without-make-prog\n        --srcdir=.\n        --curdir\n        --ruby=\/usr\/bin\/ruby\n        --with-mysql-config\n        --without-mysql-config\n        --with-mysql-dir\n        --without-mysql-dir\n        --with-mysql-include\n        --without-mysql-include=${mysql-dir}\/include\n        --with-mysql-lib\n        --without-mysql-lib=${mysql-dir}\/lib\n        --with-mysqlclientlib\n        --without-mysqlclientlib\n        --with-mlib\n        --without-mlib\n        --with-mysqlclientlib\n        --without-mysqlclientlib\n        --with-zlib\n        --without-zlib\n        --with-mysqlclientlib\n        --without-mysqlclientlib\n        --with-socketlib\n        --without-socketlib\n        --with-mysqlclientlib\n        --without-mysqlclientlib\n        --with-nsllib\n        --without-nsllib\n        --with-mysqlclientlib\n        --without-mysqlclientlib\n\n\nGem files will remain installed in \/usr\/lib\/ruby\/gems\/1.8\/gems\/mysql-2.7 for inspection.\nResults logged to \/usr\/lib\/ruby\/gems\/1.8\/gems\/mysql-2.7\/gem_make.out<\/pre>\n<p>After a couple of hours on Google, I managed to fix the issue &#8211; and I am posting the solution here for others who might run into the same problem.<\/p>\n<h2>Install the necessary Libraries<\/h2>\n<p>First, you must <strong class=\"highlight\">install a couple of mysql libraries<\/strong>(I am assuming that you have MySQL server already installed). If you are on <strong class=\"highlight\">Red Hat\/Fedora\/CentOS<\/strong> etc., run the command&#8230;<\/p>\n<pre><code class=\"cli\">sudo yum install ruby-mysql mysql-devel<\/code><\/pre>\n<p>If you are on <strong class=\"highlight\">Debian\/Ubuntu<\/strong>\/etc., run this command&#8230;<\/p>\n<pre><code class=\"cli\">sudo apt-get install libmysqlclient15-dev libmysqlclient15off libmysql-ruby<\/code><\/pre>\n<h2>Install the gem<\/h2>\n<p>Once the installation is done, enter this <strong class=\"highlight\">command to install mysql gem<\/strong>&#8230;<\/p>\n<pre><code class=\"cli\">sudo gem install mysql -- --with-mysql-config=\/usr\/bin\/mysql_config<\/code><\/pre>\n<p>If your mysql_config is not in the standard location, update the command with the right path.<\/p>\n<p>The mysql gem should be installed now. These commands worked for me &#8211; your milage may vary. If you still have trouble, try one of the following links&#8230;<\/p>\n<h2>More Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/rails.aizatto.com\/2007\/05\/19\/connecting-to-mysql-using-ruby\/\">Connecting To MySQL using Ruby<\/a><\/li>\n<li><a href=\"http:\/\/wiki.rubyonrails.org\/database-support\/mysql\">MySQL =&gt; Rails Wiki<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>I am currently working on a new Rails project. Its been a while since I worked with rails &#8211; so I updated Rails using the command &#8216;<code>sudo gem update --system<\/code>&#8216;. I started the inbuilt server and tried to open a page &#8211; and to my surprise, I found that <strong class=\"highlight\">Rails is having trouble connecting to the MySQL<\/strong> server. It was apparent that the <strong class=\"highlight\">mysql gem was missing<\/strong>. So I tried to install it using the command &#8216;<code>sudo gem install mysql<\/code>&#8216;. That failed too &#8211; this is the error I got&#8230;<\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,17,23,24],"tags":[102,124,230,240,242,287],"class_list":["post-249","post","type-post","status-publish","format-standard","hentry","category-linux","category-mysql","category-ruby","category-ruby-on-rails","tag-error","tag-gem","tag-rails","tag-ror","tag-ruby","tag-troubleshoot"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=249"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/249\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}