Linux mysql 5.7.23 主从复制(异步复制)

docker容器主节点: 172.17.0.9

docker容器子节点: 172.17.0.10

异步复制:

首先确认主库和从库是否一致,最好都是刚刚初始化的干净的数据库

如果主库正在使用不能初始化,就要先将主库的内容复制到从库上再做同步

1、先把master做一个备份,把备份出的文件传给slave

[[email protected] /]# mysqldump -uroot -p -B test > test.dump

Enter password:

[[email protected] data]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> source /data/test.dump

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Database changed

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

| test               |

+--------------------+

5 rows in set (0.00 sec)

再到slave上将数据恢复

mysql> source /mysql/test3.sql

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test1              |

| test2              |

| test3              |

+--------------------+

6 rows in set (0.00 sec)

2.开启主节点Bin-log日志

vi /etc/my.cnf

添加bin-log配置

server-id=1

log-bin=mysql-bin

binlog_format=row

systemctl restart mysqld

3.创建复制用户,赋予用户权限

mysql> grant replication slave on *.* to ‘repl‘@‘%‘ identified by ‘mysql‘;

mysql> flush privileges;

mysql> show master status;       要记住position

+------------------+----------+--------------+--------------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |

+------------------+----------+--------------+--------------------------+-------------------+

| mysql-bin.000003 |     683 |              | mysql,information_schema |                   |

+------------------+----------+--------------+--------------------------+-------------------+

1 row in set (0.00 sec)

4.再到slave

mysql>  change master to master_host=‘172.17.0.9‘,master_port=3306,master_user=‘repl‘,

->  master_password=‘mysql‘’,master_log_file=‘mysql-bin.000003‘,master_log_pos=683;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql>  start slave;

Query OK, 0 rows affected (0.09 sec)

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 172.17.0.9

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000003

Read_Master_Log_Pos: 154

Relay_Log_File: 72bd50b77c93-relay-bin.000002

Relay_Log_Pos: 320

Relay_Master_Log_File: mysql-bin.000003

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 154

Relay_Log_Space: 534

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 1

Master_UUID: 8ce1a2b2-b01c-11e8-90a0-0242ac11000a

Master_Info_File: /usr/local/mysql/data/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position: 0

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

1 row in set (0.00 sec)

看到两个yes 就基本成功了 可以在主库上进行一些操作来测试一下

5.测试结果:

主库插入数据

[[email protected] mysql]# mysql -uroot

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from t;

+------+------+------+

| id   | name | age  |

+------+------+------+

|    1 | dd   |   10 |

+------+------+------+

1 row in set (0.00 sec)

mysql> insert into t values(2,‘2222‘,11);

Query OK, 1 row affected (0.00 sec)

mysql>

登录slave数据库

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from t;

+------+------+------+

| id   | name | age  |

+------+------+------+

|    1 | dd   |   10 |

|    2 | 2222 |   11 |

+------+------+------+

2 rows in set (0.00 sec)

mysql>

mysql>

mysql>

原文地址:https://www.cnblogs.com/EikiXu/p/9591902.html

时间: 2024-10-10 03:27:53

Linux mysql 5.7.23 主从复制(异步复制)的相关文章

MySQL5.7主从复制-异步复制搭建

两台服务器,系统是Redhat6.5,MySQL版本是5.7.18.1.在主库上,创建复制使用的用户,并授予replication slave权限.这里创建用户repl,可以从IP为10.10.10.210的主机进行连接.grant replication slave on *.* to 'repl'@'10.10.10.210' identified by 'mysql'; 2.修改主服务器配置,加入如下配置:cat /etc/my.cnf[mysqld]server-id=1log-bin=

linux Mysql 主从复制 原理介绍和步骤详解

大家好,我是霸王卸甲,今天我给大家带来的是linux数据库中的主从复制的简单介绍和步骤详解. 主从复制 mysql主从复制 灵活 一主一从 主主复制 一主多从---扩展系统读取的性能,因为读是在从库读取的: 多主一从---5.7开始支持 联级复制--- 用途及条件 mysql主从复制用途 实时灾备,用于故障切换 读写分离,提供查询服务 备份,避免影响业务 主从部署必要条件: 主库开启binlog日志(设置log-bin参数) 主从server-id不同 从库服务器能连通主库 主从原理mysql主

