我有一些问题时,试图安装mysql2宝石为Rails。当我试图通过运行bundle install或gem install mysql2来安装它时,它会给我以下错误:

安装mysql2错误:错误:未能建立gem本地扩展。

我如何解决这个问题并成功安装mysql2?


当前回答

MacOS用户的另一种方式

如果你使用“brew”安装mysql:

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

X.X.X =要安装的mysql2 gem的版本号 y.y.y =你安装的mysql版本ls /usr/local/Cellar/mysql

其他回答

如果仍然报错,请按照mysql2 gem在Rails 3上的安装步骤

http://rorguide.blogspot.com/2011/03/installing-mysql2-gem-on-ruby-192-and.html

其中大多数用户都能够安装mysql2 gem。

我只是想特别为Mac用户添加这个答案。

在我更新xcode之前,我的服务器运行得非常好。当启动我的rails服务器时,错误显示如下

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/user/.rvm/rubies/ruby-1.9.3-p448/bin/ruby extconf.rb --with-mysql-
    checking for rb_thread_blocking_region()... /Users/user/.rvm/rubies/ruby-1.9.3-  
    p448/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an    
    executable file. (RuntimeError)

并且在错误消息的末尾有安装mysql2 gem的建议。所以当我尝试安装它时,我得到了这个问题中提到的错误。我得到的错误如下

ERROR:  Error installing mysql2:
ERROR: Failed to build gem native extension.

因此,正如在这篇文章中所建议的那样,我尝试了“酿造安装mysql”,并退出说mysql版本已经安装。但之前有警告说

Warning: You have not agreed to the Xcode license.
Builds will fail! Agree to the license by opening Xcode.app or running:
xcodebuild -license

然后我尝试了这个sudo xcodebuild -license,并在最后键入'agree'。您必须是root用户才能同意该许可。

在此之后,我再次尝试bundle安装,然后一切都正常工作。[甚至由于这次xcode更新,我的塔也有问题。]

这里有一个解决方案,希望对Windows用户有帮助!

在Windows上使用Rails 3使用MySQL

安装railsinstaller -> www.railsinstaller.org(我安装到c:\Rails) 安装MySQL(我使用MySQL 5.5) -> dev.mysql.com/downloads/installer/

--- for mySQL installation --- If you dont already have these two files installed you might need them to get your MySQL going vcredist_x86.exe -> http://www.microsoft.com/download/en/details.aspx?id=5555 dotNetFx40_Full_x86_x64.exe -> http://www.microsoft.com/download/en/details.aspx?id=17718 Use default install Developer Machine -MySQL Server Config- port: 3306 windows service name: MySQL55 mysql root pass: root (you can change this later) (username: root) -MySQL Server Config- --- for mySQL installation ---

安装mysql2 Gem 重点:使用Git Bash命令行(这是安装在railsinstaller) -> start/Git Bash ——with-mysql-lib="c:\Program Files\MySQL\ Server 5.5\lib"——with-mysql-include="c:\Program Files\MySQL\ Server 5.5\include"' 现在gem应该已经正确安装了 最后复制libmysql.dll文件 C:\Program Files\MySQL\MySQL Server 5.5\lib 来 Rails C: \ \ Ruby1.9.2 \ bin 安装mysql2 Gem

你现在将能够使用你的Rails应用程序与MySQL,如果你不确定如何创建一个Rails 3应用程序与MySQL阅读…


用MySQL开发一个Rails 3应用程序 打开命令提示符(不是Git Bash) -> start/cmd 导航到您的文件夹(c:\Sites) 创建新的rails应用程序

rails new world

删除文件c:\Sites\world\public\index.html 编辑文件c:\Sites\world\config\routes.rb 添加这一行-> root:to => 'cities#index'

打开命令提示符(生成视图和控制器)

rails generate scaffold city ID:integer Name:string CountryCode:string District:string Population:integer

编辑文件c:\Sites\world\app\models\city。Rb变成这样

class City < ActiveRecord::Base
 set_table_name "city"
end

编辑文件c:\Sites\world\config\database。Yml看起来像这样

development:
adapter: mysql2
encoding: utf8
database: world
pool: 5
username: root
password: root
socket: /tmp/mysql.sock

添加到gemfile

gem 'mysql2'

打开命令提示符窗口cmd,而不是Git Bash(运行你的应用程序!) 导航到应用程序文件夹(c:\Sites\world)

rails s

在这里打开浏览器-> http://localhost:3000

用MySQL开发一个Rails 3应用程序

在升级到Mac OS X Mountain Lion后尝试安装mysql2 gem时,出现“您必须先安装开发工具”错误。显然,执行此升级将删除命令行编译器。

修复:

我卸载了旧版本的Xcode(在/Developer/Library中运行卸载脚本)。然后删除“/Developer”目录。 去AppStore下载Xcode。 打开Xcode,进入首选项->下载,安装命令行工具。

根据https://github.com/brianmario/mysql2/issues/1175,我用

gem install mysql2 -- \
 --with-mysql-lib=/usr/local/Cellar/mysql/8.0.26/lib \
 --with-mysql-dir=/usr/local/Cellar/mysql/8.0.26 \
 --with-mysql-config=/usr/local/Cellar/mysql/8.0.26/bin/mysql_config \
 --with-mysql-include=/usr/local/Cellar/mysql/8.0.26/include