🦝

(作成中)MySQLレプリケーションとチューニング

2022/02/26に公開

Dockerで環境構築

https://github.com/snarai/study-mysql-tuning

レプリケーション

マスターのレプリケーション設定

レプリケーションユーザ作成・権限設定

my.cnf設定

マスターのロック・ダンプ取得

スレーブのレプリケーション設定

my.cnf設定

ダンプをインポート

スレーブ設定

スレーブ開始

MySQL再起動

マスターのロック解除

レプリケーション動作確認

マスタでデータを更新する

mysql> insert into users ( id, name ) values ( 99999, 'Takeda' );
mysql> QUIT

スレーブに反映されているか確認する

mysql> select * from isucomp.users;
mysql> QUIT

実行状況を確認

mysql> SHOW SLAVE STATUS\G
で Read_Master_log_Pos と Exec_Master_log_Pos が同値になっている

MySQL Tuner

wgetで入れたいところだが、この環境には入っていないのでzipを外からダウンロードしてインストール。
root@e77c5112654d:/# cd /var/www/MySQLTuner-perl-master
root@e77c5112654d:/var/www/MySQLTuner-perl-master# unzip MySQLTuner-perl-master.zip

MySQL Tuner 実行結果

root@e77c5112654d:/var/www/MySQLTuner-perl-master# perl mysqltuner.pl --host db --port 3306 --user docker --pass=docker --forcemem 8
 >>  MySQLTuner 1.9.4
	 * Jean-Marie Renouard <jmrenouard@gmail.com>
	 * Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.pl/
 >>  Run with '--help' for additional options and output filtering

[--] Skipped version check for MySQLTuner script
[--] Performing tests on db:3306
[OK] Logged in using credentials passed on the command line
[--] Assuming 8 MB of physical memory
[!!] Assuming 0 MB of swap space (use --forceswap to specify)
[!!] failed to execute: SHOW REPLICA STATUS\G
[!!] FAIL Execute SQL / return code: 256
[!!] failed to execute: SHOW SLAVE STATUS
[!!] FAIL Execute SQL / return code: 256
[OK] Currently running supported MySQL version 8.0.28
 
-------- Log file Recommendations ------------------------------------------------------------------
[!!] log_error is set to stderr MT can't read stderr
 
-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA 
[!!] InnoDB is enabled but isn't being used
[OK] Total fragmented tables: 0
 
-------- Analysis Performance Metrics --------------------------------------------------------------
[--] innodb_stats_on_metadata: OFF
[OK] No stat updates during querying INFORMATION_SCHEMA.
 
-------- Views Metrics -----------------------------------------------------------------------------
 
-------- Triggers Metrics --------------------------------------------------------------------------
 
-------- Routines Metrics --------------------------------------------------------------------------
 
-------- Security Recommendations ------------------------------------------------------------------
[--] Skipped due to unsupported feature for MySQL 8
 
-------- CVE Security Recommendations --------------------------------------------------------------
[OK] NO SECURITY CVE FOUND FOR YOUR VERSION
[!!] failed to execute: SHOW ENGINE PERFORMANCE_SCHEMA STATUS
[!!] FAIL Execute SQL / return code: 256
[!!] failed to execute: SHOW ENGINE PERFORMANCE_SCHEMA STATUS
[!!] FAIL Execute SQL / return code: 256
 
-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 5m 44s (10 q [0.029 qps], 13 conn, TX: 58K, RX: 1K)
[--] Reads / Writes: 100% / 0%
[--] Binary logging is enabled (GTID MODE: OFF)
[--] Physical Memory     : 8.0M
[--] Max MySQL memory    : 9.9G
[--] Other process memory: 0B
[--] Total buffers: 168.0M global + 65.9M per thread (151 max threads)
[!!] failed to execute: SHOW ENGINE PERFORMANCE_SCHEMA STATUS
[!!] FAIL Execute SQL / return code: 256
[--] P_S Max memory usage: 0B
[!!] failed to execute: SHOW ENGINE PERFORMANCE_SCHEMA STATUS
[!!] FAIL Execute SQL / return code: 256
[!!] failed to execute: SHOW ENGINE PERFORMANCE_SCHEMA STATUS
[!!] FAIL Execute SQL / return code: 256
[--] Galera GCache Max memory usage: 0B
[!!] Maximum reached memory usage: 233.9M (2923.44% of installed RAM)
[!!] Maximum possible memory usage: 9.9G (126439.06% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (0/10)
[OK] Highest usage of available connections: 0% (1/151)
[OK] Aborted connections: 0.00%  (0/13)
[--] Query cache have been removed in MySQL 8
[OK] No Sort requiring temporary tables
[OK] No joins without indexes
[OK] Temporary tables created on disk: 0% (0 on disk / 4 total)
[OK] Thread cache hit rate: 92% (1 created / 13 connections)
[OK] Table cache hit rate: 93% (2K hits / 3K requests)
[OK] table_definition_cache(2000) is upper than number of tables(79)
[OK] Open file limit used: 0% (2/1M)
[OK] Table locks acquired immediately: 100% (4 immediate / 4 locks)
[OK] Binlog cache memory access: 0% (0 Memory / 0 Total)
 
-------- Performance schema ------------------------------------------------------------------------
[--] Performance_schema is activated.
[!!] failed to execute: SHOW ENGINE PERFORMANCE_SCHEMA STATUS
[!!] FAIL Execute SQL / return code: 256
[--] Memory used by P_S: 0B
[--] Sys schema isn't installed.
 
-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is disabled.
 
-------- MyISAM Metrics ----------------------------------------------------------------------------
[--] MyISAM Metrics are disabled on last MySQL versions.
 
-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[!!] No tables are Innodb
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 128.0M/0B
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (75 %): 48.0M * 2/128.0M should be equal to 25%
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[!!] InnoDB Read buffer efficiency: 84.81% (12745 hits/ 15027 total)
[OK] InnoDB Write log efficiency: 94.31% (613 hits/ 650 total)
[OK] InnoDB log waits: 0.00% (0 waits / 37 writes)
 
-------- Aria Metrics ------------------------------------------------------------------------------
[--] Aria Storage Engine not available.
 
-------- TokuDB Metrics ----------------------------------------------------------------------------
[--] TokuDB is disabled.
 
-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.
 
-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.
 
-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] Binlog format: ROW
[--] XA support enabled: ON
[--] Semi synchronous replication Master: Not Activated
[--] Semi synchronous replication Slave: Not Activated
[--] This is a standalone server
 
-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Add skip-innodb to MySQL configuration to disable InnoDB
    MySQL was started within the last 24 hours - recommendations may be inaccurate
    Reduce your overall MySQL memory footprint for system stability
    Dedicate this server to your database for highest performance.
    Consider installing Sys schema from https://github.com/mysql/mysql-sys for MySQL
    Consider installing Sys schema from https://github.com/FromDual/mariadb-sys for MariaDB
    Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: https://bit.ly/2TcGgtU
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    innodb_log_file_size should be (=16M) if possible, so InnoDB total log files size equals to 25% of buffer pool size.

my.cnf設定修正

Discussion