LoginSignup
1
4

More than 3 years have passed since last update.

MySQLを使ってのrake db:createができない。

Posted at

MySQLを使用してrake db:createしたら下記のエラーが出ました。
かなり時間がかかってしまったので備忘録に。

エラー文

Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Couldn't create 'pictweet_development' database. Please check your configuration.
rake aborted!
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Tasks: TOP => db:create
(See full trace by running task with --trace)

原因考察と対処

1、
原因:mySQLが起動していない。
対処:SQLを再起動させる。

$ sudo mysql.server restart
ERROR! MySQL server PID file could not be found!

結果:失敗

2、アンインストールして再インストール。
対処:SQLを再インストール(よくある手法)

$ brew remove mysql
$ brew cleanup

sql関連のファイルを削除

$ sudo rm /usr/local/mysql
$ sudo rm -rf /usr/local/var/mysql
$ sudo rm -rf /usr/local/mysql*
$ sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
$ sudo rm -rf /Library/StartupItems/MySQLCOM
$ sudo rm -rf /Library/PreferencePanes/My*

sqlの自動起動設定を解除

$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

結果:失敗

3、sqlに PIDファイルを作成し、権限パスを渡す(今回はこれ)

適当な名前でいいので、PIDファイルを作成

$ touch /usr/local/var/mysql/user-no-MacBook-Pro.local.pid

作成したpidファイルにsqlの権限パスを渡してあげる
/user/local/var/mysql以下のファイルの所有者をすべて_mysqlにすることで解決。

$ sudo chown -R _mysql:_mysql /usr/local/var/mysql/
$ sudo mysql.server restart
Starting MySQL
. SUCCESS! 

結果:成功!

今回、バックアップ取らずにアンインストールしていまいましたが(重要なデータはなかった…汗)
再インストールする時はバックアップを取ることをお勧めします。

参考
https://qiita.com/sato11/items/ba887a5655217f60f2a2
https://qiita.com/carotene4035/items/e00076fe3990b9178cc0
https://qiita.com/jonakp/items/477a18d4a94c01a31583

1
4
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
1
4