Programming Journal

学習したことの整理用です。

MySQLでReadOnly権限のユーザーを作成する

概要

BIツール上でデータの更新・削除が行われることを防ぐために、MySQLでREADONLY権限のUserを作成したい。

前提

AWS踏み台サーバ経由でRDSに接続している

流れ

## EC2インスタンスへsshログインする
~/.ssh
❯ ssh -i ~/.ssh/id_rsa(秘密鍵)  ec2-user@xxxxxxx
## MySQLへログインする
[ec2-user@xxxxxxx]$ mysql -h RDSエンドポイント -p -u dbuser(ユーザー名)
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is xxxxx
Server version: 5.7.33-log Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
### SELECT権限を持つユーザー作成
mysql> GRANT SELECT ON *.* TO (新しく作るユーザー名)@'%' IDENTIFIED BY '(パスワード)';

mysql> GRANT SELECT ON *.* TO read-user@'%' IDENTIFIED BY 'passward';
## ユーザ情報の反映
mysql> FLUSH PRIVILEGES;

確認コマンド

## ユーザーの権限情報が確認できる
mysql> SHOW GRANTS FOR 'ユーザー名'@'%';
## 現状のユーザー情報確認
mysql> select user, host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| user          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
+---------------+-----------+