一:配置前说明

在centos 6环境下配置 mysql 5.6主从同步

准备两台测试的虚拟机,2台虚拟机上都安装mysql软件,并开启mysql服务
主master : 192.168.1.110
从slave : 192.168.1.109

二:配置主库

2.1: 授权给从数据库服务器

mysql> grant replication slave on *.* to 'rep1'@'192.168.1.109' identified by 'test123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

2.2: 配置主库配置文件

开启binlog,并设置server-id,每次修改配置文件后都要重启mysql服务才会生效
vi /usr/my.cnf

server-id=1
log_bin=mysql_bin
binlog-do-db=student
binlog-ignore-db=mysql
binlog_ignore_db=information_schema log-error=/var/lib/mysql/mysql_error.log # 错误日志配置
general_log=on #开启普通日志
general_log_file=/var/lib/mysql/mysql_general_log.log # 普通日志配置

说明:

server-id:master端的ID号;
log-bin:同步的日志路径及文件名,一定注意这个目录要是mysql有权限写入的(我这里是偷懒了,直接放在了下面那个datadir下面);
binlog-do-db:要同步的数据库名
还可以显示 设置不同步的数据库:
binlog-ignore-db = mysql 不同步mysql库和test库
binlog-ignore-db = test

修改配置文件后,重启服务:

service mysqld restart

2.3: 查看主服务器当前二进制日志名和偏移量

这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复

mysql> show master status;
+--------------------------+----------+------------------+-------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+----------+--------------------+--------------------------+-------------------+
| mysql_bin.000002 | 120 | student | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)

主库配置好了

三:配置从库

3.1:配置文件

vi /usr/my.cnf

server-id=2
#log_bin=/var/log/mysqlslave-bin.log #这里的log_bin和下面的log-bin是一个意思,配置其一就好了
log-bin=mysql-bin
master-host=192.168.1.110
master-user=rep1
master-password=test123456
replicate-do-db=student
replicate-ignore-db=mysql
replicate-ignore-db=infomation_schema log-error=/var/lib/mysql/mysql_error.log
general_log=on
general_log_file=/var/lib/mysql/mysql_general_log.log

配置完成后,重启mysql,

[root@localhost mysql]# service mysql restart
Shutting down MySQL.... SUCCESS!
Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).

然后就报错了
查看vi /var/lib/mysql/mysql_error.log日志,发现2行错误信息

[ERROR] /usr/sbin/mysqld: unknown variable 'master-host=192.168.1.110'
[ERROR] Aborting

说明mysql不认识这些变量,网上搜索,原因是mysql5.5+版本主从复制不支持这些变量,需要在从库上用命令来设置,那我们就来设置了,先注释掉3个变量
master-host=192.168.1.110
master-user=rep1
master-password=test123456

重新启动mysql

[root@localhost mysql]# service mysql start
Starting MySQL. SUCCESS!

用root用户登录进mysql, 用下面的命令设置

mysql> change master to master_host='192.168.1.110', master_port=3306, master_user='rep1', master_password='test123456',master_log_file='mysql_bin.000002',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

3.2:启动slave进程

mysql> slave start;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slave start' at line 1

报错了,查看错误日志:

[Note] /usr/sbin/mysqld: ready for connections.
Version: '5.6.40-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
[Warning] Neither --relay-log nor --relay-log-index were used; so replication may break when this MySQL server acts as a slave and has his hostname changed!! Please use '--relay-log=localhost-relay-bin' to avoid this problem.
[Note] 'CHANGE MASTER TO executed'. Previous state master_host='', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='192.168.1.110', master_port= 3306, master_log_file='mysql_bin.000002', master_log_pos= 120, master_bind=''.

看错误日志信息,是前面的没有设置导致的mysql_port, master_log_file的值为空和后面的设置的相冲突了,重启mysql试试看 service mysql restart
又出错了

