MySQL 5.6主从同步配置案例分享

本文环境

主库:Redhat 6.5 x64 192.168.1.180 mysql-5.6.35

备库:Redhat 6.5 x64 192.168.1.181 mysql-5.6.35

一、常规配置方式一

1. mysql主服务器配置

增加如下配置:

# vi /etc/my.cnf

[mysqld]

log-bin                = master-bin

log-bin-index          = master-bin.index

binlog_format          = mixed

server-id              = 1

修改my.cnf后重启mysql

#/etc/init.d/mysqld restart

[root@tmaster bin]# /etc/init.d/mysqld restart

Shutting down MySQL..                                      [  OK  ]

Starting MySQL.                                                 [  OK  ]

[root@tmaster bin]# mysql -uroot -p

Enter password:

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

Your MySQL connection id is 1

Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

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

mysql> show master status;

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

| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| master-bin.000002 |     2290 |              |                  |                   |

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

1 row in set (0.00 sec)

mysql>

2. 主服务器配置同步复制帐号

mysql>grant replication slave on *.* to 'repl'@'%' identified by '123456';

--刷新一下权限

mysql>flush privileges;

3. mysql从服务器配置

说明: 默认只要server-id不相同即可。

--增加如下配置:

# vi /etc/my.cnf

[mysqld]

log-bin                = slave-bin

binlog_format          = mixed

server-id              = 11

relay-log              = slave-relay-bin

relay-log-index        = slave-relay-bin.index

配置说明: 不同步mysql库可以实现主从库有不同的帐号权限,经过测试,mysql5.6.29中只在从库中配置有效。

其它参数:

binlog-do-db                = mydb    仅同步一个数据库

#replicate-ignore-db        = mysql   忽略掉mysql库,该参数产生很多意外的同步问题,还是不使用。

replicate_wild_ignore_table = mysql.% 忽略掉mysql库

最终从库my.cnf内容如下:

###

log-bin                = slave-bin

binlog_format          = mixed

server-id              = 11

relay-log              = slave-relay-bin

relay-log-index        = slave-relay-bin.index

binlog-do-db                = mydb

replicate_wild_ignore_table = mysql.%

--从库mysql重启后方可生效

# /etc/init.d/mysqld restart

[root@tslave ~]# /etc/init.d/mysqld restart

Shutting down MySQL..                                      [  OK  ]

Starting MySQL.                                            [  OK  ]

4. 测试示例

CREATE DATABASE mydb;

CREATE TABLE user (

id varchar(20) NOT NULL,

username varchar(20) NOT NULL,

password char(32) NOT NULL,

PRIMARY KEY (id)

) ;

INSERT INTO user  VALUES ('1', 'koumm', '123456');

INSERT INTO user  VALUES ('2', 'zhangsan', '123456');

INSERT INTO user  VALUES ('3', 'lisi', '123456');

INSERT INTO user  VALUES ('4', 'li2si', '123456');

INSERT INTO user  VALUES ('5', 'abc', '123456');

INSERT INTO user  VALUES ('6', 'tom', '123456');

INSERT INTO user  VALUES ('7', 'jk', '123456');

INSERT INTO user  VALUES ('8', 'xb', '123456');

5. 正常的主从配置过程

(1) 主库锁表

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

mysql> show master status;

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

| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| master-bin.000002 |     2290 |              |                  |                   |

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

mysql> show master logs;

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

| Log_name          | File_size |

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

| master-bin.000001 |       420 |

| master-bin.000002 |      2290 |

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

2 rows in set (0.00 sec)

(2) 主库备份

[root@master ~]# mysqldump -uroot -p -B mydb >/mysql/backu/mydb.sql

说明:-B参数有建库语句。

(3) 主库解开锁表功能

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

mysql>

6. 从库导入数据库,输入密码后回车

[root@slave ~]# mysql -uroot -p

Enter password:

7. 从库配置同步过程

(1) 配置同步,手动执行同步参数,该配置会写入master.info文件中。

mysql> CHANGE MASTER TO

-> MASTER_HOST='192.168.1.180',

-> MASTER_PORT=3306,

-> MASTER_USER='repl',

-> MASTER_PASSWORD='123456',

-> MASTER_LOG_FILE='master-bin.000002',

-> MASTER_LOG_POS=2290;

Query OK, 0 rows affected, 2 warnings (0.02 sec)

(2) 启动从同步进程

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.180

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: master-bin.000002

