[Mac][El Capitan][MySQL] OSX El Capitan に MySQL をインストールして、ちょっと使ってみる

# Install

```zsh
$ brew install mysql

==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.16.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.7.16.el_capitan.bottle.tar.gz
==> Using the sandbox
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

To connect run:
    mysql -uroot

A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To start mysql:
  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/5.7.16: 13,511 files, 439M
```

- 確認

```zsh
mysql: stable 5.7.16 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
Conflicts with: mariadb, mariadb-connector-c, mysql-cluster, mysql-connector-c, percona-server
/usr/local/Cellar/mysql/5.7.16 (13,511 files, 439M) *
  Poured from bottle on 2016-11-25 at 21:15:10
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mysql.rb
==> Dependencies
Build: cmake ✘
Required: openssl ✔
==> Requirements
Required: macOS >= 10.7 ✔
==> Options
--with-archive-storage-engine
	Compile with the ARCHIVE storage engine enabled
--with-blackhole-storage-engine
	Compile with the BLACKHOLE storage engine enabled
--with-debug
	Build with debug support
--with-embedded
	Build the embedded server
--with-local-infile
	Build with local infile loading support
--with-test
	Build with unit tests
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

To connect run:
    mysql -uroot

A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To start mysql:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
```

# Usage
## Start MySQL Server

```zsh
$ mysql.server start
Starting MySQL
.. SUCCESS! 
```

## Entry to MySQL
```zsh
$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 Homebrew

Copyright (c) 2000, 2016, 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.

[(none)] mysql> exit
Bye
```

## Stop MySQL Server
```zsh
$ mysql.server stop
Shutting down MySQL
.. SUCCESS! 
```

## Make Security Powerful
```zsh
mysql_secure_installation
mysql_secure_installation: [ERROR] unknown option '--show-warnings'
mysql_secure_installation: [ERROR] unknown variable 'prompt=[\d] mysql> '

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

---

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: y

---

Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

---

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

---

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) : y
Success.

---

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

---

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!
```

## Create Database
```mysql
[(none)] mysql> show databases;
+-----------------------------------+
| Database                          |
+-----------------------------------+
| information_schema                |
| mysql                             |
| performance_schema                |
+-----------------------------------+
3 rows in set (0.02 sec)

[(none)] mysql> create database foobar;
Query OK, 1 row affected (0.00 sec)

[(none)] mysql> show databases;
+-----------------------------------+
| Database                          |
+-----------------------------------+
| information_schema                |
| foobar                            |
| mysql                             |
| performance_schema                |
+-----------------------------------+
4 rows in set (0.02 sec)
```

## Create User with privilege
```mysql
[(none)] mysql> create user
    -> 'retriever'@'localhost' identified by 'ぱすわあど';

[(none)] mysql> select User,Host from mysql.user;
+-----------------+-----------+
| User            | Host      |
+-----------------+-----------+
| root            | 127.0.0.1 |
| root            | ::1       |
| retriever       | localhost |
| root            | localhost |
+-----------------+-----------+
4 rows in set (0.00 sec)

[(none)] mysql> grant all privileges on `foobar`.* TO 'retriever'@'localhost';
[(none)] mysql> FLUSH PRIVILEGES;

[(none)] mysql> show grants for 'retriever'@'localhost';
+-----------------------------------------------------------------------+
| Grants for retriever@localhost                                        |
+-----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'retriever'@'localhost'                         |
| GRANT ALL PRIVILEGES ON `foobar`.* TO 'retriever'@'localhost' |
+-----------------------------------------------------------------------+
2 rows in set (0.00 sec)
```


## 自動起動設定
http://qiita.com/satomyumi/items/43c83658a3e5bcc820ec