尝试使用mysql2 gem安装应用程序时出现错误

106

我正在尝试安装一个使用mysql2宝石的开源rails 3.2.21应用程序,但是当我尝试运行bundle命令时,会出现以下错误:

Fetching: mysql2-0.3.18.gem (100%)
Building native extensions.  This could take a while...
p
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    /Users/my_username/.rvm/rubies/ruby-2.1.2/bin/ruby -r ./siteconf20150614-72129-orqsb7.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/5.6.25/lib
-----
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
linking shared-object mysql2/mysql2.bundle
ld: warning: directory not found for option '-L/Users/travis/.sm/pkg/active/lib'
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/my_username/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/mysql2-0.3.18 for inspection.
Results logged to /Users/my_username/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/extensions/x86

我尝试通过Homebrew卸载所有安装的mysql版本,并重新安装它们,步骤如下:

brew uninstall --force mysql && brew install mysql

接下来运行:

sudo gem install mysql2

正如这里提出的一些类似问题所建议的,但仍然导致与上述相同的错误。

请有人能够提供如何启动和运行此程序的指导吗?


尝试运行“brew doctor”并检查是否有任何错误。如果有,请按照说明进行操作,然后再次尝试。 - K.M.
你已经安装了 gem 的依赖项吗?https://github.com/brianmario/mysql2#general-instructions - lcguida
@ValAsensio,我非常不同意你的观点,我认为如果可能的话,你应该始终尝试在开发中使用生产数据库。我从不在开发中使用sqlite,也没有和我一起工作的人这样做。 - niels
@niels。是的,我同意“我认为您应该始终尝试在开发过程中使用生产数据库”。我先前的那条古老评论已经没有用了,我已将其删除。 - Elvn
19个回答

2

在 Monterey M1 Mac 上为我执行的步骤

brew install openssl@3

brew install zstd

gem install mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib

bundle config --local build.mysql2 "--with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib"

bundle install

在M1 Mac w/ Monterey上安装mysql2的第三行代码会生成以下错误:`linking shared-object mysql2/mysql2.bundle ld: warning: directory not found for option '-L/opt/homebrew/Cellar/zstd/1.5.0/lib' ld: library not found for -lzstd clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [mysql2.bundle] Error 1make失败,退出码为2Gem文件将保留在/Users/u/.gem/ruby/2.6.5/gems/mysql2-0.5.3以供检查。 结果记录在/Users/u/.gem/ruby/2.6.5/extensions/-darwin-21/2.6.0-static/mysql2-0.5.3/gem_make.out中。` - brentonstrine
在Mac M1芯片上工作非常棒。 - rakesh kumar

2

这些命令的组合解决了我的问题。我使用的是Mojave系统。

brew reinstall openssl && brew link openssl --force

然后执行以下命令:

gem install mysql2 -v '0.4.10' -- \
  --with-ldflags=-L/usr/local/opt/openssl/lib \
  --with-cppflags=-I/usr/local/opt/openssl/include

1

以下命令适用于我的Mac OS 12.1操作系统(MacOS Monterey)

 gem install mysql2 -v '0.5.3' -- \
 --with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.28/lib \
 --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.28 \
 --with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.28/bin/mysql_config \
 --with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.28/include 

请参考此链接以获取更多详细信息: https://github.com/brianmario/mysql2/issues/1175

1
似乎您缺少构建mysql2 gem所需的主要文件。
sudo apt-get install libsqlite3-dev libmysqlclient-dev -y

libsqlite3-dev不是必须的,但是建议安装它作为默认的Rails数据库。


1

使用Homebrew修复Mac Catalina问题: gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl)"


0

这在我的MacOs 12.6(Monterey)上帮了我

brew install mysql openssl
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
gem install mysql2 -v '0.5.3' \
  --source 'https://rubygems.org/' -- \
  --with-cppflags=-I/usr/local/opt/openssl/include \
  --with-ldflags=-L/usr/local/opt/openssl/lib

bundle install

感谢https://github.com/brianmario/mysql2/issues/1175#issuecomment-1069721834


0

我已经使用mysql2 gem编码多年,并且不时遇到这个问题。

今天我发现这个神奇的选项-- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include在我的Mac上不再起作用了。实际上,看起来brew安装openssl的默认位置已经改变了:

$ brew reinstall openssl

...

If you need to have openssl@3 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@3 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

所以根据这条消息,我不得不对命令进行了一些更改,最终使它正常工作:

$ gem install mysql2 -v '0.5.3' -- --with-ldflags=-L/opt/homebrew/opt/openssl@3/lib --with-cppflags=-I/opt/homebrew/opt/openssl@3/include

希望这能帮助到某个人!


0

2
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

-1
我发现我必须使用--with-opt-dir=/usr/local/opt
具体来说,我将以下内容添加到我的~/.bundle/config文件中:
BUNDLE_BUILD__MYSQL2: "--with-opt-dir=/usr/local/opt"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接