mysql5.6 master-slave replication two kinds

Blog URL: http://www.linuxidc.com/Linux/2014-06/103752.htm
MySQL5.6 starts master-slave replication in two ways:
based on log (binlog);
based on GTID (Global Transaction Identifier).
It should be noted that the GTID method does not support temporary tables! So if your business system uses temporary tables, don't consider this method.
The first step: preparations
Master server: 192.168.15.9
Slave server: 192.168.15.10

The second step is to install mysql5.6
Mysql source code compilation and installation, the tutorial is in other documents The

third step: the configuration file my.cnf
to modify the configuration file of the main database my.cnf
[mysqld]
server-id=1
log-bin=mysqlmaster-bin.log
sync_binlog=1 innodb_buffer_pool_size
=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
binlog-do-db=test #Specify can synchronized database

Step 4: Modify the slave database configuration file my.cnf
[mysqld]
server-id=2
log-bin=mysqlslave-bin.log
sync_binlog=1 innodb_buffer_pool_size
=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
relay_log =mysql-relay-bin
Step 5: Start the database Run the following commands
on the master database and slave database server to restart the master database and slave database
[root@master ~]# service mysql restart
[root@slave ~]# service mysql restart
Step 6: Create an account for master-slave replication on the master database
[root@master ~]# mysql -uroot -p
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.15.% ' IDENTIFIED BY '111111';
Query OK, 0 rows affected (0.00 sec)
Note: The IP address in the above command is the IP address of the slave database server (the server that allows remote login).
Step 7: Lock the table in the main database
(prohibit re-insertion of data to obtain the binary log coordinates of the main database)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
Step 8: Check the status of the main database
(And record the value of the File field and the Position field, which is useful when configuring the slave server)
mysql> show master status;
+------------- -+------------+--------------+-----------------+--- ----------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------- ---+------------+--------------+------------------+- ------------------+
| mysqlmaster-bin.000004 | 327 | | | |
+----------------------------+----------+------------- -+--------------------------------+-------------------+ 1 row
in set (0.00 sec)
Step 9: Create a snapshot file of the master database
[root@master ~]# cd /usr/local/mysql5.6/bin
# ./mysqldump -uroot -p -h127.0.0.1 -P3306 --all-databases -- triggers --routines --events >>~ /all.sql
scp ~/all.sql [email protected]:/home/centos/all.sql
You can put all.sql in any other directory, and then use the scp command Copy to a certain directory of the remote slave database server. The execution time of
this command will vary depending on the amount of data. If the amount of data in the master database is large, it may take a long time. In this case, It is best to perform this operation at night when there is no business, otherwise the lock table operation in Step 7 will have a great impact on the business system. Step
10 : Unlock the lock table operation of the main database
[root@master ~]# mysql -uroot -p (this command is executed on the master database server)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
Step 11: Import the snapshot file created in step 7 on the slave database server to the slave database middle
[root@slave ~]# mysql -uroot -p -h127.0.0.1 -P3306 < /mnt/windows/all.sql
Step 12: Set the master database server to synchronize with the slave database server on the slave database server
[root @slave ~]# mysql -uroot -p
mysql> change master to master_host = '192.168.1.100',master_user='repl',master_password='111111',master_log_file='mysqlmaster-bin.000004',master_log_pos=327;
Note : The value of the red part is found in the eighth step, and there is no mistake here. The
thirteenth step: start the slave database replication thread
mysql> start slave;
Query OK, 0 rows affected (0.01 sec) The
fourteenth step : Query the replication thread status of the slave database
mysql> show slave status \G
**************************** 1. row ***** ************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysqlmaster-bin.000004
          Read_Master_Log_Pos: 327
              Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 289
        Relay_Master_Log_File: mysqlmaster-bin.000004
          Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
          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: 327
              Relay_Log_Space: 462
              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: 2e5e1b22-f0a9-11e3-bbac-000c297799e0
            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)

If both Slave_IO_Running and Slave_SQL_Running are yes, it means that the master-slave replication configuration is successful. Now
you can start to test whether the configuration is successful, first create a new one in the test database of the master database Table, then insert a few pieces of data, and then go to the slave database to see if it is synchronized.
Note: When there are a large number of queries from the database, you can temporarily close the replication thread from the database, wait for the query volume to drop, and then open it again, so that data will not be lost.

Guess you like

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