一、原理

mysql的主从数据同步是一个异步复制过程,需要master开启bin-log日志功能,bin-log记录了master库中的增、删、修改、更新操作的sql语句,整个过程需要开启3个线程,分别是master开启I/O线程,slave开启I/O线程和SQL线程

1、在slave服务器上执行start slave命令开启主从复制开关,主从复制开始进行,slave I/O线程会通过master创建的授权用户连接上master,并请求master从指定文件和位置之后发送bin-log日志内容

2、master接收请求后,master I/O线程更加slave发送的指定bin-log日志position点之后的内容,然后返回给slave的I/O线程;返回的信息中除了bin-log日志外,还有在master服务器记录的新的bin-log文件名及新的bin-log中的下一个指定更新位置(position)

3、slave I/O线程接收信息后,将接收的日志内容一次添加到slave端的relay-log(中继日志)文件(mysql-relay-bin.xxxxxx)的最末端,并将读取到的master端的bin-log文件名和position点记录到master.info文件中,以便下次读取时能够告知master从相应的bin-log文件名及最后一个position点开始发起请求

4、slave SQL 线程检测到relay-log中I/O线程新增加的内容有更新,会立即分析relay-log日志中的内容,将解析的sql语句按顺序在slave里执行,并记录应用中继日志的文件名及位置点在relay-log.info中,执行成功后slave库与master库数据保持一致

总结

主从复制是异步的逻辑的SQL语句级的复制

复制时,主库有一个I/O线程,从库有两个线程,I/O和SQL线程

作为复制的所有mysql节点server-id都不能相同

bin-log文件只记录对数据库有更改的sql语句(数据库内容的变更),不记录任何查询(select,slow)语句

原理流程图如下:

主从复制条件

开启binlog功能

主库要建立账号

从库要配置master.info

start slave 开启复制功能

二、环境

master:192.168.216.52

slave:192.168.216.53

mariadb版本10.2.24

[root@web2 ~]# rpm -qa Maria*MariaDB-server-10.2.24-1.el7.centos.x86_64

MariaDB-compat-10.2.24-1.el7.centos.x86_64

MariaDB-common-10.2.24-1.el7.centos.x86_64

MariaDB-client-10.2.24-1.el7.centos.x86_64

[root@web2~]#

三、安装配置

1、添加mariadb,yum源

[root@web2 ~]# cat /etc/yum.repos.d/mariadb.repo

[mariadb]

name=MariaDB

baseurl= https://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64/

gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB

gpgcheck=1

2、这里yum安装

两台机器都安装

yum install  mariadb-server

3、初始化mariadb,可以忽略

4、配置master

-------------------修改配置文件(红色部分关键,其他为优化)

[root@web2 my.cnf.d]# catserver.cnf

#

# Thesegroupsare read by MariaDB server.

# Use itforoptions that only the server (but not clients) should see

#

# See the examples of server my.cnf filesin /usr/share/mysql/#

# this is read by the standalone daemon and embedded servers

[server]

# this is onlyforthe mysqld standalone daemon

[mysqld]

server-id=1

log-bin=mysql-bin

#binlog-do-db=liting

#binlog-ignore-db=mysql

sync_binlog=1

binlog_checksum = none

binlog_format = mixed

port = 3306

socket = /var/lib/mysql/mysql.sock

skip-external-locking

key_buffer_size=256MB

max_allowed_packet=1MB

table_open_cache= 256sort_buffer_size=1MB

read_buffer_size=1MB

read_rnd_buffer_size=4MB

myisam_sort_buffer_size=64MB

thread_cache_size= 8query_cache_size=16MB

thread_concurrency= 8[mysqldump]

quick

max_allowed_packet=16MB

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size=128MB

sort_buffer_size=128MB

read_buffer=2MB

write_buffer=2MB

[mysqlhotcopy]

interactive-timeout

#

#* Galera-related settings

#

[galera]

# Mandatory settings

#wsrep_on=ON

#wsrep_provider=#wsrep_cluster_address=#binlog_format=row

