mysql5.6 主從同步配置

一:配置前說明

在centos 6環境下配置 mysql 5.6主從同步php

準備兩臺測試的虛擬機,2臺虛擬機上都安裝mysql軟件,並開啓mysql服務
主master : 192.168.1.110
從slave : 192.168.1.109mysql

二:配置主庫

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

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 = testcentos

 修改配置文件後,重啓服務:服務器

service mysqld restart

2.3: 查看主服務器當前二進制日誌名和偏移量

  這個操做的目的是爲了在從數據庫啓動後,從這個點開始進行數據的恢復app

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)

主庫配置好了dom

三:配置從庫

3.1:配置文件

vi /usr/my.cnfsocket

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,ide

[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 

四、查看從庫已有該數據庫和數據

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

此時主從庫的數據徹底一致,若是對主庫進行增刪改操做,從庫會自動同步進行操做。

相關文章
相關標籤/搜索