Rails安装mysql - 安装mysql2出错:ERROR: 无法构建本地扩展宝石。

3
我有一个使用 Rails 3.2.13 的应用程序需要部署,但是托管服务需要使用 MySQL,而不是我一直在使用的 sqlite。
在从 sqlite 转换到 MySQL 的过程中,我必须安装 ruby gem mysql2,但是在安装过程中出现了以下错误:

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

我已经尝试了 bundle installgem install mysql2,但是都出现了相同的错误消息。
我知道许多人使用的解决方案是 sudo apt-get install libmysql-ruby libmysqlclient-dev,但是我正在 Windows 上使用 Git Bash,因此我找到了一个适用于 Windows 的等价方法(该问题的 @francois 答案 this question)。我使用安装程序安装了 MySQL Server 5.6 并运行了以下命令:
gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.6\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.6\include"' 

根据我所阅读的,这应该成功安装'mysql2' ruby gem。但是奇怪的是,我仍然遇到了以下错误:

    Temporarily enhancing PATH to include DevKit... Building native extensions with: '--with-mysql-lib="c:\Program

Files\MySQL\MySQL Server 5.6\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.6\i nclude"' This could take a while... ERROR: Error installing mysql2: ERROR: Failed to build gem native extension.

        c:/RailsInstaller/Ruby1.9.3/bin/ruby.exe extconf.rb --with-mysql-lib="c:\Pro gram Files\MySQL\MySQL Server 5.6\lib" --with-mysql-include="c:\Program Files\My SQL\MySQL Server 5.6\include" checking for rb_thread_blocking_region()... yes checking for rb_wait_for_single_fd()... yes checking for rb_hash_dup()... yes

checking for rb_intern3()... yes checking for main() in -llibmysql... yes checking for mysql.h... yes checking for errmsg.h... yes checking for mysqld_error.h... yes creating Makefile

    make generating mysql2-i386-mingw32.def compiling client.c client.c: In function 'rb_raise_mysql2_error': client.c:139:3:

warning: ISO C90 forbids mixed declarations and code client.c: In function 'finish_and_mark_inactive': client.c:508:3: warning: ISO C90 forbids mixed declarations and code client.c: In function 'rb_mysql_client_abandon_results': client.c:535:3: warning: ISO C90 forbids mixed declarations and code client.c: In function 'rb_mysql_client_next_result': client.c:938:5: warning: ISO C90 forbids mixed declarations and code compiling mysql2_ext.c compiling result.c result.c: In function 'rb_mysql_result_fetch_fields': result.c:407:35: warning: comparison between signed and unsigned integer express ions linking shared-object mysql2/mysql2.so client.o: In function nogvl_connect': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:158: undefined reference tomysql_real_connect@32' client.o: In function nogvl_init': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:150: undefined reference tomysql_init@4' client.o: In function set_ssl_options': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:1078: undefined reference tomysql_ssl_set@24' client.o: In function mysql_client_options': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:723: undefined reference tomysql_options@12' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:727: undefined reference to mysql_error@4' client.o: In functionrb_mysql_info': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:267: undefined reference to mysql_info@4' client.o: In functionrb_mysql_client_warning_count': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:257: undefined reference to mysql_warning_count@4' client.o: In functionnogvl_do_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:370: undefined reference to mysql_store_result@4' client.o: In functionrb_mysql_client_more_results': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:923: undefined reference to mysql_more_results@4' client.o: In functionnogvl_select_db': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:863: undefined reference to mysql_select_db@8' client.o: In functionnogvl_ping': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:894: undefined reference to mysql_ping@4' client.o: In functionrb_mysql_client_thread_id': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:856: undefined reference to mysql_thread_id@4' client.o: In functionrb_mysql_client_last_id': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:825: undefined reference to mysql_insert_id@4' client.o: In functionnogvl_read_query_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:357: undefined reference to mysql_read_query_result@4' client.o: In functionrb_mysql_client_server_info': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:788: undefined reference to mysql_get_server_version@4' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:789: undefined reference tomysql_get_server_info@4' client.o: In function rb_mysql_client_info': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:756: undefined reference tomysql_get_client_version@0' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:757: undefined reference to mysql_get_client_info@0' client.o: In functionrb_mysql_client_real_escape': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:662: undefined reference to mysql_real_escape_string@16' client.o: In functionfinish_and_mark_inactive': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:515: undefined reference to mysql_free_result@4' client.o: In functionnogvl_send_query': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:334: undefined reference to mysql_send_query@12' client.o: In functionnogvl_close': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:190: undefined reference to mysql_close@4' client.o: In functionrb_mysql_client_escape': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:238: undefined reference to mysql_escape_string@12' client.o: In functionrb_raise_mysql2_error': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:125: undefined reference to mysql_error@4' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:126: undefined reference tomysql_sqlstate@4' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:140: undefined reference to mysql_errno@4' client.o: In functionrb_connect': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:297: undefined reference to mysql_errno@4' client.o: In functionrb_mysql_client_store_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:966: undefined reference to mysql_errno@4' client.o: In functionrb_mysql_client_next_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:939: undefined reference to mysql_next_result@4' client.o: In functionrb_mysql_client_affected_rows': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:839: undefined reference to mysql_affected_rows@4' client.o: In functionrb_mysql_client_async_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:419: undefined reference to mysql_errno@4' client.o: In functionrb_mysql_client_abandon_results': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:539: undefined reference to mysql_next_result@4' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:538: undefined reference tomysql_more_results@4' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:547: undefined reference to mysql_free_result@4' client.o: In functionnogvl_do_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:368: undefined reference to mysql_use_result@4' client.o: In functionnogvl_close': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:190: undefined reference to mysql_close@4' client.o: In functionset_charset_name': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:1067: undefined reference to mysql_options@12' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:1069: undefined reference tomysql_error@4' client.o: In function init_mysql2_client': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/cl ient.c:1105: undefined reference tomysql_get_client_info@0' result.o: In function rb_mysql_result_count': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:576: undefined reference tomysql_num_rows@4' result.o: In function rb_mysql_result_fetch_field': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:126: undefined reference tomysql_fetch_field_direct@8' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:114: undefined reference to mysql_num_fields@4' result.o: In functionrb_mysql_result_fetch_fields': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:403: undefined reference to mysql_num_fields@4' result.o: In functionnogvl_fetch_row': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:105: undefined reference to mysql_fetch_row@4' result.o: In functionrb_mysql_result_free_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:75: undefined reference to mysql_free_result@4' result.o: In functionrb_mysql_result_fetch_row': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:213: undefined reference to mysql_fetch_lengths@4' c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:215: undefined reference tomysql_num_fields@4' result.o: In function rb_mysql_result_each': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:503: undefined reference tomysql_fetch_fields@4' result.o: In function rb_mysql_result_free_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:75: undefined reference tomysql_free_result@4' result.o: In function rb_mysql_result_each': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:531: undefined reference tomysql_fetch_fields@4' result.o: In function rb_mysql_result_free_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:75: undefined reference tomysql_free_result@4' result.o: In function rb_mysql_result_each': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:490: undefined reference tomysql_num_rows@4' result.o: In function rb_mysql_result_free_result': c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\mysql2-0.3.13\ext\mysql2/re sult.c:75: undefined reference tomysql_free_result@4' collect2: ld returned 1 exit status make: *** [mysql2.so] Error 1

    Gem files will remain installed in c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9 .1/gems/mysql2-0.3.13

