[英]Error installing mysql2: Failed to build gem native extension
I am having some problems when trying to install mysql2
gem for Rails.尝试为 Rails 安装
mysql2
gem 时遇到一些问题。 When I try to install it by running bundle install
or gem install mysql2
it gives me the following error:当我尝试通过运行
bundle install
或gem install mysql2
来安装它时,它给了我以下错误:
Error installing mysql2: ERROR: Failed to build gem native extension.
安装 mysql2 时出错:错误:无法构建 gem 原生扩展。
How can I fix this and successfully install mysql2
?如何解决此问题并成功安装
mysql2
?
On Ubuntu/Debian and other distributions using aptitude:在 Ubuntu/Debian 和其他使用 aptitude 的发行版上:
sudo apt-get install libmysql-ruby libmysqlclient-dev
Package libmysql-ruby
has been phased out and replaced by ruby-mysql
.软件包
libmysql-ruby
已被淘汰并被ruby-mysql
取代。 This is where I found the solution.这是我找到解决方案的地方。
If the above command doesn't work because libmysql-ruby
cannot 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
I'm on a mac and use homebrew to install open source programs.我在 Mac 上使用自制软件安装开源程序。 I did have to install mac Dev tools in order to install homebrew, but after that it was a simple:
我确实必须安装 mac Dev 工具才能安装自制软件,但之后就很简单了:
brew install mysql
to install mysql.安装mysql。 I haven't had a mysql gem problem since.
从那以后我就没有遇到过 mysql gem 问题。
对于MacOS 莫哈韦:
gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
here is a solution for the windows users , hope it helps!这是windows用户的解决方案,希望对你有帮助!
Install railsinstaller -> www.railsinstaller.org (I installed it to c:\Rails)安装 railsinstaller -> www.railsinstaller.org (我将它安装到 c:\Rails)
Install MySQL (I used MySQL 5.5) -> dev.mysql.com/downloads/installer/安装 MySQL (我使用 MySQL 5.5)-> dev.mysql.com/downloads/installer/
--- for mySQL installation ---
--- 用于 mySQL 安装 ---
If you dont already have these two files installed you might need them to get your MySQL going如果你还没有安装这两个文件,你可能需要它们来启动你的 MySQL
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=17718vcredist_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-
-MySQL服务器配置-
port: 3306端口:3306
windows service name: MySQL55windows服务名称:MySQL55
mysql root pass: root (you can change this later)mysql root pass:root(你可以稍后更改)
(username: root)(用户名:root)
-MySQL Server Config--MySQL服务器配置-
--- for mySQL installation ---
--- 用于 mySQL 安装 ---
--- Install the mysql2 Gem ---
--- 安装 mysql2 Gem ---
Important: Do this with Git Bash Command Line (this was installed with railsinstaller) -> start/Git Bash重要提示:使用 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 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\包括”'
Now the gem should have installed correctly现在 gem 应该已经正确安装了
Lastly copy the libmysql.dll file from最后复制 libmysql.dll 文件
C:\Program Files\MySQL\MySQL Server 5.5\libC:\Program Files\MySQL\MySQL 服务器 5.5\lib
to至
C:\Rails\Ruby1.9.2\binC:\Rails\Ruby1.9.2\bin
--- Install the mysql2 Gem ------ 安装 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 --- --- 获取一个与 MySQL 一起使用的 Rails 3 应用程序 ---
Open command prompt(not Git Bash) -> start/cmd打开命令提示符(不是 Git Bash)-> start/cmd
Navigate to your folder (c:\Sites)导航到您的文件夹 (c:\Sites)
Create new rails app创建新的 Rails 应用程序
rails new world
Delete the file c:\Sites\world\public\index.html删除文件 c:\Sites\world\public\index.html
Edit the file c:\Sites\world\config\routes.rb编辑文件 c:\Sites\world\config\routes.rb
add this line -> root :to => 'cities#index'添加这一行 -> 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 prompt windows cmd, not Git Bash(run your app!)打开命令提示符windows cmd,而不是 Git Bash(运行你的应用程序!)
Navigate to your app folder (c:\Sites\world)导航到您的应用文件夹 (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 应用程序 ---
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
xxx = version of the mysql2 gem you want to install xxx = 您要安装的 mysql2 gem 的版本
yyy = the version of mysql you have installed ls /usr/local/Cellar/mysql
to find it. yyy = 你安装的 mysql 版本
ls /usr/local/Cellar/mysql
找到它。
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-dev
did the job!!!我遇到了同样的问题,对我来说,
sudo apt-get install libmysql-ruby libmysqlclient-dev
完成了这项工作!!!
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 extension MySQL Forums :: Ruby :: Help needed with installing MySQL binding for Ruby来源: MySQL Gem 安装错误:无法构建 gem 原生扩展MySQL 论坛 :: Ruby :: 为 Ruby 安装 MySQL 绑定需要帮助
如果您使用的是 yum,请尝试:
sudo yum install mysql-devel
If you are still having trouble….如果您仍然遇到问题......
Try installing尝试安装
sudo apt-get install ruby1.9.1-dev
In my case this helped:就我而言,这有帮助:
$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl/include"
Then:然后:
gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/' -- --with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib
Result:结果:
Building native extensions with: '--with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib'
This could take a while...
Successfully installed mysql2-0.5.2
Parsing documentation for mysql2-0.5.2
Installing ri documentation for mysql2-0.5.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed
See this post (WARNING: Japanese language inside).见这篇文章(警告:里面有日语)。
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
I had this issue on Windows 7. This is apparently an incompatibility issue and the solution is as follows:我在 Windows 7 上遇到了这个问题。这显然是一个不兼容问题,解决方案如下:
Download the libmySQL.dll
file from an older InstantRails installer.从旧版 InstantRails 安装程序下载
libmySQL.dll
文件。 It is available from the InstantRails GitHub repo .它可从InstantRails GitHub 存储库中获得。
Next, copy that file into your Ruby/bin folder.接下来,将该文件复制到您的 Ruby/bin 文件夹中。
Now, you are good to go ;)现在,你可以走了;)
在 Debian Stretch 上,对我有用的包是default-libmysqlclient-dev
sudo apt-get update && apt-get install -y default-libmysqlclient-dev
您必须安装一些依赖项
sudo apt-get install libmysql-ruby libmysqlclient-dev
I got this error too.我也遇到了这个错误。 Solved by installing development packages.
通过安装开发包解决。 I'm using arch and it was:
我正在使用拱门,它是:
sudo pacman -S base-devel
which installed:其中安装:
m4, autoconf, automake, bison, fakeroot, flex, libmpc, ppl, cloog-ppl, elfutils, gcc,
libtool, make, patch, pkg-config
but I think it actually needed make and gcc.但我认为它实际上需要make和gcc。 Error output said (on my machine, among other):
错误输出说(在我的机器上,等等):
"You have to install development tools first." “你必须先安装开发工具。”
So it was an obvious decision and it helped.所以这是一个显而易见的决定,而且很有帮助。
I can see most of the people have found the solution to this problem, this is mostly coused but not limited to missing packages, this happened to me after I have purged mysql and reinstalled it.我可以看到大多数人都找到了这个问题的解决方案,这主要是因为但不限于缺少包,这发生在我清除 mysql 并重新安装它之后。 I had to run this command in order to fix my problem:
我必须运行这个命令才能解决我的问题:
sudo apt-get install libmysqlclient-dev
This command helped me fix my problem这个命令帮助我解决了我的问题
I was running into this error on my mac and found that I needed to upgrade from mysql 32bit to mysql 64 bit to get this error to go away.我在我的 Mac 上遇到了这个错误,发现我需要从 mysql 32 位升级到 mysql 64 位才能让这个错误消失。 I was running OSX 10.6 on an intel macbook pro with ruby 1.9.2 and rails3.0.0
我在带有 ruby 1.9.2 和 rails3.0.0 的英特尔 macbook pro 上运行 OSX 10.6
I also needed to install xcode in order to get unix utilities like "make" that are required to compile the gem.我还需要安装 xcode 以获得编译 gem 所需的诸如“make”之类的 unix 实用程序。
once this was done I was able to run gem install mysql and gem install mysql2 without error.一旦完成,我就可以运行 gem install mysql 和 gem install mysql2 而没有错误。
对于 windows 用户:您设置 mysql 的 lib 和包含路径,例如,如果您使用 xampp,您可以这样:
gem install mysql2 -- '--with-mysql-lib="C:\xampp\mysql\lib" --withmysql-include="C:\xampp\mysql\include"'
Solution only works on Mac OS X解决方案仅适用于 Mac OS X
If you've installed MySQL with homebrew, what worked for me was uninstalling MySQL, and installing MySQL Community Edition via the MySQL website ( https://www.mysql.com/ ).如果您使用自制软件安装了 MySQL,那么对我有用的是卸载 MySQL,并通过 MySQL 网站 ( https://www.mysql.com/ ) 安装 MySQL Community Edition。
After installed, just re-enter the command to gem install mysql2
or if necessary, sudo gem install mysql2
, if you are getting permission denied problems.安装后,只需重新输入命令
gem install mysql2
或必要时, sudo gem install mysql2
,如果您遇到权限被拒绝的问题。
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.我得到了基于 Mac OS X 10.6.6 构建的 gem 1)确保安装了开发工具包 2)从源代码下载当前的 MySQL 包(在我的例子中是 5.5.8) 3)从 cmake.org 安装 cmake 工具 4 ) 按照 mysql 分发文件中 INSTALL-SOURCE 的 2.11 节中的说明进行操作 5)
sudo gem install mysql2 -- --srcdir=/usr/local/mysql/include
gem 构建成功,但是文档中有两个错误rdoc 和 ri 抱怨。 But now when I try to require 'mysql2' I get a但是现在当我尝试要求'mysql2'时,我得到了一个
LoadError: no such file to load -- mysql2/mysql2
I was hoping the error I would get was that the libmysqlclient.16.dylib couldn't be found because we figured that out in another post (search for install_name_tool).我希望得到的错误是找不到 libmysqlclient.16.dylib,因为我们在另一篇文章中发现了这一点(搜索 install_name_tool)。
My $PATH has /usr/local/mysql in it (that's where my source and built files are located), so I'm a little stumped.我的 $PATH 中有 /usr/local/mysql (这是我的源文件和构建文件所在的位置),所以我有点难过。 If anyone has any thoughts, I'll check back after a few hours of sleep.
如果有人有任何想法,我会在睡几个小时后回来查看。
download the right version of mysqllib.dll then copy it to ruby bin really works for me.下载正确版本的 mysqllib.dll 然后将其复制到 ruby bin 对我来说真的很有效。 Follow this link plases mysql2 gem compiled for wrong mysql client library
按照此链接将mysql2 gem 编译为错误的 mysql 客户端库
Got the "You have to install development tools first."得到“你必须先安装开发工具”。 error when trying to install the mysql2 gem after upgrading to Mac OS X Mountain Lion .
升级到 Mac OS X Mountain Lion 后尝试安装 mysql2 gem 时出错。 Apparently doing this upgrade removes the command line compilers.
显然进行此升级会删除命令行编译器。
To fix:修理:
You are getting this problem because you have not install MySql.您遇到此问题是因为您尚未安装 MySql。 Before install mysql2 gem.
在安装 mysql2 gem 之前。 Install MySQL.
安装 MySQL。 After that mysql2 gem will install.
之后将安装 mysql2 gem。
I just wanted to add this answer specifically for Mac Users .我只是想专门为Mac Users添加这个答案。
My server was running perfectly fine until I updated my xcode.在我更新我的 xcode 之前,我的服务器运行得非常好。 The while starting my rails server the error was shown like this
在启动我的 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)
And there was suggestion to install mysql2 gem at the end of the error message.并且在错误消息的末尾建议安装 mysql2 gem。 So when i tried installing it I got the error as above mentioned in this question.
所以当我尝试安装它时,我得到了上面这个问题中提到的错误。 The error I got is as follows
我得到的错误如下
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
So as suggested in this post I tried 'brew install mysql' and that quitted saying that mysql version so and so already installed.因此,正如这篇文章中所建议的那样,我尝试了“brew install mysql”,然后退出说已经安装了 mysql 版本的某某。 But there was warning before it saying
但在它说之前有警告
Warning: You have not agreed to the Xcode license.
Builds will fail! Agree to the license by opening Xcode.app or running:
xcodebuild -license
Then I tried this sudo xcodebuild -license
and type 'agree' at the end.然后我尝试了这个
sudo xcodebuild -license
并在最后输入“同意”。 You have to be root to agree to the license.您必须是 root 才能同意许可。
After this, I again tried bundle install and then everything is working fine as normal.在此之后,我再次尝试捆绑安装,然后一切正常。 [ Even due to this xcode updation, I had problem with my tower also.]
[即使由于这个 xcode 更新,我的塔也有问题。]
libmysql-ruby
has been phased out and replaced. libmysql-ruby
已被淘汰和替换。 New command:新命令:
sudo apt-get install ruby-mysql libmysqlclient-dev
If still getting error then follow the steps of mysql2 gem installation on Rails 3 on -如果仍然出现错误,请按照在 Rails 3 上安装 mysql2 gem 的步骤 -
http://rorguide.blogspot.com/2011/03/installing-mysql2-gem-on-ruby-192-and.html http://rorguide.blogspot.com/2011/03/installing-mysql2-gem-on-ruby-192-and.html
where most of the user were able to install mysql2 gem.大多数用户都能够安装mysql2 gem。
在 ubuntu 20.04 LTS 下,这是唯一对我有用的解决方案:
sudo apt-get install ruby-mysql2
According to https://github.com/brianmario/mysql2/issues/1175 , I fixed it by根据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
An updated solution for Windows users. Windows 用户的更新解决方案。 I am running
我在跑步
Windows 10 Windows 10
MySql 8.0.30 MySql 8.0.30
Ruby 3.1.2 Ruby 3.1.2
My Ruby version is the one from rubyinstaller.org with the MYSYS devkit included我的 Ruby 版本是来自 rubyinstaller.org 的版本,其中包含 MYSYS devkit
I downloaded a MariaDB C connector library msi installer file v3.1.17, ran it and it installed the relevant files in a directory in Program Files.我下载了 MariaDB C 连接器库 msi 安装程序文件 v3.1.17,运行它并将相关文件安装在 Program Files 的目录中。 I copied that directory to another location, renamed it so that it didn't have any spaces and ran the command:
我将该目录复制到另一个位置,将其重命名,使其没有任何空格并运行命令:
gem install mysql2 --platform=ruby -- --with-mysql-dir="c:/my-mariadb-connector-path-with-no-spaces"
This gave me a functioning gem.这给了我一个正常工作的宝石。
This is a specific version of the library and not the most recent.这是库的特定版本,而不是最新版本。 Finding your way around the download options on the MariaDB website is a bit confusing, so here is the direct download link.
在 MariaDB 网站上查找下载选项有点令人困惑,所以这里是直接下载链接。
https://dlm.mariadb.com/2319542/Connectors/c/connector-c-3.1.17/mariadb-connector-c-3.1.17-win64.msi https://dlm.mariadb.com/2319542/Connectors/c/connector-c-3.1.17/mariadb-connector-c-3.1.17-win64.msi
Before I got to this point I'd tried several things which hadn't worked.在我达到这一点之前,我尝试了几件没有奏效的事情。 I've included them below as background information, but if all you're interested in is a working solution then you can stop reading now.
我已将它们作为背景信息包含在下面,但如果您感兴趣的只是一个可行的解决方案,那么您现在可以停止阅读。
A few sources suggest that if you have the MYSYS devkit installed you can build the gem with the following command:一些消息来源表明,如果您安装了 MYSYS devkit,则可以使用以下命令构建 gem:
ridk exec gem install mysql2 --platform=ruby -- --use-system-libraries
This did not compile for me.这没有为我编译。
You may also have seen a recommendation to download an archived version - 6.1 - of the MySQl connector library.您可能还看到了下载 MySQl 连接器库的存档版本 - 6.1 的建议。 I tried this, the gem compiled, but when I tried to start my app I got an error message: "Incorrect MySQL client library version. This gem was compiled for 6.1.11 but the client library is 10.5.5."
我试过这个,编译了 gem,但是当我尝试启动我的应用程序时,我收到一条错误消息:“MySQL 客户端库版本不正确。这个 gem 是为 6.1.11 编译的,但客户端库是 10.5.5。”
In MySQL V8 and above the C library is no longer an optional extra but is included in the main installation.在 MySQL V8 及更高版本中,C 库不再是可选的附加组件,而是包含在主安装中。 I found the relevant files, copied them to a no spaces directory path and tried the usual command.
我找到了相关文件,将它们复制到无空格目录路径并尝试了常用命令。 Again the gem compiled but the app wouldn't start.
再次编译 gem,但应用程序无法启动。 This time the error message said that "This gem was compiled for 8.0.30 but the client library is 10.5.5"
这次错误消息说“这个 gem 是为 8.0.30 编译的,但是客户端库是 10.5.5”
I also tried the latest version of of the MariaDB library.我还尝试了最新版本的 MariaDB 库。 With that the error message said that the gem was compiled for 10.6.8.
有了这个错误消息说 gem 是为 10.6.8 编译的。 You need the specific version described above, and yes, it really is a MariaDB library you need even if you're using "Original" MySql as distributed by the Oracle Corporation
您需要上述特定版本,是的,它确实是您需要的 MariaDB 库,即使您使用的是 Oracle Corporation 分发的“原始”MySql
After you get the mysql-dev issues corrected, you may need to remove the bad mysql2 install.纠正 mysql-dev 问题后,您可能需要删除错误的 mysql2 安装。 Look carefully at the messages after $ bundle install.
仔细查看 $ bundle install 之后的消息。 You may need to
你可能需要
rm -rf vendor/cache/
rm -rf ./Zentest
This will clear out the bad mysql2 installation so that a final $ bundle install can create a good one.这将清除错误的 mysql2 安装,以便最终的 $ bundle install 可以创建一个好的安装。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.