3

我正在关注 Lynda 的 ruby​​ on rails 课程,我在使用 Windows 中的命令行连接到数据库时遇到问题。

我可以使用“mysql -u username -p”连接到数据库,然后输入密码。我可以毫无问题地创建数据库。

现在执行时:

rake db:schema:dump

在我的应用程序文件夹“simple_cms”中,我得到以下信息:

rake aborted!
Can't connect to MySQL server on 'localhost' (10061)

Tasks: TOP => db:schema:dump
(See full trace by running task with --trace)

运行跟踪给出:

C:\Users\ASUS\Desktop\simple_cms>rake db:schema:dump --trace
** Invoke db:schema:dump (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:schema:dump
rake aborted!
Can't connect to MySQL server on 'localhost' (10061)
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/client.
rb:44:in `connect'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/client.
rb:44:in `initialize'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/mysql2_adapter.rb:16:in `new'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/mysql2_adapter.rb:16:in `mysql2_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_pool.rb:315:in `new_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_pool.rb:242:in `loop'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_pool.rb:242:in `block in checkout'
C:/Ruby192/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_pool.rb:239:in `checkout'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_pool.rb:102:in `block in connection'
C:/Ruby192/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_pool.rb:101:in `connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/connec
tion_adapters/abstract/connection_specification.rb:145:in `connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/railti
es/databases.rake:379:in `block (4 levels) in <top (required)>'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/railti
es/databases.rake:378:in `open'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.10/lib/active_record/railti
es/databases.rake:378:in `block (3 levels) in <top (required)>'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:228:in `call'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:228:in `block i
n execute'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:223:in `each'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:223:in `execute
'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:166:in `block i
n invoke_with_call_chain'
C:/Ruby192/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_
with_call_chain'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:152:in `invoke'

C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:143:in `
invoke_task'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:101:in `
block (2 levels) in top_level'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:101:in `
each'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:101:in `
block in top_level'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:110:in `
run_with_threads'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:95:in `t
op_level'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:73:in `b
lock in run'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:160:in `
standard_exception_handling'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:70:in `r
un'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-10.0.3/bin/rake:33:in `<top (required)>
'
C:/Ruby192/bin/rake:19:in `load'
C:/Ruby192/bin/rake:19:in `<main>'
Tasks: TOP => db:schema:dump

C:\Users\ASUS\Desktop\simple_cms>

现在在我的 database.yml 文件中,我已经输入了用户名和密码,并记得将整数密码放入“”中,并在冒号和密码本身之间放置一个空格,例如:

# MySQL.  Versions 4.1 and 5.0 are recommended.
# 
# Install the MYSQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: simple_cms_development
  pool: 5
  username: simplelogin
  password: "1234"
  host: localhost

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: simple_cms_test
  pool: 5
  username: root
  password:
  host: localhost

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: simple_cms_production
  pool: 5
  username: root
  password:
  host: localhost

我什至尝试使用此处描述的 ip 切换“localhost”(同样的问题):Rails development - Can't connect to MySQL server on 'localhost' (10061)

但是当我这样做时,我没有从命令行收到任何消息。

那么为什么我无法连接到数据库呢?将不胜感激任何帮助!谢谢!尝试学习 ruby​​ 和整个安装过程已经花费了很多时间......

4

4 回答 4

3

使用 mysql 命令和使用连接器的问题在于 mysql 可以根据其配置方式具有不同的默认参数,因此很难知道发生了什么。

您应该查找一个名为 my.cnf 的文件(如果您在 Windows 上,则为 my.ini)。它可以指定不同的端口或主机。您很幸运能够通过 mysql 连接到数据库,因为您可以看到实际连接的位置。

看看你的端口:

mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show variables like 'socket';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| socket        | /tmp/mysql.sock |
+---------------+-----------------+
1 row in set (0.00 sec)

mysql> show variables like 'hostname';
+---------------+-------------+
| Variable_name | Value       |
+---------------+-------------+
| hostname      | myhostname  |
+---------------+-------------+
1 row in set (0.00 sec)

查看这些值是否与 database.yml 文件匹配。

获得这些值后,您可以尝试在该端口上远程登录。如果该端口上没有任何东西在监听,您将得到如下信息:

$ telnet localhost 11111
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
Trying fe80::1...
telnet: connect to address fe80::1: Connection refused
telnet: Unable to connect to remote host

但如果有连接,你会在某处看到一些带有“mysql”的垃圾:

$ telnet localhost 3306
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
J
5.5.28JzKEXl7?5e|bUcq/5\Q^mysql_native_passwordConnection closed by foreign host.

我在 Unix 上工作,所以你的输出可能看起来不同。

于 2013-01-04T00:42:11.347 回答
3

我在同一个琳达课程。

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: simple_cms_development
  pool: 5
  username: root
  password:
  host: 127.0.0.1
  port: 3306
  socket: C:/xampp/mysql/mysql.sock

我在 Windows 8 上并使用带有 XAMPP 包的 mysql。

localhost我已经尝试了一切,真正解决它的是从127.0.0.1

于 2013-02-24T22:12:07.590 回答
1

我在这个问题上工作了几天,尝试了互联网上几乎所有可用的解决方案。最后,我可以通过添加端口:3306 并使用“rails server”命令重新启动 WEBrick 服务器来解决该错误。

谢谢你。

于 2015-06-23T04:54:45.060 回答
0

我在 Ruby 和命令行中看到了这个可怕的东西!

Can't connect to MySQL server on 'localhost' (10061)

如果上述解决方案不起作用,我怀疑这可能是与缺少 MySQL 服务运行有关的问题。Rails 不能访问 MySQL,除非你解决了 MySQL 的 bug。

诊断

在尝试以下解决方案之前,请转到命令行并尝试从 bin 文件夹再次访问 MySQL:mysql -u username -p

如果你得到这样的东西:

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

然后试试这个:

  1. 以管理员身份运行 cmd(如果不能,您必须咨询您的 IT 代表)
  2. 转到 MySQL 中的 bin 文件夹并使用以下命令安装 MySQL 服务:
  3. mysqld --安装

    您可以收到以下两条消息中的任何一条:

    该服务已经存在!

    或者

    该服务已成功安装

    不要关闭cmd!

  4. 搜索“服务”
  5. 打开“服务”后,选择 MySQL 并单击“开始”。
  6. 从命令行尝试以身份不明的用户、root 或 simple_cms 再次访问 mysql。mysql -u 简单登录 -p
  7. 如果成功,则尝试再次运行 rails
于 2014-01-19T09:03:26.293 回答