# Mysql Can't Connect

**URL:** <https://www.ruby-forum.com/t/mysql-cant-connect/175449>\
**Category:** Rails\
**Created:** [September 14, 2009, 5:29am UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449 "2009-09-14T05:29:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 14, 2009, 5:29am UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449/1 "2009-09-14T05:29:32Z")

</div>

I’m on OS X 10.5.8 with the up-to-date Rails. I’m trying to get  
restful\_authentication to work, but I keep getting a mysql error. I’ve  
done the re-install mysql thing.

[terminal]  
=\> Booting Mongrel  
=\> Rails 2.3.2 application starting on [http://0.0.0.0:3000](http://0.0.0.0:3000)  
/Applications/MAMP/htdocs/Rails/restful\_authentication\_tutorial/vendor/  
rails/activerecord/lib/active\_record/connection\_adapters/  
mysql\_adapter.rb:576:in `real_connect': Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) (Mysql::Error) from /Applications/MAMP/htdocs/Rails/restful_authentication_tutorial/ vendor/rails/activerecord/lib/active_record/connection_adapters/ mysql_adapter.rb:576:in `connect’  
from /Applications/MAMP/htdocs/Rails/restful\_authentication\_tutorial/  
vendor/rails/activerecord/lib/active\_record/connection\_adapters/  
mysql\_adapter.rb:204:in `initialize' from /Applications/MAMP/htdocs/Rails/restful_authentication_tutorial/ vendor/rails/activerecord/lib/active_record/connection_adapters/ mysql_adapter.rb:76:in `new’  
from /Applications/MAMP/htdocs/Rails/restful\_authentication\_tutorial/  
vendor/rails/activerecord/lib/active\_record/connection\_adapters/  
mysql\_adapter.rb:76:in `mysql_connection' from /Applications/MAMP/htdocs/Rails/restful_authentication_tutorial/ vendor/rails/activerecord/lib/active_record/connection_adapters/ abstract/connection_pool.rb:223:in `send’  
from /Applications/MAMP/htdocs/Rails/restful\_authentication\_tutorial/  
vendor/rails/activerecord/lib/active\_record/connection\_adapters/  
abstract/connection\_pool.rb:223:in `new_connection' from /Applications/MAMP/htdocs/Rails/restful_authentication_tutorial/ vendor/rails/activerecord/lib/active_record/connection_adapters/ abstract/connection_pool.rb:245:in `checkout\_new\_connection’  
from /Applications/MAMP/htdocs/Rails/restful\_authentication\_tutorial/  
vendor/rails/activerecord/lib/active\_record/connection\_adapters/  
abstract/connection\_pool.rb:188:in `checkout' ... 26 levels... from /Applications/MAMP/htdocs/Rails/restful_authentication_tutorial/ vendor/rails/railties/lib/commands/server.rb:84 from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem\_original\_require’  
from /Library/Ruby/Site/1.8/rubygems/custom\_require.rb:31:in  
`require’  
from script/server:3  
[/terminal]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 14, 2009, 10:31am UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449/2 "2009-09-14T10:31:35Z")

</div>

On Sep 14, 4:27 am, Kevin M [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m on OS X 10.5.8 with the up-to-date Rails. I’m trying to get  
> restful\_authentication to work, but I keep getting a mysql error. I’ve  
> done the re-install mysql thing.

Where does MAMP keep its mysql socket ? If it’s not /var/run/mysqld  
then change your database.yml so that it points at the right place.

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 16, 2009, 5:50am UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449/3 "2009-09-16T05:50:42Z")

</div>

Thanks for the advice, I had that “duh” moment when I read it…having  
my Rails apps in the MAMP folder messed up the mysql settings.

I moved my Rails app to the Sites folder on my Mac, I was able to get  
mysql to work. Now I’m having problems with my settings somewhere, my  
browser is giving me this message:

[browser message]

ActiveRecord::StatementInvalid in Root#index

Showing app/views/root/index.html.erb where line #5 raised:

Mysql::Error: Table ‘DEV.invitations’ doesn’t exist: SHOW FIELDS FROM  
`invitations`

Extracted source (around line #5):

2: \<% unless logged\_in? -%\>\<% if\_in\_beta? do -%\>  
3: \<%= error\_messages\_for :invitation %\>  
4:

We are currently in private beta. Please enter your email  
address below and we will let know when an invitation becomes  
available.

  
5: \<% uberform\_for [:user, Invitation.new] do |f| -%\>  
6: \<%= f.text\_field :email, :label =\> “Your Email:” %\>  
7: \<%= f.submit ‘Submit’ %\>  
8: \<% end -%\>

[/browser message]

I have some null values in my database, could that be the source of my  
issue?

Thanks, Kevin

On Sep 14, 4:30 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 18, 2009, 4:42am UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449/4 "2009-09-18T04:42:53Z")

</div>

Yes, I did migrate the database. But I ran a rake db:migrate again to  
be positive and I get this error.

Mysql::Error: Duplicate entry ‘devuser’ for key 2: INSERT INTO `users`  
(`name`, `salt`, `updated_at`, `activated_at`, `crypted_password`,  
`remember_token_expires_at`, `activation_code`, `password_reset_code`,  
`enabled`, `invitation_id`, `user_type`, `remember_token`,  
`identity_url`, `login`, `email`, `created_at`, `invitation_limit`)  
VALUES(’’, ‘202e1d83b21f9d11b58926811f2b00af5e06da5e’, ‘2009-09-18  
01:11:57’, NULL, ‘faed49f6e2a53904ed62ca98399036e4ceacda39’, NULL,  
‘2c9c7129ba82cfdf7c27e06c3f240ac6a20bc0d0’, NULL, 1, NULL, ‘SiteUser’,  
NULL, NULL, ‘devuser’, NULL, ‘2009-09-18 01:11:57’, 5)

The database has the fields invitation\_id and invitation\_limit.

Thanks for your help,

Kevin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 18, 2009, 5:33am UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449/5 "2009-09-18T05:33:48Z")

