主从复制简介
主从复制搭建
本次搭建一主一从
docker run -p 3307:3306 --name mysql-master
-v /mydata/mysql-master/log:/var/log/mysql
-v /mydata/mysql-master/data:/var/lib/mysql
-v /mydata/mysql-master/conf:/etc/mysql
-e MYSQL_ROOT_PASSWORD=root
-d mysql:5.7
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eac439fb07df mysql:5.7 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
进入/mydata/mysql–master/conf目录并新建my.cnf
[root@localhost ~]# cd /mydata/mysql-master/conf
[root@localhost conf]# vim my.cnf
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=mall-mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
[root@localhost conf]# docker restart mysql-master
mysql-master
[root@localhost conf]# docker exec -it mysql-master /bin/bash
root@eac439fb07df:/# mysql -uroot -proot
mysql>
mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
授予权限
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)
[root@localhost conf]# docker run -p 3308:3306 --name mysql-slave
> -v /mydata/mysql-slave/log:/var/log/mysql
> -v /mydata/mysql-slave/data:/var/lib/mysql
> -v /mydata/mysql-slave/conf:/etc/mysql
> -e MYSQL_ROOT_PASSWORD=root
> -d mysql:5.7
[root@localhost conf]# cd /mydata/mysql-slave/conf
[root@localhost conf]# vim my.cnf
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=102
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
log-bin=mall-mysql-slave1-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
重启slave容器
[root@localhost conf]# docker restart mysql-slave
mysql-slave
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f223b1736767 mysql:5.7 "docker-entrypoint.s…" 9 minutes ago Up 34 seconds 33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp mysql-slave
eac439fb07df mysql:5.7 "docker-entrypoint.s…" 24 minutes ago Up 15 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
[root@localhost conf]# docker exec -it mysql-master /bin/bash
root@eac439fb07df:/# mysql -uroot -proot
mysql> show master status;
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| mall-mysql-bin.000001 | 834 | | mysql | |
+-----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
进入
mysql-slave
容器
mysql-slave
容器
[root@localhost conf]# docker exec -it mysql-master /bin/bash
root@eac439fb07df:/# mysql -uroot -proot
mysql> create database mysql_db;
Query OK, 1 row affected (0.00 sec)
mysql> use mysql_db;
Database changed
mysql> create table stu(id int,name varchar(20),score double);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into stu(id,name,score) values(1,'eric',99.5);
Query OK, 1 row affected (0.01 sec)
mysql> select * from stu;
+------+------+-------+
| id | name | score |
+------+------+-------+
| 1 | eric | 99.5 |
+------+------+-------+
1 row in set (0.00 sec)
[root@localhost conf]# docker exec -it mysql-slave /bin/bash
root@f223b1736767:/# mysql -uroot -proot
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mysql_db |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mysql_db;
mysql> select * from stu;
+------+------+-------+
| id | name | score |
+------+------+-------+
| 1 | eric | 99.5 |
+------+------+-------+
1 row in set (0.00 sec)
原文地址:https://blog.csdn.net/m0_72832574/article/details/134750733
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_30524.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。