4
3

More than 3 years have passed since last update.

【88日目】AWS Cloud9でMySQLで新規Railsアプリを立ち上げる際のエラー

Last updated at Posted at 2019-11-17

つい先日までパーフェクトPHPガーなどと言っていたのに突然のRailsで申し訳ありません。
実は神里さん(@yoshito410kam)という方のオンラインサロンで「チーム開発が経験できる」プログラミング初学者向けの企画がある(現在は募集中断?)とのことで、言語は違ったのですが、思い切って参加しました。

チームでgitのブランチを切ってプルリクやマージを活用する経験は一人ではなかなかできないため、自分の学習を中断してでも参加する価値があると考えたからです。

まずチーム開発を開始する前に課題があるとのことで、その課題をやる必要があるため、パーフェクトPHPは一旦中断となります。

==================================================================================

MySQLのインストールのエラー

さて、早速環境構築でつまづきました。
Cloud9でMySQLを使ってRailsアプリを新規作成して、Hello Worldを表示するアプリを作成する内容です。
実は私もRailsで簡単な掲示板は作成した経験があるので、簡単簡単と思ったのですが、初手で沼りました。

# MySQLを指定してrailsアプリを新規作成
$ rails new hello_sample -d mysql

はい早速エラーです。

An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'` succeeds before bundling.

どうもgemのmysql2のインストールに何かエラーが起きた模様。

yumでパッケージをインストールしてみます。

$ sudo yum install mysql-devel
Loaded plugins: priorities, update-motd, upgrade-helper
# 中略
Complete!

うまく言ったようです。
もう1度rails newを試してみると、今度はうまく通りました。

ソケットがないよのエラー

やったできたと思って今度はデータベースを作成

$ bundle exec rails db:create
#<Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"hello_sample_development"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'hello_sample_development'
#<Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"hello_sample_test"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'hello_sample_test'

はいエラーですorz

まず現在のソケットはsocket '/var/lib/mysql/mysql.sock'になっている模様。
※後からでもmysql_config --socketで確認できます。
しかし、こんなファイルは存在していない(と思う)。

  • つまり、ソケットを作るか?
  • それともソケットの場所を変えるか?

どちらかが必要になろうと仮説を立てた。

が、データベースはいじったら再起動が基本らしい。まずは試してみる。

$ sudo /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Initializing MySQL database:  Installing MySQL system tables...
191117  8:11:45 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
191117  8:11:45 [Note] /usr/libexec/mysql55/mysqld (mysqld 5.5.62) starting as process 15545 ...
OK
Filling help tables...
191117  8:11:45 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
191117  8:11:45 [Note] /usr/libexec/mysql55/mysqld (mysqld 5.5.62) starting as process 15552 ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/libexec/mysql55/mysqladmin -u root password 'new-password'
/usr/libexec/mysql55/mysqladmin -u root -h ip-172-31-31-51 password 'new-password'

Alternatively you can run:
/usr/libexec/mysql55/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/libexec/mysql55/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

再起動ができました。
再起動したら、改めてデータベースを作成する。

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

できました。

そんなディレクトリ見当たらないんだけど・・・? →/はルートディレクトリ

めでたしめでたし、ちゃんとデータベースは作成できました。

それはいいけど
/etc/init.d/mysqldとか、/var/lib/mysql/mysql.sockってどこにあるのって思う人も多いと思います。
多分今いるディレクトリでls -aとか試しても出てこないんだが? と言う方もいるかと。

例えば/etc/init.d/mysqldの先頭の/`はルートディレクトリと言って、一番根元のディレクトリのことです。
なので

$ cd /
$ ls -a
.   .autofsck     bin   cgroup  etc   lib    local       media  nvm-install.txt  proc  run   selinux  sys  usr
..  .autorelabel  boot  dev     home  lib64  lost+found  mnt    opt              root  sbin  srv      tmp  var

とすれば/etc/varも見つかるので安心してください。

Rails起動時にまたエラー

では早速rails sしようと思ったところ、またブラウザにエラー画面が・・・

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

どうもローカルのサーバーのソケットに接続できない模様。
パッと思いついたのは権限がないのかな、と。

権限を付与してみる

所有権を付与するchownと言うのがありますね。これで権限を付与できるようです。

$ sudo chown mysql:mysql /var/lib/mysql/mysql.sock

その後再起動
データベースいじったら必ず再起動です。

$ sudo /etc/init.d/mysqld restart

改めてrails s

Yay! You're in Rails!

繋がりました

4
3
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
4
3