【Rails v5.2】bundle install時の「ERROR: While executing gem ... (Errno::EACCES)」を解消

環境

エラー発生

$ bundle install 
:
:
Fetching mysql2 0.4.10
Installing mysql2 0.4.10 with native extensions
Errno::EACCES: Permission denied @ rb_sysopen - /usr/local/var/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/mysql2-0.4.10/CHANGELOG.md
An error occurred while installing mysql2 (0.4.10), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.10' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2

エラー文に書かれているコマンドを実行

$ gem install mysql2 -v '0.4.10' --source 'https://rubygems.org/'   
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /usr/local/var/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/mysql2-0.4.10/CHANGELOG.md

解決方法

上記のフォルダの権限を変更してもしつこくPermission deniedが出てくるので再帰的にchmodをする。

$ find <FOLDER_NAME> -type f -print | xargs sudo chmod 777       

参考

今回は参考にしなかったけど次はまったときデバッグ方法とか役立ちそう

【keras】weightの中身をみるために「RuntimeError: maximum recursion depth exceeded」と「RecursionError: cannot set the recursion limit the limit is too low」を解決

やりたいこと

画像補完のプログラムを作成しているが、学習によって作成されたweightの中にnanが混ざり込んでいてうまく画像が生成されず真っ黒の画像が出てきてしまう。 そこでweight(h5ファイル)の中身を見てどこがnanになっているか確認したい。

方針1:get_weightsを使う

以下のサイトを参考にした。
kerasのmodel.get_weights()で得られるlistの構造 - Qiita

model = PConvUnet(weight_filepath='data/logs/')
model.get_weights()

ここで以下のエラーが発生

RuntimeError: maximum recursion depth exceeded

再帰処理が最大の深さまでいっちゃてるというエラー。 これはPythonの問題らしいので、深さを増やしてあげると解決できるらしい。

import sys
sys.setrecursionlimit(10000)

すると次は以下のようなエラーが発生

RecursionError: cannot set the recursion limit the limit is too low 〜〜

方針を変える。

方針2:model.layersをforループで回す

for layer in model.layers:
    print(layer)

すると以下のようなエラーが発生。
エラー内容は以下を参考に。

TypeError: 'method' object is not iterable

ここれいうmodelは、自作のPConvUnetというものをラッパーとして使っているので、結局以下のようにしてnanを発見した。

for layer in model.model.layers:
    weights = layer.get_weights()
    for weight in weights:
        print(weight.shape)
        if np.any(np.isnan(weight)):
            print(layer.name)
            print(weights)

参考まとめ

【自分用メモ】gitで色々詰まったときにみる

自分の.gitconfig

github.com

取り消す系

直前のcommitをやり直す

$ git add -A
$ git commit --amend -m "Commit message"

git commitをやり直しする&取り消しする(「git commit --amend」と「git reset」)

addを取り消す

$ git reset HEAD <FILENAME>

git add の取り消し方法と、関連コマンドまとめ

masterへのpushを取り消す

# まずは以下2つのコマンドで特定のコミットまで戻る
$ git log --oneline # ログの番号確認
$ git reset --hard <上で確認した戻りたいcommitの番号>


$ git log --oneline # 目的の箇所まで戻れているか確認
$ git push -f

↓が詳しかった

【git】git pushを取り消す - tweeeetyのぶろぐ的めも

エラー対処

push時にerror: failed to push some refs to

$ git push origin :<BRANCH>
$ git push origin <BRANCH>

:をつけるとremoteのブランチをDeleteするということになる。 これはローカルブランチが絶対に正しいときに使う。

その他の操作

新規ファイルをstashする

# ✕
$ git stash
No local changes to save

# ◯
$ git stash -u

新規フォルダをstashする

# ✕
$ git status
On branch refactor-question-cron
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   db/sql/init.sql

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    sns_manager/


$ git stash -u
$ git status
On branch refactor-question-cron
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    sns_manager/
    # ↑フォルダがstashされてない

# ◯
$ git stash --include-untracked
$ git status
On branch refactor-question-cron
nothing to commit, working tree clean

ブランチにmasterの内容を反映させる

$ git checkout master
$ git pull origin master
$ git checkout <BRANCH_NAME>
$ git rebase <BRANCH_NAME>

Gitで開発ブランチにmasterの内容を反映させる方法 (git rebase)

ブランチを作り忘れた

$ git stash
$ git stash list
$ git checkout -b <BRANCH_NAME>

$ git stash pop # 実行後適用した状態は削除される
$ git stash apply # 削除されない

まだcommitしてない場合は以下でおk。