for inspection. Results logged to c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0. 3.13/ext/mysql2/gem_make.out

Can anyone please help?  I am having much trouble comprehending what's wrong with the installation.
5个回答

14

我在Win64环境中遇到了相同的错误。

我尝试了很多解决方案,但唯一有效的是:

  • gem install mysql2 -v '0.3.11'

1
谢谢你!你救了我!! - writeToBhuwan
谢谢,我已经尝试了所有的方法! - Will

9
我一直被同一个问题困扰,最终成功安装了mysql2。感谢odiszapc@github。我发现谷歌上除下面这个解决方案以外的其他方法对我都不起作用。
这段话是从这里复制粘贴来的,所以不归功于我。
gem uninstall mysql2

Download last MySQL connector from http://cdn.mysql.com/Downloads/Connector-C/mysql-    connector-c-noinstall-6.0.2-win32.zip

Extract it to C:\connector-6.0.2

gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\connector-6.0.2\lib" --with-mysql-include="C:\connector-6.0.2\include" --with-mysql-dir="C:\connector-6.0.2"'

我的附加信息。

  • ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
  • Rails 3.2.13
  • MySQL Server 5.6
  • mysql2-0.3.13

附注: 即使您成功安装了mysql2,您可能仍然需要一些工作(例如mysql2.so(LoadError)),这与我的情况有关,并且似乎是关于mysql2的又一个重大故障。


我不得不删除一些库和包含选项,因为它抱怨找不到它们...很奇怪...对于我来说,命令是:gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:\connector-6.0.2"' - Mark Murphy

2

我正在安装在Windows 7 64位上。 对我有用的是:

从以下链接下载32位C连接器压缩包:http://dev.mysql.com/downloads/connector/c/

我将其提取到C:\ Temp。 如果您使用不同的目录,请调整下一个命令。

gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:\Temp\mysql-connector-c-6.1.3-win32"'

0

痛苦...很多痛苦。最终做到了:

(平台:Windows64位,Ruby32位,DevKit32位...)

  1. http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick下载C语言的mysql连接器。
  2. 将连接器解压缩到一个没有空格的目录中!
  3. 将lib/libmysql.dll文件复制到ruby/bin目录中。
  4. gem install mysql2 -- --with-mysql-dir=D:\RoR\mysql-connector-c(因此提供连接器的目录而不是服务器的目录)。

然后我成功地运行了mysql2 v0.3.17...


-1
  1. http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick下载Mysql连接器。
  2. 解压它。复制lib\libmysql.dll文件并将其粘贴到Ruby的bin目录中,例如:C:\RailsInstaller\Ruby1.9.3\bin
  3. 现在重新安装Mysql2 gem
    gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.0\lib\opt" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.0\include"'
    注意:根据您的情况更改mysql安装路径
  4. 尝试捆绑(bundle)吧

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接