mysqlruby-on-railsruby-on-rails-3.2apple-m1ruby-2.3

Mysql2 error in starting rails server in mac m1


I have installed mysql2 in mac m1 using the below command

gem uninstall mysql2
gem install mysql2 -v '0.3.21' -- --srcdir=/usr/local/Cellar/mysql/8.0.28_1/bin/include --with-mysql-include=/usr/local/Cellar/mysql/8.0.28_1/include/mysql --with-mysql-lib=/usr/local/Cellar/mysql/8.0.28_1/lib --with-cppflags=-I/usr/local/opt/openssl@1.0/include --with-opt-dir=/usr/local/opt/openssl@1.0 --platform=ruby

Installation works but when I starts rails s , I get the below error

/gems/mysql2-0.3.21/lib/mysql2.rb:31:in `require': cannot load such file -- mysql2/mysql2 (LoadError)

I am using Rails 3.2.22.5 and ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin21]

I downgraded the openssl to 1.0 to install ruby 2.3.0 using rosetta

I already installed mysql8 using brew install mysql .

Above is working in my another project which is running on Rails 4.2 and Ruby 2.3.1 but dont know why its not working here.

I also tried to downgrade mysql8 to 5.7 and then install mysql2 using the below

gem install mysql2 -v 0.3.21 -- --with-mysql-config=/usr/local/Cellar/mysql@5.7/5.7.37/bin/mysql_config --srcdir=/usr/local/Cellar/mysql@5.7/5.7.37/bin/include --with-ldflags=-L/usr/local/opt/mysql@5.7/lib --with-cppflags=-I/usr/local/opt/mysql@5.7/include --platform=ruby

But same error, installation complete but getting load error in rails s

Any help to fix this error will be appreciated.


Solution

  • I am able to solve this issue by downgrading mysql to 5.7 and then installing mysql-client@5.7

    Below are the steps:

    #stop mysql
    brew services stop mysql
    
    # install mysql5.7
    brew install mysql@5.7
    
    #Link mysql5.7
    brew unlink mysql
    brew link mysql@5.7 --force
    
    # install mysqlclient
    brew install mysql-client@5.7
    
    #uninstall old mysql versions if already installed
    gem uninstall mysql2
    
    #install mysql2
    gem install mysql2 -v 0.3.21 -- --with-mysql-config=/usr/local/bin/mysql_config --with-ldflags=-L/usr/local/Cellar/openssl@1.0/1.0.2u/lib --with-cppflags=-I/usr/local/Cellar/openssl@1.0/1.0.2u/include