Rubyと筋肉とギターとわたし

エンジニア二年目の雑魚です。プログラミング・ギター・筋トレのことをメインにブログを書いていきます。

MySQLエラーでRailsサーバーが全然起動しなかったんだよ

ひっさしぶりに1からRailsの環境を作ってみました。

すると、いろいろつまずきます。

最初に躓いたのがMySQLでした。

Can't connect to local MySQL server through socket '/tmp/mysql.sock' (13) (Mysql2::Error)

$ rails s 
=> Booting Puma
=> Rails 5.0.4 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Exiting
/home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:89:in `connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (13) (Mysql2::Error)
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:89:in `initialize'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_adapters/mysql2_adapter.rb:25:in `new'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_adapters/mysql2_adapter.rb:25:in `mysql2_connection'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:729:in `new_connection'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:773:in `checkout_new_connection'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:752:in `try_to_checkout_new_connection'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:713:in `acquire_connection'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:490:in `checkout'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:364:in `connection'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:883:in `retrieve_connection'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_handling.rb:128:in `retrieve_connection'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.4/lib/active_record/connection_handling.rb:91:in `connection'
    from /home/user/rwork/projects/config/environment.rb:15:in `<top (required)>'
    from /home/user/rwork/projects/config.ru:3:in `require_relative'
    from /home/user/rwork/projects/config.ru:3:in `block in <main>'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:55:in `instance_eval'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:55:in `initialize'
    from /home/user/rwork/projects/config.ru:in `new'
    from /home/user/rwork/projects/config.ru:in `<main>'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:49:in `eval'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:49:in `new_from_string'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/builder.rb:40:in `parse_file'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/server.rb:319:in `build_app_and_options_from_config'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/server.rb:219:in `app'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/railties-5.0.4/lib/rails/commands/server.rb:84:in `app'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/server.rb:354:in `wrapped_app'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/railties-5.0.4/lib/rails/commands/server.rb:148:in `log_to_stdout'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/railties-5.0.4/lib/rails/commands/server.rb:102:in `start'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/railties-5.0.4/lib/rails/commands/commands_tasks.rb:90:in `block in server'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/railties-5.0.4/lib/rails/commands/commands_tasks.rb:85:in `tap'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/railties-5.0.4/lib/rails/commands/commands_tasks.rb:85:in `server'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/railties-5.0.4/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/railties-5.0.4/lib/rails/commands.rb:18:in `<top (required)>'
    from /home/user/rwork/projects/bin/rails:9:in `require'
    from /home/user/rwork/projects/bin/rails:9:in `<top (required)>'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
    from /home/user/rwork/projects/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
    from /home/user/rwork/projects/bin/spring:15:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

はい、出ましたMySQL2::Error

久々に現れたので、解決するのにまあまあ時間がかかりました。

mysqlを起動してみる

$ sudo mysql.server start
`mysql.server: コマンドが見つかりません` 

はいはい。PATHが通ってないんでしょ。

.bash_profileへPATHを記入。 export PATH="/usr/bin/mysql/bin:$PATH"を入れるだけ。

$ source ~/.bash_profile # .bash_profileを反映

$ sudo mysql.server start
`mysql.server: コマンドが見つかりません` 

なにぃ!?

ちょっとイミフなんで、別の方法で起動しました。

$ sudo mysql service start

$ mysql -u root -p
password入力:

これでちゃんと入れるようなので、問題なし。

mysql.serverコマンドが使えないのはまた今度とすることにしました。

もう一度rails s

さっきと同じMySQL2::Error

ん〜、よーわからん。。。

っと悩んでいると、よく考えたらrailsの方でsocketを指定していなかったことを思いだしました。

$ mysql_config --socket
/var/run/mysqld/mysqld.sock

とりあえずmysql_configでsocketの位置を確認。

/etc/mysql/my.cnfにsocket情報を追記してあげます。

$ sudo vim /etc/mysql/my.cnf

  1 #
  2 # The MySQL database server configuration file.
  3 #
  4 # You can copy this to one of:
  5 # - "/etc/mysql/my.cnf" to set global options,
  6 # - "~/.my.cnf" to set user-specific options.
  7 # 
  8 # One can use all long options that the program supports.
  9 # Run program with --help to get a list of available options and with
 10 # --print-defaults to see which it would actually understand and use.
 11 #
 12 # For explanations see
 13 # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
 14 
 15 #
 16 # * IMPORTANT: Additional settings that can override those from this file!
 17 #   The files must end with '.cnf', otherwise they'll be ignored.
 18 #
 19 
 20 !includedir /etc/mysql/conf.d/
 21 !includedir /etc/mysql/mysql.conf.d/
 22 
 23 [mysqld]
 24 character-set-server=utf8
 25 skip-character-set-client-handshake
 26 default-storage-engine=INNODB
 27 socket=/var/run/mysqld/mysqld.sock
 28 
 29 [client]
 30 socket=/var/run/mysqld/mysqld.sock
 31 
 32 [mysqldump]
 33 default-character-set=utf8
 34 
 35 [mysql]
 36 default-character-set=utf8

僕の設定はこんな感じです。
mysqldclientへsocketの位置を追記すればおkです。

そして、rails側のconfig/database.ymlへsocketの追加と設定しているパスワード・ユーザー名を追加すればおk。

パスワード・ユーザー名は環境変数を設定して見えないようにしてあげりゅこと。

  1 default: &default
  2   adapter: mysql2
  3   encoding: utf8
  4   pool: 5
  5   username: username
  6   password: password
  7   socket: /var/run/mysqld/mysqld.sock

これでrails sで問題なくpumaが動きました。
結構前に環境作ってからやってなかったんでまあまあ時間がかかってしまいました。

最後に

また最初からプロジェクトを作るときに、設定するのくっそ面倒くさいので、dockerで管理しようかなと思案中です。

あとvimの操作になかなか慣れない。。。
(コードに行番号が入っているのはvimのせい?おかげ?です)