LoginSignup
12
9

More than 3 years have passed since last update.

AWS Cloud9でmysql2がインストールできなかった時の対応方法

Last updated at Posted at 2019-06-07

概要

AWS Cloud9でmysql2をインストールするときにエラーでつまずきました。同じエラー内容でつまずいた方の参考になればうれしいのと、自分が同じ失敗をくりかえさないために今回投稿いたしました。

AWS Cloud9にて新規作成

mysqlを指定して新規作成。

rails new hello_sample -d mysql

エラー発生

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.

まずエラーコードのコマンドを試す。

gem install mysql2 -v '0.4.10

その後、bundle installするもエラー内容変わらず。

対応方法

エラーコード内容を再度ゆっくり見ていくと、赤字エラーコード以外の箇所に、mysql-develをインストールして、再度試すと記載がある。

mysql.h is missing. You may need to 'apt-get install libmysqlclient-dev' or
'yum install mysql-devel', and try again.

mysql develをインストールする。

$sudo yum install -y mysql-devel
#...成功!

bundle installをする。

$bundle install

Installing mysql2 0.4.10 with native extensions
#...成功!

成功!

(参考URL:https://qiita.com/emahiro/items/1a63d676be9fc4975759)

次にデータベースを作成する。

$ bundle exec rake db:create

2度目のエラー → 再度対応

もう大丈夫かと思ったら...
socketファイルがない、という2度目のエラー発生。

Error: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)>

mysqlを再起動することで、/var/lib/mysql/mysql.sockが自動作成されるというので、試す。

$ sudo /etc/init.d/mysqld restart
#...OK!

成功!
(参考URL:https://qiita.com/kanohisa/items/564035efd74d9c75bdcb)

再度、データベース作成。

$ bundle exec rake db:create
Created database 'hello_sample_development'
Created database 'hello_sample_test'

今度は成功!

次にmysqlが起動するか確認。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.62 MySQL Community Server (GPL)

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> 

大丈夫な様子。データベースも作成されているか確認。

show databases;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)

1度mysqlをexitで閉じて、rails sで、ちゃんと起動するか立ち上げてみる。

Yay! You’re on Rails!

解決!

まとめ

以上、AWS Cloud9でmysql2がインストールできなかった時の対応方法でした!
エラーコード内容は赤字のみならず、他の箇所も確認することで、エラー解決への道が開ける。むしろ当たり前のことなのかもしれないが、かなり大切ということを認識しました!
同じエラー内容でつまずいた方の参考になればうれしい限りです!

12
9
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
12
9