ããã¯ããªã«ãããããŠæžãããã®ïŒ
MySQL 8.4ã«ãªã£ãŠããã¬ããªã±ãŒã·ã§ã³ãè¡ãç°å¢ãäœã£ãããšããªãã£ãã®ã§ãèšå®ããŠããããšããããšã§ã
MySQLã®ã¬ããªã±ãŒã·ã§ã³
MySQL 8.4ã®ã¬ããªã±ãŒã·ã§ã³ã«é¢ããããã¥ã¡ã³ãã¯ãã¡ãã
MySQL :: MySQL 8.4 Reference Manual :: 19 Replication
MySQLã®ã¬ããªã±ãŒã·ã§ã³ã«ã¯ããã€ããªãŒãã°ã®ããžã·ã§ã³ã«ããšã¥ããã®ãšã
Global Transaction IdentifiersïŒGTIDïŒã䜿ã£ããã®ã®2çš®é¡ããããŸãã
- MySQL :: MySQL 8.4 Reference Manual :: 19.1.1 Binary Log File Position Based Replication Configuration Overview
- MySQL :: MySQL 8.4 Reference Manual :: 19.1.3 Replication with Global Transaction Identifiers
ä»åã¯GTIDã䜿ã£ãã¬ããªã±ãŒã·ã§ã³ã§æ§æããŸãã
ã¬ããªã±ãŒã·ã§ã³ã®èšå®æ¹æ³ã¯ãã¡ãã
MySQL :: MySQL 8.4 Reference Manual :: 19.1.3.4 Setting Up Replication Using GTIDs
ã¬ããªã±ãŒã·ã§ã³ã«é¢ããSQLã¯ãã¡ãã
MySQL :: MySQL 8.4 Reference Manual :: 15.4 Replication Statements
ãã®ãããã¯å®éã«ã¬ããªã±ãŒã·ã§ã³ãæ§æããæã«åºãŠããŸãããããã§ã¯GTIDã䜿ã£ãã¬ããªã±ãŒã·ã§ã³ã§ã®
å¶éäºé
ãèŠãŠãããŸãã
MySQL :: MySQL 8.4 Reference Manual :: 19.1.3.7 Restrictions on Replication with GTIDs
GTIDã䜿ã£ãã¬ããªã±ãŒã·ã§ã³ã§ã¯ã以äžã®å¶éããããšãããŠããŸãã
- éãã©ã³ã¶ã¯ã·ã§ãã«ãªã¹ãã¬ãŒãžãšã³ãžã³ã®ããŒãã«ã®æŽæ°ãšããã©ã³ã¶ã¯ã·ã§ãã«ãªã¹ãã¬ãŒãžãšã³ãžã³ã®ããŒãã«ã®æŽæ°ãåãã¹ããŒãã¡ã³ããŸãã¯ãã©ã³ã¶ã¯ã·ã§ã³ã§è¡ããªã
- ã¢ãããã¯DDLããµããŒãããã¹ãã¬ãŒãžãšã³ãžã³ïŒInnoDBïŒã®å Žåã
CREATE TABLE ... SELECTã¯ã²ãšã€ã®ãã©ã³ã¶ã¯ã·ã§ã³ãšããŠãã€ããªãŒãã°ã«èšé²ããã binlog_formatãSTATEMENTã«èšå®ãããŠããŠããµãŒããŒäžã§GTIDã䜿çšããŠããå Žåããã©ã³ã¶ã¯ã·ã§ã³å ã§CREATE TEMPORARY TABLEããã³DROP TEMPORARY TABLEã®äœ¿çšäžå¯- ãµããŒããããŠããªãSQLã®å®è¡ããšã©ãŒã§å€±æããã
sql_replica_skip_counterã䜿çšããã€ãã³ãæ°ãæå®ããã¹ãããã®äžå¯IGNORE_SERVER_IDSã®äœ¿çšäžå¯
ã¢ãããã¯DDLãšã¯ïŒãšããæ°ã«ãªããŸããããããŒã¿ãã£ã¯ã·ã§ããªãŒã®æŽæ°ãã¹ãã¬ãŒãžãšã³ãžã³ã®æäœã
ãã€ããªãŒãã°ã®æäœãåäžã®ã¢ãããã¯æäœãšããDDLã®ããšãããã§ãã
MySQL :: MySQL 8.4 Reference Manual :: 15.1.1 Atomic Data Definition Statement Support
ãŸããã¢ãããã¯DDLã¯ãã©ã³ã¶ã¯ã·ã§ã³DDLã§ã¯ãªãããã䜿çšãããšãã®æç¹ã§ã¢ã¯ãã£ããªãã©ã³ã¶ã¯ã·ã§ã³ã
ãããšæé»çã«ã³ãããããŸãã
èŠãŠã¿ããšããã䜿ããããªDDLã¯ã¢ãããã¯DDLã®ããã§ããâŠã
第93回 MySQL8.0のデータディクショナリー | gihyo.jp
DDLã®å®è¡éäžã§äžæãããããå Žåã«ãäžéå端ãªç¶æ ã«ãªããªãããã«ããä»çµã¿ã®ããã§ãã
ããã§ã¯ããã¥ã¡ã³ããèŠãã®ã¯ãããããã«ããŠãå®éã«GTIDã䜿ã£ãã¬ããªã±ãŒã·ã§ã³ãæ§ç¯ããŠã¿ãŸãããã
ç°å¢
ä»åã®ç°å¢ã¯ãã¡ãã2ã€ã®Ubuntu Linux 24.04 LTSã®ãµãŒããŒãçšæããŸãã
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 24.04.2 LTS Release: 24.04 Codename: noble $ uname -srvmpio Linux 6.8.0-57-generic #59-Ubuntu SMP PREEMPT_DYNAMIC Sat Mar 15 17:40:59 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
IPã¢ãã¬ã¹ã¯ãããã192.168.33.10ã192.168.33.11ãšããŸãã
192.168.33.10ã¯Sourceã192.168.33.11ã¯Replicaã«ããããšã«ããŸãã
MySQLãã€ã³ã¹ããŒã«ãã
ã²ãšãŸãMySQLãã€ã³ã¹ããŒã«ããŸãããã
$ curl -LO https://dev.mysql.com/get/mysql-apt-config_0.8.33-1_all.deb $ sudo dpkg -i mysql-apt-config_0.8.33-1_all.deb $ sudo apt update $ sudo apt install mysql-server
æäœã¯MySQL Shellããè¡ãããšã«ããŸãã
$ sudo apt install mysql-shell
ä»åã¯MySQL 8.4.4ãã€ã³ã¹ããŒã«ãããŸããã
$ mysqlsh --version mysqlsh Ver 8.4.4 for Linux on x86_64 - for MySQL 8.4.4 (MySQL Community Server (GPL)) $ mysqlsh root@localhost MySQL localhost:33060+ ssl SQL > select version(); +-----------+ | version() | +-----------+ | 8.4.4 | +-----------+ 1 row in set (0.0008 sec)
GTIDã¬ããªã±ãŒã·ã§ã³ãæ§æãã
ã§ã¯ãã¬ããªã±ãŒã·ã§ã³ãæ§æããŠãããŸãããã
æåã«MySQLãµãŒããŒã¯ã2ã€ãšã忢ããŠãããŸãã
$ sudo systemctl stop mysql
MySQLã®èšå®ã倿ŽããŸããããã©ã«ãã®èšå®ã¯ãããªæãã§ãã
$ grep -v '#' /etc/mysql/mysql.conf.d/mysqld.cnf [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql log-error = /var/log/mysql/error.log
Sourceã®ã»ããã¢ãã
Sourceã®èšå®ã¯ãã®ããã«ããŸããã
[mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql log-error = /var/log/mysql/error.log character_set_server = utf8mb4 collation_server = utf8mb4_0900_bin transaction_isolation = READ-COMMITTED gtid_mode = on enforce_gtid_consistency = on log_bin = binlog sync_binlog = 1 binlog_expire_logs_seconds = 864000 server_id = 1
ã¡ãªã¿ã«ããã®éšåã¯ã¬ããªã±ãŒã·ã§ã³ã«é¢ä¿ãããŸããã
character_set_server = utf8mb4 collation_server = utf8mb4_0900_bin transaction_isolation = READ-COMMITTED
ãŸãbinlog_expire_logs_secondsãèšå®ããã®ã¯ãªããšãªãã§ãã
MySQLãèµ·åã
$ sudo systemctl start mysql
ã¬ããªã±ãŒã·ã§ã³çšã®ãŠãŒã¶ãŒãäœæã
$ mysqlsh root@localhost
MySQL localhost:33060+ ssl SQL > create user repl_user@'%' identified by 'password'; Query OK, 0 rows affected (0.0633 sec) MySQL localhost:33060+ ssl SQL > grant replication slave on *.* to 'repl_user'@'%'; Query OK, 0 rows affected (0.0179 sec)
æš©éã«ã€ããŠã¯ãæªã ã«"slave"ã®ããã§ãã
ãã€ããªãŒãã°ã®ç¢ºèªã
MySQL localhost:33060+ ssl SQL > show binary log status\G
*************************** 1. row ***************************
File: binlog.000003
Position: 700
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: c6a5cdba-17a7-11f0-aaeb-525400d48b63:1-2
1 row in set (0.0014 sec)
ããã§Sourceã®ã»ããã¢ããã¯å®äºã§ãã
Replicaã®ã»ããã¢ãã
ç¶ããŠã¯Replicaã®ã»ããã¢ããã§ãã
[mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql log-error = /var/log/mysql/error.log character_set_server = utf8mb4 collation_server = utf8mb4_0900_bin transaction_isolation = READ-COMMITTED gtid_mode = on enforce_gtid_consistency = on log_bin = binlog sync_binlog = 1 binlog_expire_logs_seconds = 864000 server_id = 2 read_only = on relay_log = relay-binlog
Sourceãšã®éãã¯server_idã®å€ãšãread_onlyã§ããã
MySQLãèµ·åã
$ sudo systemctl start mysql
MySQLã«æ¥ç¶ããŸãã
$ mysqlsh root@localhost
ã¬ããªã±ãŒã·ã§ã³ã®èšå®ã
MySQL localhost:33060+ ssl SQL > change replication source to source_host = '192.168.33.10', source_port = 3306, source_auto_position = 1, source_ssl = 1; Query OK, 0 rows affected (0.0593 sec)
MySQL :: MySQL 8.4 Reference Manual :: 15.4.2.2 CHANGE REPLICATION SOURCE TO Statement
ãã®æç¹ã§ã¯ã¬ããªã±ãŒã·ã§ã³ã¯åäœããŠããŸããã
MySQL localhost:33060+ ssl SQL > show replica status\G *************************** 1. row *************************** Replica_IO_State: Source_Host: 192.168.33.10 Source_User: Source_Port: 3306 Connect_Retry: 60 Source_Log_File: Read_Source_Log_Pos: 4 Relay_Log_File: relay-binlog.000001 Relay_Log_Pos: 4 Relay_Source_Log_File: Replica_IO_Running: No Replica_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_Source_Log_Pos: 0 Relay_Log_Space: 158 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Source_SSL_Allowed: Yes Source_SSL_CA_File: Source_SSL_CA_Path: Source_SSL_Cert: Source_SSL_Cipher: Source_SSL_Key: Seconds_Behind_Source: NULL Source_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Source_Server_Id: 0 Source_UUID: Source_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Replica_SQL_Running_State: Source_Retry_Count: 10 Source_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Source_SSL_Crl: Source_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Source_TLS_Version: Source_public_key_path: Get_Source_public_key: 0 Network_Namespace: 1 row in set (0.0020 sec)
Replicaã®éå§ã
MySQL localhost:33060+ ssl SQL > start replica user = 'repl_user' password = 'password'; Query OK, 0 rows affected, 1 warning (0.1097 sec) Note (code 1759): Sending passwords in plain text without SSL/TLS is extremely insecure.
MySQL :: MySQL 8.4 Reference Manual :: 15.4.2.4 START REPLICA Statement
ããã¥ã¡ã³ãã®ããã«change replication source to ãã«ãŠãŒã¶ãŒåãšãã¹ã¯ãŒããå«ãããšã以äžã®ããã«
start replicaã«å«ããããã«èŠåãããã®ã§ããã
Note (code 1760): Storing MySQL user name or password information in the connection metadata repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START REPLICA; see the 'START REPLICA Syntax' in the MySQL Manual for more information.
start replicaã«å
¥ãããå
¥ããã§ããã£ã±ãèŠåãããã®ã§ããâŠã
Note (code 1759): Sending passwords in plain text without SSL/TLS is extremely insecure.
ã¬ããªã±ãŒã·ã§ã³ã¹ããŒã¿ã¹ã®ç¢ºèªã
MySQL localhost:33060+ ssl SQL > show replica status\G *************************** 1. row *************************** Replica_IO_State: Waiting for source to send event Source_Host: 192.168.33.10 Source_User: repl_user Source_Port: 3306 Connect_Retry: 60 Source_Log_File: binlog.000003 Read_Source_Log_Pos: 700 Relay_Log_File: relay-binlog.000002 Relay_Log_Pos: 911 Relay_Source_Log_File: binlog.000003 Replica_IO_Running: Yes Replica_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_Source_Log_Pos: 700 Relay_Log_Space: 1119 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Source_SSL_Allowed: Yes Source_SSL_CA_File: Source_SSL_CA_Path: Source_SSL_Cert: Source_SSL_Cipher: Source_SSL_Key: Seconds_Behind_Source: 0 Source_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Source_Server_Id: 1 Source_UUID: c6a5cdba-17a7-11f0-aaeb-525400d48b63 Source_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates Source_Retry_Count: 10 Source_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Source_SSL_Crl: Source_SSL_Crlpath: Retrieved_Gtid_Set: c6a5cdba-17a7-11f0-aaeb-525400d48b63:1-2 Executed_Gtid_Set: c6a5cdba-17a7-11f0-aaeb-525400d48b63:1-2 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Source_TLS_Version: Source_public_key_path: Get_Source_public_key: 0 Network_Namespace: 1 row in set (0.0017 sec)
ã¬ããªã±ãŒã·ã§ã³ãéå§ãããããã§ãã
確èª
æåŸã«åäœç¢ºèªã
Sourceã«æ¥ç¶ããŠãããŒã¿ããŒã¹ããŠãŒã¶ãŒãäœæã
$ mysqlsh root@localhost MySQL localhost:33060+ ssl SQL > create database example; Query OK, 1 row affected (0.0318 sec) MySQL localhost:33060+ ssl SQL > create user kazuhira@'%' identified by 'password'; Query OK, 0 rows affected (0.0490 sec) MySQL localhost:33060+ ssl SQL > grant all privileges on example.* to 'kazuhira'@'%'; Query OK, 0 rows affected (0.0252 sec) MySQL localhost:33060+ ssl SQL > flush privileges; Query OK, 0 rows affected (0.0149 sec)
äœæãããŠãŒã¶ãŒã§æ¥ç¶ããªããã
$ mysqlsh kazuhira@localhost/example
ããŒãã«ãšããŒã¿ã®äœæã
MySQL localhost:33060+ ssl example SQL > create table t1(c1 varchar(20)); Query OK, 0 rows affected (0.1023 sec) MySQL localhost:33060+ ssl example SQL > insert into t1(c1) values('foo'); Query OK, 1 row affected (0.0328 sec) MySQL localhost:33060+ ssl example SQL > insert into t1(c1) values('bar'); Query OK, 1 row affected (0.0218 sec) MySQL localhost:33060+ ssl example SQL > select * from t1; +-----+ | c1 | +-----+ | foo | | bar | +-----+ 2 rows in set (0.0015 sec)
Replicaã«æ¥ç¶ã
$ mysqlsh kazuhira@localhost/example
ããŒã¿ãã¬ããªã±ãŒã·ã§ã³ãããŠããããšã確èªã§ããŸãã
MySQL localhost:33060+ ssl example SQL > select * from t1; +-----+ | c1 | +-----+ | foo | | bar | +-----+ 2 rows in set (0.0019 sec)
ãªããReplicaã¯Read Onlyã§ãã
MySQL localhost:33060+ ssl example SQL > create table t2(c2 varchar(20)); ERROR: 1290: The MySQL server is running with the --read-only option so it cannot execute this statement
ããã§ç¢ºèªã§ããŸããã
ãããã«
MySQL 8.4ã§GTIDã¬ããªã±ãŒã·ã§ã³ã®èšå®ãããŠã¿ãŸããã
8.0ã®æãšå€ãã£ãŠããŸããã§ããããæã ããããã®ã確èªããªããšãªããšæããŸãã