2022-06-22に更新

MySQLのインストール(macOS Big Sur)

読了目安:17分

MySQLのインストールと最低限の設定を行ったので記録として記載しています。

環境

  • macOS Bug Sur ver. 11.4 (2021.6.25追記)

MySQLのインストール

% brew install mysql
Updating Homebrew...
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
Updated 2 formulae.

==> Downloading https://ghcr.io/v2/homebrew/core/icu4c/manifests/69.1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/icu4c/blobs/sha256:d46b8ec5c3db629e7848e9fd31e5ec99ed952d9c81c8936a2511fae803d831fd
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:d46b8ec5c3db629e7848e9fd31e5ec99ed952d9c81c8936a2511fae803d831fd?se=2021-06-25T07%3A15%3A00Z&sig=nRGhzUD9Cazstufat281ci
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/manifests/1.1.1k
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/blobs/sha256:17d94c51ddfa8364baed5f3a754063e1ca75f807194f68d0b976619cf4e69c1a
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:17d94c51ddfa8364baed5f3a754063e1ca75f807194f68d0b976619cf4e69c1a?se=2021-06-25T07%3A15%3A00Z&sig=YR%2Bly6abADu0wh1hunRl
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libevent/manifests/2.1.12
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libevent/blobs/sha256:45758b448d82b82b6bea52bc9a72593ef22f04ebdaa4b4230cadf12768252e22
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:45758b448d82b82b6bea52bc9a72593ef22f04ebdaa4b4230cadf12768252e22?se=2021-06-25T07%3A15%3A00Z&sig=5vWUPHNX%2F8bC56EQ%2B9
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/lz4/manifests/1.9.3
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/lz4/blobs/sha256:7024d0b6ee857352cbd3138f752496b87fa27252adbc6daefa4a6c64d3e347e5
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:7024d0b6ee857352cbd3138f752496b87fa27252adbc6daefa4a6c64d3e347e5?se=2021-06-25T07%3A15%3A00Z&sig=ShIwQXX%2B0E7UD5PPBebb
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/six/manifests/1.16.0_1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/six/blobs/sha256:6068e58ff59ea70f491671ad3257b129ed7d5b90a5c678348c3dfdaa14953cdd
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:6068e58ff59ea70f491671ad3257b129ed7d5b90a5c678348c3dfdaa14953cdd?se=2021-06-25T07%3A15%3A00Z&sig=aMnR071bMz0hda%2Fx86dr
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/protobuf/manifests/3.17.3
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/protobuf/blobs/sha256:d1060a6f73000c9c46a1954397a6375fb41c409d7b3cb7206fc69488313b4855
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:d1060a6f73000c9c46a1954397a6375fb41c409d7b3cb7206fc69488313b4855?se=2021-06-25T07%3A15%3A00Z&sig=nIjKs8TP6bv%2BvM2RQaID
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/zstd/manifests/1.5.0
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/zstd/blobs/sha256:eae17621cfc664d6e527a6d6aa6a000343eced0f60c81b4e2dd9a9aed7b79c3f
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:eae17621cfc664d6e527a6d6aa6a000343eced0f60c81b4e2dd9a9aed7b79c3f?se=2021-06-25T07%3A15%3A00Z&sig=dsqDM%2BUMcr788UtN3RtM
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/manifests/8.0.25_1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/blobs/sha256:db388d333de4224dcc9ca54917069c75805801f00bc1355c9dcfe5bf518c4045
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:db388d333de4224dcc9ca54917069c75805801f00bc1355c9dcfe5bf518c4045?se=2021-06-25T07%3A15%3A00Z&sig=ZDGvVzUpsFTORAFjzUluP0
######################################################################## 100.0%
==> Installing dependencies for mysql: icu4c, [email protected], libevent, lz4, six, protobuf and zstd
==> Installing mysql dependency: icu4c
==> Pouring icu4c--69.1.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/icu4c/69.1: 259 files, 72.8MB
==> Installing mysql dependency: [email protected]
==> Pouring [email protected]_sur.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺  /usr/local/Cellar/[email protected]/1.1.1k: 8,071 files, 18.5MB
==> Installing mysql dependency: libevent
==> Pouring libevent--2.1.12.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/libevent/2.1.12: 57 files, 2MB
==> Installing mysql dependency: lz4
==> Pouring lz4--1.9.3.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/lz4/1.9.3: 22 files, 657.8KB
==> Installing mysql dependency: six
==> Pouring six--1.16.0_1.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/six/1.16.0_1: 20 files, 122.7KB
==> Installing mysql dependency: protobuf
==> Pouring protobuf--3.17.3.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/protobuf/3.17.3: 210 files, 18.0MB
==> Installing mysql dependency: zstd
==> Pouring zstd--1.5.0.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/zstd/1.5.0: 31 files, 3.5MB
==> Installing mysql
==> Pouring mysql--8.0.25_1.big_sur.bottle.tar.gz
==> /usr/local/Cellar/mysql/8.0.25_1/bin/mysqld --initialize-insecure --user=ahsabkab --basedir=/usr/local/Cellar/mysql/8.0.25_1 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> 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.25_1: 300 files, 293.6MB
==> Caveats
==> 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

