一:docker安装和配置
1.下载安装docker
yum install -y docker
2.启动docker
# systemctl start docker
//centos7及以后版本
# service docker start
//centos6及之前的版本
3.添加docker加速器地址
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://3wkgvv6t.mirror.aliyuncs.com"]
}
自己的加速器地址去https://cr.console.aliyun.com/#/accelerator获取
4.下载centos镜像
docker pull registry.cn-hangzhou.aliyuncs.com/moensun/centos7
镜像可以通过docker search docker
查看,docker pull后面的地址可以去https://dev.aliyun.com/list查找
5.利用镜像创建容器,镜像ID通过docker images
查看
docker run -tid 镜像ID /bin/bash //非centos7
docker run -tid 镜像ID /usr/sbin/init //centos7版本。
6.更改容器名称
docker rename oldName newName
7.docker中创建两个容器(通过同一个镜像创建即可)
为了演示方便,我的两个容器名称为mysql_master和mysql_slave
二:两个容器中分别安装mysql
1.首先下载三个包
https://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-server-5.6.38-1.el7.x86_64.rpm
https://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-client-5.6.38-1.el7.x86_64.rpm
https://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-devel-5.6.38-1.el7.x86_64.rpm
2.把上面3个包分别复制到两个容器中
docker cp MySQL-server-5.6.38-1.el7.x86_64.rpm mysql_master1:/root/
docker cp MySQL-client-5.6.38-1.el7.x86_64.rpm mysql_master1:/root/
docker cp MySQL-devel-5.6.38-1.el7.x86_64.rpm mysql_master1:/root/
docker cp MySQL-server-5.6.38-1.el7.x86_64.rpm mysql_slave1:/root/
docker cp MySQL-client-5.6.38-1.el7.x86_64.rpm mysql_slave2:/root/
docker cp MySQL-devel-5.6.38-1.el7.x86_64.rpm mysql_slave3:/root/
3.进入到容器中
docker exec -it mysql_slave1 /bin/bash
4.安装mysql依赖包(两个容器都要安装)
yum install -y perl perl-Module-Build net-tools autoconf libaio numactl-libs
5.在两个容器中分别安装上面的三个包
rpm -ivh MySQL-server-5.6.38-1.el7.x86_64.rpm (!!!这一步会生成随机密码,一定要观察打印的日志!)
rpm -ivh MySQL-client-5.6.38-1.el7.x86_64.rpm
rpm -ivh MySQL-devel-5.6.38-1.el7.x86_64.rpm
注意:如果上面安装MySQL-server-5.6.38-1.el7.x86_64.rpm的时候报错说file /usr/share/mysql/charsets/cp850.xml from install of MySQL-server-5.6.38-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.56-2.el7.x86_64
,那么就先查一下npm -qa | grep mariadb
然后强制删掉rpm -e mariadb-libs-5.5.44-2.el7.centos.x86_64 --nodeps
,然后重新执行以上操作即可
6.启动mysql并进入
# systemctl start mysql
# mysql -uroot -p密码 (这里的密码是在第五步生成的)
7.修改mysql密码
因为mysql安装的时候是随机密码,所以登录到mysql之后要我们自己设置密码
mysql> set password = password(‘你的密码’)
8.mysql远程登录授权,并刷新权限(到这里就可以用Navicat Premium等工具连接了)
mysql> grant all privileges on *.* to 'root'@'%' identified by '****' with grant option;
mysql> flush privileges;
注意把****换成你自己的mysql中root账户的密码
三:开始mysql主从配置
其大概原理就是主节点中的DDL和DML操作会写到Binlog中,从节点有IO线程定时的从Binlog中拉取,然后写入到从节点中的Relay-log(中继日志)中,然后在自己的mysql内部实现DDL和DML
在主节点上操作
1.创建同步复制的用户
mysql> create user 'yang'@'172.17.0.%' identified by 'yang0328';
2.给同步复制用户赋权
mysql> grant replication slave on *.* to 'yang'@'172.17.0.%' identified by 'yang0328';
mysql> flush privileges;
3.开启binlog(这一步是主节点和从节点都要做的)
修改/etc/my.cnf,加入下面的代码,如果没有就自己创建:vi /etc/my.cnf
[mysqld]
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
注意server-id = 2
这一句在主节点和从节点中务必数值不同,如果相同肯定会报错!!!
我主节点设置的2,从节点设置的3,但是并非固定的,只要不同就行。
其他配置的解释自行百度。
4.重启mysql
#sysctemctl restart mysql
在从节点上操作
1.首先关闭从
mysql> stop slave
2.主从复制的最关键语句:
mysql> Change master to Master_host=’172.17.0.2’,
Master_user=’yang’,
Master_password=’yang0328’,
Master_log_file=’mysql-bin.000012’,
Master_log_pos=120;
这里的Master_log_file和Master_log_pos的值是决定成败的关键点,你需要到主节点用root登录mysql,下图中1就是Master_log_file,2就是Master_log_pos
上面的两条指令:
mysql> flush logs;
mysql> show master status \G;
- Start slave;
终于要结束了
到这里主从复制就搭建完毕了,下面来测试一下
执行show slave status \G