0
1

【MySQL8.4】EC2でMySQLのMaster、Slave構成を構築してみた

Posted at

MySQLのMaster、Slave構成を構築の勉強するために、
AWSのEC2上にMySQLをインストールし、
Master、Slave構成を構築しました。
レプリケーション実行するまでの手順をこちらで紹介します

インストール先準備

EC2を2台準備します。
セキュリティグループはログイン用にSSH(22ポート)、MySQLAurora(3306ポート)を許可します。
※今回EC2の準備方法については割愛します。

MySQL Community Serverインストール

公式サイトから対象OSに対応したRPMをダウンロードしておくこと

https://dev.mysql.com/downloads/repo/yum

インストール(Master、Slave共通)

# パッケージアップデート
$ sudo dnf update -y
# MySQLリポジトリインストール
$ sudo dnf localinstall mysql84-community-release-el9-1.noarch.rpm
# MySQL Server インストール
$ sudo dnf install -y mysql-server

設定ファイル修正(Master側)

/etc/my.cnfに以下記載する

server-id=1
log_bin=/var/log/mysql/mysql-bin.log
mysql_native_password=on

MySQL起動

$ systemctl start mysqld
$ systemctl status mysqld
$ systemctl enabled mysqld
$ systemctl is-enabled mysqld

MySQLログイン

rootユーザの初期パスワードはMySQLログ(/var/log/mysqld.log)に記載されているため、
確認する。

2024-08-13T04:12:02.154975Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: >REhP_(Hy2Ug

# パスワード確認
$ grep "password" /var/log/mysqld.log | awk -F': ' '{print $2}'
# rootでログイン
$ mysql -u root -p
Enter password: <確認したパスワード>
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.4.2

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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 rootパスワード変更

今回は特に初期パスワードでもいいが、
実際は初期パスワードは変更した方が良いため、
以下でパスワード変更する。

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY '<new password>';

レプリケーション用ユーザ作成

Slave用インスタンスにレプリケーションするためのユーザを作成する。

# rep01ユーザ作成
mysql>  CREATE USER 'rep01'@'%' IDENTIFIED BY '<new password>';
# レプリケーション権限付与
mysql>  GRANT REPLICATION SLAVE ON *.* TO 'rep01'@'%';
mysql>  FLUSH PRIVILEGES;

# ユーザ一覧確認
mysql> SELECT User, Host FROM mysql.user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| rep01            | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

# rep01権限確認
mysql>  
+-----------------------------------------------+
| Grants for rep01@%                            |
+-----------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `rep01`@`%` |
+-----------------------------------------------+
1 row in set (0.01 sec)

# plugin修正
# caching_sha2_passwordの場合はmysql_native_passwordに修正しないと、
# レプリケーション実施時にエラーになる。
mysql> SELECT user,host,plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| rep01            | %         | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.01 sec)

caching_sha2_passwordの場合は以下に修正
mysql>  ****ALTER USER 'rep01' IDENTIFIED WITH mysql_native_password BY '<password>';

mysql> SELECT user,host,plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| rep01            | %         | 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 |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

# ログアウト
exit;

Binary確認

mysql> SHOW BINARY LOG STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      441 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

FileとPositionの値を控えておくこと。

Slave側構築

設定ファイル修正

/etc/my.cnfに以下記載する

server-id=2
log_bin=/var/log/mysql/mysql-bin.log
mysql_native_password=on

MySQL再起動

$ systemctl restart mysqld

レプリケーション設定、実施

mysql> CHANGE REPLICATION SOURCE TO
    -> SOURCE_HOST='<Master側IPアドレス>',
    -> SOURCE_USER='rep01',
    -> SOURCE_PASSWORD='<rep01パスワード>',
    -> SOURCE_LOG_FILE='mysql-bin.000001',
    -> SOURCE_LOG_POS=158;

mysql> START REPLICA;

レプリケーション状態確認

特にエラーがなく、
Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
上記メッセージが出ていれば、
Master側に更新待ち状態のため問題なし

次回はMasterとSlaveの切り替え方法について投稿予定です。

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