Read_Master_Log_Pos: 2290

Relay_Log_File: slave-relay-bin.000002

Relay_Log_Pos: 284

Relay_Master_Log_File: master-bin.000002

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.%

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 2290

Relay_Log_Space: 457

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: 7baedb0e-5ea0-11e8-8692-000c292185b2

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)

ERROR:

No query specified

#查看如下两个参数为YES,说明从库运行正常。

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

(3) 测试同步情况

# 主库插入一条记录

mysql> use mydb

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> show tables;

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

| Tables_in_mydb |

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

| user           |

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

1 row in set (0.00 sec)

mysql> select * from user;

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

| id | username | password |

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

| 1  | koumm    | 123456   |

| 2  | zhangsan | 123456   |

| 3  | lisi     | 123456   |

| 4  | li2si    | 123456   |

| 5  | abc      | 123456   |

| 6  | tom      | 123456   |

| 7  | jk       | 123456   |

| 8  | xb       | 123456   |

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

8 rows in set (0.00 sec)

mysql> insert into user values ('9','liang','654321');

Query OK, 1 row affected (0.01 sec)

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from user;

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

| id | username | password |

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

| 1  | koumm    | 123456   |

| 2  | zhangsan | 123456   |

| 3  | lisi     | 123456   |

| 4  | li2si    | 123456   |

| 5  | abc      | 123456   |

| 6  | tom      | 123456   |

| 7  | jk       | 123456   |

| 8  | xb       | 123456   |

| 9  | liang    | 654321   |

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

9 rows in set (0.00 sec)

# 从库查询同步情况

mysql> use mydb

Database changed

mysql> select * from user;

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

| id | username | password |

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

| 1  | koumm    | 123456   |

| 2  | zhangsan | 123456   |

| 3  | lisi     | 123456   |

| 4  | li2si    | 123456   |

| 5  | abc      | 123456   |

| 6  | tom      | 123456   |

| 7  | jk       | 123456   |

| 8  | xb       | 123456   |

| 9  | liang    | 654321   |

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

9 rows in set (0.00 sec)

三、主从同步简单管理

1. 停止MYSQL同步

STOP SLAVE IO_THREAD;     #停止IO进程

STOP SLAVE SQL_THREAD;    #停止SQL进程

STOP SLAVE;               #停止IO和SQL进程

2. 启动MYSQL同步

START SLAVE IO_THREAD;    #启动IO进程

START SLAVE SQL_THREAD;   #启动SQL进程

START SLAVE;              #启动IO和SQL进程

3. 重置MYSQL同步

RESET SLAVE;

清除主从同步参数,它会删除master.info和relay-log.info文件,以及所有的中继日志,并启动一个新的中继日志。

适用重新再次配置一次从库的情况。

4. 查看MYSQL同步状态

SHOW SLAVE STATUS;

5. 临时跳过MYSQL同步错误

确保数据一致的情况下临时的跳过这个错误操作如下,有可能要多次。

STOP SLAVE;

SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;

START SLAVE;

# vi /etc/my.cnf

[mysqld]

#slave-skip-errors=1062,1053,1146 #跳过指定error no类型的错误

#slave-skip-errors=all            #跳过所有错误

原来参考:https://www.linuxidc.com/Linux/2016-04/130729.htm

其它相关资料参考:

实现两个MySQL数据库之间的主从同步 http://www.linuxidc.com/Linux/2016-02/128100.htm

Linux环境中MySQL主从同步--添加新的从库 http://www.linuxidc.com/Linux/2015-08/122448.htm

通过 XtraBackup 实现不停机不锁表搭建MySQL主从同步 http://www.linuxidc.com/Linux/2015-08/121806.htm

MySQL主从同步配置记录 http://www.linuxidc.com/Linux/2015-07/119939.htm

Linux下MySQL数据库主从同步配置 http://www.linuxidc.com/Linux/2016-03/129138.htm

