docker pull mysql:5.7
docker run -d --privileged=true -p 3310:3306 -v /home/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=yy123456 --name masql mysql:5.7
docker run -d --privileged=true -p 3311:3306 -v /home/mysql2/config/mysqld.cnf:/etc/mysql/mysql.conf.d -v /home/mysql2/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=yy123456 --name slsql mysql:5.7
备注:
【/home/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d -v /home/mysql/data:/var/lib/mysql】
将MYSQL主从的配置文件和数据文件挂载到宿主机上
-- 将mysqld.cnf复制到/home/mysql/config/mysqld.cnf和/home/mysql2/config/mysqld.cnf
mysqld.cnf(主)
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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=100
#启动二进制日期
log-bin=mysql-bin
#要同步的数据库
binlog-do-db=test_db
#设置不要复制的数据库
binlog-ignore-db=mysql
#设置binlog的格式
#可选值为statement、row、mix
# statement 会记录每条执行SQL
#row 记录什么数据被修改成什么样子
# mix以上两种模式的混合使用,
binlog_format=mixed
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
mysqld.cnf(从)
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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]
#服务器唯一id
server-id=110
#开启二进制日志功能
log-bin=mysql-slave-bin
#启动中继日志
relay-log=mysql-relay
#设置不要复制的数据库
binlog-ignore-db=mysql
#如果需要同步函数或过程
log_bin_trust_function_creators=true
#主从复制格式
binlog_format=mixed
#跳过主从复制中遇到错误或指定错误,避免slave中断
#如1062错误是主键重复,1032主从数据库不一致
slave_skip_errors=1062
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
docker inspect 72e5b20c5e0b | grep IPAddress
create schema test_db default character set utf8 collate utf8_general_ci; -- 创建数据库
grant select,insert,update,delete,create on test_db.* to root; -- 用户授权数据库
-- 创建用户并授权:用户:test密码:123456,ip修改为从的ip
CREATE USER 'slave'@'172.17.0.3' identified by 'yy123456';
-- 分配中权限
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.17.0.3';
flush privileges;
change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=3069;
start slave;
show slave status\G
扩展:安装容器VIM命令
-- 安装VIM
apt-get update
apt-get install vim
--查看文件
cat file