[Note] /usr/sbin/mysqld: ready for connections.
Version: '5.6.40-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
[Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
[Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
[Note] Slave SQL thread initialized, starting replication in log 'mysql_bin.000002' at position 120, relay log './localhost-relay-bin.000001' position: 4
[Note] Slave I/O thread: connected to master 'rep1@192.168.1.110:3306',replication started in log 'mysql_bin.000002' at position 120
[ERROR] Slave I/O: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593
[Note] Slave I/O thread exiting, read up to log 'mysql_bin.000002', position 120

上网搜索原因
参考:https://blog.csdn.net/cug_jiang126com/article/details/46846031
原因分析:
mysql 5.6的复制引入了uuid的概念,各个复制结构中的server_uuid得保证不一样,但是查看到直接copy data文件夹后server_uuid是相同的,show variables like '%server_uuid%';
解决方法:
找到data文件夹下的auto.cnf文件,修改里面的uuid值,保证各个db的uuid不一样,重启db即可

修改auto.cnf中的uuid配置
vi /var/lib/mysql/auto.cnf
修改server-uuid这个值,跟master主数据库的不同就好,重启mysql

[root@localhost mysql]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!

进入mysql的, 运行 slave start; 命令
报错

mysql> slave start;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slave start' at line 1

看日志,并没有发现error级别的错误, 而且复制的线程已经初始化了,所以应该是重启mysql之后,mysql会记住之前的slave start命令,把复制命令运行了

3.3:查看slave的状态

show slave status\G;
后面有个\G的结束符号,是可以格式化的来看信息,不然出来的信息就是一团乱的,

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.110
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000002
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql_bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: student
Replicate_Ignore_DB: mysql,infomation_schema
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: 120
Relay_Log_Space: 460
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: 9a672e1c-471e-11e8-a9e3-08002708e616
Master_Info_File: /var/lib/mysql/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_State: Waiting for master to send event
配置成功了

四:同步主库已有数据到从库  

主库操作:

1、停止主库的数据更新操作
mysql>flush tables with read lock;
2、新开终端,生成主数据库的备份(导出数据库)
[root@zhoujietest ~]# mysqldump -uroot -proot student > student.sql
3、将备份文件传到从库
[root@zhoujietest ~]# scp student.sql root@192.168.1.109:/root/
4、主库解锁
mysql>unlock tables;

从库操作:

1、停止从库slave
mysql>slave stop; 2、新建数据库student
mysql> create database student default charset utf8; 3、导入数据
[root@ops-dev ~]# mysql -uroot -proot student<student.sql 4、查看从库已有该数据库和数据

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
|student |
| mysql |
| performance_schema |
| test |
+--------------------+

此时主从库的数据完全一致,如果对主库进行增删改操作,从库会自动同步进行操作。

mysql5.6 主从同步配置的更多相关文章

  1. MySQL5.7主从同步配置

    主从同步,将主服务器(master)上的数据复制到从服务器(slave). 应用场景 读写分离,提高查询访问性能,有效减少主数据库访问压力. 实时灾备,主数据库出现故障时,可快速切换到从数据库. 数据 ...

  2. mysql主从同步配置(windows环境)

    mysql主从同步配置(mysql5.5,windows环境)   A主机(作为主服务器)环境:windows8.mysql5.5 ip:192.168.1.100(自己填) B主机(作为从服务器,由 ...

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

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

  4. Mysql 5.6主从同步配置

    主从同步,本质是利用数据库日志,将主库数据复制一份到从库,本质上是使用了数据复制技术. 本文概要 主库的基本配置 从库的基本配置 完全同步的步骤 注意事项 工作原理 1. 主库的基本配置 做两件事:启 ...

  5. Docker Mysql主从同步配置搭建

    Docker Mysql主从同步配置搭建 建立目录 在虚拟机中建立目录,例如路径/home/mysql/master/data,目录结构如下: Linux中 新建文件夹命令:mkdir 文件夹名 返回 ...

  6. Docker Mysql数据库主从同步配置方法

    一.背景 最近在做内部平台架构上的部署调整,顺便玩了一下数据库的主从同步,特此记录一下操作- 二.具体操作 1.先建立数据存放目录(-/test/mysql_test/) --mysql --mast ...

  7. DNS 主从同步配置

    DNS 主从同步配置 主从同步:主每次修改配置文件需要修改一下序列号,主从同步主要 看序列号. 从DNS:从是可以单独修改,主从不会报错.但从修改后,主端同步给从后 从端修改数据会丢失 主从原理:从会 ...

  8. centos:mysql主从同步配置(2018)

    centos:mysql主从同步配置(2018) https://blog.csdn.net/liubo_2016/article/details/82379115 主服务器:10.1.1.144; ...

  9. MySql数据主从同步配置

    由于需要配置MySQL的主从同步配置,现将配置过程记录下,已被以后不时之需 MySql数据主从同步   1.1. 同步介绍 Mysql的 主从同步 是一个异步的复制过程,从一个 Master复制到另一 ...

随机推荐

  1. spring @Validated 注解开发中使用group分组校验

    之前知道spring支持JSR校验,在自己定义的bean中加入@NotNull,@NotBlank,@Length等之类的校验用于处理前台传递过来的request请求,避免在写多余的代码去处理. 但是 ...

  2. 【python练习题】程序14

    #题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. #我的方法应该比网上的更加简洁,只是递归可能速度慢 n = input('请输入一个正整数:') n = int(n) X ...

  3. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [一] 初衷与架构设计

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 五.如何做全站采集 为什么要造轮子 同学们可以去各大招聘网站查看一下爬虫工程师 ...

  4. java读取excel获取数据写入到另外一个excel

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  5. BZOJ3105 新Nim游戏 【拟阵】

    题目分析: 我不知道啥是拟阵啊,但有大佬说线性基相关的都是拟阵,所以直接贪心做了. 题目代码: #include<bits/stdc++.h> using namespace std; ; ...

  6. Matplotlib学习---用matplotlib和sklearn画拟合线(line of best fit)

    在机器学习中,经常要用scikit-learn里面的线性回归模型来对数据进行拟合,进而找到数据的规律,从而达到预测的目的.用图像展示数据及其拟合线可以非常直观地看出拟合线与数据的匹配程度,同时也可用于 ...

  7. 用递归方法计算斐波那契数列(Recursion Fibonacci Sequence Python)

    先科普一下什么叫斐波那契数列,以下内容摘自百度百科: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因意大利数学家列昂纳多·斐波那契(Leonardoda Fibonacci ...

  8. Nginx反向代理服务器

    安装Nginxyum -y install nginx 修改并添加配置文件vi /etc/nginx/nginx.conf在HTTP模块中添加: client_header_timeout 3000; ...

  9. 【luogu3733】【HAOI2017】 八纵八横 (线段树分治+线性基)

    Descroption 原题链接 给你一个\(n\)个点的图,有重边有自环保证连通,最开始有\(m\)条固定的边,要求你支持加边删边改边(均不涉及最初的\(m\)条边),每一次操作都求出图中经过\(1 ...

  10. Leetcode 11.盛最多水的容器 By Python

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...