0

My gemfile:

source 'https://rubygems.org'
gem 'rails', '4.2.8'
gem 'bootstrap-sass', '~> 3.3.7'
gem 'font-awesome-rails'
gem 'sqlite3'
gem 'mysql2'
gem 'sass-rails', '~> 5.0.4'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
  gem 'sdoc', require: false
end

my database config:

default: &default
  adapter: mysql2
  pool: 25
  timeout: 5000
  encoding: utf8

development:
  <<: *default
  host: 127.0.0.1
  database: asteriskcdrdb
  username: root
  password: pwd3lfga
  port: 13306

In console I have this error after I launch the console and try to read from a table inside db:

Mysql2::Error: Malformed packet
    from /Users/michele/.rvm/gems/ruby-2.2.2/gems/mysql2-0.4.6/lib/mysql2/client.rb:89:in `connect'
    from /Users/michele/.rvm/gems/ruby-2.2.2/gems/mysql2-0.4.6/lib/mysql2/client.rb:89:in `initialize'
    from /Users/michele/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.8/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new'
michael
  • 147
  • 2
  • 19
  • Did you tried this solution [1]? Added :local_infile => true as an option. [1] https://stackoverflow.com/questions/8790874/load-data-local-infile-causes-malformed-packet-error-with-mysql2-gem – tingel2k May 05 '17 at 11:33
  • @tingel2k where put that option? in database.yml? – michael May 05 '17 at 13:35
  • tryed by console by this command: a = Mysql2::Client.new(:username=>'root', :host=>'127.0.0.1', :password=>'pwd3lfga', :database=>'asteriskcdrdb', :local_infile => true, :port=>13306) Output Of Rails Console: Mysql2::Error: Malformed packet – michael May 05 '17 at 13:46
  • Just a curious question: Your mysql port is really 13306? – tingel2k May 10 '17 at 09:25
  • @tingel2k do not look to ports or hostname, it's all correct ;) – michael May 10 '17 at 14:12
  • Which datatype are you trying to retrieve? A blob for instance could lead to this error message see -> https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_max_allowed_packet. Also please check the libmysql version on the client and compare it to the server version see -> https://github.com/brianmario/mysql2/issues/715 – tingel2k May 18 '17 at 10:04

1 Answers1

0

Turns out this is simply solved by re-installing the mysql2 gem. I think I had installed it with a MySQL version compiled from source, and then later switched to MySQL installed via brew, which led to an incompability in the client code used by the gem. That explains why it did work from the command line client but not when using the gem.

gem uninstall mysql2
gem install mysql2

and

Remove gem 'sqlite3' from Gemfile.

puneet18
  • 4,753
  • 2
  • 21
  • 28