LoginSignup
1
0

More than 3 years have passed since last update.

MySQLで接続ユーザーを作成し、権限を付与する

Posted at

アプリケーション毎に接続ユーザーを分けているが、
ユーザーの作成、GRANTのやり方のまとめ。

ユーザーの作成

セキュリティのために接続を許可するホストも指定する。

mysql> CREATE USER 'hoge'@'192.168.10.5' IDENTIFIED BY 'xxxxxxxx';

確認

mysql> select user, host from mysql.user;
+--------+------------------------+
| user   | host                   |
+--------+------------------------+
| root   | 127.0.0.1              |
| hoge   | 192.168.10.5           |
|        | localhost              |
| root   | localhost              |
|        | abc.hoge.jp            |
| root   | abc.hoge.jp            |
+--------+------------------------+
6 rows in set (0.00 sec)

hogeユーザーに権限を付与するGRANT分の作成

似た権限を持つユーザーとDBの組み合わせがあればshow grantsで参照できるので、コピーして編集すると早い。

mysql> show grants for 'root'@127.0.0.1;
+---------------------------------------------------------------------+
| Grants for root@127.0.0.1                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION |
+---------------------------------------------------------------------+
1 row in set (0.02 sec)

今回は、hogeユーザーにnew_app DBへの全権限を付与する。

mysql> GRANT ALL PRIVILEGES ON `new_app`.* TO 'hoge'@'192.168.10.5';
Query OK, 0 rows affected (0.22 sec)

付与した権限の確認

mysql> show grants for 'hoge'@192.168.10.5;
1
0
0

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
1
0