SE_BOKUのまとめノート的ブログ

SE_BOKUが知ってること・勉強したこと・考えたことetc

WSL2のubuntu20.04LTSにインストールしたMariadb10.9の初期設定

目次

WSL2のubuntu20.04LTSにインストールしたMariadb10.9の初期設定

以下の記事でインストールするところまでやったMariadb10.9を使える状態にします。

arakan-pgm-ai.hatenablog.com

共通:WSL2のUbuntuではserviceを使う

バックグラウンドで動くプログラムをLinuxでは「デーモン(daemon)」、Windowsでは「サービス( service)」といいますが、この記事では「サービス」に統一してます。

WSL2:Ubuntuでサービスの開始・終了などを行うのは「service」コマンドです。

WSL2ではないUbuntuではサービス管理に「systemctl」コマンドを使用しますが、WSL2では使えません。

serviceコマンドの主な利用例です。

開始

sudo service [Service Name] start

終了

sudo service [Service Name] stop

再起動

sudo service [Service Name] restart

確認

service [Service Name] status

インストールされているサービス一覧

 service --status-all

 

デーモンやサービスの意味は環境によって違う管理コマンドについては、こちらの記事がまとまっていてわかりやすいので、リンクをのせておきます。

rainbow-engine.com

あくまで開発用で使いたいときだけ立ち上げて、終わったら止める使い方をするので、今回は自動起動の設定とかは行っていません。

MariaDB

インストールされたMariaDBの初期設定をしていきます。

50-server.cnfでデフォルト文字コード確認

最初にデフォルトの文字コードの確認をします。

 /etc/mysql/mariadb.conf.d/50-server.cnf

上記のファイルを開いて[character-set-server]の部分を確認します。

以下のようになっていればOKです。

なっていなければ、以下のように修正しておきます。

utf8mb4になっていないと、日本語処理に問題がでるためです。

mariadbをserviceで起動

サービスの登録名を以下で確認します。

service --status-all | grep db

今回は「mariadb」が登録名でした。

その名前を指定して特権ユーザでスタートします。

sudo service mariadb start

mariadb初期設定

初期設定は以下のコマンドを実行して行います。

sudo mysql_secure_installation

root@localhostのパスワード設定もするので、sudoは必須です。

いろいろ聞いてきます。

僕がやった時の全文を例として補足を記載します。

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
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...

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.

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

Change the root password? [Y/n] n
 ... skipping.

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!

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!

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!

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!

 

最初が[Unix_Socket] 認証に切り替えるか否かの問い合わせです。

Switch to unix_socket authentication/unix_socket認証に切り替える

ここは [no] でも root のみ デフォルトで [Unix_Socket] 認証は有効なので「n」にしています。

 

次が「root」パスワードを設定するかどうかです。

Change the root password?

rootは[Unix_Socket] 認証が有効になってるので、僕は「n」にしました。

 

匿名ユーザは削除するか?の問い合わせです。

 Remove anonymous users? 

開発用に使うDBでは全く不要なので「yes」にしました。

 

リモートでのrootログインを停止するかの問い合わせです。

Disallow root login remotely? /リモートでrootログインを禁止する?

リモートでのrootログインなど不要なので「yes」です。

 

テストデータベースを削除するかどうかの問い合わせです。

 Remove test database and access to it? 

どちらでもいいのですが、僕は削除する=「yes」にしています。

 

特権情報のリロードに関する問い合わせです。

Reload privilege tables now?

これは「no」にしたら、どうなるのかを僕は知りません。

とりあえず安全策をとって「yes」にしています。

これで、初期設定は終わりです。

mariadbにrootでログインしてみる

デフォルトで [Unix_Socket] 認証は有効になっているので、以下を入力するだけでログインすることができます。

sudo mysql

いけました。

不用心にも見えますが、個人で使う開発用なので問題ないと考えています。

今回はここまでです。

ではでは。