Resolve mysql.sock connecting problems on Rails

I handled it, and I am hoping my experience could be helping you. My machine deployment: MySQL5.1.6a, ruby1.8.4, rails1.1.2.

Update: type to make true you can correctly connect database by using username and password

mysql depot_development <db/create.sql -p

, If return error, continue to read.

Several minutes earlier I tumbled at my first Rails problem, when Rails was trying to connect MySQL database, it sucks. The shell prompt appears an error:

#28000Access denied for user 'root'@'localhost' (using password: NO)

That means the Rails application can not connect my MySQL correctly, but why? Firstly I checked my configure file in config/database.yml, as the following:

development:
  adapter: mysql
  database: depot_development
  username:              #Note this
  password:
  host: localhost

test:
  adapter: mysql
  database: depot_test
  username:              #Note this
  password:
  host: localhost

production:
  adapter: mysql
  database: depot_production
  username: prod
  password: wibble
  host: localhost

I think my config file was right, and googled this error on web. It seems that the same problem is bothering mamy people who rock with Ruby on Rails. After skim over several sites I can search, all of them still can’t help me out to handle the mess. Some one suggest that it might be your MySQL setup version needed to upgrade, and others assist to type

ruby script/server environment=development

. I did it, but received same error.

Ermmmmmm, a little frustration…..eh? but back to the book Agile Web Development with Rails(1st edition), and preusing at page 52. I think the author describes this problem explicitly, yet many Rails forks who have same error didn’t get his point perfectly well. After creating a database in MySQL, you may assign such corresponding grant to users. Like:


mysql> create database depot_development;
mysql> create database depot_test;
mysql> create database depot_production;
mysql> grant all on depot_development.* to 'dave'@'localhost';
mysql> grant all on depot_test.* to 'dave'@'localhost';
mysql> grant all on depot_production.* to 'prod'@'localhost' identified by 'wibble';
mysql> exit

In config/database.yml, if you want any developer can access your database by conviention, you will left the username field blank, and find sth like this:

development:
  adapter: mysql
  database: depot_development
  username:              #blank
  password:
  host: localhost

test:
  adapter: mysql
  database: depot_test
  username:              #blank
  password:
  host: localhost

production:
  adapter: mysql
  database: depot_production
  username: prod
  password: wibble
  host: localhost

and that will cause error. Because when Rails connecting MySQL, it find the user name and password in conf/database.yml, if you left blank in username and password field, Rails will use root authority to connect database, but it can’t find what is the root password, then prompt error. That’s why it sucks rather than what MySQL version is, and you get:

#28000Access denied for user 'root'@'localhost' (using password: NO)

You must specify the correct username and password that should be the same ones when creating relevant database. Such as:

development:
  adapter: mysql
  database: depot_development
  username: dave	#dave has no password in his database
  password:
  host: localhost

test:
  adapter: mysql
  database: depot_test
  username: dave	#dave has no password in his database
  password:
  host: localhost

production:
  adapter: mysql
  database: depot_production
  username: prod	#prod has password in his database
  password: wibble	#which is 'wibble'
  host: localhost

Refresh your browser, and you see…..? Cheers.

Important Update:

I strongly recommend that same individual must hold the same username and corresponding password in your different app’s config/database.yml for true, no matter which of databases in mysql you used in various app by the same user.

For instance, I am alex and I really am ^^). I have two Rails app, depot and radiant(a simple CMS, I take a trial just now). If both depot and radiant use ‘alex’ as a mysql connect username, I must specify same user name and same password in the two database.yml, otherwise one of these app will get confused and restrained as connecting mysql, even the two app will implement their respective databases, because they all use ‘alex’ to connect mysql. So ‘alex’ needs same database password in different Rails app.

If you installed Rails before installed MySql, Rails database driver cannot find your database. You should reinstall Rails or other choice, add MySql socket location to respective section in ‘database.yml’, such as

socket: /var/lib/mysql/mysql.sock

Or, prompt saying:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

to solve this sort of error, check it out my recent post on it.

If your problem to be worked out after reading my post or still have any connecting problem, I wish you can leave your comment here. Lemme know and welcome :) .

Advertisement

12 Responses to “Resolve mysql.sock connecting problems on Rails”


  1. 1 Frits November 24, 2006 at 1:06 pm

    I hada the same problem and your posting did help me. I’m reading the book Ruby on Rails and a few paragraphs further I ran into the next problem. I made a create.sql in db and according to the book I had to execute the following command “mysql depot_development

  2. 2 Zhenzhong Wang November 25, 2006 at 5:20 am

    Is there a comment bug of K2? I’ve sent you an email.

  3. 3 Brad February 22, 2007 at 7:23 am

    I get the error message the system cannot find the file specified when I enter: mysql depot_development

  4. 4 Lunthar May 13, 2007 at 12:56 am

    I get the same error as Brad. Could someone help us?

  5. 5 death knight January 27, 2008 at 9:26 am

    thanks man solved my problem

  6. 6 johnmazarr April 6, 2008 at 4:13 am

    A Realistic $250K First Year Income Potential

    Less than four years ago I was driving this beat-up ’94 VW with a rusty muffler. Within two years of creating this system I was making more than my doctor, accountant, and attorney COMBINED… while working less than 40 hours a week FROM HOME! Together with my group of leaders we are now seeking qualified entrepeneurs sharein the incredible results of our system.

    for more information check this site out….
    make money online scams

  7. 7 Carl Bernardi May 6, 2008 at 7:49 pm

    I resolved this error on UBUNTU by creating a symbolic link. Here’s an article the explains how to do it.

  8. 9 Insawsedund November 2, 2008 at 8:40 am

    Is that a new way? There are very few things like my sympathy album I have a nice joke for you) What do you get when you cross an elephant and a skin doctor? A pachydermatologist.

  9. 10 MGDerrick February 5, 2009 at 8:43 pm

    Effexor (Venlafaxine) is an oral antidepressant drug.

  10. 11 bimal February 9, 2009 at 3:55 am

    i installed ruby and mysql separately. should my mysql folder inside ruby?

  11. 12 Alexwebmaster March 3, 2009 at 12:38 pm

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




This personal blog of Zhenzhong Wang, is mainly about Ruby on Rails and my a little spark at a mere whim.

You can Subscribe to my blog here.

a


Follow

Get every new post delivered to your Inbox.