我正在关注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文件中,我已经将用户和密码放入并记住将integerpassword放在“”中,冒号和密码本身之间的空格如下:
# 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和整个安装过程已经耗费了大量的时间......
答案 0 :(得分: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。如果在该端口上没有任何内容,您将获得类似的内容:
$ 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上工作,所以你的输出看起来可能不同。
答案 1 :(得分:3)
我在Lynda课程中。
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
的更改
答案 2 :(得分:1)
我在这个问题上工作了几天,几乎尝试了互联网上的所有解决方案。 最后,我可以通过添加port:3306并使用&#39; rails server&#39;重新启动WEBrick服务器来解决该错误。命令。
谢谢。
答案 3 :(得分:0)
我在Ruby和命令行中看到了这个可怕的东西!
Can't connect to MySQL server on 'localhost' (10061)
如果上面的解决方案不起作用,我怀疑这可能是与缺少MySQL服务运行相关的问题。除非你解决了MySQL的错误,否则Rails无法访问MySQL。
诊断
在尝试下面的解决方案之前,请转到命令行并尝试再次从bin文件夹访问MySQL: mysql -u username -p
如果你得到这样的东西:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
然后试试这个:
您可以收到以下两条消息中的任何一条:
该服务已经存在!OR
该服务已成功安装不要关闭cmd!