LoginSignup
142
121

More than 5 years have passed since last update.

railsのapp作成でmysqlを使用する時の導入とハマるところ rake db:createができるまでの流れ   

Last updated at Posted at 2016-06-03

アプリの作成

もともと作ってあったアプリをmysqlに変更するにはこちらのページが参考になるかと思います。
http://qiita.com/pchatsu/items/a7f53da2e57ae4aca065

$ rails new app-name --skip-bundle -d mysql

はまるところ1 rake db:create

$ bundle exec rake db:create

このエラーが出る

#<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"app_name_development"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"}
(If you set the charset manually, make sure you have a matching collation)
#<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"app_name_test"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"}
(If you set the charset manually, make sure you have a matching collation)

これはmysqlのサーバーが起動していないから。
結構見落としがちだから気をつける。

$ mysql.server start

Starting MySQL
. SUCCESS!

これでサーバーを立ち上げる。

はまるところ2 Access denied for user 'root'@'localhost' (using password: NO)

Access denied for user 'root'@'localhost' (using password: NO)
Access denied for user 'root'@'localhost' (using password: NO)Please provide the root password for your MySQL installation
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"app_name_development"}

このエラーは知ってれば意外と簡単に直せるんだけど、ググってもなかなかでてこない。
というかググって出て来る記事はわけわからない対処の仕方をしている人が多い気がする・・・。

というかもろ2文目に対処法書いてある。

これは単純にパスワードが設定されていないために起こるエラー。
config/database.ymlにあるファイルにあるパスワードの欄が空白なので自分で値を入れて設定する。

database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root      
  password: password       # ← ここの欄が空白なので任意のパスワードを設定する 
  host: localhost

これでok!!!!!

MacBook-Pro:app_name user_name$ rake db:create
MacBook-Pro:app_name user_name$ rails db

Enter password: 先ほど作成したパスワードの入力

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.9 Homebrew

Copyright (c) 2000, 2015, 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;
+----------------------------+
| Database                   |
+----------------------------+
|                             |
|   データベースがちゃんとつくられている|
| ことを確認する                        |
|                            |
|                            |
|                            |
|                            |
|                            |
+----------------------------+
8 rows in set (0.01 sec)

mysql>

おしまいです。

142
121
1

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
142
121