15
5

More than 1 year has passed since last update.

Raspberry PiへのMariaDB セットアップ 2022年版

Last updated at Posted at 2021-12-25

1.はじめに

Raspberry Pi + MariaDBの組み合わせはよく使っているのですが、最新の環境でRaspberry Pi OSにインストールすると外部端末からアクセスできなくなってしまいました。原因ははっきりしていませんがOSがbullseye版に変わったことによる影響かな?
いろいろと試行錯誤したので覚書がてら共有しておきます。
素人がトライ&エラーで実行しているので間違っている場所があるかもしれません。とりあえず動くことを確認しています。
ちなみに古い方法はこちらでした。

2.インストール方法

まずはアップデートしてからMariaDBをインストールします。

$ sudo apt update
$ sudo apt install -y mariadb-server

以前はインストールが完了したらMariaDBにログインしてユーザー権限等を書き換えていましたが、認証用のSocketが変更されたため書き換えができなくなりました。代わりにmysql_secure_installationというツールを使って書き換えを行いました。

$sudo mysql_secure_installation

いろいろ尋ねられますので次の通り入力します。

Enter current password for root (enter for none):  <--何も押さずにEnter
Switch to unix_socket authentication [Y/n]:        <--n
Change the root password? [Y/n]:                   <--y
New password:                                      <--MariaDBのrootユーザー用パスワードを新規作成
Re-enter new password:                             <--同じパスワードを入力
Remove anonymous users? [Y/n]:                     <--y
Disallow root login remotely? [Y/n]:               <--n
Remove test database and access to it? [Y/n]:      <--y
Reload privilege tables now? [Y/n]:                <--y

次に設定ファイル上でローカルからのアクセスに限定する行をコメントアウトします。

$ sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

開いたファイル中のbinde-addressの行を次の通りコメントアウトします。

#binde-address     =127.0.0.1

続いてMariaDBにログインして外部端末からのアクセスを許可します。

$ mysql -u root -p
Enter password:

パスワードを聞かれたら、先ほど作成したパスワードを入力し、権限変更のコマンドを入力。

MariaDB[(none)]> grant all privileges on *.* to root@"%" identified by 'your password' with grant option;
MariaDB[(none)]> exit

設定が終わったら、MariaDBを再起動して完了。

$ suod systemctl restart mysql

以上で完了です。

15
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
5