LoginSignup
55
38

More than 5 years have passed since last update.

AWS Amazon Linux2にMySQL5.7を構築する

Last updated at Posted at 2018-09-02

Amazon Linux 2 に MySQL 5.7 のインストールをしようとして思いの外詰まってしまったので手順を残しておく。

前提

  • Amazon Linux 2 AMI (HVM)

手順

デフォルトでmariaDBがデフォルトが入っているのでアンインストールしておく。

$ sudo yum list installed | grep mariadb
$ sudo yum remove mariadb-libs

リポジトリを追加する。centos7系なのでel6ではなくel7をインストールする。

$ sudo yum install http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

インストールする。

$ sudo yum install mysql-community-server

依存性の問題が解決できないときはel6で入れてしまっていなか確認する。
念のためキャッシュも消しておくとよい。

$ sudo yum clean all
$ sudo rm -rf /var/cache/yum

正しくインストールされたことを確認する。

$ yum repolist enabled | grep "mysql.*-community.*"

バージョンを確認する。

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper

$ mysqld -V
mysqld  Ver 5.7.23 for Linux on x86_64 (MySQL Community Server (GPL))

起動してみる。

$ sudo /bin/systemctl start mysqld.service

起動できているか確認してみる。

$ systemctl list-units | grep mysqld.service
mysqld.service   loaded active running   MySQL Server

自動で起動するようにしておく。

# 現在の状態を確認する
$ systemctl is-enabled mysqld.service
disabled

# enableに変更しておく
$ systemctl enable mysqld.service

ログインしてみる。

$ mysql -u root -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

デフォルトでパスワードかかっていた…
パスワードを確認する。

$ cat /var/log/mysqld.log | grep password
2018-09-01T10:27:36.011362Z 1 [Note] A temporary password is generated for root@localhost: ************

調べたパスワードで再度ログインしてみる。

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.23

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

できた!
パスワードを変更しておく。デフォルトの制約が強いのでちょっと弱めておく。

mysql> SET GLOBAL validate_password_length=8;
Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER root@localhost IDENTIFIED BY 'new password';
Query OK, 0 rows affected (0.00 sec)

文字コードを確認する。

mysql> show variables like "chara%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

設定ファイルに以下の2行を追記して再起動する。

/etc/my.cnf
[mysqld]
character-set-server=utf8

[client]
default-character-set=utf8
$ sudo /bin/systemctl restart mysqld.service

新しいユーザーを作成する。

mysql> GRANT ALL PRIVILEGES ON `app_production`.* TO `username`@localhost IDENTIFIED BY 'new password';

ログインできるか試してみる。

$ mysql -u username -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

おわり!

55
38
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
55
38