Can't connect to MySQL server on '127.0.0.1'

28,712

Solution 1

Try:

development:
  adapter: mysql
  database: app_development
  username: root
  password: 
  socket: /var/lib/mysql/mysql.sock

Then do your rake db:create to create it from rails.
or
in mysql do create database db_name
and then the above code for :development will let you use it.

Update: Matthew first needs to get mySQL installed on his (Mac) machine.
I directed him to http://dev.mysql.com/downloads/mysql/

Solution 2

Explicitly mention your port: 3306 in your database.yml

If that doesn't work then executed

netstat -tupln

and check if MySQL is listening to port 3306 or not

Share:
28,712
Matthew Berman
Author by

Matthew Berman

Updated on October 07, 2020

Comments

  • Matthew Berman
    Matthew Berman over 3 years

    I am trying to clone a repo and run it on my local machine. I don't have a DB created and I am trying to manually create my database.yml file (my first time doing this). I am not quite sure what i'm doing and keep getting errors about connecting to the MySql server. Here's my database.yml file:

    development:
      adapter: mysql
      host: 127.0.0.1
      database: db/app_development
      username: root
      password: 
    

    I also tried localhost instead of 127.0.0.1, both give me errors when trying to create the db by doing 'bundle exec rake db:create' ...i get this error:

    Can't connect to MySQL server on '127.0.0.1' (61)
    Couldn't create database for {"username"=>"root", "adapter"=>"mysql", "database"=>"db/app_development", "host"=>"127.0.0.1", "password"=>nil}, charset: utf8, collation: utf8_unicode_ci
    

    I know i'm doing something wrong but I can't quite figure out what it is. Do I need to start the mysql server or something?

    This is the output when I run mysqld_safe:

    120111 20:35:39 mysqld_safe Logging to '/usr/local/var/mysql/Matthew-Bermans-MacBook-Air.local.err'.
    120111 20:35:39 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
    120111 20:35:41 mysqld_safe mysqld from pid file /usr/local/var/mysql/Matthew-Bermans-MacBook-Air.local.pid ended
    
  • Matthew Berman
    Matthew Berman over 12 years
    No i get the same error:Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) how would I know what to put in the database: field without actually having a database created? basically, I just cloned this repo and trying to get it to work. I don't have a database created and I don't have a proper database.yml setup yet. What is the step by step process to get this to work? I bundle installed everything and it passed fine.
  • Matthew Berman
    Matthew Berman over 12 years
    I made the database.yml file exactly as you have shown. I then did bundle exec rake db:create (needed bundle exec), but I get this error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2). I am not sure how to do it "in mysql"
  • Matthew Berman
    Matthew Berman over 12 years
    Added socket: /var/lib/mysql/mysql.sock to the yml file and got this error: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
  • Michael Durrant
    Michael Durrant over 12 years
    Matthew, lets do comment cleanup! (i.e. delete the chats).