$ git checkout -b <BRANCH_NAME>

ブランチを作り忘れた時 - Qiita

特定のブランチをクローンする

$ git clone -b ブランチ名 リポジトリのアドレス

deleteしたファイルをstageにaddしたい

$ git add <FILENAME> --update

Git 、削除したファイルが stage に上手く上がらない時 - CHROMA

git logを見やすくしたい

# いい感じのコマンドを「git log」に登録して使えるようにする
$ git config --global alias.tree 'log --graph --all --format="%x09%C(cyan bold)%an%Creset%x09%C(yellow)%h%Creset %C(magenta reverse)%d%Creset %s"'

git log を見やすくする - Qiita

conflictしたファイルの一覧を表示させる

$ git diff --name-only --diff-filter=U

# エイリアスを設定
$ git config --global alias.conf '!git ls-files -u | cut -f 2 | sort -u'

Git で競合ファイル一覧を楽に見る方法 - Qiita

設定したエイリアスを確認する

$ git config --global --list | grep alias\.

【自分用メモ】MySQLのエラー色々「ERROR 1045」「ERROR 2002 」

エラーと攻略方法

雑にメモ。

$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

MySQLを停止

$ sudo mysql.server stop
$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

$ sudo touch /tmp/mysql.sock
$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)

$ mysql.server restart
ERROR! The server quit without updating PID file (/usr/local/var/mysql/Saneatsus-MacBook-Pro.local.pid).

$ sudo chown mysql:mysql /tmp # 実行しなくていい場合も
$ sudo chown -R _mysql:_mysql /usr/local/var/mysql
$ sudo mysql.server start
Starting MySQL
. SUCCESS!

$ mysql -u root -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

$ mysql -u root                                                                                                                                          
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24 Homebrew

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
$ sudo ps aux|grep mysql
$ sudo kill 9999

mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

$ sudo rm -rf /usr/local/var/mysql
$ brew uninstall mysql
$ brew install mysql
$ mysql.server start
Starting MySQL
.. ERROR! The server quit without updating PID file (/usr/local/var/mysql/Saneatsus-MacBook-Pro.local.pid).

$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)

$ mysql -u root 
:
:
mysql> 

その他

Railsでは config/settings/development.local.yml の中でpasswordを空にしちゃう。

db:
  default:
    username: "root"
    password: ""

参考

【Rails】rake db:create時にMysql2のエラーが発生(ERROR! The server quit without updating PID file)

問題内容

以下のコマンドを実行した結果,MySQL2のエラーが発生した.

$ rake db:create
:
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "timeout"=>5000, "username"=>"sane", "password"=>"sane", "database"=>"sales_aggregator_dev"}
rake aborted!
Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
:

発生手順

rake db:createを実行すると,上記エラーが発生する
mysqlのサーバをリスタートしてみたところPIDファイルが確認出来ないといわれる

$ sudo mysql.server restart
Password:
 ERROR! MySQL server PID file could not be found!
Starting MySQL
.Logging to '/usr/local/var/mysql/Saneatsus-MacBook-Pro.local.err'.
 ERROR! The server quit without updating PID file (/usr/local/var/mysql/Saneatsus-MacBook-Pro.local.pid).

Qiitaの記事を参考にpidファイルを作成した後,sudo mysql.server restartを実行したが,結果は変わらなかった.

解決方法

こっちのQiitaの記事を参考に以下を実行して解決!

$ rm -rf /usr/local/mysql
$ rm -rf /Library/StartupItems/MYSQL
$ rm -rf /Library/PreferencePanes/MySQL.prefPane
$ rm -rf /Library/Receipts/mysql-.pkg
$ rm -rf /usr/local/Cellar/mysql*
$ rm -rf /usr/local/bin/mysql*
$ rm -rf /usr/local/var/mysql*
$ rm -rf /usr/local/etc/my.cnf
$ rm -rf /usr/local/share/mysql*
$ rm -rf /usr/local/opt/mysql 

$ brew install mysql@5.7
$ echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc 
$ source ~/.zshrc
$ mysql --version
mysql  Ver 14.14 Distrib 5.7.23, for osx10.13 (x86_64) using  EditLine wrapper

$ mysql.server start
Starting MySQL
. SUCCESS!

【自分用メモ】利用規約を作成する時に参考にしたサイト

テンプレート

利用規約

プライバシーポリシー

その他

色々探している時に面白かったサイト

【自分用メモ】RubyでNokogiriを使ってスクレイピングする時に役に立ったサイトまとめ

大体ここらへんを見れば書き方がわかった。