あきらぼ

テック系ブログ

SQLデータベースで読み取り専用ユーザーを作る

部屋の環境モニターを構築中に、SQLデータサーバーに環境データを貯めていたのですが、読み取り専用のユーザーの作り方が分からなかったので、備忘録として書き留めます。

 

 

 

まず、データベースへ高い権限のあるユーザーでログインします。
私の場合はRootユーザーです。

 

私の場合はmariaDBを使用しています。

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 33705
Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

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)]> use roomconditionmonitor

私の場合は室内環境モニタ用のデータベースなので「roomconditionmonitor」という名前になっています。

 

ここでユーザーを作成します。

MariaDB [roomconditionmonitor]> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password' ;

 

今回は同一サーバーからアクセスする用のユーザーなのでLocalhostとしています。

そして、最後に今回の一番重要な権限付与です。

GRANT SELECT ON roomconditionmonitor.* TO 'roomcondition_reading_user'@'localhost';

 

SELECTの権限のみ付与しているのでデータベースの変更等はできません。

 

GRANTのMariaDBのDocumentはこちらです。

GRANT - MariaDB Knowledge Base