LoginSignup
4

More than 3 years have passed since last update.

macOS に Homebrew で MySQL 8.0 をインストールしてデータベースを作成する

Posted at

Homebrew で MySQL をインストール

$ brew install mysql
(中略)
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: 284 files, 272.5MB

MySQL サーバを起動

brew services start mysql で MySQL サーバを起動する。

$ brew services start mysql
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
(中略)
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

MySQL の初期設定を実施

mysql_secure_installation で対話型の初期設定を開始する。

$ mysql_secure_installation

いくつかの設定について尋ねてくるのでそれに答えていく。
MySQL root ユーザーのパスワードもここで設定できる。

データベースを作成

mysql コマンドを実行する。

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

CREATE DATABASE でデータベースを作成する。
ここでは testdb という名前のデータベースを作成する。

mysql> CREATE DATABASE IF NOT EXISTS testdb
    -> DEFAULT CHARACTER SET utf8mb4
    -> DEFAULT COLLATE utf8mb4_general_ci
    -> ;
Query OK, 1 row affected (0.01 sec)

ユーザーを作成

CREATE USER でユーザーを作成する。
ここではユーザー名に java、パスワードに cafebabe を指定する。

mysql> CREATE USER 'java'@'localhost' IDENTIFIED BY 'cafebabe';
Query OK, 0 rows affected (0.04 sec)

ユーザーに権限を設定する。

mysql> GRANT CREATE, DROP, SELECT, INSERT, UPDATE, DELETE ON testdb.* TO 'java'@'localhost';
Query OK, 0 rows affected (0.01 sec)

参考資料

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
What you can do with signing up
4