5.5.35 - mysql 同步_MySQL 5.6.35主从同步配置案例相关推荐

  1. php解决mysql主从同步_Mysql读写分离,主从同步实现

    随着用户量的增多,数据库操作往往会成为一个系统的瓶颈所在,因此我们可以通过实现数据库的读写分离来提高系统的性能. 通过设置主从数据库实现读写分离,主库负责"写"操作,从库负责&qu ...

  2. 用Semisynchronous Replication半同步,mk-table-checksum数据同步一致性检查,监控主从同步运行状态的脚本等方式加强SQL性能和数据审核...

    一,为A组在X单独另建一个MYSQL服务器(MYSQL B),A组平时更新的数据库为MYSQL B.待MYSQL B的数据审核准确无误后.发布给现在布置在X上的MYSQL服务器(MYSQL A).并进 ...

  3. mysql同步到另一台服务器_mysql两台服务器主从同步

    A服务器: host 192.168.1.101 port 3306 B服务器: host 192.168.1.102 port 3306 1,授权用户: A服务器 mysql>grant re ...

  4. 实战演练:MySQL手动注册binlog文件造成主从同步异常

    m 墨墨导读:本文是作者用MySQL数据库手动注册binlog文件造成主从同步异常后,详述整个分析与解决的过程. 云和恩墨大讲堂线上分享:<weblogic 优化> 时间:2019年7月3 ...

  5. 多mysql实例下开发需要注意主从同步延迟

    今天晚上服务器上线测试,遇到了一个问题! 往数据库写了一条数据之后,再读取该记录,居然读不出来,报空指针.十分费解,喊来开发组长定位问题.他的解释是:写操作用的是主库,而读操作用的是从库.在写库写完之 ...

  6. Mysql的高可用方案及主从详细配置

    1.常用的高可用MySQL解决方案: 数据库作为最基础的数据存储服务之一,在存储系统中有着非常重要的地位,因此要求其具备高可用性无可厚非.能实现不同SLA(服务水平协定)的解决方案有很多种,这些方案可 ...

  7. 时间立即同步命令_Redis复制:主从同步

    一.概述 在高并发服务当中,如果使用单个Redis实例,由于Redis采用单进程单线程处理所有请求的方式,即每次只有一个请求在处理,后面的请求排队,如果前面请求执行时间长了,则会影响后面所有请求.所以 ...

  8. mariadb mysql同步_mysql/mariadb 主从复制实现数据库同步

    环境: 主服务器:centos 6.0 mysql 5.1.67-log IP:192.168.0.107从服务器:centos 6.0 mysql 5.1.67-log IP:192.168.0.1 ...

  9. mysql 停止同步_MYSQL从库数据冲突导致同步停止

    在兼职DBA的工作中,由于同事在数据库上误操作,导致数据库同步停止,情况如下:mysql>show slave status\G; *************************** 1.  ...

最新文章

  1. android pjsip 2.5编译,在Android中构建PJSiP时出错
  2. python【数据结构与算法】二分归并模版
  3. python爬虫获取的网页数据为什么要加[0-使用 Python 爬取网页数据
  4. java postdelayed_Android开发使用Handler的PostDelayed方法实现图片轮播功能
  5. 电子科技大学计算机读博好毕业,高产博士生读博一年达毕业要求:写论文不无聊...
  6. 电信计算机知识考试,2020中国电信考试试题——专业知识一
  7. 使用html5+的plus调起相机拍照,使用canvas压缩图片,转成base64传到后台
  8. 讨论群问题:Linux 下的批量操作,第2种很赞
  9. 使用Maven构建Java项目
  10. [老老实实学WCF] 第一篇 Hello WCF
  11. 虚拟机一直安装程序正在启动服务器失败,安装使用Vmware出现的问题及解决方法...
  12. EditPlus 快捷键大全
  13. 使用esxcli命令升级VMware ESXi补丁
  14. 【备考初赛】1.1 计算机常识
  15. 开源项目学习:XINS
  16. 读《Machine Learning Done Wrong》(机器学习易犯错误)有感
  17. 常见互联网域名及含义
  18. 今日头条下拉框推广,出效果付费
  19. 官方示例(十):网页开发3D粒子系统实现降雨效果 ThingJS
  20. linux网络检查常用命令

热门文章

  1. 《程序设计技术》第二章例程
  2. HDU2024 C语言合法标识符【入门】
  3. matlab 工具函数(一) —— 添加指定 SNR 的噪声
  4. windows 系统文件 —— 特殊文件及文件类型
  5. and/or(||)的理解
  6. 实用的 Python —— base64
  7. UNIX 环境高级编程(一) apue.h 文件与apue.3e的安装
  8. 强悍的 Linux —— 权限管理(组及用户管理)
  9. matlab 实用程序片段
  10. Tricks(二十三)—— Python 实现树这种数据结构