rails db:migrate rails aborted! Mysql2 ::错误:用户'root'@'localhost'拒绝访问(使用密码:YES)

时间:2018-02-16 15:52:19

标签: mysql ruby-on-rails macos

我正在运行此迁移:

class CreateAdmins < ActiveRecord::Migration[5.1]
  def change
    create_table :admins do |t|

      t.string "first_name" :limit => 30
      t.string "last name", :limit => 30
      t.string "email", :default => '', :null => false
      t.string "password" ,:limit => 40
      t.timestamps

    end
  end

  def down
    drop_table :admins
  end
end

我收到错误说:

  

rails db:migrate rails aborted! Mysql2 ::错误:用户拒绝访问   'root'@'localhost'(使用密码:YES)

2 个答案:

答案 0 :(得分:0)

您应该在config-directory中更新database.yml文件。对于root用户,错误是由mysql响应身份验证错误引起的。您可能需要检查http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database以获取有关database.yml语法的信息。

您还可以通过运行mysql -u root -p来检查并验证shell中的登录信息并尝试输入密码。

答案 1 :(得分:0)

首先,这是正确的迁移代码:

class CreateAdmins < ActiveRecord::Migration[5.1]
  def self.up
    create_table :admins do |t|
      t.string :first_name, limit: 30
      t.string :last_name, limit: 30
      t.string :email, default: '', null: false
      t.string :password ,limit: 40

      t.timestamps
    end
  end

  def self.down
    drop_table :admins
  end
end

或只是更改 difference

class CreateAdmins < ActiveRecord::Migration[5.1]
  def change
    create_table :admins do |t|
      t.string :first_name, limit: 30
      t.string :last_name, limit: 30
      t.string :email, default: '', null: false
      t.string :password ,limit: 40

      t.timestamps
    end
  end
end

关于错误,您的database.yml for development env应如下所示:

development:
  adapter: mysql2
  encoding: utf8
  database: your_db_name
  username: your_user
  password: your_password
  host: 127.0.0.1
  port: 3306