Ruby Mac 上 ruby 使用 mysql2 报错

sp123456 · 2020年05月27日 · 最后由 sp123456 回复于 2020年05月28日 · 2038 次阅读

百度、谷歌了三四天,尝试了好几种方法,仍没解决。ruby 知识有限,所以来请教下各位。

问题是: 在 ruby 脚本中需要访问MySQL数据库,导入mysql2后,报找不到'libssl.1.1.dylib的错误

下面是功能单一的测试脚本test.rb:

#!/usr/bin/ruby
#
require "mysql2"

mysql_client = Mysql2::Client.new(
    :host =>  'localhost',
    :username => 'root',
    :password => 'xxxxx',
    :database => 'xxxx',
    :encoding => 'utf8'
)

然后在终端中执行这个脚本

ruby test.rb

下面是终端中打印的错误:

Traceback (most recent call last):
    2: from test.rb:4:in `<main>'
    1: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- mysql2 (LoadError)
    6: from test.rb:4:in `<main>'
    5: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:34:in `require'
    4: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:130:in `rescue in require'
    3: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:130:in `require'
    2: from /Library/Ruby/Gems/2.6.0/gems/mysql2-0.5.3/lib/mysql2.rb:36:in `<top (required)>'
    1: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': dlopen(/Library/Ruby/Gems/2.6.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.bundle, 0x0009): dependent dylib 'libssl.1.1.dylib' not found for '/Library/Ruby/Gems/2.6.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.bundle'. relative file paths not allowed 'libssl.1.1.dylib' - /Library/Ruby/Gems/2.6.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.bundle (LoadError)

我的 Mac 上的系统是: macOS 10.15.4 (19E287) ruby 版本是: ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

shell 的配置文件~/.bash_profile内容是:

MYSQL=/usr/local/mysql/bin
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

并且电脑上安装了openssl1.1,使用的安装命令是:

brew install openssl
gem uninstall mysql2
gem install mysql2 -- --with-cflags=\"-I/usr/local/opt/openssl/include\" --with-ldflags=\"-L/usr/local/opt/openssl/lib\"

macOS 10.15.5 & ruby 2.7.1 测试通过.

046569 回复

十分感谢,你的回答解决了我的问题。谢啦。

FinnG 回复

感谢你提供方法,那个链接里面的内容有点长😅 ,然后使用二楼的命令解决了。

ken 回复

感谢你的回答,问题已经解决了。

sp123456 关闭了讨论 05月28日 10:01
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册