Amazon Linux 2にMySQL5.7をインストールする


はじめに

Amazon Linux 2にMySQLをインストールする方法です。

インストール

以下のコマンドを実行する。

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

以下のコマンドを実行する。

sudo yum-config-manager --disable mysql80-community

以下のコマンドを実行する。

sudo yum-config-manager --enable mysql57-community

以下のコマンドを実行する。

yum info mysql-community-server

以下のコマンドを実行する。

sudo yum install mysql-community-server -y

以下のコマンドを実行する。

mysqld --version

以下のコマンドを実行する。

sudo systemctl start mysqld.service

以下のコマンドを実行する。

sudo systemctl enable mysqld.service

以下のコマンドを実行する。

systemctl status mysqld.service

rootパスワードの変更

以下のコマンドを実行する。

cat /var/log/mysqld.log | grep password

「***********」の箇所がrootパスワードとなるので、記録しておく。

2019-06-08T23:38:16.398368Z 1 [Note] A temporary password is generated for root@localhost: ***********

以下のコマンドでパスワードを変更する。

mysql_secure_installation

質問には以下のように答える。

Change the password for root ? ((Press y|Y for Yes, any other key for No) : No
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Yes
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Yes
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Yes
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Yes

文字コードの設定

以下のコマンドを実行し、viを起動する。

sudo vi /etc/my.cnf

最終行に以下を追記する。

character_set_server=utf8mb4

[client]
default-character-set=utf8mb4

以下でMySQLを再起動する。

sudo systemctl restart mysqld.service

DBの作成

ユーザーをmyuser、DB名をmydbとしてDBを作成する。

以下のコマンドを実行する。

mysql -u root -p

以下のSQLを実行する。

CREATE DATABASE mydb;

以下のSQLを実行する。
(「**********」の部分はパスワード)

CREATE USER myuser@'localhost' IDENTIFIED BY '**********';

以下のSQLを実行する。

GRANT ALL PRIVILEGES ON mydb.* TO myuser@'localhost';

以下のコマンドでログイン出来る。

mysql -u myuser -p mydb