tag:crieit.net,2005:https://crieit.net/tags/%E3%83%AC%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3/feed
「レプリケーション」の記事 - Crieit
Crieitでタグ「レプリケーション」に投稿された最近の記事
2022-08-08T04:48:35+09:00
https://crieit.net/tags/%E3%83%AC%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3/feed
tag:crieit.net,2005:PublicArticle/18264
2022-07-28T02:01:52+09:00
2022-08-08T04:48:35+09:00
https://crieit.net/posts/MySQL-Linux
MySQLでDBサーバ構築してレプリケーションさせる
<p>参考<br />
① https://videohub.oracle.com/media/MySQL%208.0%20入門セミナー%20~インストール編%20for%20Linux%20&%20アーキテクチャ編~%20%282022年6月16日%29/1_ph7s6hxj<br />
②MySQL公式ドキュメント:https://dev.mysql.com/doc/refman/8.0/ja/linux-installation.html</p>
<h1 id="1. MySQLを開始"><a href="#1.+MySQL%E3%82%92%E9%96%8B%E5%A7%8B">1. MySQLを開始</a></h1>
<h2 id="1.1. リポジトリをダウンロード"><a href="#1.1.+%E3%83%AA%E3%83%9D%E3%82%B8%E3%83%88%E3%83%AA%E3%82%92%E3%83%80%E3%82%A6%E3%83%B3%E3%83%AD%E3%83%BC%E3%83%89">1.1. リポジトリをダウンロード</a></h2>
<p>この記事では、Oracle Linuxを使用している。</p>
<pre><code>[opc@mysql-master1 ~]$ cat /etc/oracle-release
Oracle Linux Server release 8.6
</code></pre>
<p>yumリポジトリを利用する。<br />
<a target="_blank" rel="nofollow noopener" href="https://dev.mysql.com/">https://dev.mysql.com/</a><br />
から、MySQLサーバのOSバージョンと合致したリポジトリをネットに接続可能な端末にダウンロードし、MySQLサーバに配置する。</p>
<p>画面左部の「<code>Downloads</code>」タブをクリックし切り替わった画面の<code>MySQL Yum Repository</code>をクリック。<br />
すると、複数種類のリポジトリが表示されるので、利用するMySQLサーバのOSバージョンと合致したリポジトリを選択する必要がある。</p>
<pre><code>[opc@mysql-master1 ~]$ cat /etc/oracle-release
Oracle Linux Server release 8.6
</code></pre>
<p>ダウンロードしたら、利用予定のMySQLサーバに転送する。</p>
<pre><code>[root@mysql-master1 tmp]# ll
total 16
-rw-r--r--. 1 kiban kiban 14432 Jul 23 15:12 mysql80-community-release-el8-4.noarch.rpm
drwx------. 3 root root 17 Jul 23 15:05 systemd-private-e967423d4c9e455a88d0cd2ee0f307be-chronyd.service-2MooXe
drwx------. 3 root root 17 Jul 23 15:05 systemd-private-e967423d4c9e455a88d0cd2ee0f307be-unified-monitoring-agent.service-RX6Cai
drwxr-xr-x. 2 root root 6 Jul 21 17:08 unified-monitoring-agent
</code></pre>
<p>以上</p>
<h2 id="1.2. MySQLのインストール"><a href="#1.2.+MySQL%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB">1.2. MySQLのインストール</a></h2>
<p>先述の1.1節で配置したインストーラーを用いてインストール開始する。</p>
<p>まずはインストーラを解凍し、解凍が無事に完了したらちゃんと解凍されていることを確認も行う。</p>
<pre><code>[root@mysql-master1 tmp]# rpm -Uvh mysql80-community-release-el8-4.noarch.rpm
warning: mysql80-community-release-el8-4.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:mysql80-community-release-el8-4 ################################# [100%]
[root@mysql-master1 tmp]# yum repolist enabled |grep 'mysql'
mysql-connectors-community MySQL Connectors Community
mysql-tools-community MySQL Tools Community
mysql80-community MySQL 8.0 Community Server
</code></pre>
<p>RHEL8などLinux8ベースのOSの場合のみ同梱されているMySQLの無効化を行う。</p>
<pre><code>[root@mysql-master1 tmp]# yum module disable mysql
MySQL 8.0 Community Server 70 MB/s | 2.3 MB 00:00
MySQL Connectors Community 5.8 MB/s | 74 kB 00:00
MySQL Tools Community 26 MB/s | 459 kB 00:00
Dependencies resolved.
=====================================================================================================================
Package Architecture Version Repository Size
=====================================================================================================================
Disabling modules:
mysql
Transaction Summary
=====================================================================================================================
Is this ok [y/N]: y
Complete!
</code></pre>
<p>MySQLのインストールを実施。</p>
<pre><code>[root@mysql-master1 tmp]# yum install mysql-community-server
Last metadata expiration check: 0:02:03 ago on Sat 23 Jul 2022 03:22:37 PM GMT.
Dependencies resolved.
=====================================================================================================================
Package Architecture Version Repository Size
=====================================================================================================================
Installing:
mysql-community-server x86_64 8.0.29-1.el8 mysql80-community 53 M
Installing dependencies:
mysql-community-client x86_64 8.0.29-1.el8 mysql80-community 14 M
mysql-community-client-plugins x86_64 8.0.29-1.el8 mysql80-community 2.4 M
mysql-community-common x86_64 8.0.29-1.el8 mysql80-community 636 k
mysql-community-icu-data-files x86_64 8.0.29-1.el8 mysql80-community 2.1 M
mysql-community-libs x86_64 8.0.29-1.el8 mysql80-community 1.5 M
Transaction Summary
=====================================================================================================================
Install 6 Packages
Total download size: 74 M
Installed size: 347 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): mysql-community-common-8.0.29-1.el8.x86_64.rpm 25 MB/s | 636 kB 00:00
(2/6): mysql-community-client-plugins-8.0.29-1.el8.x86_64.rpm 62 MB/s | 2.4 MB 00:00
(3/6): mysql-community-icu-data-files-8.0.29-1.el8.x86_64.rpm 76 MB/s | 2.1 MB 00:00
(4/6): mysql-community-libs-8.0.29-1.el8.x86_64.rpm 64 MB/s | 1.5 MB 00:00
(5/6): mysql-community-client-8.0.29-1.el8.x86_64.rpm 95 MB/s | 14 MB 00:00
(6/6): mysql-community-server-8.0.29-1.el8.x86_64.rpm 98 MB/s | 53 MB 00:00
---------------------------------------------------------------------------------------------------------------------
Total 123 MB/s | 74 MB 00:00
MySQL 8.0 Community Server 3.0 MB/s | 3.1 kB 00:00
Importing GPG key 0x3A79BD29:
Userid : "MySQL Release Engineering <[email protected]>"
Fingerprint: 859B E8D7 C586 F538 430B 19C2 467B 942D 3A79 BD29
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Is this ok [y/N]: y
Key imported successfully
MySQL 8.0 Community Server 1.9 MB/s | 1.9 kB 00:00
Importing GPG key 0x5072E1F5:
Userid : "MySQL Release Engineering <[email protected]>"
Fingerprint: A4A9 4068 76FC BD3C 4567 70C8 8C71 8D3B 5072 E1F5
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mysql-community-common-8.0.29-1.el8.x86_64 1/6
Installing : mysql-community-client-plugins-8.0.29-1.el8.x86_64 2/6
Installing : mysql-community-libs-8.0.29-1.el8.x86_64 3/6
Running scriptlet: mysql-community-libs-8.0.29-1.el8.x86_64 3/6
Installing : mysql-community-client-8.0.29-1.el8.x86_64 4/6
Installing : mysql-community-icu-data-files-8.0.29-1.el8.x86_64 5/6
Running scriptlet: mysql-community-server-8.0.29-1.el8.x86_64 6/6
Installing : mysql-community-server-8.0.29-1.el8.x86_64 6/6
Running scriptlet: mysql-community-server-8.0.29-1.el8.x86_64 6/6
Verifying : mysql-community-client-8.0.29-1.el8.x86_64 1/6
Verifying : mysql-community-client-plugins-8.0.29-1.el8.x86_64 2/6
Verifying : mysql-community-common-8.0.29-1.el8.x86_64 3/6
Verifying : mysql-community-icu-data-files-8.0.29-1.el8.x86_64 4/6
Verifying : mysql-community-libs-8.0.29-1.el8.x86_64 5/6
Verifying : mysql-community-server-8.0.29-1.el8.x86_64 6/6
Installed:
mysql-community-client-8.0.29-1.el8.x86_64 mysql-community-client-plugins-8.0.29-1.el8.x86_64
mysql-community-common-8.0.29-1.el8.x86_64 mysql-community-icu-data-files-8.0.29-1.el8.x86_64
mysql-community-libs-8.0.29-1.el8.x86_64 mysql-community-server-8.0.29-1.el8.x86_64
Complete!
</code></pre>
<p>最後まで進んでインストール完了したら、MySQLを起動させる。<br />
まずはステータスの確認から。</p>
<pre><code>[root@mysql-master1 tmp]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
</code></pre>
<p>インストールされており、サービスとしてenableとなっていることがわかる。<br />
それでは起動させる。</p>
<pre><code>[root@mysql-master1 tmp]# systemctl start mysqld
[root@mysql-master1 tmp]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-07-23 15:45:02 GMT; 1min 29s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 11752 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 11834 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 22765)
Memory: 495.4M
CGroup: /system.slice/mysqld.service
mq11834 /usr/sbin/mysqld
Jul 23 15:44:56 mysql-master1 systemd[1]: Starting MySQL Server...
Jul 23 15:45:02 mysql-master1 systemd[1]: Started MySQL Server.
</code></pre>
<p>スタートさせた後は必ずステータスを確認して、起動していることを確認すること。</p>
<h2 id="1.3 ログインしてパスワードを変更"><a href="#1.3+%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%97%E3%81%A6%E3%83%91%E3%82%B9%E3%83%AF%E3%83%BC%E3%83%89%E3%82%92%E5%A4%89%E6%9B%B4">1.3 ログインしてパスワードを変更</a></h2>
<p>rootユーザの初期パスワードはログに記載されているのでそこで確認し、自分の覚えやすいパスワードに変更していく。その後、他のユーザを作成してパスワード設定、権限設定を順次行う。</p>
<p>まずはrootユーザのパスワードを変更する。<code>/var/log/mysql.log</code>に記載されていることを確認。</p>
<pre><code>[root@mysql-master1 log]# cat /var/log/mysqld.log
~
2022-07-23T15:44:59.597480Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: xCkVmf#Yd2ci
~
</code></pre>
<p>上記をみると、<code>xCkVmf#Yd2ci</code>が初期パスワードとして設定されていることがわかる。<br />
ではログインして変更する。</p>
<pre><code>[root@mysql-master1 log]# mysql -u root -p
Enter password: ←ここにxCkVmf#Yd2ciを入力。表示はされないよ
~
mysql> alter user root@localhost identified by 'MyNewPass1!';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
</code></pre>
<p>これでrootユーザのパスワードがMyNewPass4!に変更された。<br />
再ログインしてみて、パスワード変更されたかを確認しておこう。<br />
パスワード変更コマンドはいくつかあるので紹介する。</p>
<pre><code>> set password for ユーザ名@ホスト名='任意password`;
現在ログインしてるユーザのパスワードを変更するには
> set password='任意passwword`;
</code></pre>
<p>ちなみに、パスワードポリシーはパスワード検証コンポーネントによって定義されている。これはデフォルトで有効になってる。内容は</p>
<pre><code>mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM |
| validate_password.special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.01 sec)
</code></pre>
<p>パスワード検証コンポーネントを無効化させるには</p>
<pre><code>mysql> uninstall component 'file://component_validate_password';
Query OK, 0 rows affected (0.00 sec)
</code></pre>
<p>逆に有効化させるには<code>install component 'file://component_validate_password';</code>を入力する。</p>
<h1 id="2. MySQLアーキテクチャ"><a href="#2.+MySQL%E3%82%A2%E3%83%BC%E3%82%AD%E3%83%86%E3%82%AF%E3%83%81%E3%83%A3">2. MySQLアーキテクチャ</a></h1>
<p><a href="https://crieit.now.sh/upload_images/cd28e3ad239066b2c265d44ba4be8d2062e1794497278.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/cd28e3ad239066b2c265d44ba4be8d2062e1794497278.png?mw=700" alt="innodb-architecture.png" /></a></p>
<h2 id="2.1 sampleデータ"><a href="#2.1+sample%E3%83%87%E3%83%BC%E3%82%BF">2.1 sampleデータ</a></h2>
<p>サンプルデータをインストールする。<br />
<a target="_blank" rel="nofollow noopener" href="https://dev.mysql.com/doc/index-other.html">https://dev.mysql.com/doc/index-other.html</a><br />
今回はworld databaseのzipファイルをローカルにダウンロードし、DBサーバに転送。zipファイルを解凍してからmysqlへインポートした。</p>
<pre><code>$ unzip world-db.zip
$ cd world-db/
$ mysql -uroot -proomysqld < world.sql
</code></pre>
<p>これでインポート完了したので中身の確認をしてみる。</p>
<pre><code>mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| world |
+--------------------+
5 rows in set (0.00 sec)
mysql> use world
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.01 sec)
</code></pre>
<p>そのほかにも自分でテーブルを作成してみる。</p>
<pre><code>mysql> create table `sample1` (
-> `code` char(3) not null default '', `population` int(11) not null default '0', primary key (`code`))
-> engine=InnoDB;
Query OK, 0 rows affected, 1 warning (0.02 sec)
</code></pre>
<p>インポートしたデータはデフォルトでは/var/lib/mysqlに配置される。</p>
<pre><code>[root@mysql-master1 mysql]# ll
total 189584
-rw-r-----. 1 mysql mysql 56 Jul 23 15:44 auto.cnf
-rw-r-----. 1 mysql mysql 736313 Jul 27 15:29 binlog.000001
-rw-r-----. 1 mysql mysql 16 Jul 23 15:45 binlog.index
-rw-------. 1 mysql mysql 1680 Jul 23 15:44 ca-key.pem
-rw-r--r--. 1 mysql mysql 1112 Jul 23 15:44 ca.pem
-rw-r--r--. 1 mysql mysql 1112 Jul 23 15:44 client-cert.pem
-rw-------. 1 mysql mysql 1676 Jul 23 15:44 client-key.pem
-rw-r-----. 1 mysql mysql 196608 Jul 27 15:31 '#ib_16384_0.dblwr'
-rw-r-----. 1 mysql mysql 8585216 Jul 23 15:44 '#ib_16384_1.dblwr'
-rw-r-----. 1 mysql mysql 5942 Jul 23 15:45 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jul 27 15:29 ibdata1 ←システムtableスペース
-rw-r-----. 1 mysql mysql 50331648 Jul 27 15:31 ib_logfile0 ←REDOログデータ
-rw-r-----. 1 mysql mysql 50331648 Jul 23 15:44 ib_logfile1 ←REDOログデータ
-rw-r-----. 1 mysql mysql 12582912 Jul 23 15:45 ibtmp1
drwxr-x---. 2 mysql mysql 187 Jul 23 15:45 '#innodb_temp'
drwxr-x---. 2 mysql mysql 143 Jul 23 15:44 mysql ←この配下に一般ログやslowログがある
-rw-r-----. 1 mysql mysql 25165824 Jul 27 15:29 mysql.ibd
srwxrwxrwx. 1 mysql mysql 0 Jul 23 15:45 mysql.sock
-rw-------. 1 mysql mysql 6 Jul 23 15:45 mysql.sock.lock
drwxr-x---. 2 mysql mysql 8192 Jul 23 15:44 performance_schema
-rw-------. 1 mysql mysql 1680 Jul 23 15:44 private_key.pem
-rw-r--r--. 1 mysql mysql 452 Jul 23 15:44 public_key.pem
-rw-r--r--. 1 mysql mysql 1112 Jul 23 15:44 server-cert.pem
-rw-------. 1 mysql mysql 1676 Jul 23 15:44 server-key.pem
drwxr-x---. 2 mysql mysql 28 Jul 23 15:45 sys
-rw-r-----. 1 mysql mysql 16777216 Jul 27 15:29 undo_001
-rw-r-----. 1 mysql mysql 16777216 Jul 27 15:31 undo_002
drwxr-x---. 2 mysql mysql 87 Jul 27 15:29 world ←インポートしたデータ
</code></pre>
<h2 id="2.2 システム変数設定"><a href="#2.2+%E3%82%B7%E3%82%B9%E3%83%86%E3%83%A0%E5%A4%89%E6%95%B0%E8%A8%AD%E5%AE%9A">2.2 システム変数設定</a></h2>
<p>MySQLサーバの設定はシステム変数で設定する。設定方法は、①my.cnfで設定した後に再起動を行う、②一時的な設定を行う(一部の変数しか設定変更できない)。</p>
<h3 id="2.2.1 my.cnfで設定"><a href="#2.2.1+my.cnf%E3%81%A7%E8%A8%AD%E5%AE%9A">2.2.1 my.cnfで設定</a></h3>
<p>設定ファイルは/etc/my.cnfが読み込まれることになっている。デフォルト設定を変更して任意のディレクトリに配置したmy.cnfを読み込ませたかったら以下を実施。複数ファイルを読み込んだら後勝ちになる。</p>
<pre><code>$ mysqld --verbose --help
$ mysqld --defaults-file=/usr/local/mysql/date/my.cnf &
</code></pre>
<p>設定ファイルを変更したら基本的にはmysqlの再起動を行う。</p>
<h3 id="2.2.2 一時的な設定"><a href="#2.2.2+%E4%B8%80%E6%99%82%E7%9A%84%E3%81%AA%E8%A8%AD%E5%AE%9A">2.2.2 一時的な設定</a></h3>
<p>参考:https://dev.mysql.com/doc/refman/8.0/ja/server-system-variables.html</p>
<p>MySQLを再起動かけられないときなどは、my.cnfの編集なしに設定値を変更できる変数がある。ただし、一時的に設定変更しても、再起動やmy.cnfのリロードを行うと一時的な設定が元に戻る点に注意。</p>
<p>設定方法は以下で行える。設定はセッション単位(LOCAL)、サーバ全体(GLOBAL)での変更が可能。</p>
<pre><code>> set [global|sesison] <変数> =<値>;
</code></pre>
<p>設定した値の確認方法は以下。</p>
<pre><code>show [global|session] variables like '%<システム変数名>%';
</code></pre>
<p>実際に設定されてるシステム変数を一括で確認するには</p>
<pre><code>$ mysqld --verbose --help
</code></pre>
<p>で確認できる。</p>
<p>一時的な設定変更を永続化させることができる。その場合は設定する際にpersistを付け加えればよい。</p>
<pre><code>> set persist max_connections = 500;
</code></pre>
<p>システム変数を永続化オプションをつけて設定した場合はmy.cnfとの設定に差異がでる。そういった場合に設定変更履歴を確認するにはperformance_schemaを参照すること。</p>
<pre><code>mysql> select * from performance_schema.variables_info where variable_source='persisted';
Empty set (0.00 sec)
</code></pre>
<h3 id="2.2.3 システム変数例"><a href="#2.2.3+%E3%82%B7%E3%82%B9%E3%83%86%E3%83%A0%E5%A4%89%E6%95%B0%E4%BE%8B">2.2.3 システム変数例</a></h3>
<p>nnodb_buffer_pool_sizeInno:DBバッファプールサイズ<br />
innodb_log_buffer_sizeInno:DBログバッファのサイズ(メモリ上)<br />
innodb_log_file_size:ログファイルのサイズ(ディスク上)<br />
innodb_log_files_in_groups:ログファイルの数(デフォルト2)<br />
binlog_expire_logs_seconds:バイナリログを削除するまでの期間を秒単位で指定。デフォルトは30日。</p>
<h1 id="3. レプリケーション設定"><a href="#3.+%E3%83%AC%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E8%A8%AD%E5%AE%9A">3. レプリケーション設定</a></h1>
<p>レプリケーションの設定は4手順ある。<br />
①レプリケーション用のパラメータを設定、<br />
②マスター側にレプリケーションユーザ作成、<br />
③マスター側のバックアップを取得し、スレーブ側にリストアする。そして、<br />
④スレーブ側でレプリケーションのためにマスター情報やポジション情報を設定しレプリケーションをスタートさせる。</p>
<h2 id="3.1 レプリケーション用のパラメータ設定"><a href="#3.1+%E3%83%AC%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E7%94%A8%E3%81%AE%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E8%A8%AD%E5%AE%9A">3.1 レプリケーション用のパラメータ設定</a></h2>
<p>マスター側で設定するパラメータ</p>
<pre><code>server-id
log_bin
datedir
gtid-mode
enforce-gtid-consistency = on
log-slave-updates
</code></pre>
<h2 id="3.2 ソースサーバにレプリケーションユーザ作成"><a href="#3.2+%E3%82%BD%E3%83%BC%E3%82%B9%E3%82%B5%E3%83%BC%E3%83%90%E3%81%AB%E3%83%AC%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%A6%E3%83%BC%E3%82%B6%E4%BD%9C%E6%88%90">3.2 ソースサーバにレプリケーションユーザ作成</a></h2>
<p><a target="_blank" rel="nofollow noopener" href="https://dev.mysql.com/doc/refman/8.0/ja/replication-howto-repuser.html">https://dev.mysql.com/doc/refman/8.0/ja/replication-howto-repuser.html</a><br />
ソースサーバにレプリケーション用のユーザを作成して、そのユーザにレプリケーションスレーブ権限を付与させる。</p>
<pre><code>mysql> create user repl@'10.1.%' identified by 'password';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to repl@'10.1.%';
Query OK, 0 rows affected (0.00 sec)
</code></pre>
<p>replユーザがどこから接続可能にしたいのかによって、<code>repl@'10.1.%'</code>の書き方は変わってくる。localhostにするとほかのサーバがreplユーザでアクセスできないので注意。ユーザを作成したらパスワードの設定も忘れずに。<br />
設定が完了したら、想定通りの挙動になっているかの確認を行う。<br />
まずはマスター側でユーザの確認。</p>
<pre><code>mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| repl | 10.1.% | 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.00 sec)
</code></pre>
<p>replユーザのhostが<code>10.1.%</code>になっていることを確認。つまり、localhostに設定されていないので、①マスター側で接続を試みてもログインできないが②スレーブサーバからはログインできるということになる。確認しよう。<br />
①</p>
<pre><code>[kiban@mysql-master1 ~]$ mysql -urepl -p
Enter password:
ERROR 1045 (28000): Access denied for user 'repl'@'localhost' (using password: YES)
</code></pre>
<p>②</p>
<pre><code>mysql -h10.1.2.148 -urepl -p
Enter password:
mysql>
</code></pre>
<p>①②を通じて、想定通りの設定が行われたことが確認できた。</p>
<h2 id="3.3 dump出力"><a href="#3.3+dump%E5%87%BA%E5%8A%9B">3.3 dump出力</a></h2>
<p>元から入ってるデータベース(システムデータベース)はレプリケーションされないので、それ以外(今回でいえばworld)だけエクスポートする。</p>
<pre><code>mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| world | ←これが出力対象
+--------------------+
</code></pre>
<p>以下でエクスポートする。</p>
<pre><code>mysqldump -uroot -p<パスワード入力> --master-data=2 --socket=/var/lib/mysql/mysql.sock --hex-blob --flush-logs --default-character-set=utf8mb4 --databases world --single-transaction --triggers --routines --events > mysql_bkup_dump_20220807.sql
</code></pre>
<p>出力されたダンプをレプリカサーバに転送して、インポートさせる。</p>
<pre><code>mysql -hlocalhost -uroot -roomysqld < mysql_bkup_dump_20220807.sql
</code></pre>
<p>インポート完了したらm、mysqlサーバにログインしてworldデータベースが存在することを確認する。</p>
<pre><code>mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| world |
+--------------------+
5 rows in set (0.00 sec)
</code></pre>
<p>あることがわかった。</p>
<p>【重要】<br />
レプリカサーバ側で。datedir配下にあるauto.cnfを削除する。</p>
<h2 id="3.4 レプリケーション情報のセット"><a href="#3.4+%E3%83%AC%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E6%83%85%E5%A0%B1%E3%81%AE%E3%82%BB%E3%83%83%E3%83%88">3.4 レプリケーション情報のセット</a></h2>
<p>レプリカサーバ側でレプリケーションを行うための情報として、ソースサーバの情報をセットする。</p>
<p>まずはマスター側の情報を取得する。</p>
<pre><code>mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| master-mysql-bin.000006 | 157 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
</code></pre>
<p>続いて、上記の情報をもとにスレーブ側で情報をセット。</p>
<pre><code>mysql> change master to master_host='<マスターサーバのIPアドレス>',
-> master_user='repl',
-> master_password='<replユーザのpassword>',
-> master_log_file='master-mysql-bin.000006',
-> master_log_pos=157;
Query OK, 0 rows affected, 8 warnings (0.02 sec)
</code></pre>
<p>セットが完了したら、想定通りの設定がされているかをスレーブ側で確認。</p>
<pre><code>mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: <マスターサーバのIPアドレス>
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-mysql-bin.000006
Read_Master_Log_Pos: 157
Relay_Log_File: mysql-slave1-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-mysql-bin.000006
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 157
Relay_Log_Space: 157
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
</code></pre>
<p>設定が想定通りになっていることが確認できたので、いよいよレプリケーションを始める。</p>
<pre><code>mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
</code></pre>
<p>レプリケーションを開始すると、slaveのステータスを再度確認する。</p>
<pre><code>mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to source
Master_Host: 10.1.2.148
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-mysql-bin.000006
Read_Master_Log_Pos: 157
Relay_Log_File: mysql-slave1-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-mysql-bin.000006
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 157
Relay_Log_Space: 157
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2003
Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 1 message: Can't connect to MySQL server on '10.1.2.148:3306' (110)
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 220806 21:33:27
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
</code></pre>
<p>本来なら、マスターサーバのバイナリログを取り込んでリレーログにする<code>Slave_IO_Running</code>と、取り込まれたリレーログからイベントを取り出してデータベースに反映させる<code>Slave_SQL_Running</code>のステータスがともに<code>YES</code>になるはずである。しかし、<code>Slave_IO_Running</code>が<code>Connecting</code>になっている。</p>
<p>そこで、スレーブサーバからマスターサーバの3306番ポートへ接続できるか確認するために<code>nc -vz マスターのIP 3306</code>を実行したところ、接続できなかった。</p>
<pre><code>$ nc -vz 10.1.2.58 3306
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: No route to host.
</code></pre>
<p>ネットワーク設定に問題がないことは確認済みなので、サーバのfierwall設定を行う。</p>
<pre><code># firewall-cmd --zone=public --add-port=3306/tcp --permanet
# firewall-cmd --reload
# nc -vz 10.1.2.58 3306
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connected to 10.1.2.58:3306.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.
</code></pre>
<p>3306番ポートへ接続できることを確認。</p>
<p>スレーブのステータスを確認すると、</p>
<pre><code>Slave_IO_Running: Yes
</code></pre>
<p>となってた。<br />
レプリケーションされてることが確認できたので、マスター側で変更したデータがスレーブ側に連携されるかテストする。<br />
まずはマスター側でスキーマ作成。</p>
<pre><code>mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| world |
+--------------------+
5 rows in set (0.01 sec)
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| world |
+--------------------+
6 rows in set (0.00 sec)
</code></pre>
<p>続いて、スレーブ側でtest1スキーマが作成されていることを確認。</p>
<pre><code>mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| world |
+--------------------+
6 rows in set (0.00 sec)
</code></pre>
<p>ちゃんと作成されていた。<br />
これでレプリケーションが正常にされていることがわかった。</p>
<h1 id="4.MySQLチューニング"><a href="#4.MySQL%E3%83%81%E3%83%A5%E3%83%BC%E3%83%8B%E3%83%B3%E3%82%B0">4.MySQLチューニング</a></h1>
<h2 id="4.1"><a href="#4.1">4.1</a></h2>
<p><code>innodb_log_file_size</code>ログファイルのサイズ(ディスク上)。<code>innodb_buffer_pool_size</code>の25~100%ぐらい。</p>
kawai_mizugorou