web-dev-qa-db-fra.com

Ruby on Rails MYSQL error Accès refusé pour l'utilisateur 'root @ localhost'

Je reçois le message d'erreur suivant lors du démarrage du serveur Rails avec une nouvelle application:

/Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/client.rb:37:in 'connect': Accès refusé pour l'utilisateur 'root' @ 'localhost' (en utilisant le mot de passe: YES) (Mysql2 :: Error)

Voici mon database.yml

# MySQL.  Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
#   gem install mysql2
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: group_chat_development
  pool: 5
  username: root
  password: admin
  socket: /tmp/mysql.sock

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: group_chat_test
  pool: 5
  username: root
  password: admin
  socket: /tmp/mysql.sock

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: group_chat_production
  pool: 5
  username: root
  password: admin
  socket: /tmp/mysql.sock

Ci-dessous le message d'erreur complet

MacPro:FireDemon fred$ Rails s
=> Booting Mongrel
=> Rails 3.0.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/client.rb:37:in `connect': Access denied for user 'root'@'localhost' (using password: YES) (Mysql2::Error)
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/client.rb:37:in `initialize'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:14:in `new'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:14:in `mysql2_connection'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in `new_connection'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `checkout_new_connection'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in `block (2 levels) in checkout'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in `loop'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in `block in checkout'
    from /Users/fred/.rvm/rubies/Ruby-1.9.2-p180/lib/Ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in `checkout'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:93:in `connection'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:316:in `retrieve_connection'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:97:in `retrieve_connection'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/base.rb:1330:in `replace_bind_variables'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/base.rb:1317:in `sanitize_sql_array'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/base.rb:1224:in `sanitize_sql_for_conditions'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/relation/query_methods.rb:206:in `build_where'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/relation/query_methods.rb:77:in `where'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/base.rb:441:in `where'
    from /Users/fred/Desktop/FireDemon/app/models/user.rb:14:in `<class:User>'
    from /Users/fred/Desktop/FireDemon/app/models/user.rb:1:in `<top (required)>'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:454:in `load'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:454:in `block in load_file'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:453:in `load_file'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:340:in `require_or_load'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:491:in `load_missing_constant'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:183:in `block in const_missing'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:181:in `each'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:181:in `const_missing'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/inflector/methods.rb:124:in `block in constantize'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/inflector/methods.rb:123:in `each'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/inflector/methods.rb:123:in `constantize'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/core_ext/string/inflections.rb:43:in `constantize'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activemodel-3.0.3/lib/active_model/observing.rb:182:in `observed_class'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activemodel-3.0.3/lib/active_model/observing.rb:175:in `observed_classes'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activemodel-3.0.3/lib/active_model/observing.rb:195:in `observed_classes'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activemodel-3.0.3/lib/active_model/observing.rb:191:in `initialize'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/observer.rb:96:in `initialize'
    from /Users/fred/.rvm/rubies/Ruby-1.9.2-p180/lib/Ruby/1.9.1/singleton.rb:109:in `new'
    from /Users/fred/.rvm/rubies/Ruby-1.9.2-p180/lib/Ruby/1.9.1/singleton.rb:109:in `block in instance'
    from <internal:prelude>:10:in `synchronize'
    from /Users/fred/.rvm/rubies/Ruby-1.9.2-p180/lib/Ruby/1.9.1/singleton.rb:107:in `instance'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activemodel-3.0.3/lib/active_model/observing.rb:66:in `instantiate_observer'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activemodel-3.0.3/lib/active_model/observing.rb:39:in `block in instantiate_observers'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activemodel-3.0.3/lib/active_model/observing.rb:39:in `each'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activemodel-3.0.3/lib/active_model/observing.rb:39:in `instantiate_observers'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/railtie.rb:83:in `block (2 levels) in <class:Railtie>'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activerecord-3.0.3/lib/active_record/railtie.rb:82:in `block in <class:Railtie>'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:34:in `call'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/application/finisher.rb:46:in `block in <module:Finisher>'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/initializable.rb:25:in `instance_exec'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/initializable.rb:25:in `run'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/initializable.rb:50:in `block in run_initializers'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/initializable.rb:49:in `each'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/initializable.rb:49:in `run_initializers'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/application.rb:134:in `initialize!'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/application.rb:77:in `method_missing'
    from /Users/fred/Desktop/FireDemon/config/environment.rb:5:in `<top (required)>'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    from /Users/fred/Desktop/FireDemon/config.ru:3:in `block in <main>'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
    from /Users/fred/Desktop/FireDemon/config.ru:1:in `new'
    from /Users/fred/Desktop/FireDemon/config.ru:1:in `<main>'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:35:in `eval'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/builder.rb:35:in `parse_file'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/server.rb:162:in `app'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/server.rb:248:in `wrapped_app'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/rack-1.2.1/lib/rack/server.rb:213:in `start'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/commands/server.rb:65:in `start'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/commands.rb:30:in `block in <top (required)>'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/commands.rb:27:in `tap'
    from /Users/fred/.rvm/gems/Ruby-1.9.2-p180/gems/railties-3.0.3/lib/Rails/commands.rb:27:in `<top (required)>'
    from script/Rails:6:in `require'
    from script/Rails:6:in `<main>'
