mysql5.6 主从同步配置

Posted 九卷沉思录--关注技术架构,产品

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql5.6 主从同步配置相关的知识,希望对你有一定的参考价值。

一:配置前说明

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

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

二:配置主库

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: 配置主库配置文件

开启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

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)

主库配置好了

三:配置从库

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,

[[email protected] 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行错误信息

2018-04-24 13:16:01 5159 [ERROR] /usr/sbin/mysqld: unknown variable ‘master-host=192.168.1.110‘
2018-04-24 13:16:01 5159 [ERROR] Aborting

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

重新启动mysql

[[email protected] 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)

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

报错了,查看错误日志:

2018-04-24 13:21:33 5463 [Note] /usr/sbin/mysqld: ready for connections.
Version: ‘5.6.40-log‘ socket: ‘/var/lib/mysql/mysql.sock‘ port: 3306 MySQL Community Server (GPL)
2018-04-24 13:25:54 5463 [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.
2018-04-24 13:25:54 5463 [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
又出错了

2018-04-24 13:36:44 5787 [Note] /usr/sbin/mysqld: ready for connections.
Version: ‘5.6.40-log‘ socket: ‘/var/lib/mysql/mysql.sock‘ port: 3306 MySQL Community Server (GPL)
2018-04-24 13:36:44 5787 [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.
2018-04-24 13:36:44 5787 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2018-04-24 13:36:44 5787 [Note] Slave SQL thread initialized, starting replication in log ‘mysql_bin.000002‘ at position 120, relay log ‘./localhost-relay-bin.000001‘ position: 4
2018-04-24 13:36:44 5787 [Note] Slave I/O thread: connected to master ‘[email protected]:3306‘,replication started in log ‘mysql_bin.000002‘ at position 120
2018-04-24 13:36:44 5787 [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
2018-04-24 13:36:44 5787 [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

[[email protected] 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.查看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、新开终端,生成主数据库的备份(导出数据库) [[email protected] ~]# mysqldump -uroot -proot student > student.sql
3、将备份文件传到从库 [[email protected] ~]# scp student.sql [email protected]:/root/
4、主库解锁 mysql>unlock tables;

从库操作:

1、停止从库slave
mysql>slave stop;

2、新建数据库student
mysql> create database student default charset utf8;

3、导入数据
[[email protected]-dev ~]# mysql -uroot -proot student<student.sql 

4、查看从库已有该数据库和数据

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

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







































以上是关于mysql5.6 主从同步配置的主要内容,如果未能解决你的问题,请参考以下文章

mysql5.6安装及主从半同步配置

MySQL5.6 数据库主从(Master/Slave)同步安装与配置详解

MySQL5.6 数据库主从(Master/Slave)同步安装与配置详解

MySQL5.6 数据库主从(Master/Slave)同步安装与配置详解

centos7下mysql5.6.30配置单机多实例主从半同步复制

MySQ 数据库主从同步安装与配置详解(Master/Slave)