Last Updated: February 25, 2016
·
14.23K
· matthewbarram

Access denied for user 'root'@'localhost' (using password: NO) (Mysql2::Error)

As someone really really (like yesterday) new to coding, I have been using the Lynda.com tutorial to get start with Ruby on Rails and I have run into almost countless issues.

Most recently was the error

Access denied for user 'root'@'localhost' (using password: NO) (Mysql2::Error)

This was really frustrating as I had just sorted out another number of countless other issues.

This issue arrose when I was doing:

rails generate controller demo index

Then I would get the following results...

/Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Access denied for user 'root'@'localhost' (using password: NO) (Mysql2::Error)
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `initialize'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `mysql2_connection'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
from /Users/<username>/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
from /Users/<username>/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/railtie.rb:88:in `block in <class:Railtie>'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/<username>/simple_cms/config/environment.rb:5:in `<top (required)>'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/application.rb:103:in `require'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/application.rb:103:in `require_environment!'
from /Users/<username>/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.13/lib/rails/commands.rb:25:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

What you need to do to fix most of this error is to:

  1. Ensure you have turned on mysql.server this can be done as follows:

    mysql.server start

  2. Ensure you are in your project folder, for me this means typing:

    cd simple_cms

  3. Go to the home folder of the project then find this file /Users/matthewbarram/simple_cms/config/database.yml. Open it and scroll down to row 16-18 and make sure the database information is entered correctly (see picture) Picture If you haven't created your database you can use:

    rake db:create

Or you can do it manually by following the video instructions here - http://www.lynda.com/home/Player.aspx?lpk4=75189&playChapter=False.

  1. After I ran the command again after fixing the database file I got this error:

    Matthews-MacBook-Pro-2:simplecms <username>$ rails generate controller demo index
    /Users/<username>/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site
    ruby/2.0.0/rubygems/coreext/kernelrequire.rb:45:in require': cannot load such file -- bundler/setup (LoadError) from /Users/<username>/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:inrequire'
    from /Users/<username>/simplecms/config/boot.rb:6:in `<top (required)>'
    from /Users/<username>/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site
    ruby/2.0.0/rubygems/coreext/kernelrequire.rb:45:in require' from /Users/<username>/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:inrequire'
    from script/rails:5:in `<main>'

  2. Again make sure you are in the correct folder and then type:

    gem install bundler

  3. The gem will be installed (get confirmation):

    Fetching: bundler-1.3.5.gem (100%)

    Successfully installed bundler-1.3.5
    Parsing documentation for bundler-1.3.5
    Installing ri documentation for bundler-1.3.5
    1 gem installed

  4. Install bundler, now type:

    bundle install

  5. This will run and say something like...

    Fetching gem metadata from https://rubygems.org/..........
    Fetching gem metadata from https://rubygems.org/..
    Installing rake (10.1.0)
    Using i18n (0.6.1)
    Using multi_json (1.7.7)
    Using activesupport (3.2.13)
    Using builder (3.0.4)
    Using activemodel (3.2.13)
    Using erubis (2.7.0)
    Using journey (1.0.4)
    Using rack (1.4.5)
    Using rack-cache (1.2)
    Using rack-test (0.6.2)
    Using hike (1.2.3)
    Using tilt (1.4.1)
    Using sprockets (2.2.2)
    Using actionpack (3.2.13)
    Using mime-types (1.23)
    Using polyglot (0.3.3)
    Using treetop (1.4.14)
    Using mail (2.5.4)
    Using actionmailer (3.2.13)
    Using arel (3.0.2)
    Using tzinfo (0.3.37)
    Using activerecord (3.2.13)
    Using activeresource (3.2.13)
    Using coffee-script-source (1.6.2)
    Using execjs (1.4.0)
    Using coffee-script (2.2.0)
    Using rack-ssl (1.3.3)
    Using json (1.8.0)
    Using rdoc (3.12.2)
    Using thor (0.18.1)
    Using railties (3.2.13)
    Using coffee-rails (3.2.2)
    Using jquery-rails (3.0.1)
    Using mysql2 (0.3.11)
    Using bundler (1.3.5)
    Using rails (3.2.13)
    Using sass (3.2.9)
    Using sass-rails (3.2.6)
    Using uglifier (2.1.1)
    Your bundle is complete!
    Use bundle show [gemname] to see where a bundled gem is installed.

  6. Now finally if you run the command it will work correctly!

If you have any issues, please let me know - I am more than happy to assist if I can!

Matthew :-)

3 Responses
Add your response

PS. I couldnt format this page properly... :-)

over 1 year ago ·

Hallo!
I have did all the procedures you mentioned and I also learned from lynda.com video tutorial. I used Mac 10.7 and I installed rubystack. I created the databases, add and checked the database.yml here is you can see it again.

development:
adapter: mysql2
encoding: utf8
reconnect: false
host: localhost
database: simplecmsdevelopment
pool: 5
username: simple_cms
password: maiwandj
socket: /tmp/mysql.sock

Therefore, when I run rake db:schema:dump it print the following errors

bash-3.2$ rake db:schema:dump
/Applications/rubystack-1.9.3-18/ruby/lib/ruby/gems/1.9.1/gems/bundler-1.5.2/lib/bundler/runtime.rb:220: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777
rake aborted!
Access denied for user 'simplecms'@'localhost' (using password: YES)
/Applications/rubystack-1.9.3-18/simple
cms/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:schema:dump => environment
(See full trace by running task with --trace)

over 1 year ago ·

If u running both wampServer(mysql) and and seperate mysql server it wont work....i dont know whats the issue is so i unsinstalled wamp server and tell what it worked for me !

So try uninstalling the wamp server and try it .
Let me know it worked for u or not!

over 1 year ago ·