AWS を利用して 作成したRailsアプリを動作させるための設定をしていますが、
データベースにテストデータの投入を実行すると下記のエラーが発生しデータが作成されません。
わかる方がいらっしゃればどうぞよろしくお願いいたします(m_ _m)
###動作環境
OS:mac
Ruby:2.4.5
Ruby on Rails: 4.2.11
現状
- rails: 4.2.11.1
- EC2構築済み、RDS構築済み
- EC2にmysqlをインストール
- EC2からmysql(RDS)へ mysqlコマンドでの接続はできた
(参考: AWSのEC2からRDSに接続してmySQLを操作。EC2踏み台サーバー構築メモ|おちゃカメラ。)
- rakeコマンドによるデータベース、テーブルの作成はできました
- gem 'seed-fu'によるテストデータの作成がエラーのため実行できない
###期待する動作
gem seed-fu によるテストデータ作成が完了してほしい
エラー内容
bash
1$ bundle exec rake db:seed; 2# == Seed from /var/www/rails/my_app_name/db/fixtures/02_user.rb 3# rake aborted! 4# Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 5# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `connect' 6# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `initialize' 7# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new' 8# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `mysql2_connection' 9# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection' 10# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection' 11# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection' 12# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout' 13# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout' 14# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection' 15# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection' 16# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection' 17# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection' 18# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/connection_handling.rb:87:in `connection' 19# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction' 20# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/seed-fu-2.3.9/lib/seed-fu/runner.rb:35:in `run_file' 21# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/seed-fu-2.3.9/lib/seed-fu/runner.rb:26:in `block in run' 22# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/seed-fu-2.3.9/lib/seed-fu/runner.rb:25:in `each' 23# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/seed-fu-2.3.9/lib/seed-fu/runner.rb:25:in `run' 24# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/seed-fu-2.3.9/lib/seed-fu.rb:29:in `seed' 25# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/seed-fu-2.3.9/lib/tasks/seed_fu.rake:36:in `block (2 levels) in <top (required)>' 26# /var/www/rails/my_app_name/vendor/bundle/ruby/2.4.0/gems/rake-12.3.3/exe/rake:27:in `<top (required)>' 27# /home/ec2user/.rbenv/versions/2.4.5/bin/bundle:23:in `load' 28# /home/ec2user/.rbenv/versions/2.4.5/bin/bundle:23:in `<main>' 29# Tasks: TOP => db:seed_fu 30# (See full trace by running task with --trace)
###(データベース、テーブル作成後のEC2からmysql(RDS)へ mysqlコマンドでの接続の実行結果)
bash
1sudo yum install mysql; 2# rails、自作アプリのcloneなどは割愛 3bundle exec rake db:create RAILS_ENV=production; 4bundle exec rake db:migrate RAILS_ENV=production; 5 6 7 8 9 10mysql -h RDSエンドポイント -P 3306 -u ユーザー名 -p 11# Enter password: 12# Welcome to the MariaDB monitor. Commands end with ; or \g. 13# Your MySQL connection id is 61 14# Server version: 5.7.22 Source distribution 15# 16# Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. 17# 18# Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 19# 20# MySQL [(none)]>show databases; 21# +---------------------+ 22# | Database | 23# +---------------------+ 24# | information_schema | 25# | innodb | 26# | mysql | 27# | performance_schema | 28# | my_app_name_production | 29# | sys | 30# +---------------------+ 31# 6 rows in set (0.00 sec) 32# 33# MySQL [(none)]> use my_app_name_production; 34# Reading table information for completion of table and column names 35# You can turn off this feature to get a quicker startup with -A 36# 37# Database changed 38# MySQL [my_app_name_production]> show tables 39# -> ; 40# +-------------------------------+ 41# | Tables_in_my_app_name_production | 42# +-------------------------------+ 43# | addresses | 44# | : | 45# | : | 46# | : | 47# +-------------------------------+ 48# 22 rows in set (0.00 sec) 49MySQL [(none)]> exit 50# Bye 51 52
試したこと
参考サイト:
- mysqlのsocketエラーでrailsアプリが起動できない - Qiita
- AWSでMysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)のエラー - たれみみマンデー
EC2接続後のterminalで
mysql_config --socket
コマンドによるsocketの指定を確認、config/database.yml
のsocketを変更
ruby
1default: &default 2 adapter: mysql2 3 encoding: utf8 4 pool: 5 5 username: root 6 password: 7 socket: /var/lib/mysql/mysql.sock # mysql_config --socket の実行結果 8 9development: 10 <<: *default 11 database: my_app_name_development 12 13test: 14 <<: *default 15 database: my_app_name_test 16 17 18production: 19 <<: *default 20 database: my_app_name_production 21 host: RDSのエンドポイント 22 username: root 23 password: パスワード
mysql --help | grep my.cnf
、ps ax | grep mysql
を実行、またその実行結果
bash
1[ec2user@ip-xxx-xxx-xxx-xxx ~]$ ps ax | grep mysql 2# 25114 pts/1 S+ 0:00 mysql -h RDSのエンドポイント -P 3306 -u root -p 3# 25654 pts/4 S+ 0:00 grep --color=auto mysql 4 5[ec2user@ip-xxx-xxx-xxx-xxx ~]$ mysql --help | grep my.cnf 6# /etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf 7# order of preference, my.cnf, $MYSQL_TCP_PORT, 8 9[ec2user@ip-xxx-xxx-xxx-xxx ~]$ ps ax | grep mysql 10# 25114 pts/1 S+ 0:00 mysql -h RDSのエンドポイント -P 3306 -u root -p 11# 25654 pts/4 S+ 0:00 grep --color=auto mysql 12 13[ec2user@ip-xxx-xxx-xxx-xxx ~]$ cat /etc/mysql/my.cnf 14# cat: /etc/mysql/my.cnf: No such file or directory 15 16[ec2user@ip-xxx-xxx-xxx-xxx ~]$ cat /etc/my.cnf 17[mysqld] 18datadir=/var/lib/mysql 19socket=/var/lib/mysql/mysql.sock 20# Disabling symbolic-links is recommended to prevent assorted security risks 21symbolic-links=0 22# Settings user and group are ignored when systemd is used. 23# If you need to run mysqld under a different user or group, 24# customize your systemd unit file for mariadb according to the 25# instructions in http://fedoraproject.org/wiki/Systemd 26 27[mysqld_safe] 28log-error=/var/log/mariadb/mariadb.log 29pid-file=/var/run/mariadb/mariadb.pid 30 31# 32# include all files from the config directory 33# 34!includedir /etc/my.cnf.d 35 36[ec2user@ip-xxx-xxx-xxx-xxx ~]$ cat ~/.my.cnf 37# cat: /home/ec2user/.my.cnf: No such file or directory
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。