rails出现Can't connect to MySQL server on 'localhost' (10061)问题的解决方法

时间:2021-01-02 15:18:54
rails3 跑在win7下面,装了mysql,cmd里面mysql -u 和telnet 都没问题,但是rails3 执行rake db:create的时候出现Can't connect to MySQL server on 'localhost' (10061)的错误

rails出现Can't connect to MySQL server on 'localhost' (10061)问题的解决方法

后几经google,国外同行也有类似问题,问题描述的也比较清楚,原帖如下:
I'm trying to install ROR with a MySQL backend on a Windows 7 x64 box but they are not working together. When I did this before several months ago, it was just a mysql install and a bundle command but not this time.

I've installed MySQL 5.1 32 bit on a path with no spaces and configured the server. I had to install the mysql2 gem using the command line arguments to specify the mysql instance path to get the gem to install. However, rake db:create fails with a Can't connect to MySQL server on 'localhost' (10061) error.

I have verifed that mysql is running on the specified port. mysql, mysqladmin, and telnet localhost 3306 all work as normal. I have a firewall exception for port 3306 and it works fine. I've also turned the firewall off and it makes no difference.

I've spent hours going though google and trying many possibilities without success. Most of the documentation I have found relates to issues where mysql is not running / running on a pipe but this is not my problem.

database.yml excerpt:
common
:&common
adapter
: mysql2
encoding
: utf8
reconnect
:false
pool
:5
username
: root
password
: root
#host: 127.0.0.1
host
: localhost
port
:3307


development
:
<<:*common
database
: project_dev
  Any ideas how to fix this?  
link|improve this question edited Jan 18 at 20:09  asked Jan 18 at 19:52rails出现Can't connect to MySQL server on 'localhost' (10061)问题的解决方法user1157028
61
    虽然老外也是很多驴头不对马嘴的情况,不过还是有靠谱的答案: 大意是因为ipv6的原因导致的。 两种解决之道 1种是修改database.yml的配置host: 127.0.0.1 另种是修改mysql.ini,修改或者增加bing-address bind-address=localhost

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.

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

 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.