#default_storage_engine=InnoDB

#innodb_autoinc_lock_mode=2#

# Allow server to accept connections on all interfaces.

#

#bind-address=0.0.0.0#

# Optional setting

#wsrep_slave_threads=1#innodb_flush_log_at_trx_commit=0# this is onlyforembedded server

[embedded]

# This group is only read by MariaDB servers, not by MySQL.

# If you use the same .cnffile forMySQL and MariaDB,

# you can put MariaDB-only options here

[mariadb]

# This group is only read by MariaDB-10.2servers.

# If you use the same .cnffile forMariaDB of different versions,

# use this groupfor options that older servers don't understand

[mariadb-10.2]

[root@web2 my.cnf.d]#

----------------------授权

grant replication slave,replication client on *.* to 'tongbu'@'%' identified by '123456';

----------------------查看bin-log及position点

MariaDB [test3]>show master status;+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000003 | 320| | |

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

1 row in set (0.00 sec)

5、配置slave

--------------------修改配置

[root@web3 my.cnf.d]# catserver.cnf

#

# Thesegroupsare read by MariaDB server.

# Use itforoptions that only the server (but not clients) should see

#

# See the examples of server my.cnf filesin /usr/share/mysql/#

# this is read by the standalone daemon and embedded servers

[server]

# this is onlyforthe mysqld standalone daemon

[mysqld]

socket= /var/lib/mysql/mysql.sock

port = 3306

skip-external-locking

key_buffer_size=256MB

max_allowed_packet=1MB

table_open_cache= 256sort_buffer_size=1MB

read_buffer_size=1MB

read_rnd_buffer_size=4MB

myisam_sort_buffer_size=64MB

thread_cache_size= 8query_cache_size=16MB

thread_concurrency= 8server-id = 2#

#* Galera-related settings

#

[galera]

# Mandatory settings

#wsrep_on=ON

#wsrep_provider=#wsrep_cluster_address=#binlog_format=row

#default_storage_engine=InnoDB

#innodb_autoinc_lock_mode=2#

# Allow server to accept connections on all interfaces.

#

#bind-address=0.0.0.0#

# Optional setting

#wsrep_slave_threads=1#innodb_flush_log_at_trx_commit=0# this is onlyforembedded server

[embedded]

# This group is only read by MariaDB servers, not by MySQL.

# If you use the same .cnffile forMySQL and MariaDB,

# you can put MariaDB-only options here

[mariadb]

# This group is only read by MariaDB-10.2servers.

# If you use the same .cnffile forMariaDB of different versions,

# use this groupfor options that older servers don't understand

[mariadb-10.2]

[root@web3 my.cnf.d]#

[root@web3 my.cnf.d]# cat mysql-clients.cnf #[mysql][mysqlcheck][mysqldump]也可以写在这个文件里,master我是都卸载server.cnf里面了

#

# Thesegroups are read by MariaDB command-line tools

# Use itforoptions that affect only one utility

#

[mysql]

no-auto-rehash

[mysql_upgrade]

[mysqladmin]

[mysqlbinlog]

[mysqlcheck]

key_buffer_size=128MB

sort_buffer_size=128MB

read_buffer=2MB

write_buffer=2MB

[mysqldump]

quick

max_allowed_packet=16MB

[mysqlimport]

[mysqlshow]

[mysqlslap]

[mysqlhotcopy]

interactive-timeout

[root@web3 my.cnf.d]#

---------------------slave进入mysql设置

slave指定master ip、用户名、密码、bin-log文件名、position(下面标记红色部分)

[root@web3 my.cnf.d]# mysql

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

Your MariaDB connectionid is 12Server version:10.2.24-MariaDB MariaDB Server

Copyright (c)2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type'help;' or '\h' for help. Type '\c' to clearthe current input statement.

MariaDB [(none)]>change master tomaster_host='192.168.216.52',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=320;MariaDB [(none)]> master_host='192.168.216.52',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=320;

MariaDB [(none)]> slave start;

