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

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

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


当前回答

在Debian Stretch上,适合我的包是default-libmysqlclient-dev

Sudo apt-get update && apt-get install -y default-libmysqlclient-dev

其他回答

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

修复:

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

I got the gem built on Mac OS X 10.6.6 by 1) Ensuring the Developer tools package is installed 2) Downloading the current MySQL package (5.5.8 in my case) from Source 3) Installing the cmake tool from cmake.org 4) Following the instructions in section 2.11 of INSTALL-SOURCE from the mysql distribution files 5) sudo gem install mysql2 -- --srcdir=/usr/local/mysql/include The gem built successfully, but there are two errors in the documentation that rdoc and ri complain about. But now when I try to require 'mysql2' I get a

LoadError: no such file to load -- mysql2/mysql2

我希望我得到的错误是libmysqlclient.16。Dylib无法找到,因为我们在另一篇文章中找到了(搜索install_name_tool)。

我的$PATH有/usr/local/mysql在里面(这是我的源代码和构建文件所在的位置),所以我有点难住了。如果有人有什么想法,我会在睡了几个小时后再来看看。

从[这里][1]下载连接器C文件。(https://downloads.mysql.com/archives/c-c/) 将文件解压到一个位置,如C:\ror\mysqlC\mysql-connector-c-6.1.11-winx64\lib 使用这个模板

install mysql2——'——with-mysql-lib="$lib_location"——with-mysql-include="$include_location"'

我的安装位置是lib_location -> C:\ror\mysqlC\mysql-connector-c-6.1.11-winx64\lib

——with-mysql-lib="C:\ror\mysqlC\mysql-connector-c-6.1.11-winx64\lib"——with-mysql-include="C:\ror\mysqlC\mysql-connector-c-6.1.11-winx64\include"'

这里有一个解决方案,希望对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

如果你已经用自制软件安装了MySQL,对我来说有效的方法是卸载MySQL,然后通过MySQL网站(https://www.mysql.com/)安装MySQL社区版。

安装后,只需重新输入命令gem install mysql2,如果有必要,sudo gem install mysql2,如果你得到的权限被拒绝的问题。