'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/[email protected]/include --with-opt-dir=/usr/local/opt/[email protected] --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/[email protected]/5.7.37/bin/mysql_config --srcdir=/usr/local/Cellar/[email protected]/5.7.37/bin/include --with-ldflags=-L/usr/local/opt/[email protected]/lib --with-cppflags=-I/usr/local/opt/[email protected]/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 1:[1]

I am able to solve this issue by downgrading mysql to 5.7 and then installing [email protected]

Below are the steps:

#stop mysql
brew services stop mysql

# install mysql5.7
brew install [email protected]

#Link mysql5.7
brew unlink mysql
brew link [email protected] --force

# install mysqlclient
brew install [email protected]

#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/[email protected]/1.0.2u/lib --with-cppflags=-I/usr/local/Cellar/[email protected]/1.0.2u/include

Solution 2:[2]

Try this way, it's work for me

brew install openssl zstd mysql

LIBRARY_PATH=$(brew --prefix zstd)/lib/:$(brew --prefix openssl)/lib/ gem install mysql2 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chakreshwar Sharma
Solution 2 Nguy?n Vi?t Thái