---------------查看状态正常状态如下:(正常状态关注标记紫色部分)

MariaDB [(none)]>show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting formaster to send event

Master_Host:192.168.216.52Master_User: tongbu

Master_Port:3306Connect_Retry:60Master_Log_File: mysql-bin.000004Read_Master_Log_Pos:593Relay_Log_File: web3-relay-bin.000006Relay_Log_Pos:828Relay_Master_Log_File: mysql-bin.000004Slave_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:0Last_Error:

Skip_Counter:0Exec_Master_Log_Pos:593Relay_Log_Space:1136Until_Condition: None

Until_Log_File:

Until_Log_Pos:0Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No

Last_IO_Errno:0Last_IO_Error:

Last_SQL_Errno:0Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id:1Master_SSL_Crl:

Master_SSL_Crlpath:

Using_Gtid: No

Gtid_IO_Pos:

Replicate_Do_Domain_Ids:

Replicate_Ignore_Domain_Ids:

Parallel_Mode: conservative

SQL_Delay:0SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waitingfor the slave I/O thread to update it1 row in set (0.00mysql主从同步(4)-Slave延迟状态监控

#  1)Slave_IO_Running:该参数可作为io_thread的监控项,Yes表示io_thread的和主库连接正常并能实施复制工作,No则说明与主库通讯异常,多数情况是由主从间网络引起的问题;

2)Slave_SQL_Running:该参数代表sql_thread是否正常,YES表示正常,NO表示执行失败,具体就是语句是否执行通过,常会遇到主键重复或是某个表不存在。

3)Seconds_Behind_Master:是通过比较sql_thread执行的event的timestamp和io_thread复制好的event的timestamp(简写为ts)进行比较,而得到的这么一个差值;

NULL—表示io_thread或是sql_thread有任何一个发生故障,也就是该线程的Running状态是No,而非Yes。

0 — 该值为零,是我们极为渴望看到的情况,表示主从复制良好,可以认为lag不存在。

正值 — 表示主从已经出现延时,数字越大表示从库落后主库越多。

负值 — 几乎很少见,我只是听一些资深的DBA说见过,其实,这是一个BUG值,该参数是不支持负值的,也就是不应该出现。

6、测试,master创建一个test3的库及t1的表

[root@web2 my.cnf.d]#mysql -uroot -p

Enter password:

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

Your MariaDB connectionid is 11Server version:10.2.24-MariaDB-log MariaDB Server

Copyright (c)2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type'help;' or '\h' for help. Type '\c' to clearthe current input statement.MariaDB [(none)]>MariaDB [(none)]>create database test3;

Query OK,1 row affected (0.00sec)

MariaDB [(none)]>use test3;

Database changed

MariaDB [test3]> create table t1(id varchar(20),name varchar(20));

Query OK,0 rows affected (0.02sec)

MariaDB [test3]>show tables;+-----------------+

| Tables_in_test3 |

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

| t1 |

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

1 row in set (0.00sec)

MariaDB [test3]>

---------------slave查看已经同步过来了

MariaDB [(none)]>show databases;+--------------------+

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| test |

| test3 |

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

5 rows in set (0.00sec)

MariaDB [(none)]>

