在Windows上安装MySQL2 Gem

5
我遇到了在Windows 8.1机器上安装mysql2 gem的问题。我按照这篇文章中的指示进行操作:

在Windows 7上安装Ruby MYSQL2 gem

安装了mysql2 gem,并且没有收到任何错误消息。

接着我进行了下面的步骤。

bundle install 

执行命令,并确认以下 gem 已安装(使用 bundle show),确认我已安装以下 gem:

Using devise (3.2.2)
Using mysql2 (0.3.14)

然后我尝试做:

rails generate devise:install

这就是我得到的内容:

C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/mysql2-0.3.14/lib/mysql2.rb:8:in `require': 126: The specified module could not be found.   
- C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/mysql2-0.3.14/lib/mysql2/mysql2.so (LoadError)
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/mysql2-0.3.14/lib/mysql2.rb:8:in `<top (required)>'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:76:in `require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:72:in `each'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:72:in `block in require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:61:in `each'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:61:in `require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler.rb:131:in `require'
    from C:/Users/Joseph/googledrive/projects/rails/test_new_devise/config/application.rb:7:in `<top (required)>'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.2/lib/rails/commands.rb:43:in `require'
    from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.2/lib/rails/commands.rb:43:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

有什么想法吗?

解决方案:

答案是从stackoverflow的两个答案组合而来,加上一些修改。 stackoverflow参考资料如下:

在Windows 7上安装Ruby MYSQL2 gem ...mysql2/mysql2.so:[BUG]Segmentation fault ruby 2.0.0p247

底线是:要在Windows 8.1上使用64位环境和Ruby 2使mysql2正常工作,您需要执行以下操作:

  1. 清理:您来到这里的原因可能是尝试安装此gem并失败了,因此您需要进行一些清理:gem uninstall mysql2

  2. 下载Windows的Ruby 2.0 64位版本:

http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.0.0-p353-x64.exe?direct

  1. Run the installer. Note the directory it installs to, and make sure it's in the User PATH. go to

    Control Panel > System and Security > System - Advanced System Settings > Environment Variables
    
请确保 Ruby 安装目录下的 bin 目录路径已添加到 LOCAL 用户的 PATH 环境变量中(如果添加到 SYSTEM Path 环境变量也可以)。
3. 下载 Ruby 2 64 位 Dev Kit:

