docker安装mysql5.7, 配置主从

一、docker快捷安装mysql5.7

1.docker启动之后,拉取mysql的镜像

docker pull mysql:5.7.21

2.初次运行镜像

docker run --name mysql.5.7.21 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21

--name 为设置容器的名字,我设置为mysql.5.7.21
-p 端口映射
-e 为设置执行时的环境变量,在这里我设置mysql的root密码,相关变量可参考官网
-d 为设置镜像,镜像名:版本

3.进入mysql的容器里

docker exec -it mysql_master

4.查看docker的启动情况

docker ps -a

5.用mysql工具就可以之间连接进去进行操作了

二、使用docker给mysql添加主从

1.拉取镜像

docker pull mysql:5.7.21

2.运行2个mysql镜像,分别命名mysql_master、mysql_slave

docker run --name mysql_master -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21
docker run --name mysql_slave -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21

3.进入mysql_master进行相关主节点配置

docker exec -it mysql_master /bin/bash

查找mysql配置文件的位置

find / -name my.cnf 或 mysql --help | grep my.cnf

在/etc/mysql/my.cnf

vim /etc/mysql/ny.cnf

添加内容

[client]
port                        = 3306
socket                      = /var/lib/mysql/mysql.sock
[mysqld]
# Master node configuration 2 and slave node configuration 3, generally configured according to the Intranet IP
server-id                   = 2
port                        = 3306
datadir                     = /var/lib/mysql
socket                      = /var/lib/mysql/mysql.sock
default-storage-engine      = InnoDB
log-bin                     = mysql-bin
log-bin-index               = mysql-bin.index
relay-log                   = mysql-relay
relay-log-index             = mysql-relay.index
expire-logs-days            = 10
max-binlog-size             = 100M
max_binlog_cache_size       = 8M
log-slave-updates           = 1
binlog_cache_size           = 4M
# use MIXED binlog
binlog_format               = MIXED
#binlog_format              = ROW
#replicate-do-db            = db%.%
#replicate-ignore-db        = mysql.%
# ignore tables
replicate-wild-ignore-table = mysql.%
sync_binlog                 = 1
relay_log_recovery          = 1
log_slave_updates           = 1
skip-name-resolve
sql_mode=STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet          = 32M

修改完重启mysql_master容器或重启mysql

重启mysql:
service mysql restart
或重启容器:
docker start mysql_master

重启容器后进入mysql_master容器

docker exec -it mysql_master /bin/bash

登录mysql客户端,创建用户,授权,开启binlog

mysql -uroot -p123456
mysql> create user 'repl'@'%' identified by 'abcd_123';
mysql> grant replication slave on . to 'repl'@'%' identified by 'abcd_123';
mysql> flush privileges;

重启

service mysql restart

4.进入mysql_slave容器,连接mysql客户端

**在mysql_slave里配置和mysql_master一样的my.conf配置server-id改成3

改成和master status里显示的内容一致 Master_log_file 和 Master_log_pos

mysql> show master status;
mysql> stop slave;
mysql> Change master to Master_host='172.17.0.2', Master_user='repl', Master_password='abcd_123', Master_log_file='mysql-bin.000001', Master_log_pos=120;
mysql> start slave;
mysql> show slave status \G


Slave_IO_Running: Yes
Slave_SQL_Running: Yes
表示操作成功

5.Slave_IO_Running:NO(解决方法)

改成和master status里显示的内容一致
mysql> show master status;
mysql> stop slave;
mysql> change master to Master_Log_File='mysql-bin.000002',Master_Log_Pos=154;
mysql> start slave;
mysql> show slave status \G;

注:

  • 容器内容ip
    172.17.0.2 是容器中mysql_master里的内网ip
    172.17.0.3 是容器中mysql_slave里的内网ip
  • 镜像过度精简导致没有vim、ifconfig、ping命令处理方案:docker 镜像下安装Vim
    安装vim
    apt-get install -y vim
    安装ifconfig
    apt-get install net-tools
    安装ping命令
    apt-get install iputils-ping
  • Docker容器互访三种方式

你可能感兴趣的:(docker安装mysql5.7, 配置主从)