mariadb 和mysql主从_Mariadb/Mysql 主从复制(1)相关推荐

  1. MySQL主从(MySQL proxy Lua读写分离设置,一主多从同步配置,分库分表方案)

    Mysql Proxy Lua读写分离设置 一.读写分离说明 读写分离(Read/Write Splitting),基本的原理是让主数据库处理事务性增.改.删操作(INSERT.UPDATE.DELE ...

  2. mysql 读写分离缺点6_6\MySQL 主从同步 , MySQL 读写分离 , MySQL 性能调优

    6\MySQL 主从同步 , MySQL 读写分离 , MySQL 性能调优 day06 一,mysql 主从同步 二,数据读写分离 三,MySQL 优化 ++++++++++++++++++++++ ...

  3. mysql 主从 问题_Mysql主从复制的问题与解决

    主从复制的原理 主库将变更的操作写入bin-log日志中(增,删,改操作). 从库中的I/O线程将主库的bin-log拷贝到本地,写入relay-log(中继日志中) 从库的SQL线程从中继日志中读取 ...

  4. mysql 主从 编码_Mysql 主从复制

    MySQL Replication 主从复制(也称 AB 复制)允许将来自一个MySQL数据库服务器(主服务器)的数据复制到一个或多个MySQL数据库服务器(从服务器). 复制是异步的 从站不需要永久 ...

  5. mysql主从同步slave_MySQL主从复制(Master-Slave)实践

    MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的拓展.多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能. 下图就描述了一个多个数 ...

  6. mysql主从 单点_MySQL主从复制虽好,能完美解决数据库单点问题吗?

    一.单个数据库 服务器 的缺点 数据库服务器存在单点问题: 数据库服务器资源无法满足增长的读写请求: 高峰时数据库连接数经常超过上限. 二.如何解决单点问题 增加额外的数据库服务器,组建数据库集群: ...

  7. mysql主从应用_MySQL主从复制应用、主从复制原理

    mysql主从复制安装配置 1.基础设置准备 #操作系统: centos6.5 #mysql版本: 5.7 #两台虚拟机: node1:192.168.85.111(主) node2:192.168. ...

  8. mysql主从北_mysql主从复制(超简单)

    怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作: 1.1.版本一致 1.2.初始化表,并在后台启动mysql 1.3.修改root的密码 2.修改主服 ...

  9. mysql主从配置 51cto_Mysql主从复制配置

    Mysql主从复制配置 本文只是介绍安装和配置,关于一些名词和原理,请参考mysql手册. Mysql的复制最少需要两台mysql服务器,一台主(主机名master,ip为192.168.20.204 ...

最新文章

  1. 如何删除被锁定的文件(一)他山工具篇 WhoLockMe?
  2. iOS9网络适配 info.plist配置
  3. 【Python基础】加密你的Python源码顺便再打个包如何?
  4. pythonsuper多重继承_Python多重继承引发的问题——牛逼的super
  5. error HLP: Help compilation failed with code 1
  6. C语言回调函数 钩子函数,回调函数和钩子函数的说明
  7. 面向对象的思考过程 (马特·魏斯费尔德 著)
  8. 操作系统思考 第五章 更多的位与字节
  9. 洛谷P3698 [CQOI2017]小Q的棋盘
  10. 禁止谷歌浏览器 错误报告_报告浏览器错误
  11. BTA 常问的 Java基础39道常见面试题
  12. cs显示服务器连接失败是什么原因,CS1.5服务器连接常见问题解决 解决常见Windows 7无法识别网络的问题...
  13. Seventh season eighteenth episode,Joey got an award??????
  14. Nexus 5设备调试
  15. 正则表达式--教程一 简介(共三篇)
  16. CSDN网站中的版权投诉的标准
  17. 2021爱智先行者—数量遗传学 第一章 数量遗传学概论
  18. phonegap 修改app的名称
  19. Java实现MD5工具类
  20. 网站安全监测报告 预测2020年的网络安全发展趋势

热门文章

  1. java传输对象_如何传输Java对象
  2. 分光计游标盘ab两个游标作用_汽车防撞梁的作用究竟有多大?没有后防撞梁的汽车真的不安全吗?...
  3. GoogLenet网络解读及代码实现(Pytorch)
  4. JAVA 使用类的继承和接口实现多态
  5. python requests 示例_Python3中requests库学习01(常见请求示例)
  6. js判断字符串是否为空_每日一课 | Python 如何判断一个字符串是否包含另一个字符串?...
  7. golang python扩展_Python: C扩展初体验
  8. 学生电脑哪个牌子好_电脑桌哪个牌子好?如何选购电脑桌?2020年值得选购的电脑桌品牌推荐...
  9. html width设置没用,html style的width不起作用
  10. oracle 服务名丢失,win2003 oracle服务丢失后恢复的一个例子