简介:

Mysql 的主从同步功能,这种解决方案是企业很常见的一种。常用于备份数据库,当客户端操作主库时,主库会产生binlog日志文件,
从库通过复制主库的binlog日志文件,然后解析成相应的 SQL 语句在从库执行,实现主从一致的效果。
这种解决方案只提供了日志的同步执行功能,而从库只能提供读操作,当主服务器发生故障时,必须手动处理故障转移,一般情况下的做法是将一台从服务器改为主服务器。

Master : 192.168.1.88
Slave : 192.168.1.80

一、配置 Master

[root@master ~]# vim /etc/my.cnf

log-bin = mysql-bin # 开启 binlog 日志记录
server-id = # 设置 id 号,此 ID 号全局唯一 [root@master ~]# service mysqld start [root@master ~]# netstat -anpt | grep # 检测 Mysql 是否启动成功 [root@master ~]# chkconfig --add mysqld
[root@master ~]# chkconfig --level mysqld on [root@master ~]# iptables -I INPUT -s 192.168.1.80 -p tcp --dport -j ACCEPT [root@master ~]# service iptables save

二、配置 Slave

[root@slave ~]# vim /etc/my.cnf

server-id =                            # 设置 id 号,此 ID 号全局唯一
relay-log = mysql-relay-bin # 指定 relay-log 日志文件的命名格式
replicate-wild-ignore-table = mysql.% # 过滤不需要复制的表
replicate-wild-ignore-table = test.%
replicate-wild-ignore-table = information_schema.%
replicate-wild-ignore-table = performance_schema.% [root@slave ~]# service mysqld start [root@slave ~]# chkconfig --add mysqld
[root@slave ~]# chkconfig --level mysqld on [root@slave ~]# iptables -I INPUT -s 192.168.1.88 -p tcp --dport -j ACCEPT [root@slave ~]# service iptables save

三、小插曲

四、Master 创建复制用户并授权

[root@master ~]# mysql -u root -p123456

mysql> grant replication slave on *.* to 'rep1_user'@'192.168.1.80' identified by 'rep1_passwd';

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin. | | | |
+------------------+----------+--------------+------------------+
row in set (0.00 sec)

五、Slave 添加 Master 信息

[root@slave ~]# mysql -u root -p123456

mysql> change master to \
-> master_host='192.168.1.88',
-> master_user='rep1_user',
-> master_password='rep1_passwd',
-> master_log_file='mysql-bin.000008',
-> master_log_pos=; mysql> start slave; mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.88
Master_User: rep1_user
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-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: mysql.%,test.%,information_schema.%,performance_schema.%
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_UUID: b5e20203-86b5-11e4-b69c-000c29d099fa
Master_Info_File: /usr/local/mysql/data/master.info
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
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
row in set (0.00 sec)

## 重点关注:Slave_IO_Running 跟 Slave_SQL_Running 这两个主从复制线程,正常情况下这两项都为 YES !

## 还需要关注:Slave_IO_State、Master_Host、Master_Log_File、Read_Master_Log_Pos、Relay_Log_File、Relay_Log_Pos、Relay_Master_Log_File

## Replicate_Wild_Ignore_Table 这项可以看到同步过程中过滤了哪些库(表)。

附:

三、小插曲

## 在做主从同步前,主库已经有数据时,需要做以下处理。

mysql> flush tables with read lock;   # 主库锁表操作,让数据库只能读,不能写

## 保留此终端,重新打开一个 tty ,如果关闭此终端则锁表失效。

[root@master ~]# cd /usr/local/

[root@master local]# tar zcf mysql.tar.gz mysql   # 备份整个数据库

[root@master local]# rsync -a mysql.tar.gz 192.168.1.80:/usr/local/   # 将数据传到从库

[root@slave local]# service mysqld stop     # 关闭从库的 Mysql

[root@slave local]# mv mysql old.mysql      # 将原有 mysql 目录改名

[root@slave local]# tar zxf mysql.tar.gz    # 解压出主库的 mysql 目录

[root@master local]# service mysqld restart # 重启主库 Mysql 服务

[root@slave local]# service mysqld start    # 启动从库 Mysql 服务

