记录一下今天数据库的主从配置
- 配置docker-compose.yml
version: '3'
services:
mysqmaster:
image: mysql
restart: always
container_name: mysql-master
environment:
MYSQL_ROOT_PASSWORD: 123456 #root账户密码
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--max_allowed_packet=128M
ports:
- 3306:3306
- 33060:33060
volumes:
- ./master/data:/var/lib/mysql #data数据映射
- ./master/config/my.cnf:/etc/mysql/my.cnf # 配置
- ./master/log/err.log:/var/log/err.log # 错误日志
mysqlslave:
image: mysql
container_name: mysql-slave1
ports:
- 6607:3306
command:
- "--user=root"
volumes:
- ./slave/data:/var/lib/mysql
- ./slave/config/my.cnf:/etc/my.cnf
- ./slave/log/err.log:/var/log/err.log
environment:
MYSQL_ROOT_PASSWORD: 123456
restart: always
- 配置my.conf,master和slave除了server-id外都一样
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
server-id=1 #主从不能配置成一样
log-bin=mysql-bin
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/err.log
# Custom config should go here
!includedir /etc/mysql/conf.d/
default_authentication_plugin= mysql_native_password
default-time-zone= '+8:00'
上面两步完成后启动容器,具体方式不加赘述,请百度docker
- 设置主数据库
给某一账号赋予主从复制权限
GRANT REPLICATION SLAVE ON . TO ‘root’@’%’;
刷新权限
flush privileges
查看主数据库的状态,并记录下来,配置从数据库时会用到
其中File指的是最新binlog二进制文件的名称,Position指的是偏移量
- 配置从数据库
输入以下命令,将从数据库连接上主数据库(参数用自己配置的,端口默认是3306,可以用MASTER_PORT修改)
CHANGE MASTER TO,
MASTER_HOST=‘192.168.3.128’,
MASTER_USER=‘root’,
MASTER_PASSWORD=‘123456’,
MASTER_LOG_FILE=‘mysql-bin.000001’,
MASTER_LOG_POS=5415;
启动slave同步线程
start slave
- 查看连接状态
show slave status
注意要这两个选项都是YES才标识主从正常启动
SLAVE_IO_Running表示和主数据库的IO通信,Slave_SQL_Running表示从数据库的slave sql线程是否工作正常
当两者出问题时,要查看err日志对症下药