Saturday, August 20, 2005

Getting Rails and MySQL to play nice

Working on my first Rails application has been quite an experience -- and not the kind you think.

I've been following along with Dave Thomas' Rails book. It provides a gentle introduction to the various Rails idioms and has proven handy. Following the spirit of the instructions, I created the necessary database and tables in my MySQL instance. Chapter 6 explains that in order for Rails to communicate with a database one has to specify connection settings in the config/database.yml file. So, I provide the following

adapter: mysql
database: cc_dev
host: localhost

I issued the command

proj_root> ruby script/generate scaffold Project Add

expecting it to create a Project model object (associated with the PROJECTS table that I had already created in MySQL) that would be accessed through the Add controller. However, after a few create messages on the console, I got the following message:

No connection could be made because the target machine actively refused it. - connect(2)

Huh? Wasn't exactly the response I was expecting. I spent a long time trying various things such as: providing a username and password, providing an IP address instead of localhost etc, etc, I reached out to Google. There I came across a post by Jared Richardson on the same topic. While my context was different it suggested a cure: comment out the line


in the my.cnf file. Never having played in the bowels of MySQL, I found that on WinXP, that translates to the MYSQL_INSTALL_DIR\my.ini file. Since it did not say anything about skipping networking, I added it for good measure and commented it out by prefixing the line with a # character. Still no dice.

Now it just so happens that Jared's a colleague and a friend (small world). I wrote to him about my impasse and he suggested specifying the port in the database.yml file. I was skeptical -- since the Rails crowd had been drilling in the virtues of convention over configuration; I was thinking -- surely Rails would use defaults like with most other things. Anyway, I had wasted several hours on this problem and I had nothing to lose. So I added the line

port: 3306

so, my database.yml now looks like

adapter: mysql
database: cc_dev
host: localhost
port: 3306

Paydirt! Running the scaffold command again ran through to completion! Hope that helps you and saves you few hours.


  • At 5:12 AM, Blogger Falkayn said…

    Yeah, that did the job for me too (also on WinXP). Of course it took me a little bit longer as I'd been a bit of a smart-aleck when I setup MySQL over a year ago and had specified a different port number than the standard one.

    Still, thanks for the tip - I would have NEVER guessed that one on my own!

  • At 6:25 PM, Blogger Tuor said…

    didn't work for me

    my my.ini also didn't have any networking options.

  • At 8:03 PM, Anonymous humboldt said…

    Thanks for the post on that.

    I had set my MySQL port to 3307, not the default 3306. I found that I had to put my MySQL user and password in as well.

    Then the generate scaffold command ran successfully.

  • At 11:58 PM, Blogger flash-ripper said…

    There's the another cause of "No connection could be made because the target machine actively refused it" error: sometimes I've just forgot to run my MySQL. So after I've manually run it and then rerun WEBrick (which comes with my rails) -- all started to work good.



  • At 3:53 PM, Blogger Kerry said…

    I was having trouble with this very issue today. The tip you gave for adding the port to the database.yml did the trick.

  • At 2:22 PM, Anonymous klimbo said…

    On Vista, I found that both commenting out "skip-networking" and adding the port to the YAML file didn't work. Then I remembered that I had MySQLd running as a service, restarted it, and it worked fine, even without the port spec. D'oh...

  • At 7:56 AM, Blogger Gishu said…

    You the man!

    I was getting this when I tried generating a scaffold "Can't connect to MySQL server on 'server' (10061)"

    After some password and username, I hit upon this page,

    In the diagnostic steps suggested I saw my server running on 3307 instead of 3306.

    Quick google "database.yml port" leads to your page.
    Edit database.yml, Add port: 3307

  • At 7:57 PM, Blogger Carnator said…

    I had an error doing a tutorial when using rake db:migrate :
    rake aborted!
    can't convert Fixnum into String

    I had 2 mysql instances, in order to use the one that was not defaulted to 3306 all I did was add the line:
    port: 3214
    and that was it!
    worked like a charm.

  • At 3:47 PM, Blogger Roshan said…

    Thankyou...I would have never figured it.

  • At 11:14 PM, Blogger sumit said…

    i am a newbe to rails..a b-tech final year student. i wasnt having any trouble with my port number , but in case of any future development i need to know how to configure mysql port i did a google search and went to the first link which was helped me escape my doubt...please continue posting infos.
    thank you...

  • At 7:31 AM, Blogger Barbara Post said…

    Useful info. I added this "port: 3307" example option to

    I have then MySQL 5.0 and 5.1 running smoothly.

  • At 1:38 PM, Anonymous Anonymous said…

    Worked for me too... Thanks a lot!

  • At 9:12 AM, Blogger bbnnt said…

    i did not had any "skip-networking" in my.cnf, i could found on google hat many people made it work on windows with this configuration change... but did not worked for me.
    with this .dll file (old version, but working!), everything worked then smoothly !

  • At 4:29 PM, Anonymous Anonymous said…

    This was something I too was looking around. Thanks a lot for the tip.

  • At 10:38 AM, Anonymous Anonymous said…

    Hello! If you are working on the translation of your Rail app into multiple languages, you can use, which is a collaborative translation management platform easy to work with. It doesn't support .yaml files, but you have the option to convert them into po files, using a free converter tool like


Post a Comment

<< Home