安装mysql2时出现错误。

8

我正在尝试在服务器上安装mysql2。我的第一次不幸的尝试如下:

gem install mysql2

输出结果
    Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... no
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... yes
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=/usr/bin/ruby1.8
    --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 /home/aaalsubaie/.gems/gems/mysql2-0.3.11 for inspection.
Results logged to /home/aaalsubaie/.gems/gems/mysql2-0.3.11/ext/mysql2/gem_make.out

这并不是故事的结束

运行之后

[ps89405]$ mysql --help | grep Default -A 1
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf 

我尝试了。
 gem install mysql2 -- --with-mysql-config=~/.my.cnf --ruby=/usr/bin/ruby1.8

并且
gem install mysql2 -- --with-mysql-config=/usr/etc/my.cnf --ruby=/usr/bin/ruby1.8

并且。
gem install mysql2 -- --with-mysql-config=/etc/my.cnf --ruby=/usr/bin/ruby1.8

没有运气,出现了同样的错误。

我没有运行rvm。

我的gem环境是:

   RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/aaalsubaie/.gems/
  - RUBY EXECUTABLE: /usr/bin/ruby1.8
  - EXECUTABLE DIRECTORY: /home/aaalsubaie/.gems/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/aaalsubaie/.gems/
     - /usr/lib/ruby/gems/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

这是我第二天尝试部署我的第一个 Rails 应用程序,但一直没有成功。我曾经以为这会容易许多。
更新:
现在命令正在运行并安装 mysql2 gem,感谢 Yanhao 的帮助。
gem install mysql2 -- --with-mysql-config=/usr/bin/mysql_config

我现在有一个关于mysql2 gem的新问题, 当我运行时,出现以下情况:
bundle exec rails console

我得到了

/home/aaalsubaie/ajhezaty.com/releases/20120411095320/vendor/bundle/ruby/1.8/gems/mysql2-0.3.11/lib/mysql2.rb:9:in `require': no such file to load -- mysql2/mysql2 (LoadError)
from /home/aaalsubaie/ajhezaty.com/releases/20120411095320/vendor/bundle/ruby/1.8/gems/mysql2-0.3.11/lib/mysql2.rb:9
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:62:in `each'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:62:in `require'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:51:in `each'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:51:in `require'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler.rb:112:in `require'
from /home/aaalsubaie/ajhezaty.com/releases/20120411095320/config/application.rb:7
from /home/aaalsubaie/ajhezaty.com/releases/20120411095320/vendor/bundle/ruby/1.8/gems/railties-3.2.1/lib/rails/commands.rb:39:in `require'
from /home/aaalsubaie/ajhezaty.com/releases/20120411095320/vendor/bundle/ruby/1.8/gems/railties-3.2.1/lib/rails/commands.rb:39
from script/rails:6:in `require'
from script/rails:6
6个回答

16

这可能与你直接相关性不大,但我曾面临完全相同的问题,我认为我的答案可能会帮助其他遇到此问题的人:


这可能不是直接针对您的问题,但我曾经遇到过完全相同的问题,我认为我的回答可能会对其他遇到此问题的人有所帮助:
sudo apt-get install libmysql-ruby
sudo apt-get install libmysqlclient-dev
sudo gem install mysql2

那对我起作用了。


对于 libmysql-ruby,我得到了 E: 无法定位软件包 libmysql-ruby 的错误,但只安装 libmysqlclient-dev 仍然可以正常工作。 - Rich Steinmetz

5
如果您使用的是Mac操作系统,可以通过Homebrew安装MySQL brew install mysql,然后就可以安装该宝石了。

3
命令应该像这样:
gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

选项 "with-mysql-config" 指的是 "mysql_config" 工具,而不是配置文件。
你最好查看 mkmf.log 文件。

1
我的服务器上不存在 /usr/local/mysql/bin/ 目录!另外,我没有理解你的最后一行。 - Abdulaziz Alsubaie
谢谢,安装开发库后它可以工作了。我现在有一个新问题,你能看一下我上面的问题更新吗? - Abdulaziz Alsubaie
又要运行一个 bundle install 吗? - Reactormonk
请检查此文件是否存在,/home/aaalsubaie/ajhezaty.com/releases/20120411095320/vendor/bundle/ruby/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle。如果不存在,则可能需要重新安装gem,或通过编译/home/aaalsubaie/ajhezaty.com/releases/20120411095320/vendor/bundle/ruby/1.8/gems/mysql2-0.3.11/ext/mysql2中的代码来手动修复它。如果该文件存在,则可能存在动态库的问题。 - Yanhao

0

首先,您需要运行此命令:

sudo apt-get install libmysqlclient-dev

然后你可以运行这个:

gem install mysql2 

应该可以工作...


0

sudo apt-get install libmysqlclient-dev 系统将会安装另外两个库: mysql-common libmysqlclient16


0

"bundle exec rails console" 运行在 Bundler 的 Gemfile 上,但是你使用 rubygems 安装了 mysql2。你需要运行:

gem uninstall mysql2              # uninstall mysql2 from system
bundle exec gem uninstall mysql2  # uninstall mysql2 from bundler
bundle config build.mysql2 --with-mysql-config=/usr/bin/mysql_config  # set build option for mysql2 gem
bundle install                    # reinstall mysql2
bundle exec rails console         # start rails console using the rails exec installed via bundler 

额外说明:如果您正在使用Passenger并且刚刚更新了Ruby版本,请确保重新安装Passenger并更新您的httpd.conf以便成功启动服务器。

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