最低限の初期設定

セキュリティ設定をします。

% brew services start mysql # サーバーを起動
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 1063 (delta 10), reused 0 (delta 0), pack-reused 1045
Receiving objects: 100% (1063/1063), 305.21 KiB | 556.00 KiB/s, done.
Resolving deltas: 100% (445/445), done.
Tapped 1 command (41 files, 394KB).
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

% mysql_secure_installation # パスワード設定をする

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT 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 component?

Press y|Y for Yes, any other key for No: n # ローカル環境を想定しているのでスキップする
Please set the password for root here.

New password: # パスワードを設定

Re-enter new password: # 上記で設定したパスワードを入力
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.

# 不正アクセスなどを考慮してリモートサーバーからのrootユーザーへのアクセスを不許可
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!

これでセキュリティ設定は完了です。
次に、実際に設定したパスワードでログインしてサーバーを起動してみます。

% mysql -uroot -p # 設定したパスワードでログイン
Enter password: # パスワード入力
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.22 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> exit; # ログアウトする
Bye

% brew services stop mysql # サーバーを停止する
Stopping `mysql`... (might take a while)
==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)

これで一連の動作確認は完了です。

新規ユーザーを作成する場合

以下はMySQLでRails用に新しいユーザーを作って権限付与する方法です。(参考記事)
デフォルトのrootユーザーは全てのデータベースへのフルアクセス権を持つスーパーユーザーなのでカスタム権限を持つユーザーをRails用にします。rootユーザーでログインしMySQLシェル内で新たな」ユーザーを作成します。

# '@'以降は接続元のホストを示している
mysql> CREATE USER '新しいユーザー名'@'localhost' IDENTIFIED BY '任意のパスワード';

# '@'以降を%にすると、「接続元のホストはどこでもOK」という意味になる
mysql> CREATE USER '新しいユーザー名'@'%' IDENTIFIED BY '任意のパスワード';

新しいユーザーをここでは「sample」とします。「sample」へデータベースの操作権限を与えます。

mysql> GRANT ALL PRIVILEGES ON * . * TO 'sample'@'localhost';
# 「your_database_name.*」は、当該データベースの全てのテーブルを操作できるという意味
mysql> GRANT ALL PRIVILEGES ON your_database_name.* TO 'rails6'@'%';

リロードして変更を反映します。

mysql> FLUSH PRIVILEGES;

以下のコマンドでユーザー一覧が確認できます。

mysql> SELECT user , host FROM mysql.user;
ツイッターでシェア
みんなに共有、忘れないようにメモ

光の勢力

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント