【MySQL】MacにHomebrewでMySQLの環境を作成してみよう!

MySQL

MacにHomebrewをしようしてのMySQLの環境構築方法を紹介します。

事前準備

Homebrewのインストール

Homebrewは、macOS、Linuxで動作するパッケージ管理システムのひとつでです。
パッケージ管理システムとは、ソフトウェアをインターネットを通じて、インストール・アンインストール・アップデート・必要な外部ソフトウェアの取得を自動で行えれるシステムです。

Homebrewのインストール確認

以下コマンドを実行してHomebrewがインストールされているか確認しましょう。
「-bash: brew: command not found」と表示された場合はHomebrewがインストールされていないので、次の手順に従ってインストールを行なってください。

brew -v

Homebrewのインストール方法

Homebrewをインストールするには、以下コマンドを実行してください。
Homebrewのインストール確認の際に実行したコマンド「brew -v」を実行してください。
インストールしたHomebrewのバージョンが返ってきたらインストール成功です。
※本記事実行時のバージョンは「Homebrew 3.6.21」が返ってきました。

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

MySQLのインストール

MySQLのインストール確認

以下コマンドを実行してMySQLがインストールされているか確認します。
「-bash: mysql: command not found」と表示された場合はMySQLがインストールされていないので、次の手順に従ってインストールを行なってください。

mysql --version

MySQLのインストール方法

MySQLをインストールするには、以下コマンドを実行してください。
MySQLのインストール確認の際に実行したコマンド「mysql –version」を実行してください。
インストールしたMySQLのバージョンが返ってきたらインストール成功です。
※本記事実行時のバージョンは「mysql Ver 8.0.32 for macos12.6 on x86_64 (Homebrew)」が返ってきました。

brew install mysql

MySQLの初期設定

MySQLの初期設定を行うために、「mysql.server start」コマンドを実行しMySQLを起動してから
以下コマンドを実行してください。

mysql_secure_installation

上記コマンドを実行すると以下「VALIDATE PASSWORD COMPONENT」という強固なパスワードのみを設定できるようにする設定を使用するかを聞かれるのですが、
今回は開発環境なので、「No」と入力して「Enter」を押します。

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:

次にrootユーザのパスワードを新規で入力します。

Please set the password for root here.
New password:
Re-enter new password:

次に誰でもMySQLにアクセス出来る匿名ユーザを削除するか質問されるので、
「Yes」と入力し「Enter」を押します。

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

次にMySQLに「localhost」以外からrootユーザでアクセスを禁止にするか質問されるます。
rootユーザが何処からもアクセスされるとセキュリティ的に問題があり、
外部からは制限をかけたユーザにてアクセスさせる想定なので、
外部からのアクセスを禁止する「Yes」と入力し「Enter」を押します。

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

次にデフォルトで作成されているテスト用のデータベース「test」を削除するか質問されます。
基本データベース「test」を使用することはないので、「Yes」と入力し「Enter」を押します。

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

次にこれまで行なった設定を反映させるか質問されるので、
「Yes」と入力し「Enter」を押します。

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

「All done!」と表示されたら以上で設定完了となります。

MySQLの使用方法

MySQLの起動方法

以下コマンドを実行することにより、MySQLを起動することが出来ます。
起動せずにMySQLにアクセスするコマンドなどを実行すると「Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)」のようなエラーが発生してしまいます。

mysql.server start

MySQLの終了方法

以下コマンドを実行することにより、MySQLを終了することが出来ます。

mysql.server stop

MySQLの起動自動化

PCを再起動するたびにMySQLを起動するのがめんどくさい場合は、以下コマンドを実行することによりPC起動時に自動的にMySQLを起動することが出来ます。

brew services start mysql

MySQLの起動自動化の無効

自動起動を設定していたが、PCの負荷などで使用したい場合のみ起動するようにしたい場合は、以下コマンドを実行することにより無効化を行うことが出来ます。

brew services stop mysql

MySQLへのログイン方法

「root」ユーザでのログインする際は、以下コマンドを実行し、次にパスワードを聞かれるので、
ユーザのパスワードを入力しログインを行なってください。
※別ユーザでアクセスを行う場合は、「root」の部分を変更してください。

mysql --user=root --password

コメント

タイトルとURLをコピーしました