20200519のMySQLに関する記事は4件です。

Rails AWSデプロイ エラー遭遇まとめ

参考文献

Rubyインストール編

rbenv インストールエラー

$ rbenv install -v 2.6.5
configure: error: in `/tmp/ruby-build.202005191817.10626/ruby-2.6.5':
configure: error: no acceptable C compiler found in $PATH

解決コマンド

$ sudo yum install gcc openssl-devel
$ sudo yum install -y gcc-6 bzip2 openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel
$ sudo yum erase ruby.noarch
$ sudo yum install gcc

MySQL編

root初期パスワード在り処

$ cat /var/log/mysqld.log | grep password
A temporary password is generated for root@localhost: ************

初期パスワードログイン

$ mysql_secure_installation
Enter password for user root: ************
New password: ************
Re-enter new password: ***********

RMgick編

RMagickインストールエラー

$ bundle install --path vendor/bundle
An error occurred while installing rmagick (3.0.0), and Bundler cannot continue.
Make sure that `gem install rmagick -v '3.0.0' --source 'https://rubygems.org/'`

解決コマンド

$ sudo yum -y install ImageMagick
$ sudo yum -y install ImageMagick-devel

Nginx編

Nginxインストールエラー

$ sudo yum install nginx
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
パッケージ nginx は利用できません。
エラー: 何もしません

解決コマンド

$ sudo amazon-linux-extras install nginx1.12
                 or
$ sudo yum install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm -y
$ sudo yum install nginx -y

nginx.conf設定後のpostメソッド対策

$ cd /var/lib
$ sudo chmod -R 775 nginx
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

HerokuデプロイでMySQLのConnectionErrorが出たときの対処

状況

heroku上にアプリを作成するまでは完了していて、URLにアクセスしたら「Welcome to your new app!」と出て、
herokuへgitでプッシュまでしたのですが、最後にデータベースを作成しようとしました。しかし、ここでConnectionError。

$ heroku run rails db:migrate
:1033:in `retrieve_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:90:in `connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:172:in `migrate'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/command.rb:48:in `invoke'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/commands.rb:18:in `<top (required)>'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `block in require'
/app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:257:in `load_dependency'
/app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `require'
/app/bin/rails:9:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

解決した方法

色々なサイトをみているうちに、DATABASE_URLが関係あるということがわかったので、見てみる。

$ heroku config | grep CLEARDB_DATABASE_URL
=> CLEARDB_DATABASE_URL=mysql://~省略

「mysql://」を「mysql2://」に変える必要がある!
gemではmysql2を使っていたので対応させる必要があった。

なので上書きします

$ heroku config:set DATABASE_URL=mysql2://~省略
Setting DATABASE_URL and restarting  アプリ名... done, v9
DATABASE_URL: mysql2://~省略

これで上書きされました。
そして再度データベース作成をしてみます

$ heroku run rails db:migrate
...省略
Migrating to CreateIntros (20200518235947)
== 20200518235947 CreateIntros: migrating =====================================
-- create_table(:intros)
   (10.1ms)  CREATE TABLE `intros` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL)
   -> 0.0106s
== 20200518235947 CreateIntros: migrated (0.0106s) ============================

これで正常にDBが作成され、公開できます。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【credentials】CircleCIのテストでエラー!「ActiveRecord::StatementInvalid: Mysql2::Error: Table ‘〇〇’ doesn’t exist: SHOW FULL FIELDS FROM `〇〇`」

はじめに

ローカルでRSpecのテストをパスした後、CircleCIのテストでタイトルのエラーとなり、それを解決するまでの記録。

検証環境

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.15.3
BuildVersion:   19D76
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin19]
$ rails -v
Rails 5.2.4.2

rails db:migrate

エラーの内容から、テーブルがないなら、db:migrateか?と思い、

 $ bin/rails db:migrate RAILS_ENV=production

をターミナルで試す。

すると、

rails aborted!
ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit`
/Users/aa/projects/my_app/config/environment.rb:5:in `<top (required)>'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate => db:load_config => environment
(See full trace by running task with --trace)

、と言われる。

教えてくれている通りに
rails credentials:edit
を行うと、、

$ rails credentials:edit
No $EDITOR to open file in. 

ファイルを開くための $EDITOR がありません。

また検索し、credentialsをeditする為の$EDITORを登録する。

# .bash_profileに環境変数 EDITOR を設定する
$ echo 'export EDITOR="vi"' >> ~/.bash_profile
# bash_profileを読み込ませる
$ source ~/.bash_profile
# 登録できている事を確認する。
$ echo $EDITOR

これで、credentialsを確認できるようになり、内容を確認したが、Missing secret_key_baseという事はなく、登録されている。

次に、
Rails 5.2 で ActiveSupport::MessageEncryptor::InvalidMessage
こちらを拝見し、

CircleCIの環境変数に登録しているmaster.keyとその時点でのローカルのもの(config/master.key)が一致しているか確認するが、
問題なく一致していた。

ここで、気づく。
登録されているsecret_key_baseが間違っている?
また検索し、

$ RAILS_ENV=production bundle exec rails secret

を実行。
長いキーが返ってくるので、それをcredentialに登録し直す。

これで再度プッシュすると、自分の環境だとうまくテストがパスした。

参考にさせて頂いた記事

終わりに。

最後まで読んで頂きありがとうございます:bow_tone1:
転職の為、未経験の状態からRailsを学習しております。正しい知識を着実に身に着け、実力のあるエンジニアになりたいと考えています。継続して投稿していく中で、その為のインプットも必然的に増え、成長に繋がるかと考えています。
今現在、初心者だからといって言い訳はできないですが、投稿の内容に間違っているところや、付け加えるべきところが多々あるかと思いますので、ご指摘頂けると幸いです。この記事を読んで下さりありがとうございました。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

ubuntuにMySQL導入するメモ

MySQLインストール

sudo apt install mysql-server mysql-client libmysqlclient-dev
sudo service mysql status

開発用ユーザー作成(rootのpassはrootからNewPasswordに変更)

まずはrootでログイン

sudo mysql -u root -p

開発ユーザーの追加

GRANT ALL PRIVILEGES ON *.* TO ユーザー名@localhost IDENTIFIED BY 'パスワード' WITH GRANT OPTION;

保存

flush privileges;

ついでにrootも初期パスワードから変更

ALTER USER 'root'@'localhost' IDENTIFIED BY '新しいパスワード';

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む