docker pull mysql
# 此时我下载的是8.0.27
#启动主节点
docker run -d -p 3307:3306
--privileged=true
-v /usr/local/mysql_cluster/master/log:/var/log/mysql
-v /usr/local/mysql_cluster/master/data:/var/lib/mysql
-v /usr/local/mysql_cluster/master/conf:/etc/mysql/conf.d
-e MYSQL_ROOT_PASSWORD=密码
--name mysql_master mysql
#启动从节点
docker run -d -p 3308:3306
--privileged=true
-v /usr/local/mysql_cluster/slave/log:/var/log/mysql
-v /usr/local/mysql_cluster/slave/data:/var/lib/mysql
-v /usr/local/mysql_cluster/slave/conf:/etc/mysql/conf.d
-e MYSQL_ROOT_PASSWORD=密码
--name mysql_slave mysql
## 说明
## --privileged==true 权限认证相关
## -v 数据挂载目录
/usr/local/mysql_cluster/master/conf
下新建my.cnfvim my.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
## 设置server_id,同一局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=mall-mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
# 设置默认时区
default-time_zone='+8:00'
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
## 设置server_id,同一局域网中需要唯一
server_id=102
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
log-bin=mall-mysql-slave1-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
## relay_log配置中继日志
relay_log=mall-mysql-relay-bin
## log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1
## slave设置为只读(具有super权限的用户除外)
read_only=1
# 设置默认时区
default-time_zone='+8:00'
master容器实例内创建数据同步用户
-- 创建slave用户以及设置密码
CREATE USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
-- 授权slave
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
说明: mysql8版本,需要手动设置密码校验为 mysql_native_password
忘记设置,通过修改的方式进行
mysql8版本设置账号要加一步:ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
docker restart mysql_master;
docker restart mysql_slave;
#若启动失败通过下命令查看报错
docker log --tail 20 容器编号
show master status;
File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
---|---|---|---|---|
mall-mysql-bin.000001 | 2625 | mysql |
## 进入
docker exec -it mysql-slave /bin/bash
## 登录mysql
mysql -uroot -p
change master to master_host='主库主机ip地址', master_user='步骤4创建的账号', master_password='步骤4创建的密码', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=2625, master_connect_retry=30;
参数说明:
master_host:主数据库的IP地址;
master_port:主数据库的运行端口;
master_user:在主数据库创建的用于同步数据的用户账号;
master_password:在主数据库创建的用于同步数据的用户密码;
master_log_file:指定从数据库要复制数据的日志文件,通过查看主数据的状态,获取File参数;
master_log_pos:指定从数据库从哪个位置开始复制数据,通过查看主数据的状态,获取Position参数;
master_connect_retry:连接失败重试的时间间隔,单位为秒。
发现Slave_IO_Running
与Slave_SQL_Running
为 NO状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 47.116.20.187
Master_User: slave
Master_Port: 3307
Connect_Retry: 30
Master_Log_File: mall-mysql-bin.000001
Read_Master_Log_Pos: 2625
Relay_Log_File: mall-mysql-relay-bin.000002
Relay_Log_Pos: 1508
Relay_Master_Log_File: mall-mysql-bin.000001
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: 2625
Relay_Log_Space: 1722
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: 0
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: 101
Master_UUID: 3205ec3d-76ee-11ed-a6b9-0242ac110002
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:
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:
start slave;
该命令启动后可能会发现错误
此类报错和max_allowed_packet相关。首先max_allowed_packet控制着主从复制过程中,一个语句产生的二进制binlog event大小,它的值必须是1024的倍数 。
1 该参数在主备库的配置大小不一样,主库的配置值大于从库的配置值。 从主库传递到备库的binlog event大小超过了主库或者备库的max_allowed_packet大小。
2 主库有大量数据写入时,比如在主库上执行 laod data,insert into … select 语句,产生大事务。
Got fatal error 1236 from master when reading data from binary log: 'binlog truncated in the middle of event; consider out of disk space on master; the first event 'mall-mysql-bin. 000001' at 1146, the last event read from './mall-mysql-bin. 000001' at 125, the last byte read from './mall-mysql-bin. 000001' at 1446.'
设置一下同步日志允许大小就行了
在主从数据库上均执行:
set global max_allowed_packet =1*1024*1024*1024;
-- 重启
stop slave;
start slave;
发现Slave_IO_Running
与Slave_SQL_Running
变为YES状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 47.116.20.187
Master_User: slave
Master_Port: 3307
Connect_Retry: 30
Master_Log_File: mall-mysql-bin.000001
Read_Master_Log_Pos: 2625
Relay_Log_File: mall-mysql-relay-bin.000002
Relay_Log_Pos: 1508
Relay_Master_Log_File: mall-mysql-bin.000001
Slave_IO_Running: YES # 关注这两个参数
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: 2625
Relay_Log_Space: 1722
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: 0
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: 101
Master_UUID: 3205ec3d-76ee-11ed-a6b9-0242ac110002
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:
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: