【Mac】MySQLをインストール

2022年6月13日

MacにMySQLをインストールする手順です。

関連記事
【Mac】Composerをインストールする
【Mac】PHPをインストールする
【Mac】MySQLをインストールする
関連記事
【Mac】XAMPPをインストールする
【Mac】MAMPをインストールする

Mac MySQLのインストール手順

インストール可能なMySQLの検索

sei@MacBook-Pro ~ % brew search mysql
==> Formulae
automysqlbackup            mysql-client@5.7           mysql@5.6
mysql                      mysql-connector-c++        mysql@5.7
mysql++                    mysql-sandbox              mysqltuner
mysql-client               mysql-search-replace       qt-mysql

==> Casks
mysql-connector-python     mysql-utilities            navicat-for-mysql
mysql-shell                mysqlworkbench             sqlpro-for-mysql

MySQL 5.7(バージョン指定)のインストール

sei@MacBook-Pro ~ % brew install mysql@5.7
Running `brew update --preinstall`...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
helmify
==> Updated Formulae
Updated 14 formulae.

==> Downloading https://ghcr.io/v2/homebrew/core/mysql/5.7/manifests/5.7.38
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/5.7/blobs/sha256:ad4b3ffe
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Pouring mysql@5.7--5.7.38.monterey.bottle.tar.gz
==> /usr/local/Cellar/mysql@5.7/5.7.38/bin/mysqld --initialize-insecure --user=s
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

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"' >> ~/.zshrc

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/include"

For pkg-config to find mysql@5.7 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/mysql@5.7/lib/pkgconfig"


To restart mysql@5.7 after an upgrade:
  brew services restart mysql@5.7
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/mysql@5.7/bin/mysqld_safe --datadir=/usr/local/var/mysql
==> Summary
🍺  /usr/local/Cellar/mysql@5.7/5.7.38: 320 files, 233.5MB
==> Running `brew cleanup mysql@5.7`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
sei@MacBook-Pro ~ % 

パスを通す

上記インストール中のログに表示されているコマンドを実行し、

sei@MacBook-Pro ~ % echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

設定の反映コマンドを実行

sei@MacBook-Pro ~ % source ~/.zshrc 

MySQLサーバーを起動してみる

sei@MacBook-Pro ~ % mysql.server start
Starting MySQL
. SUCCESS! 

MySQLサーバーにログインしてみる

sei@MacBook-Pro ~ % mysql -u root     
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38 Homebrew

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> help

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.

For server side help, type 'help contents'

mysql> exit
Bye

MySQLサーバーを停止してみる

sei@MacBook-Pro ~ % mysql.server stop 
Shutting down MySQL
.. SUCCESS! 
mac mysql

参考サイト

macローカル環境にMySQLをインストールしデータベース環境を設定する

M1 Mac に homebrew で MySQL 8 をインストールする – Qiita

Macへのmysqlインストール | マサトッシュブログ