発生したエラーと経緯
最初に書いておきますが、タイトルのエラーが解決できなかったので、ここではMySQLのアンインストール方法とインストール方法を記載します。アンインストール方法に関しては、この次の章「MySQLのアンインストール方法」からご覧ください。
Progateの「MySQLの開発環境を用意しよう」というコースで、Homebrewをインストール・MySQLをインストールしました。その後、問題なくNode.jsアプリケーションとMySQLを接続できて、うまく機能しました。
数日開けて、再度そのアプリの機能追加をしようと、一度開いてみたらエラーが発生(ここのエラーの詳細は忘れてしまいました)。
調べていくうちに、MySQLがうまく起動できてないから発生しているエラーとのこと。
なんでこうなったかは、全くわからないです。寝てるうちに何かやらかしたのか・・・。
下記のコマンドを実行する。
①まずはMySQLを起動。起動ができました。
$ brew services start mysql@5.7
==> Successfully started `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)
②rootユーザーとしてログイン。パスワード入力後、エラー発生。
$ mysql --user=root --password
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
色々な記事を参照(目次:※色々な記事参照)したら、「mysql.sockを作る」との言葉が多かったので、そちらのコマンドを入力、再度ログインをする。
$ sudo touch /tmp/mysql.sock
$ mysql --user=root --password
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)
エラーが(2)から(38)になる。
何をしてもダメだったので、テラテイルで質問をしました。
「MySQLが起動しない / ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.soc...」
すると、
参考にした記事はどれもLinuxの説明なので、記事通りに作業するとMySQLは
たぶん動かなくなります。
とのコメントがきました(ご教授ありがとうございます)。
そこで、なんとか足掻くのをやめて、アンインストールしてから再インストール、ログインすることにしました。
MySQLのアンインストール方法
①MySQLが入ってることを確認。
$ brew list | grep mysql
Warning: Calling `brew list` to only list formulae is deprecated! Use `brew list --formula` instead.
mysql
mysql@5.7
②mysqlとmysql@5.7の2つがありましたので、2つとも削除。
$ brew uninstall mysql
Uninstalling /usr/local/Cellar/mysql/8.0.22... (294 files, 296.5MB)
$ brew uninstall mysql@5.7
Uninstalling /usr/local/Cellar/mysql@5.7/5.7.32... (319 files, 234.4MB)
③MySQL関連のファイル等を削除。
$ sudo rm -rf /usr/local/var/mysql
$ sudo rm -rf /usr/local/Cellar/mysql*
$ sudo rm -rf /usr/local/bin/mysql*
$ sudo rm -rf /usr/local/var/mysql*
$ sudo rm -rf /usr/local/etc/my.cnf
$ sudo rm -rf /usr/local/share/mysql*
$ sudo rm -rf /usr/local/opt/mysql*
$ sudo rm -rf /etc/my.cnf
⑤mysql.sockがあるか見る。
$ cd /tmp
$ ls
29E53B57-E064-4128-8239-1A6E7CAF05A6 F7F4F917-5D78-4874-88E9-ED41110EEF96
628F17C8-D701-40FB-AA5D-2449ED2A0999 VulcanConfig.data
94369BC8-C242-459A-BF30-332BAF5994C1_IN adobegc.log
94369BC8-C242-459A-BF30-332BAF5994C1_OUT com.adobe.AdobeIPCBroker.ctrl-apple
AlTest1.err com.apple.launchd.TqfAD8Zt1L
AlTest1.out com.google.Keystone
BC30B851-E570-4A65-81CC-4448498A0FD5 mysql.sock
C73BCD6D-677B-4B82-8802-5BA6336B48A8 mysql.sock.lock
ExmanProcessMutex powerlog
⑥mysql.sockとmysql.sock.lockがあったので、削除します。
$ sudo rm -rf mysql.sock mysql.sock.lock
これでアンインストールは完了です。下記のコマンドを実行して、「: No such file or directory」が出てればアンインストールできています。
$ mysql --version
MySQLのインストール方法
あとは、MySQLをインストールすれば完了です。
❶今回は、バージョン5.7をインストールします。
$ brew install mysql@5.7
mysql@5.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have mysql@5.7 first in your PATH run:
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> /xxxx/xxx/xx
For compilers to find mysql@5.7 you may need to set:
export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
export CPPFLAGS="-I/usr/local/opt/mysql@5.7/xxx"
For pkg-config to find mysql@5.7 you may need to set:
export PKG_CONFIG_PATH="/usr/local/xxx/mysql@5.7/xxx/xxx"
To have launchd start mysql@5.7 now and restart at login:
brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
/usr/local/opt/mysql@5.7/bin/mysql.server start
==> Summary
🍺 /usr/local/Cellar/mysql@5.7/5.7.32: 319 files,
❷ここで、上記から下記の部分を探して、>>以降の「/xxxx/xxx/xx」部分をコピペします(人によって書いてあるものは異なります)。
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> /xxxx/xxx/xx
❸先ほどコピペした部分を「source」のあとに貼り付け、実行します。
$ source /xxxx/xxx/xx
❹MySQLのバージョンみて、無事にインストールされているか確認します。
$ mysql --version
mysql Ver 14.14 Distrib 5.7.32, for osx10.15 (x86_64) using EditLine wrapper
❺MySQLを実行します。
$ brew services start mysql@5.7
==> Successfully started `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)
appurunoMacBook-Air:goto_app apple$ mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y
〜略〜
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
❻パスワードを入力後、色々と質問されますが、エンターキー(回答:NO)で大丈夫です。rootユーザーとして先ほどのパスワードを入力してログインします。
$ mysql --user=root --password
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32 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>
なんとか、MySQLにログインできました!
アンインストールしてしまったので、Node.jsアプリとの接続は切れたり、データは消えてしまったりしたので、再度がんばります!
ご指摘やアドバイスがありましたらよろしくお願い致します。
※色々な記事参照
参考にした記事を一覧で載せておきます。
●MySQLが起動しない ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
●mysqlが起動できない
●MySQLにログインしようとすると「ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)」というエラーが出力される
●[2002] MySQLのソケットエラー の原因と対処法
●MySQL への接続でエラー - ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
●ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'(2)→(13)→(111)→起動
●Sequel proの起動について。エラー文:Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
●「Can't connect to local MySQL server through socket」エラーについて