运行 ‘bundle install’ 或者 ‘gem install mysql2′ 遇到以下错误mysql
> gem install mysql2 ERROR: Error installing mysql2: The 'mysql2' native gem requires installed build tools. Please update your PATH to include build tools or download the DevKit from 'http://rubyinstaller.org/downloads' and follow the instructions at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
或者是git
Error installing mysql2: ERROR: Failed to build gem native extension. C:/Ruby192/bin/ruby.exe extconf.rb
通常是由于没有安装编译器和没有安装mysqlgithub
最简单的解决办法:sql
若是rails 版本<3.1.0就安装mysql 0.2.6。若是rails 版本是3.1.*才须要安装mysql 0.3.*。切记!windows
默认安装的是mysql2 0.3.7,这个版本变化较多,能够安装旧版本gem,经测试安装0.2.6不会报错。ruby
gem install mysql2 -v 0.2.6
若是你必定要安装最新版本,能够参照下面方法。bash
Linux下解决方法:服务器
# sudo apt-get install libmysql-ruby libmysqlclient-dev
Windows下比较麻烦,要先安装DevKit和Mysql。测试
安装DevKit见个人另外一篇文章 windows下安装DevKitui
安装Mysql就不细说了,下载后双击便可
以上两步完成后,要这样安装mysql2
> gem install mysql2 -- '--with-mysql-dir="C:\Program Files\MySQL\MySQL Server 5.5"' #必定要注意引号,不少朋友忘记带引号,致使安装出错 Updating installed gems Updating mysql2 Temporarily enhancing PATH to include DevKit... Building native extensions. This could take a while... Successfully installed mysql2-0.3.2 Gems updated: mysql2 Installing ri documentation for mysql2-0.3.2... Enclosing class/module 'mMysql2' for class Client not known Installing RDoc documentation for mysql2-0.3.2... Enclosing class/module 'mMysql2' for class Client not known
这表示安装成功了,可是使用时可能还会报错。
最后一步:将mysql安装目录下的bin下的libmysql.dll复制到 ruby安装目录/lib下。
注意:为保证版本一致,libmysql.dll不要从网上下载。尽可能从本身安装目录下复制。
附:
有时启动服务器时报错
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (no such file to load -- active_record/connection_adapters/mysql2_adapter) (RuntimeError)
可是当gem install activerecord-mysql2-adapter 时会报找不到这个gem。
通常是由于安装的mysql2版本过新,由于mysql2 0.3.2版本开始放弃支持rails 3.0.*。
解法:
gem uninstall mysql2 gem install mysql2 -v 0.2.7参考文献:http://rubyer.me/blog/138/