Rails 开发 - 无法连接到“本地主机”上的 MySQL 服务器 (10061)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10792862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:33:37  来源:igfitidea点击:

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

mysqlruby-on-railsmysql2

提问by LearningRubyOnRails

I'm a Rails developer newbie using MySQL as the database. I can successfully connect to MySQL using the command:

我是使用 MySQL 作为数据库的 Rails 开发新手。我可以使用以下命令成功连接到 MySQL:

MySQL -u macDaddy -p

at the command prompt, so I know the user is valid and MySQL is running. But when I try to run

在命令提示符下,所以我知道用户是有效的并且 MySQL 正在运行。但是当我尝试跑步时

rake db:schema:dump

at the command line I get this error: rake aborted! Can't connect to MySQL server on 'localhost' (10061)

在命令行我收到这个错误:rake aborted!无法连接到“本地主机”上的 MySQL 服务器 (10061)

Is something wrong with my database.yml? Here it is:

我的database.yml 有问题吗?这里是:

 development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: bookmobile
  pool: 5
  username: macDaddy
  password: booklover
  host: localhost
  socket: mysql
  port: 3306

I've also tried removing the port and socket lines but I still get the same error. Please help. Here are my versions: developing on Windows 7

我也试过删除端口和套接字线,但我仍然遇到同样的错误。请帮忙。这是我的版本:在 Windows 7 上开发

MySQL Ver 14.14 distrib 5.5.21 for win64 Server version 5.5.21

MySQL Ver 14.14 distrib 5.5.21 for win64 Server version 5.5.21

Rails 3.2.1

导轨 3.2.1

Thanks!

谢谢!

回答by futureal

My best guess is that the machine, which you indicated as Windows, has IPv6 networking enabled. Thus when you try to go to localhost, it is resolving to "::1". This is in fact the local machine, however, default MySQL installs normally have bind-address set to 127.0.0.1, which would cause localhost to fail in this setup.

我最好的猜测是,您表示为 Windows 的机器启用了 IPv6 网络。因此,当您尝试访问 localhost 时,它会解析为“::1”。这实际上是本地机器,但是,默认的 MySQL 安装通常将绑定地址设置为 127.0.0.1,这会导致 localhost 在此设置中失败。

You might be able to verify this by running ping localhostfrom the command prompt, and seeing if you get a response like:

您可以通过ping localhost从命令提示符运行来验证这一点,并查看是否收到如下响应:

 Reply from ::1: time<1ms

To fix this, you can change your config to specify:

要解决此问题,您可以更改配置以指定:

 host: 127.0.0.1

Alternately, you can change MySQL's configuration to allow a different bind-address, e.g. localhost instead of 127.0.0.1.

或者,您可以更改 MySQL 的配置以允许不同的绑定地址,例如 localhost 而不是 127.0.0.1。

回答by N Khan

simply change your host to host:127.0.0.1

只需将您的主机更改为 host:127.0.0.1

回答by Mr.Right

Change the value of hostin the database.ymlto be "127.0.0.1"will work. Or,you can modify your pc's hosts file, add an item as "localhost 127.0.0.1".

database.yml主机的值更改为“127.0.0.1”即可。或者,您可以修改您电脑的主机文件,添加一个项目为“localhost 127.0.0.1”。

回答by ziggrat

I had a similar error but it was because the port: 3306 was missing in the database.yml. Once I added port: 3306 issue was resolved.

我有一个类似的错误,但这是因为在 database.yml 中缺少端口:3306。一旦我添加了端口:3306 问题就解决了。

回答by Someth Victory

It's because you don't have password set in your database server(mysql), try this:

这是因为你的数据库服务器(mysql)没有设置密码,试试这个:

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: bookmobile
  pool: 5
  username: macDaddy
  password:
  host: localhost
  socket: mysql
  port: 3306

回答by Kathirmalan

make sure the port number that you have set while creating MySQL(e.g.: 7777), please add port number with appropriate 'username' and 'password'.

确保您在创建 MySQL 时设置的端口号(例如:7777),请使用适当的“用户名”和“密码”添加端口号。

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: admin
  host: localhost
  port: 7777

in the database.yml in config directory of your app directory.

在应用程序目录的 config 目录中的 database.yml 中。

回答by Sanath

Make sure mysql server is running.

确保 mysql 服务器正在运行。