• 主从复制原理

  Mysql的Replication是一个异步的复制过程,从一个Mysql Instance(master)复制到另一个Mysql Instance(slave)。中间需要三个线程slave端有1个I/O线程,一个SQL线程,Master端一个I/O线程。

  要实现Mysql的Replication,首先在Master端打开Binary log(mysql-bin.xxxxxx)功能。因为原理是slave从Master端获取mysql-bin.xxxxxx,然后在slave端按照顺序依次执行日志中的各种操作。可以在my.cnf配置文件的[mysqld]组中添加"log-bin"参数项。

  • 复制的基本过程:

  a、Slave的IO线程链接多Master,并请求日志文件的指定位置之后的内容。

  b、Master接收到请求后,负责复制的IO线程根据请求的信息读取指定日志指定位置的日志信息,返回给Slave的IO线程。内容还包括本次返回的信息在Master端的Binary Log的日志文件名和位置。

  c、Slave端的IO线程街道信息后,将内容写入Slave端的Relay Log(mysql-relay-log.xxxxxx)末端,并将Master端的bin-log文件和位置记录到master-info文件中,以便Slave的IO线程下次连接Master的时候使用。

  d、Slave端的SQL线程检测到Relay Log中的新内容后,马上解析Log内容,还原成在Master端的真实执行的Query语句,并执行。两端执行了相同的Query语句,二者数据同步。

  • 准备工作

从原理的分析中可以看到,实现Mysql Replication 至少需要两台Mysql实例。

这里在虚拟机vmware中准备了两个台centos5,因为同时开多个虚拟机会很卡,就简单的开启两个吧。

Master:192.168.80.7

Slave:192.168.80.6

wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.21.tar.gz
yum install vim* -y
yum install -y gcc gcc-c++
yum install -y bison*
yum install cmake
yum install ncurses* -y
  • 安装Mysql Server
tar zxvf mysql-5.6.21.tar.gz  -C /usr/local/src/
cd /usr/local/src/mysql-5.6.21/
groupadd mysql
useradd -r -g mysql mysql
cmake .
make install
cd /usr/local/mysql/
chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data
bin/mysqld_safe --user=mysql & #测试mysql是否安装成功
cp support-files/mysql.server /etc/init.d/mysql
chkconfig --add mysql #将mysql脚本加入服务 可以使用service管理
ln -s `pwd`/my.cnf /etc/my.cnf
  • Master端

修改Master端的主配置文件

