简体   繁体   中英

Rails mysql Access denied for user 'root'@'localhost' (using password: NO) error

I know there are a ton of questions and answers related to this question but for all I have tried, it just didn't work for me. When I run my rake tasks such as bin/rake recognize:init or even rake db:prepare or rake db:create , I get the error message below:

Error Log

mac@MacBook-Pro-2 recognize-develop % bin/rake recognize:init
Please require the stackprof gem falling back to fast_stack
Access denied for user 'root'@'localhost' (using password: NO)
Couldn't drop database 'database_name'
rake aborted!
Mysql2::Error::ConnectionError: Access denied for user 'root'@'localhost' (using password: NO)
/Users/mac/Downloads/recognize-develop/lib/tasks/recognize.rake:233:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:drop:_unsafe
(See full trace by running task with --trace)

/Users/mac/Downloads/recognize-develop/lib/tasks/recognize.rake:233

desc "Initialize Recognize. To be run from fresh install."
  task :init => :environment do
    prevent_production!
    ENV['DISABLE_DATABASE_ENVIRONMENT_CHECK'] = "1"

    Rake::Task['tmp:clear'].invoke
    Rake::Task['db:drop'].invoke # This is LINE 233. It doesn;t go beyond this.
    Rake::Task['db:create'].invoke
    Rake::Task['db:migrate'].invoke

    load "#{Rails.root.to_s}/db/schema.rb" # dont load schema b/c there is some issue with mysql index lengths when done this way
    Rails.application.load_seed

  end

database.yml file

defaults: &defaults
  adapter: mysql2
  username: <%= ENV['DATABASE_MYSQL_USERNAME'] || 'root' %>
  password: <%= ENV['DATABASE_MYSQL_PASSWORD'] || '' %>
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci
  port: 3306
  host: <%= ENV['DATABASE_MYSQL_HOST'] || '127.0.0.1' %>
  variables:
    sql_mode: TRADITIONAL  


development:
  <<: *defaults
  database: <%= ENV['DATABASE_MYSQL_DATABASE'] || 'database_name' %>

Ruby Version ruby 2.7.2p137

Rails Version Rails 6.0.3.7

I have tried everything. I can communicate with my MySQL in the terminal and run query but I get error with this app using MySQL. How can I go about it?

Maybe you should create a user in the database with sufficient permissions to use it in your application instead of using root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM