Parallels Desktop 20%OFFセール中Check!

HomebrewでMacにMariaDBをインストールする手順と設定

MariaDBのインストール手順

MySQLにするべきかMariaDBにするべきか、それが問題だ。と言ったのはシェイクスピアだったでしょうか。同じように動くならどっちでもいいよね。という訳でMariaDBHomebrewでインストールしていきます。

これで、MacにApache、MariaDB、PHPが入ってMAMP的な環境ができあがります。

環境
  • Mac mini (M1, 2020)
  • macOS Monterey
  • Homebrew 3.4.1

Homebrewを使います。まだ導入していない場合はこちらもどうぞ。

目次

MariaDBのインストール

インストール前に brew searchbrew info をチェックしましょう。

% brew search mariadb
==> Formulae
mariadb                       mariadb-connector-odbc        mariadb@10.2                  mariadb@10.4                  qt-mariadb
mariadb-connector-c           mariadb@10.1                  mariadb@10.3                  mariadb@10.5

% brew info mariadb
mariadb: stable 10.7.3 (bottled)
Drop-in replacement for MySQL
https://mariadb.org/
Conflicts with:
  mariadb-connector-c (because both install `mariadb_config`)
  mysql (because mariadb, mysql, and percona install the same binaries)
  mytop (because both install `mytop` binaries)
  percona-server (because mariadb, mysql, and percona install the same binaries)
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mariadb.rb
License: GPL-2.0-only
==> Dependencies
Build: bison ✘, cmake ✘, fmt ✘, pkg-config ✘
Required: groonga ✘, openssl@1.1 ✔, pcre2 ✔, zstd ✔
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To restart mariadb after an upgrade:
  brew services restart mariadb
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mariadb/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
==> Analytics
install: 20,084 (30 days), 38,742 (90 days), 152,861 (365 days)
install-on-request: 20,043 (30 days), 38,661 (90 days), 152,602 (365 days)
build-error: 26 (30 days)

はい、大丈夫そうですね(何が)。全部読んだことにして、インストールしていきます。

% brew install mariadb

(中略)

==> Installing mariadb
==> Pouring mariadb--10.7.3.arm64_monterey.bottle.tar.gz
==> /opt/homebrew/Cellar/mariadb/10.7.3/bin/mysql_install_db --verbose --user=<ユーザ名> --basedir=/opt/homebrew/Cellar/mariadb/10.7.3 --datadir=/opt/homebrew/var/mysql
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To restart mariadb after an upgrade:
  brew services restart mariadb
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mariadb/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
==> Summary
🍺  /opt/homebrew/Cellar/mariadb/10.7.3: 909 files, 173.6MB

インストール完了です。いえい。

MariaDBの起動と停止

起動・停止のコマンド

brew services start mariadbMariaDB を起動して、自動起動を有効にする
brew services stop mariadbMariaDB を停止して、自動起動を無効にする
brew services restart mariadbMariaDB を再起動する
Homebrewのコマンド
mysql.server startMariaDB を起動する
mysql.server stopMariaDB を停止する
mysql.server restartMariaDB を再起動する
MariaDBのコマンド

動作確認

起動してみましょう。

% brew services start mariadb
==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)

% ps ax | grep mysql
14871   ??  S      0:00.02 /bin/sh /opt/homebrew/opt/mariadb/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
14942   ??  S      0:00.10 /opt/homebrew/opt/mariadb/bin/mariadbd --basedir=/opt/homebrew/opt/mariadb --datadir=/opt/homebrew/var/mysql --plugin-dir=/opt/homebrew/opt/mariadb/lib/plugin --log-error=/opt/homebrew/var/mysql/mac.local.err --pid-file=mac.local.pid
15285 s000  S+     0:00.00 grep mysql

MariaDBのセキュリティ設定

まず、MariaDBを起動していない場合は起動します。起動しておかないと次のステップでコケるので注意。

次に、対話形式でセキュリティ設定をしてくれる mysql_secure_installationsudo で実行します。対話形式だから便利〜助かる〜と決して思わないのは長い英文を読まなければならないからなのは私だけでしょうか。

% sudo mysql_secure_installation
Password:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
なぜ sudo するのか

MariaDBでは root が unix_socket という認証プラグインを使ってログインするように初期設定されます。これはOSのユーザ名とDBのユーザ名が同じならばパスワードなしでログインできる機能です。

sudo しなかった場合、OSのユーザ名とDBのユーザ名が一致しない状態でMariaDBに root でログインしようとしてしまうため、ERROR 1698 (28000): Access denied for user ‘root’@’localhost’ というエラーが出て先に進めなくなってしまいます。

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

