MySQL Replication 是 MySQL 一个非常重要的功能,主要用于主服务器和从服务期之间的数据复制操作。
MySQL 从 3.25.15 版本开始提供数据库复制(replication)功能。MySQL 复制是指从一个 MySQL 主服务器(master)将数据复制到另一台或多台 MySQL 从服务器(slaves)的过程,将主数据库的 DDL 和 DML 操作通过二进制日志传到从服务器上,然后在从服务器上对这些日志重新执行,从而使得主从服务器的数据保持同步。
在 MySQL 中,复制操作是异步进行的,slaves 服务器不需要持续地保持连接接收 master 服务器的数据。
MySQL 支持一台主服务器同时向多台从服务器进行复制操作,从服务器同时可以作为其他从服务器的主服务器,如果 MySQL 主服务器访问量比较大,可以通过复制数据,然后在从服务器上进行查询操作,从而降低主服务器的访问压力,同时从服务器作为主服务器的备份,可以避免主服务器因为故障数据丢失的问题。
2025-04-14 01:10:32 2025-04-13 17:10:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 9.2.0-1.el9 started. 2025-04-14 01:10:32 2025-04-13 17:10:32+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' 2025-04-14 01:10:32 2025-04-13 17:10:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 9.2.0-1.el9 started. 2025-04-14 01:10:32 2025-04-13 17:10:32+00:00 [Note] [Entrypoint]: Initializing database files 2025-04-14 01:10:34 2025-04-13 17:10:34+00:00 [Note] [Entrypoint]: Database files initialized 2025-04-14 01:10:34 2025-04-13 17:10:34+00:00 [Note] [Entrypoint]: Starting temporary server 2025-04-14 01:10:35 2025-04-13 17:10:35+00:00 [Note] [Entrypoint]: Temporary server started. 2025-04-14 01:10:35 '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock' 2025-04-14 01:10:36 2025-04-14 01:10:36 2025-04-13 17:10:36+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql 2025-04-14 01:10:36 2025-04-14 01:10:36 2025-04-14 01:10:36 2025-04-13 17:10:36+00:00 [Note] [Entrypoint]: Stopping temporary server 2025-04-14 01:10:38 2025-04-13 17:10:38+00:00 [Note] [Entrypoint]: Temporary server stopped 2025-04-14 01:10:38 2025-04-14 01:10:38 2025-04-13 17:10:38+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up. 2025-04-14 01:10:38 2025-04-14 01:10:32 2025-04-13T17:10:32.829404Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start. 2025-04-14 01:10:32 2025-04-13T17:10:32.830098Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 9.2.0) initializing of server in progress as process 79 2025-04-14 01:10:32 2025-04-13T17:10:32.832825Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2025-04-14 01:10:32 2025-04-13T17:10:32.914388Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2025-04-14 01:10:33 2025-04-13T17:10:33.415821Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. 2025-04-14 01:10:34 2025-04-13T17:10:34.698740Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end. 2025-04-14 01:10:34 2025-04-13T17:10:34.734155Z 0 [System] [MY-015015] [Server] MySQL Server - start. 2025-04-14 01:10:34 2025-04-13T17:10:34.878050Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 9.2.0) starting as process 120 2025-04-14 01:10:34 2025-04-13T17:10:34.885613Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2025-04-14 01:10:34 2025-04-13T17:10:34.969908Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2025-04-14 01:10:35 2025-04-13T17:10:35.105640Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. 2025-04-14 01:10:35 2025-04-13T17:10:35.105660Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel. 2025-04-14 01:10:35 2025-04-13T17:10:35.107053Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. 2025-04-14 01:10:35 2025-04-13T17:10:35.117643Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock 2025-04-14 01:10:35 2025-04-13T17:10:35.117699Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '9.2.0' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL. 2025-04-14 01:10:35 Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it. 2025-04-14 01:10:35 Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it. 2025-04-14 01:10:35 Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it. 2025-04-14 01:10:36 Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it. 2025-04-14 01:10:36 Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it. 2025-04-14 01:10:36 Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it. 2025-04-14 01:10:36 2025-04-13T17:10:36.242466Z 11 [Warning] [MY-010604] [Repl] Neither --relay-log nor --relay-log-index were used; so replication may break when this MySQL server acts as a replica and has his hostname changed!! Please use '--relay-log=mysql-slave-1-relay-bin' to avoid this problem. 2025-04-14 01:10:36 2025-04-13T17:10:36.244511Z 11 [System] [MY-010597] [Repl] 'CHANGE REPLICATION SOURCE TO FOR CHANNEL '' executed'. Previous state source_host='', source_port= 3306, source_log_file='', source_log_pos= 4, source_bind=''. New state source_host='mysql-master', source_port= 3306, source_log_file='binlog.000002', source_log_pos= 158, source_bind=''. 2025-04-14 01:10:36 2025-04-13T17:10:36.250291Z 12 [Warning] [MY-010897] [Repl] 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. 2025-04-14 01:10:36 2025-04-13T17:10:36.259416Z 12 [System] [MY-014001] [Repl] Replica receiver thread for channel '': connected to source 'sync_user@mysql-master:3306' with server_uuid=7c1315d5-1889-11f0-8558-5ebe6b5171aa, server_id=1. Starting replication from file 'binlog.000002', position '158'. 2025-04-14 01:10:36 2025-04-13T17:10:36.265450Z 18 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 9.2.0). 2025-04-14 01:10:37 2025-04-13T17:10:37.538090Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 9.2.0) MySQL Community Server - GPL. 2025-04-14 01:10:37 2025-04-13T17:10:37.538106Z 0 [System] [MY-015016] [Server] MySQL Server - end. 2025-04-14 01:10:38 2025-04-13T17:10:38.274697Z 0 [System] [MY-015015] [Server] MySQL Server - start. 2025-04-14 01:10:38 2025-04-13T17:10:38.406387Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 9.2.0) starting as process 1 2025-04-14 01:10:38 2025-04-13T17:10:38.408957Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2025-04-14 01:10:38 2025-04-13T17:10:38.466741Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2025-04-14 01:10:38 2025-04-13T17:10:38.595740Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. 2025-04-14 01:10:38 2025-04-13T17:10:38.595758Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel. 2025-04-14 01:10:38 2025-04-13T17:10:38.597260Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. 2025-04-14 01:10:38 2025-04-13T17:10:38.607365Z 0 [Warning] [MY-010604] [Repl] Neither --relay-log nor --relay-log-index were used; so replication may break when this MySQL server acts as a replica and has his hostname changed!! Please use '--relay-log=mysql-slave-1-relay-bin' to avoid this problem. 2025-04-14 01:10:38 2025-04-13T17:10:38.609950Z 5 [Warning] [MY-010897] [Repl] 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. 2025-04-14 01:10:38 2025-04-13T17:10:38.612440Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock 2025-04-14 01:10:38 2025-04-13T17:10:38.612482Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '9.2.0' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL. 2025-04-14 01:10:38 2025-04-13T17:10:38.618029Z 5 [System] [MY-014001] [Repl] Replica receiver thread for channel '': connected to source 'sync_user@mysql-master:3306' with server_uuid=7c1315d5-1889-11f0-8558-5ebe6b5171aa, server_id=1. Starting replication from file 'binlog.000002', position '158'.
进入从节点容器:
1
$ docker exec -it mysql-slave-1 /bin/bash
1
$ mysql -uroot -p
使用 show replica status \G; 查询从节点在状态,从查询结果中可以看到在启动容器之前在从节点初始化 SQL 中指定的 binlog 文件 binlog.000002 和开始复制位置 158:
Replica I/O for channel '': Error connecting to source 'sync_user@mysql-master-1:3306'. This was attempt 1/10, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
# usage: docker_process_init_files [file [file [...]]] # ie: docker_process_init_files /always-initdb.d/* # process initializer files, based on file extensions docker_process_init_files() { # mysql here for backwards compatibility "${mysql[@]}" mysql=( docker_process_sql )