Mariadb/Mysql 主从复制(1)
一、原理
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.-.el7.centos.x86_64
MariaDB-compat-10.2.-.el7.centos.x86_64
MariaDB-common-10.2.-.el7.centos.x86_64
MariaDB-client-10.2.-.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=
2、这里yum安装
两台机器都安装
yum install mariadb-server
3、初始化mariadb,可以忽略
参考:https://www.cnblogs.com/zhangxingeng/p/10882845.html
4、配置master
-------------------修改配置文件(红色部分关键,其他为优化)
[root@web2 my.cnf.d]# cat server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
# # this is read by the standalone daemon and embedded servers
[server] # this is only for the 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 =
sort_buffer_size = 1MB
read_buffer_size = 1MB
read_rnd_buffer_size = 4MB
myisam_sort_buffer_size = 64MB
thread_cache_size =
query_cache_size = 16MB
thread_concurrency = [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=
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=
#innodb_flush_log_at_trx_commit= # this is only for embedded server
[embedded] # This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb] # This group is only read by MariaDB-10.2 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for 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 '';
----------------------查看bin-log及position点
MariaDB [test3]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin. | | | |
+------------------+----------+--------------+------------------+
row in set (0.00 sec)
5、配置slave
--------------------修改配置
[root@web3 my.cnf.d]# cat server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
# # this is read by the standalone daemon and embedded servers
[server] # this is only for the 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 =
sort_buffer_size = 1MB
read_buffer_size = 1MB
read_rnd_buffer_size = 4MB
myisam_sort_buffer_size = 64MB
thread_cache_size =
query_cache_size = 16MB
thread_concurrency =
server-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=
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=
#innodb_flush_log_at_trx_commit= # this is only for embedded server
[embedded] # This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb] # This group is only read by MariaDB-10.2 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for 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里面了
#
# These groups are read by MariaDB command-line tools
# Use it for options 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 connection id is
Server version: 10.2.-MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> change master to master_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
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.216.52
Master_User: tongbu
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: web3-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
row in set (0.00 sec) 需要关注如下几点
引用:https://www.cnblogs.com/kevingrace/p/5685511.html mysql主从同步(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 connection id is
Server version: 10.2.-MariaDB-log MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
MariaDB [(none)]> create database test3;
Query OK, row affected (0.00 sec) MariaDB [(none)]> use test3;
Database changed
MariaDB [test3]> create table t1(id varchar(),name varchar());
Query OK, rows affected (0.02 sec) MariaDB [test3]> show tables;
+-----------------+
| Tables_in_test3 |
+-----------------+
| t1 |
+-----------------+
row in set (0.00 sec) MariaDB [test3]>
---------------slave查看已经同步过来了
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test3 |
+--------------------+
rows in set (0.00 sec) MariaDB [(none)]>
转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/10901452.html
Mariadb/Mysql 主从复制(1)的更多相关文章
- Centos7 mariadb (mysql)主从复制实现
一.mysql基本命令 .启动mysql systemctl start mariadb .linux客户端连接自己 mysql -uroot -p -h 127.0.0.1 .远程链接mysql服务 ...
- Linux(5)- MariaDB、mysql主从复制、初识redis
一.MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...
- Linux - centos7 下 MySQL(mariadb) 和 主从复制
目录 Linux - centos7 下 MySQL(mariadb) 和 主从复制 MySQL(mariadb) 安装MySQL(mariadb) 配置数据库的中文支持 在远程用 mysql客户端去 ...
- MySQL主从复制原理及配置详细过程以及主从复制集群自动化部署的实现
一.复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重 ...
- 分布式数据存储-MySQL主从复制
前言 一.主从复制过程 MySQL的主从复制能力是通过三个线程来实现的,两个在Slave端的I/O和SQL两个线程,还有一个在Master端I/O线程: Binlog dump thread:Mast ...
- MariaDB设置主从复制[转载]
3. MariaDB设置主从复制 标签: mariadbMySQL主从复制 翻译人员: 铁锚 翻译日期: 2013年12月25日 原文链接: Setting Up Replication 主从复制 ...
- 数据库集群 MySQL主从复制
MySQL主从复制 本节内容我们联系使用MySQL的主从复制功能配置Master和Slave节点,验证数据MySQL的数据同步功能. 因为要使用多个MySQL数据库,所以不建议在电脑上安装多个MySQ ...
- mysql主从复制、redis基础、持久化和主从复制
一.mysql(mariadb)基础 1.基础命令(centos7操作系统下) 1.启动mysql systemctl start mariadb 2.linux客户端连接自己 mysql -uroo ...
- centos7下部署mysql主从复制
首先大致看一下这个图 环境说明: 系统:centos7 IP:master:192.168.7.235 slave:192.168.7.226 mysql版本MySQL-5.7 1.Master 下载 ...
随机推荐
- Android填坑—Error:Execution failed for task ':app:transformClassesWithDexForRelease'
昨晚正在干着自己的事,另外一个同学说项目打包不了,但是可以debug运行.又急着需要打包apk发给那边人去测试.真的是搞事情,赶紧打开项目试试打包.项目从之前的$Eclipse$中转过来的,清楚的记得 ...
- Somatic hypermutation (or SHM) is a cellular mechanism by which the immune system adapts to the new foreign elements that confront it (e.g. microbes), as seen during class switching. Somatic hypermut
Somatic hypermutation (or SHM) is a cellular mechanism by which the immune system adapts to the new ...
- java udp通信
package net.kkxm.kms; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.n ...
- TweenJS----前端常用的js动画库,缓动画和复制动画的制作,效果非常好。
TweenJS类库主要用来调整和动画HTML5和Javascript属性,提供了简单并且强大的tweening接口. http://www.createjs.cc/tweenjs/ http://ww ...
- 阶段5 3.微服务项目【学成在线】_day18 用户授权_11-前端集成认证授权-身份校验
把下面赋值到nginx中 前端的服务需要配置一下 重启nginx 启动教学管理的前端 没有登陆直接就进来教学管理的后端了 下面我们要做的就是这两件事 1.前端页面校验用户的身份,如果用户没有登录则跳转 ...
- 123457123457#0#-----com.threeapp.mouseRunner01----儿童老鼠跑酷游戏
com.threeapp.mouseRunner01----儿童老鼠跑酷游戏
- Ubuntu 14.04 apache安装配置
http://jingyan.baidu.com/article/6d704a130c8a0d28da51ca5f.html Ubuntu 14.04 apache安装配置 1.安装 ~# apt-g ...
- Swift学习 (二)
2.控制流: 主要有三种类型的语句 if,switch和新增的guard for,while break,continue 关于if 语句里的条件不再需要使用()包裹了. 1 2 3 4 let nu ...
- iOS-MPMoviePlayerViewController使用
其实MPMoviePlayerController如果不作为嵌入视频来播放(例如在新闻中嵌入一个视频),通常在播放时都是占满一个屏幕的,特别是在 iPhone.iTouch上.因此从iOS3.2以后苹 ...
- 常见问题:计算机网络/运输层/TCP
TCP 面向连接,全双工,点对点. TCP头格式 TCP包没有IP地址,IP地址在网络层的IP协议中,TCP包包括源端口号,目标端口号 一个TCP连接需要四个元祖表明是同一连接(src_ip,src_ ...