http://cdn.rubyinstaller.org/archives/devkits/DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe

  1. Run the installer and note the location of the install directory. The instructions say that once you're done with the install, you need to run the devkitvars.bat file to set the environment variables. This DOES NOT work for Windows 8.1. You need to repeat the instructions to set the local path described in #2 above, to set the path environment variable for the local user to add the ruby dev kit bin directory, and the ruby dev kit mingw bin directory. For example, if the dev kit was installed to c:\ruby2devkit, and you installed ruby2 to the C:\Ruby200-x64 directory, then your edit your path statement to look like this:

    C:\Ruby200-x64\bin;C:\ruby2-devkit\bin;c:\ruby2-devkit\mingw\bin
    
  2. Next you need to install the mysql-connector. DO NOT download and run the self-installer. Instead, download the zip file and unpack it. Note the directory you unpacked it to (for the purposes of this post, let's assume you unpacked it to c:\mysql-connector:

http://dev.mysql.com/downloads/file.php?id=450612

  1. The libmysql.lib included in the MySQL Connector 64 bit is not compatible with the mingw64-gcc compiler. You need to generate mingw64 compatible libmysql.lib file.

  2. Download the tools you need

    https://structure-svm-map.googlecode.com/files/svm-map-win.zip

  3. Unzip this file to a local directory, let's assume that you unzipped it to c:\svm-map

  4. Edit the PATH environment path, as described earlier, to now look like this:

    C:\Ruby200-x64\bin;C:\ruby2-devkit\bin;c:\ruby2-devkit\mingw\bin;c:\svm-map;C:\svm-map\python-mingw-lib
    
  5. Generate the new mysql libraries:

    (make sure you're running as administrator)
    
    cd c:\mysql-connector\lib
    
    gendef.exe libmysql.dll
    
    dlltool -v --dllname libmysql.dll --def libmysql.def --output-lib libmysql.lib
    
    copy libmysql.dll C:\Ruby200-x64\bin
    
    copy libmysql.lib C:\Ruby200-x64\bin
    
  6. Install the gem as follows (note the use of forward slashes instead of backslashes, it will fail if you use backslashes):

    gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/mysql-connector/"'

希望这可以帮助你,也希望有人能关注一下那些想在Windows 8.1上进行Ruby on Rails开发的用户。

try gem install mysql2 - house9
1
MySQL2 gem已经安装。请仔细阅读我的问题。 - EastsideDev
我使用64位Ruby 2.0在Windows8.1 64位上运行。我可以确认这个解决方案是有效的。在第9步之前,我必须执行一个额外的步骤。当我尝试其他人的解决方案时,Bundle无法安装mysql gem,但它仍然创建了gem目录并安装了一些文件。在第9步之前删除它们使它工作! - Peter Kirby
谢谢Peter Kirby。我会将此添加为步骤0。 - EastsideDev
在第10步,我建议使用管理员权限执行命令。 gendef.exe 没有通知文件 libmysql.def 未生成。 - Leonel Sanches da Silva
2个回答

8
当您安装了mysql2时,是否下载了它所需的库文件?
我没有在Windows 8上安装过它,但是Windows 7需要this file来提供mysql所需的依赖项以帮助其工作。您可以查看各种 教程了解如何做到这一点,我将在此概述一个简单的概览: - 解压mysql connector file C:\mysql-connector-c-6.1.3-win32\(或类似路径,不带任何空格) - 再次运行mysql2 gem安装程序:
gem uninstall mydsql
gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\mysql-connector\lib" --with-mysql-include="C:\mysql-connector\include" --with-mysql-dir="C:\mysql-connector"'

底线是我认为你的mysql2 gem没有加载运行所需的文件,可能没有显示错误,因为它是Windows 8。

1
我正在使用64位连接器,而不是32位。除此之外,如果您查看我的原始问题,我说我按照我包含的链接中的说明,那是一个更详细的版本您在此提供的建议。唯一的问题是,您提供的示例不起作用,因为它使用Windows风格的目录分隔符,但安装程序预期Linux风格的目录分隔符。 - EastsideDev
1
在 Windows 8 和 Connector 6.1.6 中,你必须仅设置 mysql-dir:gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:\mysql-connector-c-6.1.6-winx64"'。 - Juan R
谢谢,伙计 - 你正在运行哪个版本的Ruby? - Richard Peck
这适用于我,Windows 10 - Ruby 3.0.2 / MySQL 8。 - Fahad
终于在@RichardPeck的帮助下解决了问题,但接下来我遇到了与tzinfo-data相关的问题。在像这样注释掉平台部分gem "tzinfo-data" # , platforms: %i[ mingw mswin x64_mingw jruby ]之后,我终于让我的应用程序运行起来了...现在要切换到mssql :D - Mladen Oršolić

3
我希望这些内容能在安装MariaDB上使用mysql2时对在Windows 10机器上的用户有所帮助。
出于某种原因,我尝试使用连接器库遇到了许多问题。此外,我可以下载的库版本是6,而我使用的MariaDB版本使用的是10版库。
我首先下载并安装了MariaDB。
我使用了以下命令:
gem install mysql2 --platform=ruby -- --with-mysql-lib="path_to:\MariaDB 10.5\"

这样编译并安装gem没有任何问题。

在database.yml文件中:

default: &default adapter: mysql2 encoding: utf8 reconnect: false database: 数据库名称 pool: 5 username: 数据库用户 password: 数据库密码 socket: tmp/mysql.sock


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