本篇文章从零开始搭建 MySQL 主从个复制的架构~~
由于搭建过程与 MySQL 版本有很大的关系,所以本文基于 MySQL 8.0.33
主库 IP:192.168.1.1
从库 IP:192.168.1.2
下面步骤都在主库中操作~
xxxxxxxxxx# 为从库创建一个账号 repl,密码为 123456,从库 IP 没有限制CREATE USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
# 赋予该账号复制权限GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
# 刷新权限生效FLUSH PRIVILEGES;在主库的/etc/my.cnf文件中添加下面内容:
xxxxxxxxxx[mysqld]# binlog 日志存储位置log-bin=/opt/homebrew/var/mysql/binlog# master id,必须不重复server-id = 1# binlog 记录的格式为 rowbinlog_format = row# 同步的数据库binlog-do-db = study# 不同步的数据库binlog-ignore-db = mysql配置好后重启主库 MySQL,执行SHOW MASTER STATUS;可以查看主库中 binlog 文件名和位置 Position 的值,这部分数据在配置从库时会用到:
xxxxxxxxxxmysql> show master status;+---------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+---------------+----------+--------------+------------------+-------------------+| binlog.000014 | 1361 | study | mysql | |+---------------+----------+--------------+------------------+-------------------+1 row in set (0.01 sec)在从库的/etc/my.cnf文件中添加下面内容:
xxxxxxxxxx[mysqld]# binlog 日志存储位置log-bin=/var/lib/mysql/binlog# slave id,必须不重复server-id = 2# binlog 记录的格式为 rowbinlog_format = row# 同步的数据库binlog-do-db = study# 不同步的数据库binlog-ignore-db = mysql# 从库只可以读read_only = ON配置好后重启从库 MySQL,然后执行下面语句和主库建立连接:
xxxxxxxxxx# SOURCE_HOST 主库 ip# SOURCE_USER、SOURCE_PASSWORD 主库授权给从库的账号和密码# SOURCE_LOG_FILE 主库中 binlog 文件名# SOURCE_LOG_POS 主库中 binlog 位置的值 PositionCHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.1.1',SOURCE_PORT=3306,SOURCE_USER='repl',SOURCE_PASSWORD='123456',SOURCE_LOG_FILE='binlog.000014',SOURCE_LOG_POS=1361;xxxxxxxxxx# 启动主从复制START REPLICA;# 停止主从复制STOP REPLICA;# 重启主从复制工作RESTART REPLICA;# 清除从库主从复制关系RESET REPLICA;除此之外,还可以通过SHOW REPLICA STATUS\G;查看从库主从复制的状态:
xxxxxxxxxxmysql> SHOW REPLICA STATUS\G;*************************** 1. row *************************** Replica_IO_State: Waiting for source to send event Source_Host: 192.168.124.13 Source_User: rep1 Source_Port: 3306 Connect_Retry: 60 Source_Log_File: binlog.000014 Read_Source_Log_Pos: 1361 Relay_Log_File: LFool-relay-bin.000003 Relay_Log_Pos: 624 Relay_Source_Log_File: binlog.000014 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: 1361 Relay_Log_Space: 1602 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Source_SSL_Allowed: No Source_SSL_CA_File: Source_SSL_CA_Path: Source_SSL_Cert: Source_SSL_Cipher: Source_SSL_Key: Seconds_Behind_Source: 0Source_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: 2 Source_UUID: 93c73bc6-eb9f-11ed-b7cf-52361ce7344f 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: 86400 Source_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Source_SSL_Crl: Source_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Source_TLS_Version: Source_public_key_path: Get_Source_public_key: 0 Network_Namespace: 1 row in set (0.00 sec)从库相应的数据库和表需要手动提前创建好,主从复制不会自动创建数据库和表
主从复制只会自动同步开始复制之后的数据,而开始复制之前的数据需要手动复制到从库
主库和从库的 server-id 必须不重复