在 windows 上安装 ruby 和 mysql2 7 rails 上的 64 位 returns 错误

installing ruby with mysql2 on windows 7 64 bit returns error on rails s

我尝试在 windows 7 上安装 ruby 并使用 mysql2

已下载 windows 7 64 位的最新安装程序 here 下载 Devkit 并按照此步骤找到 here 以安装 devkit

这个gem安装了吗rails

已从 here following the instruction found here

下载 mysql

现在安装完所有内容后,我使用此行创建了我的应用程序

rails 新 test_app -d mysql

将我的目录更改为我的新应用程序后

i 运行 这一行 rails s

我收到了这个错误

C:\Ruby22-x64\my_test_app>rails s
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2/mysq
l2.rb:2:in `require': cannot load such file -- mysql2/2.2/mysql2 (LoadError)
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/li
b/mysql2/mysql2.rb:2:in `<top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/li
b/mysql2.rb:31:in `require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/li
b/mysql2.rb:31:in `<top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.4/lib/bundler/ru
ntime.rb:76:in `require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.4/lib/bundler/ru
ntime.rb:76:in `block (2 levels) in require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.4/lib/bundler/ru
ntime.rb:72:in `each'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.4/lib/bundler/ru
ntime.rb:72:in `block in require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.4/lib/bundler/ru
ntime.rb:61:in `each'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.4/lib/bundler/ru
ntime.rb:61:in `require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.4/lib/bundler.rb
:134:in `require'
        from C:/Ruby22-x64/my_test_app/config/application.rb:7:in `<top (require
d)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:78:in `require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:78:in `block in server'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:75:in `tap'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:75:in `server'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:39:in `run_command!'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

我在这里缺少什么? mysql2 在我的 Gemfile 里面。

虽然我没有将我的 rails 连接到我的数据库或配置 database.yml,因为我找不到任何关于如何做的说明。

在 config/database.yml 中对您的开发环境进行以下设置:

development:
  adapter: mysql2 
  database: db_name 
  username: your_username
  password: your_password

基本上following the instructions in here 我刚刚在 Windows 7 64 位上完成了从零到功能齐全的安装。

在你的情况下,问题可能在于 gem mysql2 的版本或它的安装方式;您可以先卸载并重新安装 0.2.6 版本 @see Gediminas answer,如果您有不同的版本,请检查 GemFile。 这使我免于遇到以下问题:

--with-mysql-dir=.... --with-mysql-lib=.... --with-mysql-include=....


无论如何,我会详细说明我的清单的每个步骤,这样您就可以看到您的安装是否存在差异

注意:根据http://rubyinstaller.org/downloads/我select2.1.*(不是最后一个)我下载了标准的32位版本因为64位版本的 Ruby 在 Windows 区域相对较新,并非所有软件包都已更新以与其兼容。要使用此版本,您需要一些有关编译器和解决依赖性问题的知识,如果您只是想使用该语言,这可能太复杂了。 Also reading on the same page : Ruby 2.0.0 及以上 -->: mingw64-32-4.7.2

cd C:\DevKit 
ruby dk.rb init 
ruby dk.rb install 
devkitvars.bat 
  • 在我的例子中不要忘记复制 libmysql.dll,在安装 mysql community 5.6 之后,我发现三个不同的 dll(维度)具有相同的名称:
cd \
find . -name "libmysql.dll"
./Program Files/MySQL/MySQL Connector.C 6.1/lib/libmysql.dll
./Program Files/MySQL/MySQL Server 5.6/lib/libmysql.dll
./Program Files/MySQL/MySQL Workbench 6.2 CE/libmysql.dll``

我肯定选择这个选项:

C:>cd C:\Ruby21\bin>
C:\Ruby21\bin> copy "\Program Files\MySQL\MySQL Server 5.6\lib\libmysql.dll" .
        1 file(s) copied.
  • 我在安装 gems 时遇到了一些问题,因此我决定更新 rubigems,this helps me a lot.
C:\Ruby21\bin>gem update --system
...
[snip]
...
C:\Ruby21\bin>gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 2.4.6
  - RUBY VERSION: 2.1.6 (2015-04-13 patchlevel 336) [i386-mingw32]
  - INSTALLATION DIRECTORY: C:/Ruby21/lib/ruby/gems/2.1.0
  - RUBY EXECUTABLE: C:/Ruby21/bin/ruby.exe
  - EXECUTABLE DIRECTORY: C:/Ruby21/bin
  - SPEC CACHE DIRECTORY: C:/Users/rondadev/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: C:/ProgramData
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-mingw32
  - GEM PATHS:
     - C:/Ruby21/lib/ruby/gems/2.1.0
     - C:/Users/rondadev/.gem/ruby/2.1.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - C:\DevKit\bin
     - C:\DevKit\mingw\bin
     - C:\Ruby21\bin
     - C:\Windows\system32
     - C:\Windows
     - C:\Windows\System32\Wbem
     - C:\Windows\System32\WindowsPowerShell\v1.0\
     - C:\Program Files (x86)\Windows Kits.1\Windows Performance Toolkit\
     - C:\Program Files\Microsoft SQL Server0\Tools\Binn\
     - C:\Program Files (x86)\Microsoft SDKs\TypeScript.0\
     - C:\Program Files (x86)\MySQL\MySQL Fabric 1.5.4

准备安装 rails 并创建第一个应用程序:

gem install rails

gem install mysql2 -v 0.2.6

rails new test_app -d mysql

最后一步:

  1. 已连接 MySQL Workbench 并创建了一个名为 test_app_development(要创建新模式,您可以按工具栏中带有圆柱体和加号的第 4 个图标。请参阅下一个屏幕截图)

  2. config/database.yml 中的固定用户名和密码,如 sansarp 答案

  3. 迁移了数据库

迁移后你的模式应该是这样的:

  1. 执行rails -s

  2. 浏览器打开成功http://localhost:3000

```

C:\Projects\test_app>rake db:migrate --trace
DL is deprecated, please use Fiddle
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump

C:\Projects\test_app>rails s
DL is deprecated, please use Fiddle
=> Booting WEBrick
=> Rails 4.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-04-29 00:20:21] INFO  WEBrick 1.3.1
[2015-04-29 00:20:21] INFO  ruby 2.1.6 (2015-04-13) [i386-mingw32]
[2015-04-29 00:20:21] INFO  WEBrick::HTTPServer#start: pid=4144 port=3000


Started GET "/" for ::1 at 2015-04-29 00:20:34 +0200
Processing by Rails::WelcomeController#index as HTML
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/templates
/rails/welcome/index.html.erb (0.0ms)
Completed 200 OK in 31ms (Views: 15.6ms | ActiveRecord: 0.0ms)