LoginSignup
1
1

More than 3 years have passed since last update.

Ruby: bundle exec rake db:create でエラーが出た時に対処した方法 -未来の自分に向けて

Posted at

[version内容]
ProductName: Mac OS X
ProductVersion: 10.15.2
BuildVersion: 19C57
mysql Ver 8.0.19 for osx10.15 on x86_64 (Homebrew)
Rails version: 6.0.2.1
Ruby version: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin19]

まず、rails appの作成

rails new sample -d mysql

すると、すかさずエラーが!!

An error occurred while installing mysql2 (0.5.3), and Bundler cannot
continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'`
succeeds before bundling.
In Gemfile:
 mysql2
     run bundle binstubs bundler
Could not find gem 'rails (~> 6.0.2, >= 6.0.2.1)' in any of the gem sources
listed in your Gemfile.
     run bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`
    rails webpacker:install
Could not find gem 'rails (~> 6.0.2, >= 6.0.2.1)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

と表示されたため、

gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

を実行すると、さらにエラーが

ERROR: While executing gem ... (Errno::EACCES)
  Permission denied @ rb_sysopen - /Users/PC名/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/CHANGELOG.md

と表示されたため、

sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

を実行してみる

bundle install

を実行し

cd sample
rails s

してみると、エラー

Could not find gem 'rails (~> 6.0.2, >= 6.0.2.1)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

では

Bundle install

してみると、エラー

ERROR: While executing gem ... (Errno::EACCES)
  Permission denied @ rb_sysopen - /Users/PC名/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/CHANGELOG.md

と表示される

sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

してみると

ERROR: Error installing mysql2:
    ERROR: Failed to build gem native extension.
  current directory: /Users/PC名/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
/Users/PC名/.rbenv/versions/2.6.3/bin/ruby -I /Users/PC名/.rbenv/versions/2.6.3/lib/ruby/2.6.0 -r ./siteconf20200301-31071-1cavam6.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
Using mysql_config at /usr/local/bin/mysql_config
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... no
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/8.0.19/lib
-----
creating Makefile

current directory: /Users/PC名/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/PC名/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/PC名/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/PC名/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0-static/mysql2-0.5.3/gem_make.out

と初心者には厳しい、エラーの嵐

なので

gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'`

を実行するも

ERROR: While executing gem ... (Errno::EACCES)
  Permission denied @ rb_sysopen - /Users/PC名/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/CHANGELOG.md

となったので

sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

を実行し

rails s 

すると

Could not find gem 'rails (~> 6.0.2, >= 6.0.2.1)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

と表示。

gemfileにgem ‘rails (~> 6.0.2, >= 6.0.2.1)’ 

と記載されているがどうすればいいかわからず途方にくれる。

その後、linker command failed with exit code 1と、
mysqlをセットでググる。

というヒントを頂き、
以下の記事を参考にトライするも不可

【Rails】MySQL2がbundle installできない時の対応方法

その後色々試してみて、以下で bundle install可能になりました。

bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

実行後

$ bundle install
$ rails s 

で(抜粋)以下のようなエラー

configuration.rb:95:in `rescue in load': Webpacker configuration file not found /Users/PC名/Desktop/hello/sample/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /Users/PC名/Desktop/hello/sample/config/webpacker.yml (RuntimeError)
rails webpacker:install

を実行し

【Rails】Rails6 開発時につまづきそうな webpacker, yarn 関係のエラーと解決方法

を参考にインストール

rails webpacker:install

で表示される以下のエラーを検討

Webpacker configuration file not found /Users/PC名/Desktop/hello/sample/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /Users/PC名/Desktop/hello/sample/config/webpacker.yml (RuntimeError)
yarn -v

をすると

-bash: yarn: command not found

と表示される。

brew install yarn
yarn -v
yarn install v1.22.0

を実行し、

rails webpacker:install
rails s

すると

Mysql2::Error::ConnectionError (Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)):

と表示されたため

Markdown: mysqlが起動できない(Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2))

を参考に

sudo touch /tmp/mysql.sock
sudo mysql.server restart

とすると

ERROR! MySQL server process #19810 is not running!
Starting MySQL
.. ERROR! The server quit without updating PID file (/usr/local/var/mysql/mbp.local.pid).

となる。それではと

mysqlの起動エラー対応 in mac

を参考に

sudo chmod -R 777 /usr/local/var/mysql/ 
mysql.server start

実行し

Starting MySQL
.......... ERROR! The server quit without updating PID file (/usr/local/var/mysql/mbp.local.pid).

と表示され、

touch /usr/local/var/mysql/mbp.local.pid
mysql.server start

その後、

Starting MySQL
.rm: /tmp/mysql.sock: Permission denied
2020-03-01T13:13:48.6NZ mysqld_safe Fatal error: Can't remove the socket file:
/tmp/mysql.sock.
Please remove the file manually and start /usr/local/Cellar/mysql/8.0.19/bin/mysqld_safe again;
mysqld daemon not started
 ERROR! The server quit without updating PID file (/usr/local/var/mysql/mbp.local.pid).

となり、

mysqlがどうしても起動しない

を参考に

ls -la /usr/local/var/mysql/mbp.local.pid
ls: /usr/local/var/mysql/mbp.local.pid: No such file or directory
sudo chown _mysql:_mysql /usr/local/var/mysql/mbp.local.pid
usage: chown [-fhnv] [-R [-H | -L | -P]] owner[:group] file ...
    chown [-fhnv] [-R [-H | -L | -P]] :group file ...
mysql.server start
Starting MySQL
... ERROR! The server quit without updating PID file (/usr/local/var/mysql/mbp.local.pid).

と表示されるので

sudo rm -rf /usr/local/var/mysql
brew uninstall mysql
brew install mysql

などトライし

mysql 起動時のThe server quit without updating PID file エラーの回避法

を参考に

touch /usr/local/var/mysql/mbp.local.pid

【8.0.12】mysql2が開けないエラーの解消方法

を参考に

cd /tmp
sudo rm -rf mysql.sock
mysql.server start
Starting MySQL
.. SUCCESS! 
``
となったので

Rails s
```
すると

ActiveRecord::NoDatabaseError

と表示され

bundle exec rake db:create
Rails s

を行い、完成しました。

ふぅ(´-`) やっと完了です。

この記事が、同じエラーで困っている方の役にも立てたら幸いです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1