mysql主从复制的异步复制与同步复制

异 步复制:MySQL本身支持单向的.异步的复制.异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时 – 最重要的是这意味着当应用系统的事务提交已经确认时数据并不能在同一时刻拷贝/应用到从机.通常这个延时是由网络带宽.资源可用性和系统负载决定的.然 而,使用正确的组件并且调优,复制能做到接近瞬时完成. 同步复制:同步复制可以定义为数据在同一时刻被提交到一台或多台机器,通常这是通过众所周知的“两阶段提交”做到的.虽然这确实给你在多系统中保持一致性,但也由于增加了额外的消息交换而造成性能下

MySQL 5.5 主从复制异步、半同步以及注意事项详解

大纲 一.前言 二.Mysql 基础知识 三.Mysql 复制(Replication) 四.Mysql 复制(Replication)类型 五.Mysql 主从复制基本步骤 六.Mysql 主从复制(异步) 七.Mysql 主从复制(半同步) 八.Mysql 复制工具 九.Mysql 复制注意事项 十.Mysql 复制过滤 一.前言 从这一篇博客开始我们就来学习mysql的高级课程,在前面的几篇博客我们讲解了mysql基础知识.mysql日志类型.mysql配置文件.mysql备份策略,这一篇

mysql 架构篇系列 2 复制架构一主一从搭建(异步复制)

一. 环境准备 1.1 主库环境(172.168.18.201) 环境 说明 查看脚本 操作系统版本 CentOS Linux release 7.4.1708 (Core) cat /etc/redhat-release 操作系统用户名和密码 root  js*2015 IP地址 172.168.18.201 ip addr 网关Gateway 172.168.18.1 cat /etc/sysconfig/network-scripts DNS 172.168.16.11 mysql 版本

呕心沥血博一文--MySQL主从异步复制

目录 1.概述 2.主从时间同步 3.主从服务器mysql部署 4.配置.启动从服务 5.测试 6.总结 1.概述 mysql内建的复制功能是mysql应用的"水平扩展"的架构,是mysql高性能运用的基础,为一个服务器配置一个或多个备库来进行数据的同步(准备的说不应该说是"同步",因为主从服务器的数据根本不可能做同步).在这样一个分布式的架构中各个服务器间时间应该同步才是,能为后期日志的统计分析提供更为准备的信息,这样才能准确反应出某个时间点我们系统的访问情况.

(5.4)mysql高可用系列——MySQL异步复制

关键词:mysql复制,mysql异步复制,mysql传统异步复制 [1]实验环境 操作系统:CentOS linux 7.5 数据库版本:5.7.24 数据库架构:主从复制,主库用于生产,从库用于数据容灾和主库备机,采用默认传统的异步复制. 主库IP:192.168.1.201 端口:3306 从库IP:192.168.1.202 端口:3306 [2]配置启动主从复制 #[2.1]配置my.cnf配置文件 #主库参数 # 主库增加参数 [mysqld]log-error_verbosity=

MYSQL 完全备份、主从复制、级联复制、半同步

mysql 完全备份 1,启用二进制日志,并于数据库分离,单独存放 vim /etc/my.cnf 添加 log_bin=/data/bin/mysql-bin 创建/data/bin文件夹并授权 chown mysql.mysql /data/bin 2,完成备份数据库 mysqldump -A --single-transaction --master-data=2 | xz > /data/all.sql.xz 3,对数据库进行增删改 INSERT hellodb.students(stu

(5.6)mysql高可用系列——MySQL中的GTID异步复制

[1]实验环境 操作系统:CentOS linux 7.5 数据库版本:5.7.24 数据库架构:主从复制,主库用于生产,从库用于数据容灾和主库备机,采用默认传统的异步复制. 主库IP:192.168.1.201 端口:3306 从库IP:192.168.1.202 端口:3306 原文地址:https://www.cnblogs.com/gered/p/11405864.html