the glue

やってみたことで忘れそうなこと、役立ちそうなことなどをまとめています。たまに何気ない日常の話もします。

rails で migrate がコケる

経緯

Railsrake db:migrate が突然コケるようになってすこしハマったのでメモ。

Table 'performance_schema.session_variables' doesn't exist

とのこと。
確かにそのようなテーブルはありませんでした。

対応

stackoverflow.com

結局 StackOverflow に書いてあったままですが、どうやらmysqlのアップデートをしたせいなようです。

$ mysql_upgrade -u root -p --force
$ mysql.server restart

でmigrateが通るようになりました。

Mac で Rails を使う

Mac ( OS X ) に Rails をインストールしようとするといつもコケるのでメモ

前提

  • OS X Yosemite
  • Homebrew が導入済みであること
  • Homebrew で Ruby をインストール済みであること
$ brew -v
Homebrew 0.9.5

$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]

よくあるエラー

Building native extensions.  This could take a while...
ERROR:  Error installing rails:
    ERROR: Failed to build gem native extension.

    /usr/local/opt/ruby/bin/ruby -r ./siteconf20150421-1193-63p9ed.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
-----
The file "/usr/include/iconv.h" is missing in your build environment,
which means you haven't installed Xcode Command Line Tools properly.

To install Command Line Tools, try running `xcode-select --install` on
terminal and follow the instructions.  If it fails, open Xcode.app,
select from the menu "Xcode" - "Open Developer Tool" - "More Developer
Tools" to open the developer site, download the installer for your OS
version and run it.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/usr/local/Cellar/ruby/2.2.2/bin/$(RUBY_BASE_NAME)
    --help
    --clean
    --use-system-libraries
    --enable-static
    --disable-static
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --enable-cross-build
    --disable-cross-build

extconf failed, exit code 1

Gem files will remain installed in /usr/local/lib/ruby/gems/2.2.0/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /usr/local/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-14/2.2.0/nokogiri-1.6.6.2/gem_make.out

要は nokogiri が入らないわけです。
(あと xcode-select --install もやれって言われているような... )

手順

必要なものをインストール

# とりあえず言われたことをやりましょう
$ xcode-select --install

$ brew tap homebrew/dupes
$ brew install libxml2 libxslt
$ brew install libiconv

nokogiri だけ先にインストール

いろいろ試してダメダメだったので調べてたらこんなのを見つけたので試したら通りました。
https://github.com/sparklemotion/nokogiri/issues/1210#issuecomment-67758774

# まずは nokogiri をなんとかインストール
$ gem install nokogiri -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2- lib=/usr/lib/

# rails をインストール
$ gem install rails

できました

$ rails -v
Rails 4.2.1

bundle install で mysql2 のgemがインストールできない

問題

Railsアプリケーションをデプロイする時、bundle install中にいつもmysql2のgemがインストールできないと怒られるので、メモ

こんな感じのことをよく言われる

       --without-make-prog                                                                                [50/1324]
        --srcdir=.
        --curdir
        --ruby=/usr/local/bin/ruby
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-mysql-config
        --without-mysql-config
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-mlib
        --without-mlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-zlib
        --without-zlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-socketlib
        --without-socketlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-nsllib
        --without-nsllib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-mygcclib
        --without-mygcclib
        --with-mysqlclientlib
        --without-mysqlclientlib

extconf failed, exit code 1

Gem files will remain installed in /home/youk/amc/amc_login_server/vendor/bundle/ruby/2.1.0/gems/mysql2-0.3.17 for i
nspection.
Results logged to /home/youk/amc/amc_login_server/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/mysq
l2-0.3.17/gem_make.out

An error occurred while installing mysql2 (0.3.17), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.17'` succeeds before bundling.

対応

Ubuntu

$ sudo apt-get install libmysqld-dev

CentOS / Amazon Linux

$ sudo yum install mysql-devel

できた

$ bundle install --path vendor/bundle
...
...
Your bundle is complete!
It was installed into ./vendor/bundle

いつも思うのですが、 sudo gem install mysql2 をしろ
みたいなのじゃなくてもう少しまともなメッセージが出てほしいですね。 まぁエラーメッセージが「エラー」とかよりはマシか