加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby Mysql无法连接到localhost(10061)

发布时间:2020-12-17 01:26:27 所属栏目:百科 来源:网络整理
导读:我从Lynda跟踪 ruby on rails课程,我在使用 Windows命令行连接数据库时遇到问题. 我可以使用“mysql -u username -p”连接到数据库,然后输入密码.我可以创建数据库而且没有问题. 现在执行时: rake db:schema:dump 在我的应用程序文件夹“simple_cms”中,我
我从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:UsersASUSDesktopsimple_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:UsersASUSDesktopsimple_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和整个安装过程已经耗费了大量的时间……

解决方法

使用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 22222
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/5Q^mysql_native_passwordConnection closed by foreign host.

我在Unix上工作,所以你的输出可能看起来不一样.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读