一、原理

  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)的更多相关文章

  1. Centos7 mariadb (mysql)主从复制实现

    一.mysql基本命令 .启动mysql systemctl start mariadb .linux客户端连接自己 mysql -uroot -p -h 127.0.0.1 .远程链接mysql服务 ...

  2. Linux(5)- MariaDB、mysql主从复制、初识redis

    一.MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...

  3. Linux - centos7 下 MySQL(mariadb) 和 主从复制

    目录 Linux - centos7 下 MySQL(mariadb) 和 主从复制 MySQL(mariadb) 安装MySQL(mariadb) 配置数据库的中文支持 在远程用 mysql客户端去 ...

  4. MySQL主从复制原理及配置详细过程以及主从复制集群自动化部署的实现

    一.复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重 ...

  5. 分布式数据存储-MySQL主从复制

    前言 一.主从复制过程 MySQL的主从复制能力是通过三个线程来实现的,两个在Slave端的I/O和SQL两个线程,还有一个在Master端I/O线程: Binlog dump thread:Mast ...

  6. MariaDB设置主从复制[转载]

     3. MariaDB设置主从复制 标签: mariadbMySQL主从复制 翻译人员: 铁锚 翻译日期: 2013年12月25日 原文链接:  Setting Up Replication 主从复制 ...

  7. 数据库集群 MySQL主从复制

    MySQL主从复制 本节内容我们联系使用MySQL的主从复制功能配置Master和Slave节点,验证数据MySQL的数据同步功能. 因为要使用多个MySQL数据库,所以不建议在电脑上安装多个MySQ ...

  8. mysql主从复制、redis基础、持久化和主从复制

    一.mysql(mariadb)基础 1.基础命令(centos7操作系统下) 1.启动mysql systemctl start mariadb 2.linux客户端连接自己 mysql -uroo ...

  9. centos7下部署mysql主从复制

    首先大致看一下这个图 环境说明: 系统:centos7 IP:master:192.168.7.235 slave:192.168.7.226 mysql版本MySQL-5.7 1.Master 下载 ...

随机推荐

  1. Git git rm和git rm --cached

    git rm 和 git rm --cached 的区别 git rm file git commit -m "xxx" git push origin master 删除本地及仓 ...

  2. 开发WINDOWS服务程序

    开发WINDOWS服务程序 开发步骤: 1.New->Other->Service Application 2.现在一个服务程序的框架已经搭起来了,打开Service1窗口,有几个属性说明 ...

  3. Android:Recents和AMS中历史任务的区别

    1.1 任务和返回栈 - 实际数据模型  这个是指在调度体系里实际保存的TaskRecord实例,而ActivityRecord-TaskRecord-ActivityStack之间的关系建议看官方文 ...

  4. Android资源文件命名规范学习手册

    [推荐] 资源文件需带模块前缀.[推荐] layout 文件的命名方式. Activity 的 layout 以 module_activity 开头 Fragment 的 layout 以 modu ...

  5. Flutter 中使用Future消除Callback Hell

    /先分别定义各个异步任务 Future<String> login(String userName, String pwd){ ... //用户登录 }; Future<String ...

  6. javascript——语法 && 结构

    原文链接:Understanding Syntax and Code Structure

  7. python学习:python的常用功能示例2

    1. python 写入txt with open("douban.txt","w") as f: f.write("这是个测试!") fi ...

  8. pod install [!] Unable to find a specification for `XXX`

    今天下载了别人的源码学习的时候,执行pod install报错如下: 解决办法: 更新下pod即可. pod update install成功.

  9. JS读取本地文件及目录的方法

    Javascript是网页制作中离不开的脚本语言,依靠它,一个网页的内容才生动活泼.富有朝气.但也许你还没有发现并应用它的一些更高级 的功能吧?比如,对文件和文件夹进行读.写和删除,就象在VB.VC等 ...

  10. Linq to sql中继承类映射转换问题

    类型为的数据成员“Int32 VTOUID”不是类型的映射的一部分.该成员是否位于继承层次结构根节点的上方? 想躲开Linq to sql中问题限制可真是不容易: http://www.makaido ...