1. 拷贝my.cnf文件
将容器内配置文件的位置/etc/my.cnf,拷贝到centos下
查看容器内的配置文件所在位置
在/etc/localhost/software创建mysql文件,并在mysql下创建文件夹3306和3310,并在两个文件夹下创建conf和data文件夹,并将my.cnf文件拷贝到conf文件夹下
先跳转到conf文件夹下,然后输入命令docker cp mysql_3306:/etc/my.cnf ./
2.删除之前的mysql docker
docker stop mysql_3306
docker rm mysql_3306
docker ps -a
3.创建运行容器
docker run -itd --name mysql_3306 --privileged=true -p 3306:3306 -v /usr/local/software/mysql/3306/data:/var/lib/mysql -v /usr/local/software/mysql/3306/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/software/mysql/3306/mysql-files:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=123 mysql
4.修改conf文件夹下的my.cnf文件
3306
# 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
default_authentication_plugin=mysql_native_password
# Custom config should go here
!includedir /etc/mysql/conf.d/
server-id=200
log-bin=mysql-bin
binlog_format=row
3310
# 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
default_authentication_plugin=mysql_native_password
# Custom config should go here
!includedir /etc/mysql/conf.d/
server-id=201
log-bin=mysql-slave-01-bin
relay_log=edu-mysql-relay-bin
read-only=1
5.master中创建slave账号操作从服务
进入mysql_3306容器:docker exec -it mysql_3306 bash
登录mysql:mysql -uroot -p123
创建主从账号”slave”:
- create user 'slave'@'%' identified by '123';
- grant replication slave, replication client on *.* to 'slave'@'%';
- alter user 'slave'@'%' identified with mysql_native_password by '123';
- flush privileges;
获取master容器的状态:
show master status;
获取mysql_master的ip地址
docker inspect mysql_3306 |grep IPA
mysql从服务器
修改从配置文件(前面已改过,就是3310下的my.cnf文件)
和上述一样,在对应的文件夹下创建3310的容器,然后进入mysql配置主从
change master to master_host='172.17.0.2',master_user='slave',master_password='123',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=157;
开启主从:start slave
查看结果:mysql> show slave status \G;