Mysql Replication 主从同步的更多相关文章

  1. mysql数据库主从同步

    环境: Mater:   CentOS7.1  5.5.52-MariaDB  192.168.108.133 Slave:   CentOS7.1  5.5.52-MariaDB  192.168. ...

  2. mysql5.5 Replication 主从同步

    mysql5.5 Replication 主从同步 ------------------[主]------------------[mysqld]server-id=1 log-bin=mysql-b ...

  3. MySQL Replication, 主从和双主配置

    MySQL Replication, 主从和双主配置 MySQL的Replication是一种多个MySQL的数据库做主从同步的方案,特点是异步,广泛用在各种对MySQL有更高性能,更高可靠性要求的场 ...

  4. MySQL数据库主从同步安装与配置总结

    MySQL的主从同步是一个很成熟的架构,优点为: ①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力: ②在从主服务器进行备份,避免备份期间影响主服务器服务: ③当主服务器出现问题时 ...

  5. MYSQL配置主从同步

    MYSQL配置主从同步 mysql主服务器配置 vim /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql. ...

  6. mysql数据库主从同步读写分离(一)主从同步

    1.mysql数据库主从同步读写分离 1.1.主要解决的生产问题 1.2.原理 a.为什么需要读写分离? 一台服务器满足不了访问需要.数据的访问基本都是2-8原则. b.怎么做?  不往从服务器去写了 ...

  7. mysql 之 主从同步(单向同步和双向同步)

    一. 实验环境部署 主服务器(MySQL-01) IP: 192.168.8.241  端口3306  ,操作系统:Centos6.5 64位 从服务器(MySQL-02)  IP: 192.168. ...

  8. Linux下MySQL数据库主从同步配置

    说明: 操作系统:CentOS 5.x 64位 MySQL数据库版本:mysql-5.5.35 MySQL主服务器:192.168.21.128 MySQL从服务器:192.168.21.129 准备 ...

  9. MySQL数据库主从同步实战过程

       Linux系统MySQL数据库主从同步实战过程 安装环境说明 系统环境: [root@~]# cat /etc/redhat-release CentOS release 6.5 (Final) ...

随机推荐

  1. bzoj 2002 Bounce 弹飞绵羊

    bzoj 2002 Bounce 弹飞绵羊 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树. 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后 ...

  2. python面试题(十)

    Python中基本数据结构的操作 元组 列表 字典 集合 定义 新增 更改 删除 2.请尽可能列举python列表的成员方法,并给出一下列表操作的答案: (1)a=[1, 2, 3, 4, 5], a ...

  3. anycast简单总结

    一针见血,言简意赅的总结 bgp+anycast就是不同服务器用了相同的ip地址 anycast 技术特点 bgp+anycast就是多个主机使用相同ip地址的一种技术,当报文发给该地址时,根据路由协 ...

  4. poj 2187 Beauty Contest——旋转卡壳

    题目:http://poj.org/problem?id=2187 学习材料:https://blog.csdn.net/wang_heng199/article/details/74477738 h ...

  5. Java 实现 RSA 非对称加密

    非对称加密算法:用两个密钥(公钥/私钥)对数据进行加密和解密. 一.原理 非对称加密算法实现机密信息交换的基本过程是: 1)甲方生成一对密钥并将其中的一把作为公用密钥向其它方公开; 2)得到该公用密钥 ...

  6. 一篇文章学LINQ(原创)

    本篇文章主要介绍linq的基本用法,采用sql和linq比较的方式由浅入深进行学习, 注意:此文章是根据真实表来进行案例说明,表结构如下:  表1:    Student(学生表)           ...

  7. callback&&callback()

    如果存在回调函数就执行!这是利用了 JS &&符号的一个小技巧&& 符号在前面为假时就不会执行后面的语句了所以这个就相当于 if(callback){ callback ...

  8. linux用户,组,文件等操作

    参考: https://blog.csdn.net/chengqiuming/article/details/78601977   , https://www.cnblogs.com/123-/p/4 ...

  9. python 面向对象(类的特殊成员)

    python 面向对象: (思维导图 ↑↑↑↑) 类的特殊成员 python的类成员存在着一些具有特殊含义的成员 1.__init__: 类名() 自动执行 __init__ class Foo(ob ...

  10. Hibernate学习4—关联关系一对多映射

    一.班级学生一对多映射实现(单向) Class 1 * Student 一对多 在学生多的一方,配置一的对象:   com.cy.model.Class: package com.cy.model; ...