Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
121
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

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

アプリの作成

もともと作ってあったアプリを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>

おしまいです。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
121
Help us understand the problem. What are the problem?