Install mysql2 -v '0.3.19' via bundle (Gemfile) on rails - mysql

I have an error while trying to 'bundle install' when listing 'mysql2' on the Gemfile on OSX Lion. I'm currenly building an app with Rails. If I run 'bundle install', it hangs forever. First I tried to fix the issue by modifying the gem file to 'gem 'mysql2', :git => 'git://github.com/sodabrew/mysql2.git', :ref => 'a2800f'', as suggested here: https://github.com/brianmario/mysql2/pull/654. The infinite loop got fixed, I ran 'bundle install' sucessfully but when I run 'rake db:create', I got this error:
rake aborted!
LoadError: dlopen(/Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle
Reason: image not found - /Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle
/Users/macbook/Desktop/tutorial/billingleap2/moviestore/config/application.rb:7:in `<top (required)>'
/Users/macbook/Desktop/tutorial/billingleap2/moviestore/Rakefile:4:in `<top (required)>'
I also try to 'brew install mysql' and it worked, but then on 'rake db:create', I got this error:
#<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"moviestore_development"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"}
(If you set the charset manually, make sure you have a matching collation)
#<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8","pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"moviestore_test"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"}
(If you set the charset manually, make sure you have a matching collation)
So then I 'brew uninstall mysql' and that problem was gone, but now I have the first one.
Trying 'gem install mysql2 -v '0.3.19'' also hangs.
What could be the problem?

As you are using bundler, you have to fix the first issue (why bundle install is failing) and install the mysql2 gem using bundler. There must have some gem dependency issue that you need to look at.
And, you should run: bundle exec rake db:migrate as you are using bundler and Gemfile etc. that will ensure your rake task runs in the context of the current project.
Update: 1
If you already ran: bundle install successfully and installed the mysql2 gem successfully, then try running:
`bundle exec rake db:create`
If this causes you the specified error (which means your libmysqlclient library is not loading for some reason):
rake aborted!
LoadError: dlopen(/Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle
Reason: image not found - /Users/macbook/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/bundler/gems/mysql2-a2800f86754b/lib/mysql2/mysql2.bundle
/Users/macbook/Desktop/tutorial/billingleap2/moviestore/config/application.rb:7:in `<top (required)>'
/Users/macbook/Desktop/tutorial/billingleap2/moviestore/Rakefile:4:in `<top (required)>'
then you should look at this answer and this one too which have some prescribed solutions for similar issue. Try those see if that works.
Update: 2
So, mysql gem has some development dependencies which needs to be installed in your system before you can actually use mysql. Do the following:
sudo gem install mysql-server mysql-client
sudo gem install libmysql-ruby libmysqlclient-dev
sudo gem install mysql
And, then try again:
bundle install
Update: 3
First, to find your socket file:
mysqladmin variables | grep socket
It will give you something like this:
| socket | /tmp/mysql.sock
Then, add a line to your config/database.yml:
development:
adapter: mysql2
host: localhost
username: root
password: xxxx
database: xxxx
socket: /tmp/mysql.sock

Related

Ruby on rails "NameError: uninitialized constant Mysql2::Client::REMEMBER_OPTIONS"

I am trying to learn web development with ruby on rails. I have been following a course on Lynda.com and my steps were as follows:
I installed Ruby + DevKit 2.4.4-1(x64)
I installed rails as in the command line as follows
gem install rails --version 5.0.0
I installed MySQL version 5.7.21
I installed mysql2 gem as follows
gem install mysql2
I installed Atom text editor
I created a new project rails new kudaweb -d mysql
I created the required databases in the MySQL shell
CREATE DATABASE kudaweb_development; and CREATE DATABASE kudaweb_test;
I then created a new user and granted all privileges as follows
GRANT ALL PRIVILEGES ON kudaweb_development.* TO 'rails_user'#'localhost' IDENTIFIED BY 'mypassword'
and i did this for the test database as well
I configured the database.yml file as follows:
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: rails_user
password: mypassword
host: localhost
I ran the following code to check if I had correctly configured the database
rails db:schema:dump
and I got the following error
C:\Users\Dj K-Staxx\Desktop\RUBY\kudaweb>rails db:schema:dump
rails aborted!
NameError: uninitialized constant Mysql2::Client::REMEMBER_OPTIONS
bin/rails:4:inrequire'
bin/rails:4:in <main>'
Tasks: TOP => db:schema:dump
I have no idea how to resolve this. I have searched the internet for solutions but to no avail. I am using windows 7 64bit and for the mysql2 gem I am using version 0.5.0
Seems like there may be some config issue between your 0.5.0 gem and mysql. It may not have built correctly. see: https://github.com/brianmario/mysql2/issues/954
Option 1: try reverting to 0.4.9 or 0.4.10 gem
set your gemfile to:
# ./Gemfile
...
gem 'mysql2', '0.4.9'
...
Option 2: try to compile the gem locally with c-connector
You may need a local C-connector to properly build the gem locally. see: https://www.digitalgyan.org/how-to-install-ruby-on-rails-mysql2-gem-on-windows-10/
that may be as simple as downloading the appropriate files from MySQL: https://dev.mysql.com/downloads/connector/c/
In either scenario, you may benefit from a quick script to test out connection (to rule out Rails as the problem).
require 'mysql2'
client = Mysql2::Client.new(host: "localhost", username: "rails_user", password: "mypassword")
update mysql2 gem in the Gemfile as:
gem 'mysql2', '~> 0.5.2'

Getting this error when trying to create a DB with rake db:create on google could SQL server

I get this error when I run rake db:create
root#guidir-smaill-group-1-po7p:/home/acc/site/site# RAILS_ENV=production rake db:create
#<Mysql2::Error: Lost connection to MySQL server at 'reading initial communication packet', system error: 0>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>"pass", "host"=>"173.144.230.182", "database"=>"dbname"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"}
(If you set the charset manually, make sure you have a matching collation)
The error seems fairly common and I've read a bunch of posts here on SO on how that fixed it. But they all include removing and re installing MySQL with a command like these
export ARCHFLAGS="-arch i386 -arch x86_64" gem install mysql -- --with-mysql-dir=/usr/local \ --with-mysql-config=/usr/local/bin/mysql_config
sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Problem is that im using an SQL instance on google cloud and I dont want to uninstall the mysql setup they have there. How can I get the same result without re installing mysql so I can run rake db:create successfully?

Ruby on Rails : MySQL client library issue when trying to create database

I have a rails application which is running on Rails 3.2.8 and Ruby 1.9.3.I am also using Mysql server as database.
When I run rake db:migrate, I get the following error:
rake aborted!
Incorrect MySQL client library version! This gem was compiled for 6.0.0 but the
client library is 5.5.34.
C:/Rails SampleApps/MyAppName/config/application.rb:7:in `<top (required)>'
C:/Rails SampleApps/MyAppName/Rakefile:5:in `require'
C:/Rails SampleApps/MyAppName/Rakefile:5:in `<top (required)>'
(See full trace by running task with --trace)
How do I solve this issue?
Any help is appreciated..
Since you are using windows, you have two options to solve this :
Either use 2.8.1 version of mysql gem by writing
gem 'mysql', '2.8.1' #in your gemfile
run
bundle install
OR
Simply use mysql2 gem
gem 'mysql2'
and put the libmysql.dll file in installed MySQL bin directory with the one which you can get from the below link in location lib/libmysql.dll from
http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick
gem 'mysql2' ## gemfile
rm Gemfile.lock
bundle install
rake db:drop && rake db:create

issue in installing mysql2 gem with rails3 on mac

Installing mysql2 (0.2.6) with native extensions /Library/Ruby/Site/1.8/rubygems/installer.rb:483:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework /Versions/1.8/usr/lib/ruby/ruby.h
Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/ext/mysql2/gem_make.out
from /Library/Ruby/Site/1.8/rubygems/installer.rb:446:in `each'
from /Library/Ruby/Site/1.8/rubygems/installer.rb:446:in `build_extensions'
from /Library/Ruby/Site/1.8/rubygems/installer.rb:198:in `install'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/source.rb:96:in `install'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/installer.rb:55:in `run'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/spec_set.rb:12:in `each'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/spec_set.rb:12:in `each'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/installer.rb:44:in `run'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/installer.rb:8:in `install'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/cli.rb:226:in `install'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor/task.rb:22:in `send'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor/task.rb:22:in `run'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor/invocation.rb:118:in `invoke_task'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor.rb:246:in `dispatch'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/lib/bundler/vendor/thor/base.rb:389:in `start'
from /Library/Ruby/Gems/1.8/gems/bundler-1.0.10/bin/bundle:13
from /usr/bin/bundle:19:in `load'
from /usr/bin/bundle:19
EDIT
Mohit-Jains-MacBook-Pro:casecreed mohit$ locate mysql_config
/Applications/XAMPP/xamppfiles/bin/mysql_config
/usr/local/mysql-5.5.9-osx10.6-x86_64/bin/mysql_config
/usr/local/mysql-5.5.9-osx10.6-x86_64/man/man1/mysql_config.1
and after doing what u suggested...
ie
sudo gem install mysql2 -- –with-mysql-config=/usr/local/mysql-5.5.9-osx10.6-x86_64/bin/mysql_config
here is the error..
Mohit-Jains-MacBook-Pro:casecreed mohit$ rake db:create
(in /Users/mohit/projects/casecreed)
rake aborted!
dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.16.dylib
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
/Users/mohit/projects/casecreed/Rakefile:4
(See full trace by running task with --trace
EDIT2
screenshot of whole process after answer was modified..
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Couldn't create database for {"reconnect"=>false, "encoding"=>"utf8", "username"=>"root", "adapter"=>"mysql2", "database"=>"the_casecreed_test", "host"=>"localhost", "pool"=>5, "password"=>nil}, charset: utf8, collation: utf8_unicode_ci
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Couldn't create database for {"reconnect"=>false, "encoding"=>"utf8", "username"=>"root", "adapter"=>"mysql2", "database"=>"the_casecreed_development", "host"=>"localhost", "pool"=>5, "password"=>nil}, charset: utf8, collation: utf8_unicode_ci
EDIT3
development.yml
EDIT4
Even not able to connect to mysql.
Check this screenshot.
EDIT5
This is the solution but i am not able to understand, why mysql server was not starting..
Screenshot
Try the following, which seems to have worked for others:
From the Terminal, run locate mysql_config and identify the path to the mysql_config binary
Use that path in the following command to install the gem:sudo gem install mysql2 -- –with-mysql-config=/path/you/identified/above/mysql_config
[Edit]
In response to the new error, e.g.:
Mohit-Jains-MacBook-Pro:casecreed mohit$ rake db:create
(in /Users/mohit/projects/casecreed)
rake aborted!
dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.16.dylib
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
/Users/mohit/projects/casecreed/Rakefile:4
(See full trace by running task with --trace
I believe this is due to mysql2 not using the full path to libmysqlclient.16.dylib. Others have had luck with:
sudo install_name_tool -change libmysqlclient.16.dylib /usr/local/mysql/lib/libmysqlclient.16.dylib /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
However, be sure to replace /usr/local/mysql/lib/libmysqlclient.16.dylib with the path to your own libmysqlclient.16.dylib. I'm not positive what it would be, but you may be able to find it with locate (if not, check around in the lib directory for /usr/local/mysql-5.5.9-osx10.6-x86_64).
download 64 bit from here and then try to install mysql2 gem
http://dev.mysql.com/downloads/mysql/
it will work..
Try installing mysql from macports
sudo ports install mysql5
It took ages to complete, but once done run
gem install mysql2
and everything should work just fine

rake aborted! uninitialized constant Mysql2

Goksel-Eryigits-Mac-mini:blog geryit$ rake db:migrate --trace
(in /Users/geryit/Sites/blog)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
Client does not support authentication protocol requested by server; consider upgrading MySQL client
/Users/geryit/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/mysql.rb:453:in `read'
/Users/geryit/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/mysql.rb:130:in `real_connect'
/Users/geryit/.rvm/gems/ruby-1.8.7-p330/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:600:in `connect'
/Users/geryit/.rvm/gems/ruby-1.8.7-p330/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:164:in `initialize
.
.
.
database.yml
development:
adapter: mysql
encoding: utf8
database: test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
Have you defined
gem 'mysql2'
in Gemfile? For Rails 3 and Bundler, it's not enough to install the gem, but to include it in Gemfile.
They work together now without any problems. The problem was Mysql version and mysql gem flags. I removed MYSQL 5.5 and installed Mysql 5.1 back. Everything is fixed. Thanks.
I wrote a blog post about installing, it can be helpful : https://geryit.com/blog/installing-mysql-with-rails-on-mac-os-x-snow-leopard/
Try uninstalling the mysql gem. It might be conflicting with the mysql2 gem you're using in your database.yml.
I was getting 'uninitialized constant Mysql' on Snow Leopard and Rails 3. Adding 'mysql' gem to Gemfile fixed it for me.