format_list_bulleted
【MySQL】MySQLのインストール方法について解説(MacOS)
最終更新日時:2020-05-16 02:30:27



データベースを用いることは多くのデータを扱うブログ型のアプリなどを作成する上で必須です。今回はSQLの一つとして人気が高いのがMySQLのMacOSへのインストールについて解説します。

MySQLのMacOSへの導入手順

MySQLのMacOSへのインストールはhomebrewを用いて行います。homebrewはMacOSのソフトウェアなどを管理するものです。インストールができたらMySQLのサーバーを立ち上げてログイン等の設定を行います。

  1. homebrewでMySQLをインストール
  2. MySQLのサーバーを立ち上げる
  3. ログイン等の設定を行う

MySQLを導入する

MySQLのインストール

以下のコマンドをターミナルで実行します。

タイトル:ターミナル

$ brew update
$ brew install mysql

brewとはhomebrewのコマンドであることを意味しています。まずはbrew updateでhomebrewを最新にアップデートし、その後、mysqlをインストールしています。

mysqlがインストールされたかどうか以下のコマンドで確認してみましょう。

タイトル:ターミナル

$ brew info mysql
➜  ~ brew install mysql 
Warning: mysql 8.0.19 is already installed and up-to-date
To reinstall 8.0.19, run `brew reinstall mysql`
➜  ~ brew info mysql
mysql: stable 8.0.19 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/8.0/en/
Conflicts with:
  mariadb (because mysql, mariadb, and percona install the same binaries.)
  percona-server (because mysql, mariadb, and percona install the same binaries.)
/usr/local/Cellar/mysql/8.0.19 (286 files, 289.2MB) *
  Poured from bottle on 2020-03-23 at 14:38:52
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mysql.rb
==> Dependencies
Build: cmake ✘
Required: openssl@1.1 ✔, protobuf ✘
==> Requirements
Required: macOS >= 10.10 ✔
==> 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
==> 

いろいろ長いですが、8.0.19がインストールされたことがわかります。これでmysqlのインストールは完了です。

MySQLでサーバーを立ち上げてログインする

続いてMySQLでセキュリティの設定をしてログインしてみます。インストールは既に終了しているのでMySQLの初期化をしているという位置づけです。

まずは以下のコマンドでMySQLのサーバーを立ち上げます。

タイトル:ターミナル

$ mysql.server start
Starting MySQL
 SUCCESS! 

上のように表示されればサーバの立ち上げ成功です。続いてmysqlでのログイン時のパスワードについて設定していきます。

タイトル:ターミナル

$ mysql_secure_installation

のコマンドを叩いて質問に沿って答えていきましょう。rootユーザーでのパスワードの設定を変更できるのでここでパスワードを設定しておくことを推奨します。

それではログインしてみます。-u rootはrootユーザーとしてログインすること、-pはパスワード付きでログインしていることを意味しています。

タイトル:ターミナル

$ mysql -u root -p

先ほどパスワードを設定していたらパスワードの入力を求められるので入力しましょう。

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.19 Homebrew

Copyright (c) 2000, 2020, 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モニター

mysql> exit
Bye

これでログアウトできます。以上で簡単な初期化とログインは完了です。

この記事のまとめ

本記事ではMySQLのMacOSヘのインストールとログインについて解説しました。最後に内容の要点についてまとめておきます。

  • homebrewを用いてMySQLをMacOSにインストールすることができる
  • mysql_secure_installationコマンドでセキュリティの初期設定をすることができる

皆さんもMySQLを使ってデータベース型のアプリケーションを作成してみましょう!