【Mac】HomebrewでMySQLをインストールする方法

MacにMySQLをインストールする方法をご紹介します。
MySQLは「高速性」と「堅牢性」を追及したマルチユーザー・マルチスレッドのオープンソースの世界で最も普及しているリレーショナルデータベースマネジメントシステム(RDBMS)です。
今回はMacでソフトウェアのインストールを簡単に行うことができるパッケージマネージャー「Homebrew」(ホームブリュー)を使用してインストールしたいと思います。

使用した環境

  • MacBook Pro (13-inch, 2017, Four Thunderbolt 3 Ports)
  • macOS Moajave バージョン 10.14 4
  • mysql Ver 8.0.23 for osx10.14 on x86_64 (Homebrew)

MySQLのインストール

MySQLがインストールされているか確認します。

$ mysql --version

下記コマンドを実行しMySQLのインストールを開始します。

$ brew update
$ brew install mysql

再度、下記コマンドを実行するとインストールされたMySQLのバージョンが確認できます。

$ mysql --version
mysql  Ver 8.0.23 for osx10.14 on x86_64 (Homebrew)

MySQL サービスの起動・終了方法

サービス起動

$ mysql.server start

サービス終了

$ mysql.server stop

サービス自動起動の有効化

$ brew services start mysql

サービス自動起動の無効化

$ brew services stop mysql

MySQLパスワードの設定

MySQLサービスを起動し、下記コマンドでセキュリティの設定を行います。

$ mysql_secure_installation

上記コマンドを実行すると最初に「VALIDATE PASSWORD COMPONENT」という強固なパスワードの設定を助けるプラグインを使用するか問われます。

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:

今回は開発環境での使用を想定しているため、何も入力せず Enter キーを押下します。

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

Please set the password for root here.

New password:

Re-enter new password:

次に匿名ユーザーを削除するか質問されます。Y を入力し 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) :

次に外部からのrootユーザーでのログインを禁止するか質問されます。
今回は禁止したいため、Y を入力し 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) :

MySQLにデフォルトで用意されている「test」データベースを削除するか質問されます。
今回は、Y を入力し 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) :

これまでの変更を反映させるか質問されるので、間違いがないか確認し Y を入力し Enter をキーを押下します。

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へログインするにはターミナルで下記コマンドを実行します。

ユーザー名「root」、パスワード「password」の時の例です。
「-u」と「-p」の後ろにそれぞれ入力します。「-u」「-p」と値の間にスペースを入れないよう注意してください。

$ mysql -uroot -ppassword

ログインに成功すると下記のように「mysql >」と表示されます。

Mac MySQLログイン画面

コメント

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