現在のrootのパスワードを入力します。初期値は未設定なので何も入力せずにEnterします。

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
 ... skipping.

もう一度説明しますね。unix_socket authentication とは、 OSのユーザ名とDBのユーザ名が同じ場合にUNIXソケットを使って認証する機能です。なんかイヤなので使いません。n です。

Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

rootのパスワードを設定するか。Y です。Y って入力しなくてもいいんだけど絶対入力する派です。真面目なので。そして、パスワードを2回入力します。

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] Y
 ... Success!

anonymousユーザを削除するか。Y です。

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? [Y/n] Y
 ... Success!

ローカル以外からのrootログインを禁止するか。Y です。開発環境なので外部からの接続はしません。

By default, MariaDB 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? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

テスト用のデータベースとそのアクセス権を削除するか。Y です。これ、どうせみんな削除するんだから初めから作らなければいいのにね。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

ここまでの変更を反映するために権限テーブルをリロードするか。Y です。これ、n の人いるんですか。

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

セキュリティ関連の設定が終わりました。

ユーザの作成

MariaDBに接続するためのユーザを作成します。いつも root で接続するわけにはいきませんからね。と言いつつ、まずは root でログインします。

% mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.7.3-MariaDB Homebrew

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

作成済みのユーザを確認します。

MariaDB [(none)]> select User, Host, Password from mysql.user;
+-------------+-----------+-------------------------------------------+
| User        | Host      | Password                                  |
+-------------+-----------+-------------------------------------------+
| mariadb.sys | localhost |                                           |
| root        | localhost | ***************************************** |
| username    | localhost | invalid                                   |
+-------------+-----------+-------------------------------------------+
3 rows in set (0.002 sec)

mariadb.sys はMariaDBさんが使うユーザです。root の他に username というユーザが作成されています。これはMacのログインユーザと同じ名前です。このユーザをそのまま使うならば、次のSQLコマンドで権限とパスワードを設定します。

MariaDB [(none)]> GRANT ALL ON *.* TO 'username'@'localhost' IDENTIFIED BY 'pasuwaado';
Query OK, 0 rows affected (0.023 sec)

MariaDB [(none)]> select User, Host, Password from mysql.user;
+-------------+-----------+-------------------------------------------+
| User        | Host      | Password                                  |
+-------------+-----------+-------------------------------------------+
| mariadb.sys | localhost |                                           |
| root        | localhost | ***************************************** |
| username    | localhost | ***************************************** |
+-------------+-----------+-------------------------------------------+
3 rows in set (0.002 sec)

新しいユーザを作成する場合も、上記のSQLコマンドでOKです。

MariaDB(MySQL)のユーザを新規作成する時に CREATE USERGRANT のどちらを使うか問題があります。私はGRANT派です。だって CREATE で作成してもどうせ GRANT で権限を付与しなければならないじゃないですか。だったら1回で済むほうがいいですよね。

MariaDBの設定

設定ファイルの場所は /opt/homebrew/etc/my.cnf です。中をこっそり覗いてみましょう。

% less /opt/homebrew/etc/my.cnf
#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include *.cnf from the config directory
#
!includedir /opt/homebrew/etc/my.cnf.d

ディレクトリ /opt/homebrew/etc/my.cnf.d をインクルードしています。このファイルに直接設定を書き込むのではなく、my.cnf.d に設定ファイルを置きなさいと。そう言われているような気がします。そうしましょう。

% vi /opt/homebrew/etc/my.cnf.d/my.cnf

とりあえず文字コードの設定。絵文字が使えるように🍺。

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

設定を反映させるために再起動。

% brew services restart mariadb
Stopping `mariadb`... (might take a while)
==> Successfully stopped `mariadb` (label: homebrew.mxcl.mariadb)
==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)

設定を確認します。

% mysql -u root -p
Enter password:

MariaDB [(none)]> show variables like 'char%';
+--------------------------+-----------------------------------------------------------+
| Variable_name            | Value                                                     |
+--------------------------+-----------------------------------------------------------+
| character_set_client     | utf8mb4                                                   |
| character_set_connection | utf8mb4                                                   |
| character_set_database   | utf8mb4                                                   |
| character_set_filesystem | binary                                                    |
| character_set_results    | utf8mb4                                                   |
| character_set_server     | utf8mb4                                                   |
| character_set_system     | utf8mb3                                                   |
| character_sets_dir       | /opt/homebrew/Cellar/mariadb/10.7.3/share/mysql/charsets/ |
+--------------------------+-----------------------------------------------------------+

いい感じ〜。終わりです。

でわでわ

MariaDBのインストール手順

この記事が気に入ったら
いいね または フォローしてね!

シェアしてね

コメント

コメントする

目次