スポンサーリンク

【MySQL】ユーザーの権限付与

 

参照DBをmysqlに切り替え

USE mysql

登録されているユーザを確認

mysql> SELECT user, host FROM mysql.user;
+—————+———–+
| user | host |
+—————+———–+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
| sawaryot | localhost |
+—————+———–+
4 rows in set (0.00 sec)

権限を表示

mysql> show grants for ‘sawaryot’@’localhost’;
+———————————————-+
| Grants for sawaryot@localhost |
+———————————————-+
| GRANT USAGE ON *.* TO ‘sawaryot’@’localhost’ |
+———————————————-+
1 row in set (0.00 sec)

mysql>

 

(必要なら)ユーザを作成

作成するユーザーのパスワードのルールを必要なら変えておく

私はルールを緩くしたいので以下の文を実行

set global validate_password_length=4;
set global validate_password_policy=low;

ルール確認
show variables like ‘validate_password%’;

 

そしてユーザー作成

CREATE USER user_name;
CREATE USER user_name IDENTIFIED BY 'password';

※ユーザ指定の書式例
user_name@host_name

※ワイルドカードをホストに使うときはシングルクォートでくくる
user_name@'%'
'username'@'192.168.128.%'

 

ちなみにユーザー削除は

drop user user_name;

 

権限付与

GRANT ALL PRIVILEGES ON `DB名`.テーブル TO 'ユーザ名'@'ホスト名';
GRANT SELECT,UPDATE,INSERT,DELETE ON `DB名`.テーブル TO 'ユーザ名'@'ホスト名';

 

-- test_dbの全権限をtestユーザーに追加
grant all on test_db.* to test@localhost identified by {パスワード};


-- 全DBの全権限をtestユーザーに追加
grant all on *.* to test@localhost identified by {パスワード};

など

 

 

mysql> show grants for ‘sawaryot’@’localhost’;
+———————————————-+
| Grants for sawaryot@localhost |
+———————————————-+
| GRANT USAGE ON *.* TO ‘sawaryot’@’localhost’ |
+———————————————-+
1 row in set (0.00 sec)

mysql> grant all on *.* to sawaryot@localhost identified by ‘任意のパスワード’;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘sawaryot’ at line 1
mysql> grant all on *.* to sawaryot@localhost identified by ‘sawaryot’;
Query OK, 0 rows affected, 1 warning (0.00 sec)

 

ここで警告が出たので確認

 

mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Warning
Code: 1287
Message: Using GRANT statement to modify existing user’s properties other than privileges is deprecated and will be removed in future release. Use ALTER USER statement for this operation.
1 row in set (0.00 sec)

 

IDENTIFIED BY PASSWORDで指定するパスワードが生のパスワードだとワーニングが出るらしい

create userするときまたはしたあとに権限を設定してくださいという話

こちらの記事が参考になる

 

権限を確認してみる

mysql> show grants for ‘sawaryot’@’localhost’;
+——————————————————-+
| Grants for sawaryot@localhost |
+——————————————————-+
| GRANT ALL PRIVILEGES ON *.* TO ‘sawaryot’@’localhost’ |
+——————————————————-+
1 row in set (0.00 sec)

mysql>

 

警告は出るが一応権限変更は行えているっぽい

 

スポンサーリンク

(必要なら)権限削除

構文

revoke {権限内容} on {権限対象} to {ユーザー名}@{ホスト名} identified by {パスワード}

-- testユーザーからfile権限を削除
revoke file on *.* from test@localhost;

 

コメント

タイトルとURLをコピーしました