2011-03-23 171 views
21

我在安裝mysql2 gem時遇到問題。在Mac OSX上安裝用於Ruby on Rails的mysql2 gem 10.6

這來了,當我做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 

而且當我使用這個rails server出現:

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

回答

48

它似乎無法找到MySQL庫。你安裝了MySQL嗎?
嘗試通過homebrew安裝它:

brew install mysql 

或通過任何你喜歡的MacPorts或芬克或安裝它,然後再試一次:

gem install mysql2 
+0

謝謝你!這工作!現在,當我啓動rails服務器並單擊「關於您的應用程序的環境...」時出現:Mysql2 ::錯誤 無法通過套接字'/tmp/mysql.sock'連接到本地MySQL服務器(2) Rails .root:/ Users/useruser/example你知道這意味着什麼嗎? – Maze 2011-03-23 21:12:10

+0

你有沒有設置mysql服務器運行? 「mysql.server start」將完成這項工作,並且不要忘記爲root用戶配置訪問權限。 – ALoR 2011-03-23 21:21:34

+0

這出現了:。錯誤!沒有更新文件,pid文件管理器退出。 – Maze 2011-03-23 22:57:49

1

你必須指定一些額外的環境變量上安裝這種寶石適用於Mac OSX 10.6的64位體系結構:

env ARCHFLAGS="-arch x86_64" gem install mysql2 

如果你正在使用RVM,你可以將其添加爲在〜/ .rvmrc文件的默認選項:

rvm_archflags="-arch x86_64" 
+0

沒有工作:(也許我做錯了,你能解釋一下更詳細介紹瞭如何做到這一點嗎? – Maze 2011-03-23 19:23:28

+0

你得到同樣的錯誤或不同的錯誤?如果你有你的mysql安裝在默認以外的地方,你可能還需要指定其他標誌,如--with-opt-dir。 – 2011-03-23 19:25:12

+0

我得到了同樣的錯誤,它說:Gem文件將保持安裝在/ Users/useruser /中。 rvm/gems/ruby​​-1.9.2-p136/gems/mysql-2.8.1進行檢查。也許可以幫助嗎?是的,我正在使用rvm – Maze 2011-03-23 19:31:28

1

我有相同的問題,並試圖安裝錯誤mysql2。我以爲我安裝MySQL的mamp會工作正常,並花了幾個小時玩弄路徑來實現它 - 沒有成功。

終於在ALoR上找到了這篇文章,並通過自制軟件安裝了一個新版本 - 但是 - 一定要按照所有來自自制軟件的說明!我錯過了一些步驟,並浪費了一個小時跟蹤這個問題 - 在這裏是指那些說明: https://stackoverflow.com/a/11061487/1241271

的MySQL安裝成功後,我跑:sudo gem install mysql2和它的工作就像一個魅力。

希望這可以幫助別人浪費幾個小時,因爲他們忽視閱讀文檔(newb錯誤)。

20

此命令爲我工作:

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

如果您使用mysql網站提供的二進制文件安裝它,你的Mac。 – gmaliar 2013-03-27 10:15:28

+0

謝謝你,正如蓋伊所述,如果你使用mysql網站來安裝 – Brenden 2013-04-16 22:19:46

+0

這也適用於我..但我沒有使用mysql網站來安裝。 – Siddharth 2016-10-11 21:08:28

14

我不需要MySQL的Mac OS X上,因爲我對流浪箱安裝了MySQL。所以,我剛安裝了mysql-connector-c。

brew install mysql-connector-c 
gem install mysql2 
4

以下爲我工作

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

如果您使用自制安裝mysql,brew install mysql,這個工作對我來說:

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

XXX =版本mysql2寶石你想安裝
yyy =你安裝的mysql版本ls /usr/local/Cellar/mysql找到它。

獲得MySQL的版本

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

       ... 

然後如果你想與捆綁安裝:

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

我愛你! 'ü拯救我的生命:D – 2017-09-11 20:52:32

7

下面的命令成功地爲我工作。

x.x.x =您要安裝的mysql2版本。

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

謝謝,我在macOS 10.12.5,Ruby 2.3.4,Rails 5.0.3上安裝MariaDB 10.2(通過brew)後爲我工作 – smile2day 2017-05-25 09:34:31