eureka

MacでMySQLのパスを通して起動するまで

1

こんにちは、今日も元気に開発中のしょうみゆです。

Macに入っているMySQLを立ち上げたり落としたりしようとしたくなったときに、mysqlなんでコマンドないよ!ってbashに怒られたのでその覚書きで。

パスを通してmysqlコマンドを使用できるようにする

MySQLにログインしたいな〜。

$ mysql -u root

えいっ!

-bash: mysql: command not found

てな感じで怒られたのでまずはパスを通すところから。

$ echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
$ vim ~/.bash_profile
export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"

vimで開いたファイルの最後の行に上記の記述があればOK。:qでvimを閉じて次のコマンドでパスを反映させる。

$ source ~/.bash_profile

MySQLを起動してログインする

早速起動するぜ!

$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

おおっとw起動して再度実行。

$ 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.27 Homebrew

Copyright (c) 2000, 2019, 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を使用しているので個別に起動したり落としたりすることにしている。
落とすときはこのコマンドで。

$ mysql.server stop
Shutting down MySQL
. SUCCESS!

そんな感じで!おわり!

1