LoginSignup
9
6

More than 5 years have passed since last update.

Rails-Mysqlのrake db:migrateができない問題

Last updated at Posted at 2016-01-15

前回の問題はこちら

・Rails(mac OSX / CentOS)でsocketがないよって言われた時のもろもろ
 http://qiita.com/taiga8899/items/918563a1b04b106d32fb

今回はその続きです。
さあDBもできたし早速railsで基本的なアプリ(名前:tts)を作ろう!と思ってこんなふうに打って

$rails g scaffold hand card1:string card2:string

みたら以下のように怒られました。

Please install the mysql2 adapter: gem install activerecord-mysql2-adapter (can't activate mysql2 (~> 0.3.10), already activated mysql2-0.4.2. Make sure all dependencies are added to Gemfile.) (LoadError)

なるほど。じゃあGemfileにviしてactiverecord-mysql2-adapterいれるね。ついでに
$bundle install
$bundle update
しておこう。んでもっかいscaffoldをrails gするとちゃんと通る。
そのあと、
$rake db:migrate
しようと思ったら今度はこれだよ。

rake aborted!
Mysql2::Error: Unknown database 'tts_development'

みたいに怒られました。

前回の記事で書いた通り、rails+mysqlではあらかじめDBを作らないとうまくrakeできないという。そうでした。というわけで早速mysqlを立ち上げて以下のクエリを通しました。

$ create database tts_development character set utf8 collate utf8_general_ci;
$ grant all on tts_development.* to root@'%' identified by '';

これでdatabase.ymlにある通りのmysql設定になりました。んじゃもう一回rake db:migrateしますか。

そしたら次はこれだよ。

NoMethodError: undefined method `accept' for nil:NilClass

うーわーなんじゃこれ。ググろ。
〜(5分後)〜
http://kiyotakagoto.hatenablog.com/entry/2013/06/04/004437
んん……?
いやrails2はちゃんと入ってるし(-v 0.4.2)activerecord2のアダプタがないと怒られたんですが……? とりあえずgemfileからコメントアウトしますか。bundleしてからはいrake。

rake aborted!
LoadError: Please install the mysql2 adapter: gem install activerecord-mysql2-adapter (can't activate mysql2 (~> 0.3.10), already activated mysql2-0.4.2. Make sure all dependencies are added to Gemfile.)

はいそうですか。意味わかんねえ。

というわけでちょっと調べてきます。解決したら続き書きます。だれか原因がわかったら教えてください。(rakeが通りました。以下確認)


どうも原因として2点考えられます。

・railsそのもののバージョンの悪さ
・mysql2のgemfileのバージョン指定の必要性
・mysqlのDB作成のやり方のまずさ

まずは1点目
どうもrails 3系でうまくいかないのでrails4に切り替えました。

次に2点目
http://qiita.com/kzkiq2nd/items/b29cb9cc478f490a591e
http://takuyan.hatenablog.com/entry/20110112/1294822377
ここに書いてある通り、なんかgem installで引っ張ってくる最新版のmysql2はぶっ壊れてるらしいです。んなこと知るか。というわけでバージョンを指定しましょう。

3点目
rake db:migrateする前にdbをmysqlサーバ内に作らないといけないのですが、その作り方をrake db:createでscaffoldした設定を利用して作成する必要があるようです。んなこと知るか。というわけでコマンドを打ちましょう。

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