【環境構築】MySQLのパスワードが分からず、最終的にディレクトリごと削除して解決した話(オススメしない)。
(※2020/05のメモを記録)
はじめに
環境構築の際、MySQLのパスワードが分からず詰まったので、以下に解決まで至った流れを記していきます。 これはあくまで私の環境上の話なので、どこまで参考になるか分かりませんし、皆様のお時間を無駄にしたくはないので、最初に解決策と主に使用したコマンドを記載しておきます。
ご興味ある方は最後までご一読頂ければと思います。
解決策
- mysqlの5.7系と8系がインストールされてたから8系を削除。
- mysqlが5.7系のパスを指すように
zshrc
で設定。 - 過去にインストールしていたMySQL5.7系のパスワードが分からないから、
sudo rm -rf /usr/local/var/mysql
でディレクトリごと削除。 - 改めて
brew install mysql@5.7
を実行し、完全に新しいrootユーザーが作成されたことで、パスワード無しでログインできた。
主に使用したコマンド
MySQL
- brew uninstall mysql
- which mysql
- ps aux | grep mysql
- source ~/.zshrc
- sudo rm -rf /usr/local/var/mysql
試したこと①とりあえずログインしようとしてみる
MySQLのバージョン確認。8系が入ってる。
$ mysql --version > mysql Ver 8.0.19 for osx10.15 on x86_64 (Homebrew)
パスワードが分からない。何も覚えてねえ、、
$ mysql.server start Starting MySQL .. SUCCESS! $ mysql -u root -p > Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) $ mysql -u root > ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
勿論bundle exec rails db:create
は実行できない。
$ bundle exec rails db:create Access denied for user 'root'@'localhost' (using password: NO) Couldn't create 'runteq_rails_advanced_development' database. Please check your configuration. rails aborted! Mysql2::Error::ConnectionError: Access denied for user 'root'@'localhost' (using password: NO)
$ which mysql > /usr/local/bin/mysql
試したこと②MySQL8系をアンインストールし、5.7系を使ってみる
パスワードがわからんから一旦brew uninstall mysql
してみると、8系がアンインストールされた。
which mysql
を打つとnot found
と出る。
$ brew uninstall mysql Uninstalling /usr/local/Cellar/mysql/8.0.19... (286 files, 289.2MB) $ which mysql mysql not found
ps aux
で現在実行されているプロセスを確認できる。grep
コマンドで文字列検索を行えるので、ps aux | grep mysql
を実行し、実行中のMySQLが無いか確認してみる。すると、mysqlの5.7系のプロセスが実行されている事が分かった。
whichコマンドでは5.7系が見つからなかったけど、5.7系のプロセス自体は実行されているため、mysqlと打てば5.7系のディレクトリにパスが通るよう設定する。
$ ps aux | grep mysql fune 617 0.0 0.0 4682956 2000 ?? S 30 420 1:29.79 /usr/local/opt/mysql@5.7/bin/mysqld --basedir=/usr/local/opt/mysql@5.7 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/opt/mysql@5.7/lib/plugin --log-error=ryota21.local.err --pid-file=ryota21.local.pid fune 497 0.0 0.0 4280012 8 ?? S 30 420 0:00.04 /bin/sh /usr/local/opt/mysql@5.7/bin/mysqld_safe --datadir=/usr/local/var/mysql fune 30815 0.0 0.0 4268280 636 s000 R+ 3:27PM 0:00.01 grep mysql
zshrc
にexport PATH="/usr/local/opt/mysql@5.7/bin:$PATH"
と記載。
export PATH="/usr/local/opt/mysql@5.7/bin:$PATH
source
コマンドでzshrcファイルに記載されているコマンドを実行し、パスを通す。
$ source ~/.zshrc`
which mysql
と打つと、zshrcで指定したパスを指すようになった。
$ which mysql /usr/local/opt/mysql@5.7/bin/mysql
まだpasswordを空でEnter押してもログインできない。。。
$ mysql -u root -p > Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
MySQL5.7系もアンインストールし、再インストールしてみる。 やっぱりパスワードが分からず、ログインできない、、😭
$ mysql.server stop Shutting down MySQL .... SUCCESS! mysqlの5.7系をアンインストール $ brew uninstall mysql@5.7 mysqlの5.7系を再インストール $ brew install mysql@5.7 mysqlを起動 $ mysql.server start mysqlにログイン $ mysql -uroot > ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) $ mysql -u root -p > Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
試したこと③:ディレクトリごとMySQLを削除
パスワードがマジで分からないので、もうディレクトリごと消してやれ、、!と決意。
一旦プロセスに残っているMySQLをmysql.server stop
で停止させる。
$ ps aux | grep mysql fune 32217 0.0 0.0 4278520 676 s000 S+ 3:41PM 0:00.01 grep mysql fune 32210 0.0 0.4 4681888 31748 s000 S 3:38PM 0:00.41 /usr/local/Cellar/mysql@5.7/5.7.29/bin/mysqld --basedir=/usr/local/Cellar/mysql@5.7/5.7.29 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql@5.7/5.7.29/lib/plugin --log-error=ryota21.local.err --pid-file=/usr/local/var/mysql/ryota21.local.pid fune 32111 0.0 0.0 4280120 688 s000 S 3:38PM 0:00.04 /bin/sh /usr/local/Cellar/mysql@5.7/5.7.29/bin/mysqld_safe --datadir=/usr/local/var/mysql --pid-file=/usr/local/var/mysql/ryota21.local.pid プロセスを停止する $ mysql.server stop
grepしてみると、MySQLでgrepしたというプロセスしか残ってない。 MySQL自体のプロセスは止まっているということ。
$ ps aux | grep mysql fune 32236 0.0 0.0 4278520 672 s000 S+ 3:42PM 0:00.00 grep mysql
MySQLをアンインストールする $ brew uninstall mysql@5.7 ディレクトリの中に削除してはいけないものが無いか確認しておく。 $ open /usr/local/var/mysql MySQLのディレクトリ自体を削除。 $ sudo rm -rf /usr/local/var/mysql
5.7系を再インストールしてみたら、やっとログインできた!!!
5.7系をインストール $ brew install mysql@5.7 $ mysql.server start Starting MySQL . SUCCESS! $ mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.29 Homebrew Copyright (c) 2000, 2020, 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>
おわりに
やっぱり環境構築は大変だと改めて実感しました。。。 いい勉強にはなったので、もっと場数踏んで、環境構築を行う速度を上げたいものですね。