[英]rails db:migrate rails aborted! Mysql2::Error: Access denied for user 'root'@'localhost' (using password: YES)
I'm running this migration: 我正在运行此迁移:
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
I get an error saying: 我收到一条错误消息:
rails db:migrate rails aborted! rails db:migrate rails已中止! Mysql2::Error: Access denied for user 'root'@'localhost' (using password: YES) Mysql2 :: Error:用户'root'@'localhost'的访问被拒绝(使用密码:是)
You should update your database.yml file, in the config-directory. 您应该在配置目录中更新database.yml文件。 The error is caused by mysql responding with an authentication error, for your root user. 该错误是由mysql对您的root用户响应的身份验证错误引起的。 You might wanna check http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database for information on the database.yml syntax. 您可能想查看http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database以获得有关database.yml语法的信息。
You can furthermore check and validate the login in shell by running mysql -u root -p
and try entering your password. 您还可以通过运行mysql -u root -p
并尝试输入密码来检查和验证Shell中的登录。
For the first, this is correct migration code: 首先,这是正确的迁移代码:
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
or just change 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
About error, your database.yml
for development env, should looks like this: 关于错误,用于开发环境的database.yml
应该如下所示:
development:
adapter: mysql2
encoding: utf8
database: your_db_name
username: your_user
password: your_password
host: 127.0.0.1
port: 3306
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.