Rails error installing mysql2 (mysql2-0.3.20)

人走茶凉 提交于 2019-12-03 01:05:24
Samantha Blasbalg

I just ran into this problem today. mysql got updated recently, so what worked for me was:

brew install mysql@5.6
brew link mysql@5.6 --force
bundle install

Alternatively, I did not try this solution, but it seems to have worked for other people.

Installing Mysql 2 gem fails

Abhishek Sinha

I faced the same issue when installing mysql2-0.3.21 on Mac pro.

Below mentioned solution worked for me (run the below mentioned 3 commands in the terminal pointing to your project folder):

brew install mysql

gem install mysql2 -v '0.3.21' -- --srcdir=/usr/local/mysql/include

bundle install

This worked for me - macOS Catalina

brew uninstall mysql
gem uninstall mysql2

brew install mysql@5.7
brew link mysql@5.7 --force
gem install mysql2 -v '0.3.21'

# Gemfile
gem 'mysql2', '0.3.21'

bundle update mysql2

My MacBook Pro had a disk crashed during upgrade to MacOS Mojave (not pretty!), so I had to reestablish all my code projects afterwards.

One particular project proved extremely hard to get up and running and I've spent several hours today looking for a way to get mysql2-0.3.21 running for an older RoR project; Ruby version 2.1.1p76 with Rails 4.1.1.

Finally, I succeeded by following this advice: https://github.com/brianmario/mysql2/issues/1010#issuecomment-460257986

I did the following steps:

  1. Start by removing all installed versions of mysql2 from your gemset:

    gem uninstall mysql2
    
  2. Then install MySQL 5.6, I already have MySQL 8.0.17 installed, so 5.6 is installed as a keg only which is fine:

    brew install mysql@5.6
    
  3. Then install mysql2 version 0.3.21 with the following command:

    gem install mysql2 -v 0.3.21 -- --with-mysql-config=/usr/local/Cellar/mysql@5.6/5.6.42/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
    

The OpenSSL part can probably be left out and you can change the mysql2 version number depending on what version of Rails your project is running.

After the above steps I was able to start the Rails console again and query the database:

Loading development environment (Rails 4.1.1)
2.1.1 :001 > User.count
   (18.7ms)  SELECT COUNT(*) FROM `users`  WHERE `users`.`deleted_at` IS NULL
 => 1222
2.1.1 :002 >
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!