</div>

Would deleting the user through phpmyadmin be the best way of dropping  
the database or do I need to delete the database file altogether?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 18, 2009, 5:12am UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449/6 "2009-09-18T05:12:44Z")

</div>

If this is your dev environment I would drop the database and just do  
a clean new rake db:migrate. It’s trying to insert the user devuser  
that I’m assuming is already in the database or your migration files  
tries to insert the user twice… hard to say without the full --trace  
output from the command…

Try to drop the db and re-run the migration that would be what I would  
do…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 18, 2009, 8:32am UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449/7 "2009-09-18T08:32:54Z")

</div>

@Kevin. Did you use rake db:migrate to build your database? I think  
hemidull was recommending to drop the schema and redo it. But if you  
are building your database externally, then the error shown above  
means that the table doesn’t exist. If table does exist, it means  
your database.yml configuration file is pointing to the wrong schema.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 16, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449/8 "2009-09-16T18:08:15Z")

</div>

This is your problem:  
Mysql::Error: Table ‘DEV.invitations’ doesn’t exist: SHOW FIELDS FROM  
`invitations`

Do you have a database DEV that has a table invitations? Did you  
migrate your database?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2009, 7:00pm UTC](https://www.ruby-forum.com/t/mysql-cant-connect/175449/9 "2009-09-20T19:00:50Z")

</div>

@Mukund To answer your question:  
Yes, I used db:migrate to build the database, then I used phpmyadmin  
to see what the result was, I’m new to rails programming and thought  
it would be useful to examine the result.

I think I have the connection to the database correct, the problem  
appears (to me) to be in the setup before I migrate.

I’m doing the restful\_authentication with all the bells & whistles  
tutorial. I started over again to go from the beginning. When I get to  
the migration I get an error, here’s the initial error and the full  
trace:

## ERROR:

# == SetUpFirstAdminUser: migrating

rake aborted!  
An error has occurred, all later migrations canceled:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.activated\_at=

## FULL TRACE:

# (in /Users/Me/Sites/RAT2/restful\_authentication\_tutorial) \*\* Invoke db:migrate (first\_time) \*\* Invoke environment (first\_time) \*\* Execute environment \*\* Execute db:migrate == SetUpFirstAdminUser: migrating

rake aborted!  
An error has occurred, all later migrations canceled:

Mysql::Error: Duplicate entry ‘userDEV’ for key 2: INSERT INTO `users`  
(`name`, `salt`, `updated_at`, `activated_at`, `crypted_password`,  
`remember_token_expires_at`, `activation_code`, `password_reset_code`,  
`enabled`, `invitation_id`, `user_type`, `remember_token`,  
`identity_url`, `login`, `email`, `created_at`, `invitation_limit`)  
VALUES(’’, ‘1a9bde41a2d6d664d03e32676c479a07f301d2a6’, ‘2009-09-20  
16:23:19’, NULL, ‘b984b8c4f7258b854fab69f85c8b2a95116b41c9’, NULL,  
‘1194519fe5c980f38f0d8ffd77f2c8b94625af2b’, NULL, 1, NULL, ‘SiteUser’,  
NULL, NULL, ‘userDEV’, NULL, ‘2009-09-20 16:23:19’, 5)  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/plugins/  
rows\_logger/init.rb:36:in `log' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/connection_adapters/mysql_adapter.rb: 320:in`execute\_without\_analyzer’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/plugins/  
footnotes/lib/rails-footnotes/notes/queries\_note.rb:104:in  
`execute_without_query_stats' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activesupport/lib/active_support/core_ext/benchmark.rb:10:in`realtime’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/plugins/  
footnotes/lib/rails-footnotes/notes/queries\_note.rb:104:in  
`execute_without_query_stats' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/plugins/ query_stats/lib/query_stats/recorder.rb:16:in`execute’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activesupport/lib/active\_support/core\_ext/benchmark.rb:10:in  
`realtime' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/plugins/ query_stats/lib/query_stats/recorder.rb:15:in`execute’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/connection\_adapters/abstract/  
database\_statements.rb:259:in `insert_sql' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/connection_adapters/mysql_adapter.rb: 330:in`insert\_sql’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/connection\_adapters/abstract/  
database\_statements.rb:44:in `insert_without_query_dirty' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/connection_adapters/abstract/ query_cache.rb:18:in`insert’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/base.rb:2902:in  
`create_without_timestamps' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/timestamp.rb:29:in`create\_without\_callbacks’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/callbacks.rb:266:in `create' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/base.rb:2868:in`create\_or\_update\_without\_callbacks’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/callbacks.rb:250:in `create_or_update' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/base.rb:2539:in`save\_without\_validation’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/validations.rb:1009:in  
`save_without_dirty' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/dirty.rb:79:in`save\_without\_transactions’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/transactions.rb:229:in `send' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/transactions.rb:229:in`with\_transaction\_returning\_status’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/connection\_adapters/abstract/  
database\_statements.rb:136:in `transaction' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/transactions.rb:182:in`transaction’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/transactions.rb:228:in  
`with_transaction_returning_status' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/transactions.rb:196:in`save’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/transactions.rb:208:in  
`rollback_active_record_state!' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/transactions.rb:196:in`save’  
./db/migrate//20080806025753\_set\_up\_first\_admin\_user.rb:9:in  
`up_without_benchmarks' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/migration.rb:282:in`send’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/migration.rb:282:in `migrate' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ 1.8/benchmark.rb:293:in`measure’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/migration.rb:282:in `migrate' ( __DELEGATION__ ):2:in` **send** ’  
( **DELEGATION** ):2:in `migrate' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/migration.rb:486:in`migrate’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/migration.rb:562:in `call' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/migration.rb:562:in`ddl\_transaction’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/migration.rb:485:in `migrate' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/migration.rb:472:in`each’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/migration.rb:472:in `migrate' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ activerecord/lib/active_record/migration.rb:400:in`up’  
/Users/Me/Sites/RAT2/restful\_authentication\_tutorial/vendor/rails/  
activerecord/lib/active\_record/migration.rb:383:in `migrate' /Users/Me/Sites/RAT2/restful_authentication_tutorial/vendor/rails/ railties/lib/tasks/databases.rake:116 /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in`call’  
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in`each’  
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in`invoke\_with\_call\_chain’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/  
1.8/monitor.rb:242:in `synchronize' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in`invoke\_with\_call\_chain’  
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in`invoke\_task’  
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in`each’  
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in`standard\_exception\_handling’  
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in`run’  
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in  
`standard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in`run’  
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31  
/usr/bin/rake:19:in `load’  
/usr/bin/rake:19

* * *

## Here’s my SetupFirstAdminUser file, should I change ‘settings’ in the user.email line? What goes there?

class SetUpFirstAdminUser \< ActiveRecord::Migration  
def self.up  
#Be sure to change these settings for your initial admin user  
user = SiteUser.new  
user.login = “userDEV”  
user.email = APP\_CONFIG[‘settings’][‘removed\_email\_address@domain.invalid’]  
user.password = “mypassword”  
user.password\_confirmation = “mypassword”  
user.save(false)  
role = Role.new  
#Admin role name should be “admin” for convenience  
role.name = “admin”  
role.save  
admin\_user = SiteUser.find\_by\_login(“admin”)  
admin\_role = Role.find\_by\_name(“admin”)  
admin\_user.activated\_at = Time.now.utc  
admin\_user.roles \<\< admin\_role  
admin\_user.save(false)  
end

def self.down  
admin\_user = User.find\_by\_login(“admin”)  
admin\_role = Role.find\_by\_name(“admin”)  
admin\_user.roles = []  
admin\_user.save  
admin\_user.destroy  
admin\_role.destroy  
end  
end