[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=test
binlog-ignore-db=mysql

备份Master的data目录

这里采用停库,冷备份。实际上还可以热备份需要锁库

flush tables with read tables;
unlock tables;

service mysql stop
tar czvf data.tar.gz /usr/local/mysql/data ###/usr/local/mysql/data 为Master端mysql的datadir 将data.tar.gz覆盖掉从服务器的datadir

 

  • Slave端

将data.tar.gz覆盖掉从服务器的datadir

获取Master的Bin-log的Log Postion

show Master  status

| mysql-bin.000003 |      120 | test,test    | mysql,mysql      |  

在Master端,创建复制Binary Log日志的用户【才操作需要再Master端执行】

create user 'repl'@'%' identified by '123456';
grant replication slave on *.* to 'repl'@'%';
flush privileges; #如不及时刷新会有错误

接下来需要修改Slave的主配置文件

[mysqld]
server-id=3
log-bin=mysql-bin
binlog-do-db=test
binlog-ignore-db=mysql

修改完以后,重启salve端的mysql服务

service mysql restart

在mysql里执行:

mysql>CHANGE MASTER TO
MASTER_HOST='192.168.80.7',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=120;
start slave

至此,Mysql Replaction搭建完成。

  • 检查是否正常执行

在Master端执行

show master status;
| mysql-bin.000001 | 120 | test,test | mysql,mysql |

显示如上图说明配置成功。

在Slave端,

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.7
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1291
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 1454
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: 1291
Relay_Log_Space: 1631
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: 22dd6712-695b-11e4-b733-000c292f9c4c
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 the slave I/O thread to update it
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
1 row in set (0.00 sec)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
这两个线程必须正常。
显示结果如上则表示Mysql Replication正常工作。 可以在Master端做一个CURD操作,来检查是否正常同步。 /********************************************************/
  • 可能的问题:

我安装的时候出现的问题有
1、创建用户的时候 出现这个错误'repl@%',应该为 'repl'@'%'
2、因为复制data目录的时候将data目录下的auto.cnf也复制了。
auto.cnf的内容如下:

[auto]
server-uuid="xxxx"

导致uuid重复的错误。

3、配置 change master to的时候

CHANGE MASTER TO
MASTER_HOST='192.168.80.7',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=120;

将master_log_file的名称写错了.写成了'mysql_bin.00003'

在这里还可能用到这些命令

show variables like 'server_id';
set global server_id=2;

/**************************************/

  • 对于uuid

查看uuid:
blkid
ls -l /dev/disk/by-uuid/
dumpe2fs /dev/sda6 | less (查看指定设备的UUID)

产生uuid:
uuidgen
为设备指定uuid
sudo tune2fs -U `uuidgen` device

另一篇,可以参考:http://www.cnblogs.com/luckcs/articles/2543607.html

搭建mysql主从复制---Mysql Replication的更多相关文章

  1. Linux下Mysql主从复制(Master-Slave)与读写分离(Amoeba)实践

    一.为什么要做Mysql的主从复制(读写分离)?通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低.为了提升业务系统性能,优化用户体验,可以通过做主从复制(读写分离)来减轻 ...

  2. mysql 主从复制以及binlog 测试

    ###mysql查看binlog日志内容 https://blog.csdn.net/nuli888/article/details/52106910 mysql的binlog日志位置可通过show ...

  3. MySQL主从复制&读写分离&分库分表

    MySQL主从复制 MySQL的主从复制只能保证主机对外提供服务,从机是不提供服务的,只是在后台为主机进行备份数据 首先我们说说主从复制的原理,这个是必须要理解的玩意儿: 理解: MySQL之间的数据 ...

  4. MySQL主从复制及读写分离

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

  5. linux mysql主从复制

    centos7 安装 mariadb 1 yum 源  -- 配置阿里的 2 rmp 方式 3 源码编译方式  -- 专业DBA 一些知识点: 虚拟环境 不影响 redis/ mariadb/mysq ...

  6. mysql主从复制(简单直观)

    mysql主从复制   mysql主从复制(超简单) 怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作:  1.1.版本一致  1.2.初始化表,并在后 ...

  7. mysql主从复制(linux下)

    转至:http://369369.blog.51cto.com/319630/790921 怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作:   1. ...

  8. MySQL主从复制与读写分离实践

    MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践  目录: 介绍 MySQL的安装与配置 MySQL主从复制 MySQL读写分离 编译安装lua 安装配置MySQ ...

  9. centos 配置mysql主从复制

    mysql+centos7+主从复制   MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公 ...

随机推荐

  1. [转]非常好的vsftpd安装于配置

    环境:CentOS 5.0 操作系统一.安装:1.安装Vsftpd服务相关部件:[root@KcentOS5 ~]# yum install vsftpd*Dependencies Resolved= ...

  2. 深入浅出JMS(一)——JMS简单介绍

    假设手机仅仅能进行实时通话,没有留言和短信功能会怎么样?一个电话打过来,正好没有来得及接上,那么这个电话要传递的信息肯定就收不到了.为什么不能先将信息存下来,当用户须要查看信息的时候再去获得信息呢?伴 ...

  3. systemtap 列出所有linux 内核模块与相关函数0

    diskiohttp://blog.163.com/digoal%40126/blog/static/16387704020131015105532435/ [root@localhost linux ...

  4. careercup-树与图 4.5

    4.5 实现一个函数,检查一棵二叉树是否为二叉查找树. 参考:http://blog.csdn.net/sgbfblog/article/details/7771096 C++实现代码: #inclu ...

  5. html 笔记

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. 利用Qt调用计算器

    之前有了第一个项目那么很快就会有第二个 这次 我们来调用 一些系统函数. 就不从头写了. 直接写比较重要的地方,如果又不太懂的地方欢迎小纸条或者参见利用 QT制作一个 helloworld http: ...

  7. redis resque消息队列

    Resque 目前正在学习使用resque .resque-scheduler来发布异步任务和定时任务,为了方便以后查阅,所以记录一下. resque和resque-scheduler其优点在于功能比 ...

  8. JQuery对象与DOM对象分析

    一.定义: DOM对象(文档对象模型):暂时这么理解:通过JavaScript获取的HTML元素,称为DOM对象.如:var domID=document.getElementById("i ...

  9. Android开发5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  10. 趣拍proguard配置

    # Add project specific ProGuard rules here.# By default, the flags in this file are appended to flag ...