学習記録

アウトプット用に作りました

MySQLをインストールする

Homebrewがインストールされているかを確認。

Homebrew 3.1.7がインストールされていることが確認できました。

$ brew -v
Homebrew 3.1.7
Homebrew/homebrew-core (git revision aacf707e0e; last commit 2021-05-15)
Homebrew/homebrew-cask (git revision 9181a2ccc7; last commit 2021-05-15)

Homebrewを用いて「MySQL」をインストール。

まずmysqlのバージョンを確認すると-bash: mysql: command not foundとなったのでhomebrewにMySQLはインストールされていませんでした。なのでbrew install mysql@5.7でインストールをします。

$ mysql --version
-bash: mysql: command not found
$ brew install mysql@5.7
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (heroku/brew, homebrew/core and homebrew/cask).
==> New Formulae
clazy               gitbackup           mathlibtools        nomino
elan-init           ipinfo-cli          neovim-remote
==> Updated Formulae
Updated 223 formulae.
==> Deleted Formulae
osquery
==> New Casks
enclave                    mem                        vamiga
jgrennison-openttd         privileges
==> Updated Casks
Updated 185 casks.

==> Downloading https://ghcr.io/v2/homebrew/core/mysql/5.7/manifests/5.7.34
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/5.7/blobs/sha256:baf50315
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Pouring mysql@5.7--5.7.34.big_sur.bottle.tar.gz
==> /usr/local/Cellar/mysql@5.7/5.7.34/bin/mysqld --initialize-insecure --user=S
==> 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"' >> /Users/SAYO/.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
🍺  /usr/local/Cellar/mysql@5.7/5.7.34: 319 files, 234.7MB

MySQLが使えるようにするための2つの初期設定。

  1. パスの設定
  2. パスワードの設定

MySQLのインストールはできましたが、そのままではMySQL用のコマンドが使えません。これからコマンドを使えるようにするために実行ファイルの参照先を設定します。

$ echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> /Users/SAYO/.bash_profile

設定を反映させるために次のコマンドをターミナルで実行します。

$ source ~/.bash_profile

最後に設定が正しく完了したことを確認してみましょう。 以下のコマンドでインストールしたMySQLのバージョンが表示されればパスの設定は完了です。

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

② パスワードの設定はMySQLが起動している時に行います。 以下のコマンドでMySQLを起動しましょう。

$ brew services start mysql@5.7
==> Successfully started `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)

起動することができたら、次のコマンドをターミナルで実行してパスワードの設定します。

コマンドを実行すると最初にVALIDATE PASSWORD PLUGINという強固なパスワード設定を助けるプラグインを使用するかどうかを質問されます。今回はローカル環境のみでの使用を想定しているので、何も入力せずEnterキーを押して次に進みます。

次にrootユーザーのパスワードを設定します。

※ rootユーザーとは「管理者」のことで、全ての操作を行う権限を持った特別なユーザーです。

ここで任意のパスワードを設定することができます。この画面ではキーボードを押しても何も表示されませんが、正常に入力されていますので落ち着いて入力し、Enterキーを押してください。

$ 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: 
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) : 

 ... skipping.

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

 ... skipping.
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) : 

 ... skipping.
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) : 

 ... skipping.
All done!

MySQLにログイン。

まずはMySQLを起動させます。すでに起動していたので、一応リスタートしました。

$ brew services start mysql@5.7
Service `mysql@5.7` already started, use `brew services restart mysql@5.7` to restart.
$ brew services restart mysql@5.7
Stopping `mysql@5.7`... (might take a while)
==> Successfully stopped `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)
==> Successfully started `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)

起動できたので次はログインをします。mysql>が表示されたら成功です。

$ mysql --user=root --password
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 Homebrew

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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からログアウトをするときはexit;を入力するとログアウト完了です。

mysql> exit;
Bye

作業が終わったらMySQLを停止させましょう。

$ brew services stop mysql@5.7
Stopping `mysql@5.7`... (might take a while)
==> Successfully stopped `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)