mysql5.6 master-slave synchronization configuration

1: Instructions before configuration

Configure mysql 5.6 master-slave in centos 6 environment 

Prepare two virtual machines for testing, install and install mysql software, and open the mysql service
master master: 192.168.1.110
slave slave: 192.168.1.109

Two: Configure the master database
1: Authorize the slave database server
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: Configure the main library configuration file
Open binlog, and set the server-id, every time you modify the configuration file, you must restart the mysql service to take effect
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  #Error log configuration 
general_log=on #Open general log general_log_file 
=/ var /lib/ mysql /mysql_general_log. log #General log configuration 

illustrate:

server-id: ID number of the master;
log-bin: synchronized log path and file name, be sure to pay attention to this directory if mysql has permission to write (I am lazy here and put it directly under the datadir below);
binlog -do-db: The name of the database to be synchronized
can also display the database that is not synchronized:
binlog-ignore-db = mysql does not synchronize the mysql library and test library
binlog-ignore-db = test

 

After modifying the configuration file, restart the service: service mysqld restart

3: View the current binary log name and offset of the primary server. The purpose of this operation is to restore data from this point after the database is started.

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)

The main library is configured

Two: configure the slave library

1: Configuration file

vi /usr/my.cnf

server-id=2
 # log_bin=/var/log/mysqlslave-bin.log #log_bin here and log-bin below mean the same thing, just configure one of them 
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

After the configuration is complete, restart 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).

Then an error was reported.
Check the vi /var/lib/mysql/mysql_error.log log and found 2 lines of error messages
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

It means that mysql does not know these variables, search online, the reason is that mysql5.5+ version master-slave replication does not support these variables, you need to use commands to set on the slave library, then we will set it, first comment out the 3 variables
master- host=192.168.1.110
master-user=
rep1 master-password=test123456

restart mysql

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

Log in to mysql with the root user, and use the following command to set

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: Start the slave process

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=''.

Looking at the error log information, the value of mysql_port and master_log_file is empty due to the absence of the previous settings, which conflicts with the later settings. Restart mysql and try to see if service mysql restart
is wrong again.

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

Internet search reasons for
reference: https://blog.csdn.net/cug_jiang126com/article/details/46846031
Reason analysis:
The replication of mysql 5.6 introduces the concept of uuid, the server_uuid in each replication structure must be guaranteed to be different, but the direct After copying the data folder, the server_uuid is the same, show variables like '%server_uuid%';
Solution:
find the auto.cnf file in the data folder, modify the uuid value in it, and ensure that the uuid of each db is different, restart the db Can

Modify the uuid configuration in auto.cnf
vi /var/lib/mysql/auto.cnf
Modify the value of server-uuid, it looks different from the master database, restart mysql

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

Enter mysql, run slave start; command
error

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

Looking at the log, no error at the error level was found, and the replication thread has been initialized, so after restarting mysql, mysql will remember the previous slave start command and run the replication command

3. Check the status of the slave

show slave status\G;
there is a \G ending symbol behind it, which can be formatted to view the information, otherwise the information will be a mess,

mysql> show slave status;
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+-------------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
| Slave_IO_State                   | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File             | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB     | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          |Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                                     | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position |
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+-------------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
| Waiting for master to send event | 192.168.1.110 | rep1        |        3306 |            60 | mysql_bin.000002 |                 120 | localhost-relay-bin.000004 |           283 | mysql_bin.000002      | Yes              | Yes               | student         | mysql,infomation_schema |                    |                        |                         |                             |          0 |            |            0 |                 120 |             460 | None            |                |             0 | No                 |                    |                    |                 |      
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)

The slave library is waiting for the master library to update, Slave_IO_State: Waiting for master to send event
is configured successfully

Three: Synchronize the existing data of the master database to the slave database  

Main library operation:

1. Stop the data update operation of the main database
 mysql > flush tables with read lock;
2. Open a new terminal and generate a backup of the main database (export the database) [root@zhoujietest ~] # mysqldump -uroot -proot student > student.sql
3. Transfer the backup file to the slave library [root@zhoujietest ~] # scp student.sql [email protected]:/root/
4. Unlock the main library mysql >unlock tables;

Operation from the library:

1. Stop the slave library slave
 mysql > slave stop;

2. Create a new database student
 mysql > create database student default charset utf8;

3. Import data
[root@ops -dev ~] # mysql -uroot -proot student<student.sql 

4. Check that the database and data already exist in the slave library

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

At this time, the data of the master and slave libraries are completely consistent. If you add, delete, or modify the master library, the slave library will automatically synchronize the operation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325232935&siteId=291194637