mysql5.6 主从同步配置
一:配置前说明
在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 主从同步配置的更多相关文章
- MySQL5.7主从同步配置
主从同步,将主服务器(master)上的数据复制到从服务器(slave). 应用场景 读写分离,提高查询访问性能,有效减少主数据库访问压力. 实时灾备,主数据库出现故障时,可快速切换到从数据库. 数据 ...
- mysql主从同步配置(windows环境)
mysql主从同步配置(mysql5.5,windows环境) A主机(作为主服务器)环境:windows8.mysql5.5 ip:192.168.1.100(自己填) B主机(作为从服务器,由 ...
- Linux下MySQL数据库主从同步配置
说明: 操作系统:CentOS 5.x 64位 MySQL数据库版本:mysql-5.5.35 MySQL主服务器:192.168.21.128 MySQL从服务器:192.168.21.129 准备 ...
- Mysql 5.6主从同步配置
主从同步,本质是利用数据库日志,将主库数据复制一份到从库,本质上是使用了数据复制技术. 本文概要 主库的基本配置 从库的基本配置 完全同步的步骤 注意事项 工作原理 1. 主库的基本配置 做两件事:启 ...
- Docker Mysql主从同步配置搭建
Docker Mysql主从同步配置搭建 建立目录 在虚拟机中建立目录,例如路径/home/mysql/master/data,目录结构如下: Linux中 新建文件夹命令:mkdir 文件夹名 返回 ...
- Docker Mysql数据库主从同步配置方法
一.背景 最近在做内部平台架构上的部署调整,顺便玩了一下数据库的主从同步,特此记录一下操作- 二.具体操作 1.先建立数据存放目录(-/test/mysql_test/) --mysql --mast ...
- DNS 主从同步配置
DNS 主从同步配置 主从同步:主每次修改配置文件需要修改一下序列号,主从同步主要 看序列号. 从DNS:从是可以单独修改,主从不会报错.但从修改后,主端同步给从后 从端修改数据会丢失 主从原理:从会 ...
- centos:mysql主从同步配置(2018)
centos:mysql主从同步配置(2018) https://blog.csdn.net/liubo_2016/article/details/82379115 主服务器:10.1.1.144; ...
- MySql数据主从同步配置
由于需要配置MySQL的主从同步配置,现将配置过程记录下,已被以后不时之需 MySql数据主从同步 1.1. 同步介绍 Mysql的 主从同步 是一个异步的复制过程,从一个 Master复制到另一 ...
随机推荐
- commons-lang
今天在编码的过程中,对于null,采用==null进行判断.并且为了过滤"",使用了str.trim().length()==0,当str为null时,报空指针异常. 于是决定使用 ...
- 【Python练习题】程序3
3.题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?x+100 = n*nx+100+168 = m * m 所以(m+n)*(m-n) =168 #题 ...
- codeforces158C
Cd and pwd commands CodeForces - 158C Vasya is writing an operating system shell, and it should have ...
- codeforces740B
Alyona and flowers CodeForces - 740B Little Alyona is celebrating Happy Birthday! Her mother has an ...
- HTC Vive 基础入门 基于Unity3D引擎
任务2: 01-概述 07:08 任务3: 02-HTC Vive设备的安装 08:33 任务4: 03-下载Steam与SteamVR 03:05 任务5: 04-使用Steam VR 调试设备 1 ...
- 关于mysql 5.7 版本登录时出现错误 1045的随笔
之前学习的时候用的都是oracle 但是现在在工作中大部分用的都是mysql,所以自己也就装了个mysql,下载.安装教程都是从网上百度的,花了挺长时间才装好,心也是挺累的,教程挺多,就是不知道该用哪 ...
- kubernetes增加污点,达到pod是否能在做节点运行
master node参与工作负载 (只在主节点执行)使用kubeadm初始化的集群,出于安全考虑Pod不会被调度到Master Node上,也就是说Master Node不参与工作负载. 这里搭建的 ...
- Python里的单下划线,双下划线,以及前后都带下划线的意义
Python里的单下划线,双下划线,以及前后都带下划线的意义: 单下划线如:_name 意思是:不能通过from modules import * 导入,如需导入需要:from modules imp ...
- 使用树莓派 Raspberry Pi 播放豆瓣 FM
安装 mplayersudo apt-get install mplayer 安装 Python-pipsudo apt-get install python-pip 通过 python-pip 安装 ...
- 聊聊GarbageCollectionNotificationInfo
序本文主要研究一下GarbageCollectionNotificationInfo CompositeDatajava.management/javax/management/openmbean/C ...