MacPro:FireDemon fred$ 
17
user852974

On dirait que votre utilisateur ne peut pas se connecter à la base de données MySQL. Essayez ces commandes dans votre console:

mysql -u root -p

Et lorsque vous y êtes invité, indiquez le mot de passe sous la forme 'admin'

Si cela est possible, alors vous devriez être prêt à partir.

16
sameera207

Le mot de passe est incorrect pour l'utilisateur root dans votre fichier config/database.yml. Vous pouvez toujours utiliser la commande mysqladmin pour changer le mot de passe de l'utilisateur root si vous l'avez oublié.

12
Chris Ledet

Conseil pratique - J'étais coincé là-dessus aussi, mais j'ai trouvé ce bouclage (127.0.0.1)! = 'Localhost' ... J'avais Host: 127.0.0.1 et je voyais ce problème exactement. Un changement rapide en "localhost" a corrigé cela pour moi ... espérons que cela aidera quelqu'un.

development:
 adapter: mysql2
 database: mydb
 Host: localhost
 username: root
 password: mypass
3
lucygenik

Donc, je suis resté coincé avec le pendant un moment. 

VÉRIFIEZ le fichier config/database.yml et vérifiez vos paramètres.

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: root
  socket: ( check what your socket location is by typing mysqladmin version into termnal)

Puis allez dans le terminal et tapez

mysql -u root -p

Et fermez finalement le terminal et ouvrez-le à nouveau, puis 

bundle instal

puis

Rails s

C'est comme si vous deviez regrouper à nouveau l'installation pour que les modifications soient prises en compte.

J'espère que cela pourra aider!

1
Sabba Keynejad

La réponse est dans votre question> erreur MYSQL Accès refusé pour l'utilisateur 'root @ localhost'

Appliquer l'utilisateur root avec le mot de passe administrateur à votre base de données MySQL group_chat_development

update Il me semble que vous ne souhaitez peut-être pas utiliser msQL pour vos bases de données de développement et de test, auquel cas modifiez les paramètres de test et de développement du fichier database.yml

# SQLite version 3.x
#   gem install sqlite3-Ruby (not necessary on OS X Leopard)
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

Si vous ne savez pas comment utiliser les outils MySQL, vous devrez vous habituer à les utiliser très bientôt, car vous en aurez probablement besoin dans votre environnement de serveur de production. http://dev.mysql.com/doc/ Utilisez l’Administrateur MySQL pour définir vos autorisations utilisateur.

1
jamesc

Si vous définissez le mot de passe root mysql pour la première fois, tapez les commandes suivantes comme suit:

mot de passe mysqladmin

Vous serez invité à entrer un nouveau mot de passe et à le confirmer.

Après confirmation, essayez de vous connecter avec l’utilisateur root et le mot de passe que vous venez de définir.

mysql root @ 'votre-nouveau-mot de passe'

Note: Vous devez vous connecter en tant que root, vous serez le seul à pouvoir définir le mot de passe mysql.

Merci.

Pour moi, c'est résolu en définissant le nom d'utilisateur, au lieu de le laisser vide 

development:
    adapter: mysql2
    database: my_db
    encoding: utf8
    username: # ADD DATABASE USERNAME
    password: # ADD DATABASE PASSWORD
    Host: localhost

Donc, je viens de le changer pour toutes les bases de données à la suivante

development:
    adapter: mysql2
    database: my_db
    encoding: utf8
    username: root
    password: # ADD DATABASE PASSWORD
    Host: localhost

Et cela fonctionne bien, espérons que cela aidera

0
Hussam Kurd