MySQL 安装mysql2时出错:无法构建gem本机扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3608287/
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 17:00:45  来源:igfitidea点击:

Error installing mysql2: Failed to build gem native extension

mysqlruby-on-railsrubyrubygemsmysql2

提问by spacemonkey

I am having some problems when trying to install mysql2gem for Rails. When I try to install it by running bundle installor gem install mysql2it gives me the following error:

尝试mysql2为 Rails安装gem时遇到一些问题。当我尝试通过运行安装它bundle installgem install mysql2它给我以下错误:

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

安装 mysql2 时出错:错误:无法构建 gem 本机扩展。

How can I fix this and successfully install mysql2?

如何解决此问题并成功安装mysql2

回答by spacemonkey

On Ubuntu/Debian and other distributions using aptitude:

在 Ubuntu/Debian 和其他使用 aptitude 的发行版上:

sudo apt-get install libmysql-ruby libmysqlclient-dev

Package libmysql-rubyhas been phased out and replaced by ruby-mysql. Thisis where I found the solution.

libmysql-ruby已被淘汰并由ruby-mysql. 是我找到解决方案的地方。

If the above command doesn't work because libmysql-rubycannot be found, the following should be sufficient:

如果上述命令由于libmysql-ruby找不到而不起作用,则以下内容就足够了:

sudo apt-get install libmysqlclient-dev

On Red Hat/CentOS and other distributions using yum:

在 Red Hat/CentOS 和其他使用 yum 的发行版上:

sudo yum install mysql-devel

On Mac OS X with Homebrew:

在带有Homebrew 的Mac OS X 上:

brew install mysql

回答by Kelsin

I'm on a mac and use homebrewto install open source programs. I did have to install mac Dev tools in order to install homebrew, but after that it was a simple:

我在 Mac 上使用自制软件安装开源程序。我确实必须安装 mac Dev 工具才能安装自制软件,但之后就很简单了:

brew install mysql

to install mysql. I haven't had a mysql gem problem since.

安装mysql。从那以后我就没有遇到过 mysql gem 问题。

回答by Francois

here is a solution for the windows users, hope it helps!

这是Windows用户的解决方案,希望对您有所帮助!

Using MySQL with Rails 3 on Windows

在 Windows 上使用带有 Rails 3 的 MySQL

  • Install railsinstaller-> www.railsinstaller.org(I installed it to c:\Rails)

  • Install MySQL(I used MySQL 5.5) -> dev.mysql.com/downloads/installer/

  • 安装 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 filesinstalled you might need them to get your MySQL going

vcredist_x86.exe -> http://www.microsoft.com/download/en/details.aspx?id=5555dotNetFx40_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 ---

--- 用于 mySQL 安装 ---

如果您还没有安装这两个文件,您可能需要它们来让您的 MySQL 运行

vcredist_x86.exe -> http://www.microsoft.com/download/en/details.aspx? id=5555dotNetFx40_Full_x86_x64.exe -> http://www.microsoft.com/download/en/details.aspx?id=17718

使用默认安装开发者机器

-MySQL Server Config-
port: 3306
windows service name: MySQL55
mysql root pass: root (以后可以改这个)
(username: root)
-MySQL Server Config-

--- 用于 mySQL 安装 ---



--- Install the mysql2 Gem ---

Important: Do this with Git Bash Command Line(this was installed with railsinstaller) -> start/Git Bash

gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"'

Now the gem should have installed correctly

Lastly copy the libmysql.dll file from
C:\Program Files\MySQL\MySQL Server 5.5\lib
to
C:\Rails\Ruby1.9.2\bin

--- Install the mysql2 Gem ---

--- 安装 mysql2 Gem ---

重要提示:使用 Git Bash 命令行执行此操作(这是使用 railsinstaller 安装的)-> start/Git Bash

gem install mysql2 -- '--with-mysql-lib="c:\ Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"'

现在 gem 应该已经安装正确

最后复制 libmysql.dll 文件从
C:\Program Files\MySQL\MySQL Server 5.5\lib

C:\Rails\Ruby1.9.2\bin

--- 安装 mysql2 Gem ---


You will now be able to use your Rails app with MySQL, if you are not sure how to create a Rails 3 app with MySQL read on...


如果您不确定如何创建带有 MySQL 读取的 Rails 3 应用程序,您现在可以将 Rails 应用程序与 MySQL 一起使用...



--- Get a Rails 3 app going with MySQL ---

Open command prompt(not Git Bash) -> start/cmd
Navigate to your folder (c:\Sites)
Create new rails app

--- 使用 MySQL 获取 Rails 3 应用程序 ---

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

rails new world

Delete the file c:\Sites\world\public\index.html
Edit the file c:\Sites\world\config\routes.rb
add this line -> root :to => 'cities#index'

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

Open command prompt (generate views and controllers)

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

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



Edit the file c:\Sites\world\app\models\city.rb to look like this



编辑文件 c:\Sites\world\app\models\city.rb 看起来像这样

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

Edit the file c:\Sites\world\config\database.yml to look like this

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

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

add to gemfile

添加到 gemfile

gem 'mysql2'

Open command promptwindows cmd, not Git Bash(run your app!)
Navigate to your app folder (c:\Sites\world)

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

rails s

Open your browser here -> http://localhost:3000

在此处打开浏览器 -> http://localhost:3000

--- Get a Rails 3 app going with MySQL ---

--- 使用 MySQL 获取 Rails 3 应用程序 ---

回答by OfficeYA

I have several computers, 32 and 64 bits processor, they run on Ubuntu Linux, Maverick (10.10) release.

我有几台计算机,32 位和 64 位处理器,它们运行在 Ubuntu Linux,Maverick (10.10) 版本上。

I had the same problem, and for me, the sudo apt-get install libmysql-ruby libmysqlclient-devdid the job!!!

我遇到了同样的问题,对我来说, sudo apt-get install libmysql-ruby libmysqlclient-dev完成了工作!!!

回答by aruanoc

Another way for MacOS users

MacOS 用户的另一种方式

If you used "brew" to install mysql:

如果您使用“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 = 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以找到它。

回答by Allen

For MacOS Mojave:

对于MacOS 莫哈韦沙漠

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

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

回答by Jo?o Pereira

Have you tried using

你有没有试过使用

 gem install mysql -- --with-mysql-lib=/usr/lib/mysql/lib 

to specify the location of thebase directory as well as the path to the MySQL libraries that are necessary to complete the gem installation?

指定基本目录的位置以及完成 gem 安装所需的 MySQL 库的路径?

Sources: MySQL Gem Install ERROR: Failed to build gem native extensionMySQL Forums :: Ruby :: Help needed with installing MySQL binding for Ruby

资料来源: MySQL Gem 安装错误:无法构建 gem 本机扩展MySQL 论坛 :: Ruby :: 需要帮助为 Ruby 安装 MySQL 绑定

回答by Mark Locklear

If you are using yum try:

如果您使用的是 yum,请尝试:

sudo yum install mysql-devel

回答by capu

If you are still having trouble….

如果您仍然遇到问题......

Try installing

尝试安装

   sudo apt-get install ruby1.9.1-dev

回答by Carlos Pardilla

This solved my problem once in Windows:

这在 Windows 中解决了我的问题:

subst X: "C:\Program files\MySQL\MySQL Server 5.5" 
gem install mysql2 -v 0.x.x --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt 
subst X: /D