.
├── docker-compose.yml
└── mysql
└── conf
│ └── my.cnf
└── data
└── log
需要
docker-compose.yml
和my.cnf
两个文件
创建文件夹
mkdir -p mysql/conf
配置文件
cat >> mysql/conf/my.cnf <<EOF
# @Author: Johnny
# master
[mysqld]
# [必须]服务器唯一ID
server-id=888
# [必须]启用二进制日志
log-bin=mysql-bin
# 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql
# 确保binlog日志写入后与硬盘同步
sync_binlog = 1
# 跳过所有的错误,继续执行复制操作
slave-skip-errors = all
# default
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
EOF
# @Author: Johnny
version: '3'
services:
mysql:
container_name: mysql-master
image: mysql:8
restart: always
volumes:
- $PWD/mysql/log:/var/log/mysql
- $PWD/mysql/conf/my.cnf:/etc/my.cnf
- $PWD/mysql/data:/var/lib/mysql
ports:
- '3306:3306'
command: [
'--explicit_defaults_for_timestamp=true',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_general_ci',
'--max_connections=3000'
]
environment:
MYSQL_ROOT_PASSWORD: "shield"
将上面的内容准备好后,安装:
docker-compose up -d
进入容器
docker exec -it mysql-master bash
登录mysql
mysql -uroot -pshield
执行sql
# 查看配置的服务ID,显示的应该是配置的ID
mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id | 888 |
| server_id_bits | 32 |
+----------------+-------+
# 看master信息 File 和 Position 从服务上要用
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
# 创建同步账户并开启权限,只是同步账号
mysql> create user 'sync'@'%' identified by '123456';
mysql> grant replication slave,replication client on *.* to 'sync'@'%';
# 刷新信息生效
mysql> flush privileges;
mysql8
默认的加密插件caching_sha2_password
,所以直接使用上面的密码无法进行链接
详情可参考这篇文章
解决办法
mysql> use mysql;
mysql> update user set host='%' where user='root';
mysql> alter user 'root'@'%' identified with mysql_native_password by'用户密码';
# 刷新权限
mysql> FLUSH PRIVILEGES;
如果是一台主机安装2个mysql,注意更新
docker-compose.yml
中的端口,避免端口冲突
.
├── docker-compose.yml
└── mysql
└── conf
│ └── my.cnf
└── data
└── log
需要
docker-compose.yml
和my.cnf
两个文件
创建文件夹
mkdir -p mysql/conf
配置文件
cat >> mysql/conf/my.cnf <<EOF
cat >> mysql/conf/my.cnf <
# @Author: Johnny
version: '3'
services:
mysql:
container_name: mysql-slave
image: mysql:8
restart: always
volumes:
- $PWD/mysql/log:/var/log/mysql
- $PWD/mysql/conf/my.cnf:/etc/my.cnf
- $PWD/mysql/data:/var/lib/mysql
ports:
- '3306:3306'
command: [
'--explicit_defaults_for_timestamp=true',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_general_ci',
'--max_connections=3000'
]
environment:
MYSQL_ROOT_PASSWORD: "shield"
将上面的内容准备好后,安装:
docker-compose up -d
docker exec -it mysql-slave bash
mysql -uroot -pshield
执行sql
#查看server_id是否生效
mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id | 999 |
| server_id_bits | 32 |
+----------------+-------+
# 连接主mysql服务 master_log_file 和 master_log_pos的值要填写主master里查出来的值 注意这里使用的docker-compose 内部服务的端口和ip,实际应该为真实的master_host,参数看后面的介绍
mysql> change master to master_host='mysql-master',master_user='sync',master_password='123456',master_port=3306,master_log_file='mysql-bin.000001', master_log_pos=154,master_connect_retry=30;
######################################################
### 连接主mysql参数说明:
### master_port:Master的端口号,指的是容器的端口号
### master_user:用于数据同步的用户
### master_password:用于同步的用户的密码
### master_log_file:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值
### master_log_pos:从哪个 Position 开始读,即上文中提到的 Position 字段的值
### master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒
######################################################
# 开启slave
mysql> start slave;
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: mysql-master
Master_User: sync
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1166
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 1334
Relay_Master_Log_File: 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: 1166
Relay_Log_Space: 1543
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: 888
Master_UUID: 05f48dfa-4afd-11ee-821c-0242ac170002
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:
1 row in set, 1 warning (0.01 sec)
看到下列信息代表链接成功
Slave_IO_Running:Yes;
Slave_SQL_Running:Yes;