【问题标题】:Can't get Rails Server to work with MySQL无法让 Rails Server 与 MySQL 一起使用
【发布时间】:2026-01-30 03:15:01
【问题描述】:

我正在尝试让我的 Rails 应用程序与 MySQL 而不是默认 SQLite 一起使用。我创建了一个新项目,强制使用似乎可以正常工作的 MySQL。

它在 Gem 文件中添加了 gem 条目,如下所示:

source 'https://rubygems.org'

    gem 'rails', '3.2.13'

    # Bundle edge Rails instead:
    # gem 'rails', :git => 'git://github.com/rails/rails.git'

    gem 'mysql2'

当我运行 bundle 命令时,它显示它正在使用 mysql gem:

Using mysql2 <0.3.11>

我也配置了 database.yml 文件:

development:
 adapter: mysql2
 encoding: utf8
 reconnect: false
 database: dbname
 pool: 5
 username: uname
 password: pass
 host: hostname

test:
 development:
 adapter: mysql2
 encoding: utf8
 reconnect: false
 database: dbname
 pool: 5
 username: uname
 password: pass
 host: hostname

production:
 development:
 adapter: mysql2
 encoding: utf8
 reconnect: false
 database: dbname
 pool: 5
 username: uname
 password: pass
 host: hostname

但是当我尝试运行 rails 服务器时,我得到了这个:

C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/l ib/mysql2/mysql2.rb:2:in require': 126: The specified module could not be found . - C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-min gw32/lib/mysql2/1.9/mysql2.so (LoadError) from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11- x86-mingw32/lib/mysql2/mysql2.rb:2:in' 来自 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11- x86-mingw32/lib/mysql2.rb:9:in require' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11- x86-mingw32/lib/mysql2.rb:9:in' 来自 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.3.4/ lib/bundler/runtime.rb:72:in require' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.3.4/ lib/bundler/runtime.rb:72:inblock (2 级别) in require' 来自 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.3.4/ lib/bundler/runtime.rb:70:in each' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.3.4/ lib/bundler/runtime.rb:70:inblock in require' 来自 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.3.4/ lib/bundler/runtime.rb:59:in each' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.3.4/ lib/bundler/runtime.rb:59:inrequire' 来自 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.3.4/ lib/bundle.rb:132:in require' from C:/Users/n00151956/Desktop/RubyProjects/Demo/config/application.rb: 7:in' 来自 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1 3/lib/rails/commands.rb:53:in require' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1 3/lib/rails/commands.rb:53:inblock in ' 来自 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1 3/lib/rails/commands.rb:50:in tap' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1 3/lib/rails/commands.rb:50:in' 来自脚本/rails:6:in require' from script/rails:6:in'

我能够让 Rails 服务器使用默认 SQLite 运行,但在我的一生中,我无法让它与 MySQL 一起使用。如果有人可以帮助我解决这个问题,那将是很大的帮助!

谢谢

【问题讨论】:

  • 尝试运行bundle exec rails server
  • 返回相同的东西。
  • 你下载了MySQL Connector/C,然后将libmysql.dll从它复制到你的railsinstaller安装吗?
  • 不,我该怎么做?

标签: ruby-on-rails


【解决方案1】:
  1. 从 -mysql-connector 下载 libmysql.dll 文件并将其放入 C:\RailsInstaller\Ruby1.9.3\bin 应该在这里Image
  2. 以管理员身份打开命令提示符并以如下方式启动mysql服务器:C:\Program Files\MySQL\MySQL Server 5.0\bin\mysql

更新

development:
  adapter: mysql2
  database: proj_development
  username: root
  password: pass
  host: 127.0.0.1
  socket: /tmp/mysql.sock

test:
  adapter: mysql2
  database: proj_test
  username: root
  password: pass
  host: 127.0.0.1
  socket: /tmp/mysql.sock

production:
  adapter: mysql2
  database: proj_production
  username: root
  password: pass
  host: 127.0.0.1
  socket: /tmp/mysql.sock

【讨论】:

  • 当我运行第二个命令时,我得到了这个错误:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
  • 如果 MySQL 不在 localhost 上运行怎么办?我让它在不同的服务器上运行。
  • 很好,你给我的那个错误我引用了ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061),这表明你的应用程序正在尝试连接到你的本地主机。但是,如果您的 MySQL 在不同的服务器上运行,则输入正确的主机名。默认情况下,它将尝试连接到 localhost
  • 我已经在 database.yml 文件中指定了完整的主机名,但它仍然无法正常工作
  • 好吧,从进一步的研究看来,来自 MySQL 网站 - dev.mysql.com/doc/refman/5.6/en/can-not-connect-to-server.html 它指出:错误 (2003) Can't connect to MySQL server on 'server' (10061) 表明网络连接被拒绝。您应该检查是否有一台 MySQL 服务器正在运行,它是否启用了网络连接,并且您指定的网络端口是服务器上配置的。
【解决方案2】:

Install Mysql如果你没有安装的话。

  1. 下载 MySQL-connector(zip 文件)- 下载 noinstall 版本(不用于安装)。你应该把它解压到c:\mysql-connector-c-your-version-download

  2. libmysql.dllc:\mysql-connector-c-your-version-download复制到C:\RailsInstaller\Ruby1.9.3\bin

  3. 安装 MySQL gem

    gem install mysql --platform=ruby -- --with-mysql-dir=C:/mysql-connector-c-your-version-download

【讨论】: