MySQLをmacOS Sierra 10.12にインストールする

#前提
Homebrewがインスールされていること


#Homebrewのパッケージを一覧表示
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ brew search mysql
==> Formulae
automysqlbackup            mysql-connector-c          mysql@5.5
mysql                      mysql-connector-c++        mysql@5.6
mysql++                    mysql-sandbox              mysql@5.7
mysql-client               mysql-search-replace       mysqltuner
mysql-cluster              mysql-utilities

==> Casks
homebrew/cask/mysql-connector-python     homebrew/cask/navicat-for-mysql
homebrew/cask/mysql-shell                homebrew/cask/sqlpro-for-mysql
homebrew/cask/mysql-utilities
```

#mysqlをインストールする

```
<コンピューター名>-no-MBP:~ <ユーザー名>$ brew install mysql
==> Installing dependencies for mysql: openssl@1.1
==> Installing mysql dependency: openssl@1.1
==> Downloading https://homebrew.bintray.com/bottles/openssl@1.1-1.1.1c.sierra.b
==> Downloading from https://akamai.bintray.com/b0/b0ac9931918d90518992569308814
######################################################################## 100.0%
==> Pouring openssl@1.1-1.1.1c.sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because openssl/libressl is provided by macOS so don't link an incompatible version.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

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

==> Summary
🍺  /usr/local/Cellar/openssl@1.1/1.1.1c: 7,963 files, 18.2MB
==> Installing mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-8.0.17_1.sierra.bottl
==> Downloading from https://akamai.bintray.com/ba/ba03dc26a70a862640766a46eb18d
######################################################################## 100.0%
==> Pouring mysql-8.0.17_1.sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql/8.0.17_1/bin/mysqld --initialize-insecure --user=<ユーザー名> --basedir=/usr/local/Cellar/mysql/8.0
==> 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

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/8.0.17_1: 284 files, 276.1MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /usr/local/Cellar/openssl/1.0.2r... (1,795 files, 12.4MB)
Removing: /usr/local/Cellar/pcre/8.36... (203 files, 5.2MB)
Removing: /usr/local/Cellar/pkg-config/0.28... (10 files, 576.5KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/ruby-build--20190615.tar.gz... (61KB)
Removing: /usr/local/Cellar/xz/5.2.1... (91 files, 1.4MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/aspell-0.60.6.1_1.sierra.bottle.1.tar.gz... (113MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/autoconf-2.69.tar.gz... (1.8MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/curl-openssl-7.64.1.sierra.bottle.tar.gz... (1MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/direnv-2.17.0.sierra.bottle.tar.gz... (1.9MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/freetds-1.1.5.sierra.bottle.tar.gz... (2.8MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/freetype-2.10.0.sierra.bottle.tar.gz... (888.3KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/gettext-0.19.8.1.sierra.bottle.tar.gz... (7.8MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/glib-2.60.2.sierra.bottle.tar.gz... (4.4MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/heroku-7.24.1.tar.xz... (6.6MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/heroku-node-11.14.0.tar.xz... (8.3MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/httpd-2.4.39.sierra.bottle.tar.gz... (7.0MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/icu4c-64.2.sierra.bottle.tar.gz... (26.2MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/jemalloc-5.2.0.sierra.bottle.tar.gz... (614.5KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/libev-4.24.sierra.bottle.tar.gz... (140.5KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/libevent-2.1.8.sierra.bottle.tar.gz... (755.4KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/libpq-11.2.sierra.bottle.tar.gz... (5.2MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/libssh2-1.8.2.sierra.bottle.tar.gz... (265.3KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/mongodb-community-4.0.9.tgz... (81MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/nghttp2-1.38.0.sierra.bottle.tar.gz... (981.2KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/nodebrew-1.0.0.tar.gz... (26.3KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/openldap-2.4.47.sierra.bottle.tar.gz... (2.7MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/openssl-1.0.2r.sierra.bottle.tar.gz... (3.7MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/php@7.1-7.1.29.sierra.bottle.tar.gz... (18.3MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/portable-ruby-2.3.7.leopard_64.bottle.tar.gz... (12.4MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/postgresql-11.2_1.sierra.bottle.tar.gz... (9.9MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/python-3.7.3.sierra.bottle.tar.gz... (14.5MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/readline-8.0.0_1.sierra.bottle.tar.gz... (519.5KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/sqlite-3.28.0.sierra.bottle.tar.gz... (1.8MB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/unixodbc-2.3.7.sierra.bottle.tar.gz... (561.9KB)
Removing: /Users/<ユーザー名>/Library/Caches/Homebrew/webp-1.0.2.sierra.bottle.1.tar.gz... (842.9KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/apr... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/apr-util... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/aspell... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/autoconf... (4 files, 31.9KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/automake... (4 files, 47KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/brotli... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/c-ares... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/curl-openssl... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/direnv... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/freetds... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/freetype... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/gdbm... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/gettext... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/glib... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/gmp... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/go... (2 files, 228.9KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/heroku... (120B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/heroku-node... (125B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/httpd... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/icu4c... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/jansson... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/jemalloc... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/jpeg... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libev... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libevent... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libffi... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libidn... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libmetalink... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libpng... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libpq... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libssh2... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libtiff... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libtool... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/libzip... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/mcrypt... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/mhash... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/mongodb-community... (132B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/nghttp2... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/node... (4 files, 153.8KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/nodebrew... (110B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/openldap... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/openssl... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/pcre... (7 files, 253KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/php@7.1... (12 files, 2.3KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/pkg-config... (7 files, 361KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/postgresql... (1.2KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/python... (3 files, 132.6KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/rbenv... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/readline... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/rtmpdump... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/ruby-build... (2 files, 187B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/sqlite... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/tidy-html5... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/unixodbc... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/watchman... (7 files, 171KB)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/webp... (68B)
Removing: /Users/<ユーザー名>/Library/Logs/Homebrew/xz... (5 files, 773.7KB)
Pruned 193 symbolic links and 39 directories from /usr/local
Error: Permission denied @ utime_internal - /Users/<ユーザー名>/Library/Caches/Homebrew/.cleaned
```
最後の行に前回同様のエラーが表示されている



#/usr/localの所有者を変更するのを試してみるが検討違いのような気がする
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ sudo chown -R <ユーザー名>  /usr/local
Password:
```

#mysqlをインストールする
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ brew install mysql
Warning: mysql 8.0.17_1 is already installed and up-to-date
To reinstall 8.0.17_1, run `brew reinstall mysql`
```

#mysqlを再インストールする
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ brew reinstall mysql
==> Reinstalling mysql 
==> Downloading https://homebrew.bintray.com/bottles/mysql-8.0.17_1.sierra.bottle.tar.gz
Already downloaded: /Users/<ユーザー名>/Library/Caches/Homebrew/downloads/5e5ae2b493f1ff0bbbd4980e384d08066293f8a4e8b6f14774a78a3d0d4851ef--mysql-8.0.17_1.sierra.bottle.tar.gz
==> Pouring mysql-8.0.17_1.sierra.bottle.tar.gz
==> 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

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/8.0.17_1: 284 files, 276.1MB
==> `brew cleanup` has not been run in 30 days, running now...
Pruned 0 symbolic links and 2 directories from /usr/local
Error: Permission denied @ utime_internal - /Users/<ユーザー名>/Library/Caches/Homebrew/.cleaned
```

最後の行に前回同様のエラーが表示されている


#Homebrewの当該ファイルを削除する
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ rm /Users/<ユーザー名>/Library/Caches/Homebrew/.cleaned
override rw-r--r--  root/staff for /Users/<ユーザー名>/Library/Caches/Homebrew/.cleaned? y
```

#mysqlをインストールする
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ brew install mysql
Warning: mysql 8.0.17_1 is already installed and up-to-date
To reinstall 8.0.17_1, run `brew reinstall mysql`
```

#mysqlを再インストールする
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ brew reinstall mysql
==> Reinstalling mysql 
==> Downloading https://homebrew.bintray.com/bottles/mysql-8.0.17_1.sierra.bottle.tar.gz
Already downloaded: /Users/<ユーザー名>/Library/Caches/Homebrew/downloads/5e5ae2b493f1ff0bbbd4980e384d08066293f8a4e8b6f14774a78a3d0d4851ef--mysql-8.0.17_1.sierra.bottle.tar.gz
==> Pouring mysql-8.0.17_1.sierra.bottle.tar.gz
==> 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

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/8.0.17_1: 284 files, 276.1MB
==> `brew cleanup` has not been run in 30 days, running now...
```

今回はエラーが出なかった


#mysqlを起動するがエラーがでる
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
```


#まずbrewコマンドでmysqlを自動起動するようにする
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ brew services start mysql
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
```

#mysqlを起動できた
```
<コンピューター名>-no-MBP:~ <ユーザー名>$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17 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.
```


#helpコマンドでコマンド一覧を表示させてみる
```
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'
```

#結論
Homebreのエラーとmysqlのエラーがあったがmysqlのインストール自体は最初から出来たいた可能性がある。まず、brewコマンドでmysqlを自動起動するようにしてからでないと起動出来ないようである。PostgreSQLの場合とは違うようである