43
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLユーザ作成について

Last updated at Posted at 2019-01-26

私は今までPHP学習にMySQLを使ってきていましたが、ユーザや権限周りをあまり触ってきませんでした。
DB操作は全てroot権限で行なっていました。ユーザ作成や権限周りに触れる機会があったので、自分用にまとめてみようと思います。

DBにアクセス

$ mysql -u root -p

MySQLユーザー作成

ユーザの確認

MySQLのユーザ一覧を確認。

SELECT  host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
4 rows in set (0.00 sec)

ユーザの作成

--ユーザ作成
CREATE USER 'user_name'@'localhost';

--MySQL 5.7以前パスワード設定 / 変更
SET PASSWORD FOR 'user_name'@'localhost' IDENTIFIED BY 'user_password';

--MySQL 8.0 以降パスワード設定 / 変更
ULTER USER 'user_name'@'localhost' IDENTIFIED BY 'user_password';

--ユーザ作成&パスワード設定 バージョン共通
CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'your_password;

試しにやってみる。

CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';

SELECT  host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
| localhost | test             |
+-----------+------------------+
5 rows in set (0.00 sec)

ユーザ削除

DROP USER test;

ユーザの権限設定

権限の確認

mysql> SHOW GRANTS FOR 'test'@'localhost';
+------------------------------------------+
| Grants for user@localhost                |
+------------------------------------------+
| GRANT USAGE ON *.* TO `user`@`localhost` |
+------------------------------------------+

権限の追加

概要などは下記URLが詳しいので参考になりました。

[MySQL]権限の確認と付与

43
39
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
43
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?