0
0

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 3 years have passed since last update.

MySQLのコマンドラインでユーザー設定

Posted at

最初に

MySQLのコマンドラインで入力をキャンセルする方法

.sql
mysql> \c
mysql> ^C

\cを入力するか、Ctrl+Cでキャンセルできます。

作成済みのユーザーを確認する

.sql
select user, host from mysql.user;

現在接続しているユーザーを確認する

.sql
select user(), current_user();

ユーザーを作成する

パスワードなしのユーザーを作る

.sql
CREATE USER user;

パスワードありのユーザーを作る

.sql
CREATE USER user IDENTIFIED BY 'password';

ユーザー名・パスワード・ホスト名の設定されたユーザーを作る

  • ユーザー名 :user
  • ホスト名 : localhost
  • パスワード : password
.sql
create user `user`@`localhost` IDENTIFIED BY 'password';

ユーザーの権限を確認する

.sql
show grants for user@localhost;
--show grants for ユーザー名@ホスト名
.sql
mysql> show grants for user@localhost;
+------------------------------------------+
| Grants for user@localhost                |
+------------------------------------------+
| GRANT USAGE ON *.* TO `user`@`localhost` |
+------------------------------------------+
1 row in set (0.00 sec)

USAGE という権限は「何も権限がない」という権限です。

ユーザーにDB操作権限を与える

  • 対象 :user@localhost
.sql
grant create on *.* to user@localhost;
.sql
mysql> show grants for user@localhost;
+-------------------------------------------+
| Grants for user@localhost                 |
+-------------------------------------------+
| GRANT CREATE ON *.* TO `user`@`localhost` |
+-------------------------------------------+
1 row in set (0.00 sec)

CREATE になりました。

特定のDBの権限を与える

.sql
grant create on sampleDB.* to user@localhost;
-- grant create on データベース名.* to ユーザ名@localhost;

もっと詳しく権限を設定できます。
ユーザーに権限を設定する(GRANT文)

ユーザーを消す

.sql
drop user user;
--drop user ユーザー名;

参考HP

https://www.dbonline.jp/mysql/user/index1.html
https://www.dbonline.jp/mysql/user/index4.html
https://qiita.com/ysti/items/b4a84691c357167c89c2
https://www.dbonline.jp/mysql/user/index6.html

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?