5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Mac】MySQL5.7をインストールする。(おまけにセキュリティーの設定もする)

Last updated at Posted at 2019-06-22

#対象読者

  • MySQLが入っていない人
  • HoMtSQLを入れてみたい人
  • エラーでなかなか入らない人

#前提

  • MacはMojaveのOSで動作確認
  • MySQLの言葉を知っていてる人
  • Homebrewはインストール済み

自分のメモ書きのため、随時更新

#手順

##MySQL5.7をインストールする。

MySQL5.7をインストールします。

$ brew install mysql@5.7
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
glooctl                                 h3                                      k3d                                     postgrest                               terragrunt                              youtube-dl

==> Downloading https://homebrew.bintray.com/bottles/mysql@5.7-5.7.26.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/81/815ae8aa8c765f57e806ff9197eaa5783b138c493d68333c553b25f441b6af3e?__gda__=exp=1561175800~hmac=ef410ed9bef72a578728ab88bc0290406ed1f72a09853aef7128c272a2041775&response-content-disposition=
######################################################################## 100.0%
==> Pouring mysql@5.7-5.7.26.mojave.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

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

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

たくさんのログが書かれていますが、ここではインストールの説明です。
実はここの説明文が重要で、
例えば下記のバージョンを確認するコマンドを打ったとき、

$ mysql --version
bash: command not found: mysql

と出たらインストールはされているが、mysqlのコマンドが見つからないと出てきます。
ここでインストールの時に出ていた、下記のメッセージに従います。

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

と書いているように、実際に```.bash_profile```のファイルの中に
```'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"'```を記載して反映します。
下記が手順です。

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

その後バージョンを確認して、インストールを行いましょう。

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.26, for osx10.14 (x86_64) using  EditLine wrapper

バージョンを確認することができました。
ここで次にMySQLを実際に使えるかインストールのメッセージにもあった

To connect run:
    mysql -uroot

ここのmysql -urootを打って確認しましょう。
下記の表示になったら、

$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25 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>

#(おまけ)セキュリティーの設定について
インストールのメッセージにもあった

We've installed your MySQL database without a root password. To secure it run
    mysql_secure_installation```

このコマンドを打ってセキュリティーの設定に進みましょう。
最初に「VALIDATE PASSWORDプラグインを設定しますか?」と推奨されます。
```y```を打って進めてください。

```console
$ 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

その後進めると次の表示が出るかと思います。

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: 0

セキュリティの強度をLOWMEDIUMSTRONGの3段階でパスワードを選ぶことができます。
今回はLOWで進めますので、0を選択します。

Please set the password for root here.

New password:

Re-enter new password:

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

ここでは、パスワードを入力していきます。同じパスワードを二回入力しましょう。最後に
パスワードの強度が100段階中で表示されます。
指定したパスワードでよければ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.

MySQLはデフォルトで匿名ユーザの情報が入っています。
いらないので消しましょう。

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.

rootユーザでリモートのログイン許可の可否について聞かれています。
基本的にlocalhostでしか使用しないので、必要ありません。
yを押しましょう。

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.

デフォルトでtestのDBが作成されています。
必要ないので削除しましょう。
yを押して進めます。

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!

DBに更新があった場合、即時リロードして反映するかという説明です。
yを押して有効にしましょう。

以上でセキュリティーの設定は終わりです。

最後にmysqlにログインしてp確認しましょう。

先ほどはmysql -urootでもログインはできたのですが、
今回はPWを設定したので
mysql -root -pと入力する必要があります。
そして、MySQLのセキュリティー設定で決めたPWを入力したら、無事ログインすることができます。

$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.25 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>

以上でセキュリティーの設定が終わりました。
おまけの部分が長くなりましたが以上です!

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?