构建docker镜像,其中数据卷配置内容在下面,结构目录以下html
version: '3.7' services: db: # images 8.x image: mysql restart: always environment: MYSQL_ROOT_PASSWORD: 456123 command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --explicit_defaults_for_timestamp=true --lower_case_table_names=1 ports: - 3309:3306 volumes: - ./data:/var/lib/mysql - ./my.cnf:/etc/my.cnf
更新配置文件mysql
# 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] 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 # Custom config should go here # [必须]启用二进制日志 log-bin=mysql-bin # [必须]服务器惟一ID,默认是1 1~255 server-id=1 sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
配置操做redis
启动mysqlsql
docker-compose up -d
更新配置docker
查看容器shell
docker ps
进入mysql交互数据库
docker exec -it 518a92715f6f /bin/bash
登陆mysqlbash
mysql -u root -p
配置服务器
#在主库上建立同步用户并受权 CREATE USER 'replicate'@'112.74.41.236' IDENTIFIED BY '123456'; GRANT REPLICATION SLAVE ON *.* TO 'replicate'@'112.74.41.236'; FLUSH PRIVILEGES; #最后增长远程访问用户 并赋予全部权限,远程访问测试用 CREATE USER antsdouble IDENTIFIED BY '123456'; GRANT ALL ON *.* TO 'antsdouble'@'%'; #用navicate12及以上能够不用修复修复远程登陆报报 caching_sha2_password异常 # mysql8 用新的驱动 driver-class-name: com.mysql.cj.jdbc.Driver ALTER USER 'antsdouble'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; ALTER USER 'antsdouble'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; FLUSH PRIVILEGES; #查询master的状态,此处File,Position数据在配置从库时用到 show master status;
相关说明网络
server-id是惟一的,主从不能相同,server-id为1表示mysq数据库为主数据库,server-id为2表示mysql的数据库为从数据库
为什么必须挂载到/etc/my.cnf 文件上,而不是/etc/mysql/my.cnf 文件上?由于mysql默认配置文件位置在/etc/mysql/my.cnf,挂在方式没法改变容器中文件内容,my.conf内容不会改变,my.cnf中没有咱们自定义的配置内容,启动mysql容器会报错
lower_case_table_names:忽略表名、列名等数据结构的大小写(注意:不是每行记录内容的大小写!)。 log-bin:开启二进制记录。这是为了主从复制而作的设置。本文使用RBR(Row-Based Replication)模式。 slow_query_log=1:开启慢查询日志。若是某一条SQL执行的时间超过long_query_time设置的秒数,那么就记录下来。记录文件路径能够使用show variables;命令,在变量名是slow_query_log_file下查找到具体的日志文件路径。 long_query_time=1:单位是秒。指若是某一条SQL语句执行时间超过1秒,就记录下来。必须开启慢查询日志了之后,此变量才能使用。 log_error:开启错误日志。show variables like 'log_error'; 就能够查询到日志文件的路径。mysql的docker官方镜像若是设置别的取值会致使容器没法正常启动。
更新配置文件
# 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] 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 # Custom config should go here # [必须]启用二进制日志 log-bin=mysql-bin # [必须]服务器惟一ID,默认是1 server-id=1 sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
配置操做
进入交互模式,方法同主库
配置从库
change master to master_host='112.74.41.236',master_port=3309,master_user='replicate',master_password='123456',master_log_file='binlog.000006',master_log_pos=3862;
说明
master_port:Master的端口号,指的是容器的端口号 master_user:用于数据同步的用户 master_password:用于同步的用户的密码 master_log_file:指定 Slave 从哪一个日志文件开始复制数据,即上文中提到的 File 字段的值 master_log_pos:从哪一个 Position 开始读,即上文中提到的 Position 字段的值 master_connect_retry:若是链接失败,重试的时间间隔,单位是秒,默认是60秒
查看
show slave status\G # 查询slave的状态,Slave_IO_Running及Slave_SQL_Running进程必须正常运行,即YES状态,不然都是错误的状态 错误能够在2标记处查看到缘由,也能够经过 show slave status;
可能产生的缘由
网络不通 检查ip,端口 密码不对 检查是否建立用于同步的用户和用户密码是否正确 pos不对 检查Master的 Position
Could not execute Update_rows event on table oa.bui_bill_sum; Can't find record in 'bui_bill_sum', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000138, end_log_pos 160051747
解决,临时
进入sql 执行 STOP SLAVE; SET GLOBAL sql_slave_skip_counter =1; #表示跳过一步错误,后面的数字可变 START SLAVE;
永久
在my.cnf中配置 slave-skip-errors = 1032 就会跳过全部的1032的错误,多个用逗号分隔
mysql并无从my.cnf文件中更新server_id,既然这样就只能手动修改了
set global server_id=2; #此处的数值和my.cnf里设置的同样就行 slave start;
原文出处:https://www.cnblogs.com/ants_double/p/11763415.html