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 1 year has passed since last update.

CentOS上のMySQLに新規データベースを作成した際の作業メモ

Posted at

はじめに

概要

CentOS上に構築したMySQLにデータベース「dev」を作成し、そのデータベースに外部からアクセスするためのユーザー「devUser」を作成する。

環境など

  • サーバ
    • OS:CentOS 7.9
    • ホスト名:CentOS
    • MySQL:Ver 8.4.0

データベースの作成

認証プラグイン「mysql_native_password」の有効化

実行ユーザー:root
mysql> SHOW PLUGINS;
+----------------------------------+----------+--------------------+---------+---------+
| Name                             | Status   | Type               | Library | License |
+----------------------------------+----------+--------------------+---------+---------+
~略~
| mysql_native_password            | DISABLED | AUTHENTICATION     | NULL    | GPL     |
+----------------------------------+----------+--------------------+---------+---------+
mysql> 
実行ユーザー:root
[root@CentOS ~]# vi /etc/my.cnf


[mysqld]
~略~
mysql_native_password=ON


[root@CentOS ~]# 
実行ユーザー:root
[root@CentOS ~]# systemctl restart mysqld 
[root@CentOS ~]#
実行ユーザー:root
mysql> SHOW PLUGINS;
+----------------------------------+----------+--------------------+---------+---------+
| Name                             | Status   | Type               | Library | License |
+----------------------------------+----------+--------------------+---------+---------+
~略~
| mysql_native_password            | ACTIVE   | AUTHENTICATION     | NULL    | GPL     |
+----------------------------------+----------+--------------------+---------+---------+
48 rows in set (0.00 sec)
mysql>

データベース「dev」の作成

実行ユーザー:root
mysql> CREATE DATABASE dev CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
mysql> 

ユーザー「devUser」の作成

実行ユーザー:root
mysql> SELECT User, Host, Plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User             | Host      | Plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
mysql> 
実行ユーザー:root
mysql> CREATE USER 'devUser'@'%' IDENTIFIED WITH mysql_native_password BY '**********';
mysql>
実行ユーザー:root
mysql> SELECT User, Host, Plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User             | Host      | Plugin                |
+------------------+-----------+-----------------------+
| devUser          | %         | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
mysql> 

ユーザー「devUser」にデータベース「dev」への権限を付与

実行ユーザー:root
mysql> show grants for 'devUser'@'%';
+-------------------------------------+
| Grants for devUser@%                |
+-------------------------------------+
| GRANT USAGE ON *.* TO `devUser`@`%` |
+-------------------------------------+
mysql> 
実行ユーザー:root
mysql> GRANT ALL PRIVILEGES ON dev.* TO 'devUser'@'%';
mysql> 
実行ユーザー:root
mysql> show grants for 'devUser'@'%';
+--------------------------------------------------+
| Grants for devUser@%                             |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO `devUser`@`%`              |
| GRANT ALL PRIVILEGES ON `dev`.* TO `devUser`@`%` |
+--------------------------------------------------+
mysql> 
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?