MySQL 使用 Mac OSX 10.6 为 Ruby on Rails 安装 mysql2 gem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5409761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:15:35  来源:igfitidea点击:

Installing mysql2 gem for Ruby on Rails with Mac OSX 10.6

mysqlruby-on-railsrubyosx-snow-leopardgem

提问by Maze

I am having a problem installing the mysql2 gem.

我在安装 mysql2 gem 时遇到问题。

This comes up when I do gem install mysql2:

当我这样做时会出现这种情况gem install mysql2

Marks-MacBook-Pro:~ Mark$ gem install mysql2
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
ERROR: Failed to build gem native extension.

/Users/useruser/.rvm/rubies/ruby-1.9.2-p136/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/useruser/.rvm/rubies/ruby-1.9.2-p136/bin/ruby
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mlib
    --without-mlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-zlib
    --without-zlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-socketlib
    --without-socketlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-nsllib
    --without-nsllib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mygcclib
    --without-mygcclib
    --with-mysqlclientlib
    --without-mysqlclientlib


Gem files will remain installed in /Users/useruser/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6 for inspection.
Results logged to /Users/useruser/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6/ext/mysql2/gem_make.out

Also when I use rails serverthis comes up:

此外,当我使用rails server它时会出现:

Could not find gem 'mysql2 (>= 0, runtime)' in any of the gem sources listed in your Gemfile.

回答by ALoR

It seems that it cannot find the MySQL library. Have you installed MySQL?
Try installing it via homebrew:

似乎找不到MySQL库。你安装 MySQL 了吗?
尝试通过自制软件安装它:

brew install mysql

or install it via macports or fink or whatever you prefer.

或通过 macports 或 fink 或任何您喜欢的方式安装它。

Then try again with:

然后再试一次:

gem install mysql2

回答by Sushma Satish

This command worked for me:

这个命令对我有用:

gem install mysql2 -- --srcdir=/usr/local/mysql/include

回答by Aamir

Following command worked for me successfully.

以下命令成功为我工作。

x.x.x = version of mysql2 you want to install.

xxx = 要安装的 mysql2 版本。

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

回答by Igor Shubovych

I don't need MySQL on Mac OS X, because I have MySQL installed on Vagrant box. Therefore, I just installed mysql-connector-c.

我在 Mac OS X 上不需要 MySQL,因为我在 Vagrant 盒子上安装了 MySQL。因此,我只是安装了mysql-connector-c。

brew install mysql-connector-c
gem install mysql2

回答by Mayank Jain

The following worked for me

以下对我有用

brew install mysql 
brew install mysql-connector-c
gem install mysql2

回答by aruanoc

If you used homebrewto install mysql, brew install mysql,this worked for me:

如果您使用自制软件安装 mysql brew install mysql,这对我有用:

gem install mysql2 -v 'x.x.x' -- --with-mysql-config=/usr/local/Cellar/mysql/y.y.y/bin/mysql_config

x.x.x = version of the mysql2 gem you want to install
y.y.y = the version of mysql you have installed ls /usr/local/Cellar/mysqlto find it.

xxx = 你要安装的 mysql2 gem
的版本 yyy = 你已经安装的 mysql 版本ls /usr/local/Cellar/mysql以找到它。

to get the version of mysql

获取mysql的版本

brew info mysql                                                                                                                                                                                                     
mysql: stable 5.7.19 (bottled)
               ...
/usr/local/Cellar/mysql/5.7.19 (322 files, 233MB) *

               ...

then if you want to install with bundle:

那么如果你想用捆绑安装:

bundle config build.mysql --with-mysql-config=/usr/local/Cellar/mysql/y.y.y/bin/mysql_config

回答by Pan Thomakos

You'll have to specify some additional environment variables to install this gem on the 64 bit architecture for Mac OSX 10.6:

您必须指定一些额外的环境变量才能在 Mac OSX 10.6 的 64 位架构上安装这个 gem:

env ARCHFLAGS="-arch x86_64" gem install mysql2

If you are using rvm you can add this as your default option in the ~/.rvmrc file:

如果您使用的是 rvm,您可以将其添加为 ~/.rvmrc 文件中的默认选项:

rvm_archflags="-arch x86_64"

回答by yellowaj

I had the exact same issue and errors trying to install mysql2. I thought my mamp install of MySQL would work fine and spent a few hours playing around with paths to get that to work - no success.

我在尝试安装 mysql2 时遇到了完全相同的问题和错误。我认为我的 MySQL 的 mamp 安装可以正常工作,并花了几个小时玩弄路径以使其正常工作 - 没有成功。

Finally came across this post from ALoR and installed a fresh version through homebrew - however - be sure to follow ALL the instructions from homebrew! I missed a few steps and wasted another hour tracking down that problem - here are those instructions: https://stackoverflow.com/a/11061487/1241271

终于从 ALoR 看到了这篇文章,并通过自制软件安装了一个新版本——但是——一定要遵循自制软件的所有说明!我错过了几个步骤,又浪费了一个小时来追踪这个问题——这里是这些说明:https: //stackoverflow.com/a/11061487/1241271

After MySQL was successfully installed, I ran: sudo gem install mysql2and it worked like a charm.

成功安装 MySQL 后,我运行:sudo gem install mysql2它就像一个魅力。

Hope this helps someone from wasting several hours because they neglected to read the docs (newb mistake).

希望这可以帮助某人浪费几个小时,因为他们忽略了阅读文档(新手错误)。

回答by BMA88

Make sure openssl is installed on Mac via Homebrew.

确保通过 Homebrew 在 Mac 上安装了 openssl。

brew install openssl

Install mysql2 gem.

gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/i

回答by CrypTech Studios

I know this has been answered multiple times however, here's what's working for me with: May 20, 2019 OSX Mojave 10.14.4 MySQL Ver 14.14 Distrib 5.7.24, for osx10.14 (x86_64) using EditLine wrapper ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17] Rails 5.2.1

我知道这已经被多次回答了,但是这对我有用:2019 年 5 月 20 日 OSX Mojave 10.14.4 MySQL Ver 14.14 Distrib 5.7.24,用于 osx10.14 (x86_64) 使用 EditLine 包装器 ruby​​ 2.5.07p0 (2019) -12-25 修订版 61468) [x86_64-darwin17] Rails 5.2.1

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
gem install mysql2 -- --with-mysql-config=/usr/local/Cellar/mysql\@5.7/5.7.24/bin/mysql_config

change "/usr/local/Cellar/[email protected]/5.7.24/bin/mysql_config" to whatever your current valid path is from "/usr/local/Cellar/mysql"

将“/usr/local/Cellar/[email protected]/5.7.24/bin/mysql_config”更改为您当前有效路径来自“/usr/local/Cellar/mysql”的任何内容

Hope this helps!

希望这可以帮助!