7
8

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.

Linux環境にMySQLサーバを構築する

Last updated at Posted at 2021-08-08

目的

Linux環境にMySQLサーバを構築し、外部端末から接続出来るように設定します。

構築環境

OS:Amazon Linux2
MySQL:8.0.26

yumのリポジトリリストを更新

yumのリポジトリリストにMySQLYumリポジトリを追加

追加するMySQLYumリポジトリのバージョンや対応するプラットフォームはこちらのページで確認来ます。

$ sudo yum -y localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

リポジトリ追加を確認

以下のコマンドでMySQLYumリポジトリが正常に追加されていることを確認します。

$ yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64     MySQL Connectors Community          121+20
mysql-tools-community/x86_64          MySQL Tools Community                   68
mysql80-community/x86_64              MySQL 8.0 Community Server             171

MySQLをインストール

MySQLをインストール

$ sudo yum -y install mysql-community-server

インストールされていることを確認

$ mysql --version
mysql  Ver 8.0.26 for Linux on x86_64 (MySQL Community Server - GPL)

systemdでMySQLを起動・停止

systemdでMySQLを起動

$ sudo systemctl start mysqld

MySQLの起動を確認

runnning状態になっていれば起動OKです。

$ systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2021-08-08 05:41:48 UTC; 20s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 3416 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 3487 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─3487 /usr/sbin/mysqld

MySQLを自動起動するように設定

$ sudo systemctl enable mysqld

MySQLにログイン

MySQLのrootユーザーのパスワードを確認する

MySQLインストール時のrootユーザーのパスワードはmysqld.logから確認することが出来ます。

$ sudo cat /var/log/mysqld.log | grep root
2021-08-08T05:41:44.729287Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: xG/+aKr*S5Wh

ルートユーザーでログイン

$ mysql -u root -p
Enter password: 

ルートのパスワードを任意に変更

ルートのパスワードを任意に変更する
※8文字以上かつ英大文字・小文字・数字・記号を含める必要がある

mysql> SET PASSWORD = 'Password@1';
Query OK, 0 rows affected (0.00 sec)

パスワードの成約を破るとエラーになります。

mysql> SET PASSWORD = '1';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

外部からアクセスを許可

外部からアクセスするように新規ユーザーを作成

mysql> create user 'devuser'@'%' identified by 'Password@1';

grant文を実行

新規ユーザーにgrant文を実行し、作成したユーザーがどこからアクセスしてくるか明示します。
今回はIPアドレスで絞りたくないので、「%」を指定しています。
※接続元を絞る場合は「%」ではなく、任意のIPアドレスを使用してください。

mysql> grant all privileges on *.* to 'devuser'@'%';

データベースを作成(外部接続の確認用)

データベース作成

mysql> CREATE DATABASE dbtest;
Query OK, 1 row affected (0.00 sec)

作成したデータベースを確認(一覧)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| dbtest             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

使用するデータベースを選択

mysql> use dbtest;
Database changed

外部からの接続確認

MySQLのWorkbenchで確認します。
ホストとユーザーを指定してテスト接続が出来れば成功です。

スクリーンショット 2021-08-08 15.51.24.png

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?