Macにbrew でMariaDBを入れてmysqlのローカル練習環境を作る

MacでさっくとMySQLを試す

ちょこっとMySQLをMacのローカル環境で試してみたいことってあると思います。

「MySQL」から派生したオープンソースリレーショナルデータベースシステムMariaDBをインストールしてMySQLのテスト環境を構築したいと思います。

MariaDBとMySQLはほとんど同じものです。MySQLは権利関係の都合でややこしくなりそうなので、世界中でMariaDBが使用されています。

brew update
brew install mariadb
brew services start mariadb

mysql_secure_installationでセキュリティ関連の設定を行います。

$ mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...

デフォルト(インストール直後)ではパスワードは設定されていないので空のままEnterします。
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

rootのパスワードを設定するか聞かれます。

Yesにします。

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 users)を削除するか聞かれます。anonymous usersはテストようなので削除します。

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!

テスト環境ではリモート環境からこのMacのMariaDBに接続することはないのでYesにします。

本番環境では、データベースの用途によっては、ここを'n'にして設定を行います。

リモート環境からデータベースを利用できれば、便利は便利です。

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!

デフォルトでtestというデータベースが存在しているのですが、それを削除するかどうかを決定します。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.Reload privilege tables now? [Y/n] Y
... Success!Cleaning up...All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.Thanks for using MariaDB!

ここまでの設定を適用するか聞かれます。もちろんYesにします。

それではmysqlに接続してみます。

$ mysql -u root -p
Enter password:

パスワードを聞かれますので、先程設定したものを入力します。

これでMySQLが使えるようになりました。

絵文字🍺に対応できるようにutf8mb4対応にしておく

🍺(ビール)などの絵文字に対応するにはutf8mb4(ユーティーエフ8マルチバイト4)に対応する必要があります。utf8はutf8mb3の別名なので、かなりの範囲の文字に対応していますが、一部の難読漢字や絵文字にはutf8では対応していません。

utf8mb4 4バイト (理論的には”2の32乗”種類)の文字に対応できる

utf8(utf8mb3のエイリアス) 3バイト(理論的には”2の24乗”種類)の文字に対応できる

難しいことは置いといて、簡単に言うと、絵文字に対応したいかどうかです。

まずcharacter setがutf8mb4に設定されているか確認します。

$ mysql -u root -p -e "show variables like 'character%'"
Enter password:
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/Cellar/mariadb/10.3.10/share/mysql/charsets/ |
+--------------------------+---------------------------------------------------------+
mariadb 10.3.10のデフォルト設定ではutf8mb4設定されていないcharacter_setがcharacter_set_client, character_set_connection, character_set_results, character_set_systemの4つがあります。
MacではMySQLの設定ファイルは /usr/local/etc/my.cnf.d/ に置きます。
client.cnfを作成し以下のようにします。.cnf ファイルをvim で開くと
$pという文字が見えますこれは削除しません。
$p
[client]
default-character-set=utf8mb4
mariadbをリスタートして設定を読み込ませます。
$ brew services restart mariadb
$ mysql -u root -p -e "show variables like 'character%'"
Enter password:
+--------------------------+---------------------------------------------------------+
| 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 | utf8 |
| character_sets_dir | /usr/local/Cellar/mariadb/10.3.10/share/mysql/charsets/ |
+--------------------------+---------------------------------------------------------+
character_set_systemはutf8mb4に変更できません。これは仕様です。
つまり、systemのcharacter_set では絵文字が使えません。
例えば、データベースのtable名に絵文字は使用できません。...table名に絵文字を使う必要はないですよね・・・・・😁

コメント