基础环境准备
# 获取centos镜像,注:以下镜像为整合了 ansible 的centos镜像,可以解决大量的依赖问题 docker pull ansible/centos7-ansible
容器中安装基础工具
启动容器:
docker run -itd --privileged --name centos-mysql ansible/centos7-ansible /usr/sbin/init
进入容器:
docker exec -it centos-mysql bash
yum install wget yum install net-tools yum install initscripts yum install openssh-server yum install libaio yum install numactl
安装Mysql
cd /usr/local/ # 下载mysql wget http://mirrors.163.com/mysql/Downloads/MySQL-8.0/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz # 解压 tar xvJf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz mv mysql-8.0.26-linux-glibc2.12-x86_64 mysql # 创建data文件 mkdir /usr/local/mysql/data # 创建用户组与用户并授权 groupadd mysql useradd -g mysql mysql chown -R mysql.mysql /usr/local/mysql # 初始化基本信息 cd /usr/local/mysql/bin/ ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --initialize
记住生成的临时用户名和密码,保存下来,之后会用到
编辑配置文件
查看my.cnf 文件位置 ./mysql –help | grep my.cnf
这个实际上就是mysql尝试去读取配置文件的路径顺序,没有找到话,mysql会走默认配置
所以,看下这几处位置是否有配置文件,有的话,编辑该文件,没有的话,直接在第一读取位置,创建配置文件,如: 创建 /etc/my.cnf 配置文件。
/etc/my.cnf:
[client] default-character-set=utf8 [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data port=3306 socket=/tmp/mysql.sock character-set-server=utf8 collation-server=utf8_general_ci sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
添加mysqld服务到系统
# cp -a 保留原文件属性 cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql # 授予执行权限 chmod +x /etc/init.d/mysql chkconfig --add mysql
启动mysql
service mysql start
将mysql命令添加到服务
ln -s /usr/local/mysql/bin/mysql /usr/bin/
登录mysql 使用之前随机生成的密码
修改root密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'; #其中123456是新的密码自己设置 flush privileges; # 使新密码生效
使支持远程连接
use mysql; update user set host='%' where user='root'; flush privileges; exit
制作镜像
退出容器,制作mysql镜像
docker stop centos-mysql docker commit centos-mysql centos-mysql-80
创建Master
docker run -itd -p 3306:3306 --privileged --name mysql-master centos-mysql-80 /usr/sbin/init
进入容器
docker exec -it mysql-master /bin/bash
修改/etc/my.cnf, 增加以下配置
# master log_bin=master-bin log_bin_index=master-bin.index server-id=1 sync-binlog=1 binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=sys gtid_mode = on enforce_gtid_consistency = 1 log_slave_updates = 1 plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" loose_rpl_semi_sync_master_enabled = 1 loose_rpl_semi_sync_slave_enabled = 1 loose_rpl_semi_sync_master_timeout = 5000
重启mysql
service mysql restart
授权
# 登录mysql,配置slave访问master的用户的ip权限,注:identified by 后需要填写你root账户对应的密码 # 创建账户 create user 'root'@'%' identified by '123456' # 赋予权限,with grant option这个选项表示该用户可以将自己拥有的权限授权给别人 grant all privileges on *.* to 'root'@'%' with grant option # 使生效 flush privileges;
创建Slave
docker run -itd -p 3307:3306 --privileged --name mysql-slave-1 centos-mysql-80 /usr/sbin/init
进入容器
docker exec -it mysql-slave-1 /bin/bash
修改/etc/my.cnf, 增加以下配置
# slave server_id=2 relay_log_index = slave_relay_bin.index relay_log= slave_relay_bin innodb_log_file_size= 256M expire-logs-days = 1 read_only=1 gtid_mode = on enforce_gtid_consistency = 1 log_slave_updates = 1 binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=sys # 候选时需要设置参数 log-bin = mysql-bin log-bin-index = mysql-bin.index plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" loose_rpl_semi_sync_master_enabled = 1 loose_rpl_semi_sync_slave_enabled = 1 loose_rpl_semi_sync_master_timeout = 5000
重启mysql
# 删除auto.cnf文件,否则连接master会失败 rm /usr/local/mysql/data/auto.cnf service mysql restart
设置master相关信息
配置前,我们需要先获取master相关信息,进入mysql-master容器,登录mysql,执行 show master status 查看:
获取mysql-master容器ip地址:
docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-master
进行从库的master配置
change master to master_host='172.17.0.2', master_port=3306, master_user='root', master_password='123456',master_log_file='master-bin.000001', master_log_pos=550; start slave; show slave status \G
# show slave satus 这两项为Yes,则成功 Slave_IO_Running: Yes Slave_SQL_Running: Yes
使用同样的方式创建salve2,注:需要保证my.inf配置中server_id保持唯一
测试主从复制
master创建数据库 test
mysql-slave-1
mysql-slave-2
至此,mysql主从集群部署完成~