【问题标题】:Ruby on Rails MYSQL error Access denied for user 'root@localhost'Ruby on Rails MYSQL 错误访问被拒绝用户'root@localhost'
【发布时间】:2011-07-30 23:31:43
【问题描述】:

使用新应用启动 Rails 服务器时出现以下错误:

/Users/fred/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/client.rb:37:in `connect':访问被拒绝用户'root'@'localhost'(使用密码:YES)(Mysql2::Error)

这是我的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

以下是完整的错误信息

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$ 

【问题讨论】:

    标签: mysql ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    您的用户似乎无法连接到 MySQL 数据库。在您的控制台中尝试以下命令:

    mysql -u root -p
    

    当出现提示时,将密码输入为“admin”

    如果这是可能的,那么你应该很高兴。

    【讨论】:

    • 好吧,IDK 这个命令如何解决这个问题。但我有一个朋友有完全相同的问题,我们运行这个来解决这个问题:CREATE DATABASE db_development; GRANT ALL PRIVILEGES ON db_development.* TO 'user'@'localhost';CREATE DATABASE db_development; GRANT ALL PRIVILEGES ON db_test.* TO 'user'@'localhost';FLUSH PRIVILEGES。这将创建数据库并更改所有权。另外,请确保使用CREATE USER 'user'@'localhost' IDENTIFIED BY 'password'; 创建用户。还要更改 database.yml 代码以获取用户名和密码。
    【解决方案2】:

    config/database.yml 文件中的 root 用户密码错误。如果您忘记了 root 用户的密码,您可以随时使用mysqladmin 命令更改它。

    【讨论】:

    • 你的意思是运行“mysqladmin -u root password admin”吗?因为如果我尝试运行它,我会得到-bash: mysqladmin: command not found
    • 你的mysql在哪里?你是怎么安装的?
    • @ChrisLedet 我也有这个问题(OSX)并用自制软件安装了mysql。我能做些什么??我安装 mysql 时从未创建过密码。
    【解决方案3】:

    这对我有用

    mysql&gt; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password’;


    mysql> FLUSH PRIVILEGES;
    mysql> exit;
    

    以上命令尝试后:

    mysql -u root -p
    

    【讨论】:

      【解决方案4】:

      方便的主页提示 - 我也被困在这个问题上,但发现 loopback (127.0.0.1) != 'localhost'... 我有 host: 127.0.0.1 并且看到了这个确切的问题。快速更改 'localhost' 为我解决了这个问题......希望它对某人有所帮助。

      development:
       adapter: mysql2
       database: mydb
       host: localhost
       username: root
       password: mypass
      

      【讨论】:

        【解决方案5】:

        答案就在你的问题中 > MYSQL 错误 Access denied for user 'root@localhost'

        将具有管理员密码的 root 用户应用到您的 group_chat_development MySQL 数据库

        更新 在我看来,您可能根本不希望将 msQL 用于您的开发和测试数据库,在这种情况下将您的 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
        

        如果您不知道如何使用 MySQL 工具,那么您应该尽快习惯使用它们,因为您很可能在生产服务器环境中需要它们 您将在此处找到指向您正在使用的 MySQL 相关版本的链接http://dev.mysql.com/doc/ 使用 MySQL Administrator 设置您的用户权限。

        【讨论】:

          【解决方案6】:

          所以我被这个问题困扰了一段时间。

          检查文件 config/database.yml 并检查您的设置。

          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)
          

          然后进入终端并输入

          mysql -u root -p
          

          最后关闭终端并重新打开它,然后

          bundle install
          

          然后

          rails s
          

          看起来您必须再次捆绑安装才能进行更改。

          希望有帮助!

          【讨论】:

            【解决方案7】:

            如果您是第一次设置 mysql root 密码,请输入以下命令:

            mysql管理员密码

            系统将提示您输入新密码并确认密码。

            确认后尝试使用刚刚设置的用户root和密码登录。

            mysql root@'your-new-password'

            注意:您必须以 root 身份登录,然后才能设置 mysql 密码。

            谢谢。

            【讨论】:

              【解决方案8】:

              对我来说,它通过定义用户名来解决,而不是留空

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

              所以我只是将所有数据库的它更改为以下

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

              它工作正常,希望这会有所帮助

              【讨论】:

                【解决方案9】:

                您应该在 database.yml 文件的 devlepment 中提供“host:”字段。 该主机名必须是“IP”地址,如“127.0.0.1”或其他东西.. 即“主机:127.0.0.1” 并再次重新启动服务器并输入地址,如“127.0.0.1:3000” 这里 3000 是端口号...

                【讨论】:

                • 为什么要修改 ip/port?接受的答案已经指向无效的凭据
                【解决方案10】:

                如果您使用oracle包安装,请尝试通过以下方式登录您的mysql

                mysql -u username
                

                然后运行

                FLUSH PRIVILEGES;
                

                然后设置你的密码

                SET PASSWORD FOR 'username'@'localhost' = PASSWORD('[password]');
                

                【讨论】:

                • 使用 PASSWORD('[password]');密码是什么?
                • 报价中的任何内容。那应该是填写空白占位符...即SET PASSWORD FOR 'username'@'localhost' = PASSWORD('123456');将其设置为123456
                猜你喜欢
                • 2019-05-15
                • 2013-05-08
                • 2013-08-22
                • 1970-01-01
                • 2012-08-11
                • 1970-01-01
                • 2023-02-16
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多