15

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

    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

    rake db:schema:dump

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

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

    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
  • MySQL Ver 14.14 distrib 5.5.21 for win64
  • Server version 5.5.21
  • Rails 3.2.1

Thanks!

Karl Wilbur
  • 5,898
  • 3
  • 44
  • 54
  • What happens if you specify 127.0.0.1 instead of localhost? – DVG May 29 '12 at 04:10
  • Is mysqld listening on 3306? Try `netstat -an | grep 3306`. If you don't see something like `*.3306 *.* LISTEN` then that could be the problem. – Jared Beck May 29 '12 at 04:27

7 Answers7

42

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.

futureal
  • 3,025
  • 1
  • 22
  • 33
  • As a newbie it took me far too long to realize you first need to **install mysql community server separately** since it's not included in ruby on rails or ruby mine. – Nicolasome Mar 02 '21 at 06:49
  • Furthermore the configuration file mentioned in this answer is located at config -> database.yml – Nicolasome Mar 02 '21 at 06:50
6

simply change your host to host:127.0.0.1

N Khan
  • 366
  • 1
  • 6
  • 15
6

Change the value of host in the database.yml to 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".

Mr.Right
  • 61
  • 1
  • 1
5

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.

ziggrat
  • 106
  • 1
  • 7
1

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

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: bookmobile
  pool: 5
  username: macDaddy
  password:
  host: localhost
  socket: mysql
  port: 3306
Someth Victory
  • 4,492
  • 2
  • 23
  • 27
1

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

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.

Kathirmalan
  • 66
  • 1
  • 8
0

Make sure mysql server is running.

Sanath
  • 29