概要
本番環境(aws/ec2)で,データベースを作りたいがMysqlのエラーが出る。
環境
ruby '2.6.5'
rails '6.0.0'
エラーメッセージ
行うコマンド
[ec2-user@ip-172-31-23-189 <リポジトリ名>]$rails db:create RAILS_ENV=production
エラーメッセージ
Access denied for user 'root'@'localhost' (using password: YES) Couldn't create 'rooad_production' database. Please check your configuration. rails aborted! Mysql2::Error::ConnectionError: Access denied for user 'root'@'localhost' (using password: YES) bin/rails:4:in `<main>' Tasks: TOP => db:create (See full trace by running task with --trace)
自分で行ったこと
① MySQLにパスワードを設定しているかを確認。
$ mysql_secure_installation
②開発環境と本番環境のパスワード統一
default: &default adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: <%= ENV['DATABASE_PASSWORD'] %> socket: /tmp/mysql.sock development: <<: *default database: rooad_development # 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: <<: *default database: rooad_test # As with config/credentials.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is # ever seen by anyone, they now have access to your database. # # Instead, provide the password as a unix environment variable when you boot # the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database # for a full rundown on how to provide these environment variables in a # production deployment. # # On Heroku and other platform providers, you may have a full connection URL # available as an environment variable. For example: # # DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" # # You can use this database configuration with: # # production: # url: <%= ENV['DATABASE_URL'] %> # production: <<: *default database: rooad_production username: root password: <%= ENV['DATABASE_PASSWORD'] %> socket: /var/lib/mysql/mysql.sock
③MySQLを再起動
作られてないので、再起動は無駄と思いますが念の為。
$ sudo systemctl restart mysqld.service Failed to restart mysqld.service: Unit not found. $ sudo mysql.server restart sudo: mysql.server: コマンドが見つかりません
④MySQLのrootユーザーのパスワードの確認
$ mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 37 Server version: 5.5.68-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
エラー解決に参考にした記事
https://qiita.com/naota7118/items/b62d71484e21d6739d68
socket: /var/lib/mysql/mysql.sockを消したら
[ec2-user@ip-172-31-35-186 road]$ rails db:create RAILS_ENV=production Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) Couldn't create 'rooad_production' database. Please check your configuration. rails aborted! Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) bin/rails:4:in `<main>' Tasks: TOP => db:create (See full trace by running task with --trace)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/15 00:25
2021/10/15 00:46
2021/10/15 01:13
2021/10/15 01:27
2021/10/15 05:50