LoginSignup
0
1

More than 3 years have passed since last update.

RailsのApp作成のデータベース作成(rake db:create)でハマるところ

Posted at

環境

OS:AmazonLinux2
Ruby:ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
Rails:Rails 5.0.0

初めに

Railsアプリケーションを作成してる際に$ rake db:createでDBを作れず、いろいろとエラーに苦戦したので、共有します。

ハマるところ① $ rake db:create

$ rake db:createを実行すると、エラー発生。

The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
#<Mysql2::Error: 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"=>"shift-management_development"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'shift-management_development'
#<Mysql2::Error: 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"=>"shift-management_test"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'shift-management_test'

これは、データベースのサーバーが起動してないから。
次のコマンドでデータベースサーバーを起動する。

$ sudo systemctl start mysqld

ここでエラーが発生した場合は、ハマるところ③へ。

ハマるところ② $ mysql.server start

$ sudo systemctl start mysqld ではなく $ mysql.server startを実行すると、

このエラーが出ることがある。

bash: mysql.server: command not found

これはMySQLを起動するコマンドは複数あり、使えないコマンドを実行したことが原因。
それぞれのコマンドを簡単にまとめると、
$ mysql.server startはローカル環境などでよく使われるが、使えない環境もある。
$ sudo systemctl start mysqldは環境に依存せず、MySQLを起動することが可能である。

より具体的な違いについては以下のリンクが参考になると思います。
MySQL $ mysql.server startコマンドと$ systemctl start mysqldコマンドの違いを調べてみた

なので、$ sudo systemctl start mysqldを実行する。

ハマるところ③ $ sudo systemctl start mysqld

$sudo systemctl start mysqld

このエラーが出る場合。

Failed to start mysqld.service: Unit not found.

これは、データベースがMySQLではないことが原因。
AmazonLinux2では、MariaDB という MySQL とは別のデータベースがデフォルトで既にインストールされています。

DBサーバー(MariaDB)を起動
$ sudo systemctl start mariadb

起動状況の確認
$ sudo systemctl status mariadb

OS起動時に自動的に起動するように設定
$ sudo systemctl enable mariadb

これで、データベースを作成します。
$ rake db:create

The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Created database 'shift-management_development'
Created database 'shift-management_test'

できました。

参考

MySQL $ mysql.server startコマンドと$ systemctl start mysqldコマンドの違いを調べてみた

0
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
0
1