当前位置:网站首页>MySQL log management and master-slave replication

MySQL log management and master-slave replication

2022-06-13 05:12:00 I love Qianxi


With 192.168.25.131 For example

One 、mysql Log management

MYSQL Log management :
binlog How to configure ?
log_bin: switch ; Set storage position
server_id:5.7 You must add server_id

Be careful : In production , Logs and data should be separated ( Use different hardware disks )
The configuration file is as follows

vim /etc/my.cnf
server_id=6
log_bin=/application/mysql/data/mysql-bin

stay binlog View files in the directory

[[email protected] data]# ll
total 123076
-rw-r----- 1 mysql mysql      612 Aug 31 16:36 mysql-bin.000001   # Binary 
-rw-r----- 1 mysql mysql      154 Aug 31 16:36 mysql-bin.000002   # Every time you restart, a new digital value increases 
-rw-r----- 1 mysql mysql       82 Aug 31 16:36 mysql-bin.index    # For the index of the above two binary files , Include name and location 

stay sql The interior can be viewed in this way

mysql> show variables like '%log_bin%';
+---------------------------------+-----------------------------------------+
| Variable_name                   | Value                                   |
+---------------------------------+-----------------------------------------+
| log_bin                         | ON                                      |
| log_bin_basename                | /application/mysql/data/mysql-bin       |
| log_bin_index                   | /application/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF                                     |
| log_bin_use_v1_row_events       | OFF                                     |
| sql_log_bin                     | ON                                      |
+---------------------------------+-----------------------------------------+

binlog What was recorded ?

Records the operations of all change classes in the database (DDL,DML,DCL)
about DDL.DCL, Record statements that have occurred , For example, building a database
DML(insert,upfate,delete): The premise is that the statement that has been submitted , Can be recorded to binlog in
About record format :
ROW : RBR Row record mode , What is recorded is the change of lines , A lot of logs , Rigorous enough , No logging errors will occur
STATEMENT : SBR Statement record mode , What is recorded is the operation statement , Less logs , High readability . For the operation of function classes , Errors will be made when recovering in the future
MIXED : MBR Mixed recording mode , hand mysql At your own discretion , But no one uses
5.7 The default is RBR, Is the enterprise suggestion mode

stay mysql You can view

mysql> select @@binlog_format;
+-----------------+
| @@binlog_format |
+-----------------+
| ROW             |
+-----------------+

Binary log events (event)
The smallest recording unit of a binary log
about DML,DCL, A statement is a event
about DML In a word : Log only committed transactions
For example , Is divided into 4 individual event
begin; 120-340
DML1 340-460
DML2 460-550
commit; 550-760

mysql Log management -slowlog

Optimize related logs , Dedicated to recording sql Statement log , Positioning is inefficient sql Statement tool log

Open slow log

 Off by default 
mysql> select @@slow_query_log;
+------------------+
| @@slow_query_log |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)

 Turn on :
vim /etc/my.cnf
slow_query_log=1
slow_query_log_file=/application/mysql/data/db-slow.log
long_query_time=0.1
log_queries_not_using_indexes
 Save and restart the database 

Check it again slowlog The location of

[[email protected] data]# /application/mysql/data
[[email protected] data]# ll
total 123096
-rw-r----- 1 mysql mysql      186 Sep  6 11:33 db-slow.log
 This is the slow log we set up 

Analyze slow logs :

mysqldumpslow -s c -t 10 /application/mysql/data/db-slow.log
-s c Sort by number ,-t Before removal 10 The slowest log 

The following are the results of slow log analysis , Several times , How long did it last
 Insert picture description here

Two 、 Log backup recovery and migration

1.DBA Responsibilities in database backup and recovery
1. Design backup strategy : be completely ready , The incremental , Time , Automatically
2. Daily backup check
Backup existence ; Whether the backup space is enough

2. Backup type
Hot standby :

During normal operation of database business , Back up the data , And consistent recovery (innodb)

Warm preparation :

Lock table backup , Can only query , Do not modify (myisam)

Cold standby :

Close the database business , Data backup

3. Backup tools
A. Logical backup tool :
be based on sql Statement for backup
mysqldump ( advantage : No need to download , The backup is SQL, High readability , Easy for backup processing , Text form , High compression ratio , Save disk space ; shortcoming : Depends on the database engine , Need to read data from disk , And then convert to SQL Statement to dump , Consume CPU and IO resources ; Suggest 100G The amount of data in is mysqldump, exceed TB The above may also choose mysqldump, Cooperate with distributed systems ,1EB=1024PB=1000000TB)
mysqlbinlog

B. Physical backup tool :
Disk based data file backup , Copy disk files directly
xtrabackup(XBK) ( advantage : Similar to direct CP Data files , There is no need to manage logical relations , Higher performance ; shortcoming : Poor readability , Low compression ratio , Need more disk space ; Suggest :>100G<TB)

4. practice
mysqldump Use

 Client general commands , Related to links :-u,-p,-S,-h,-P
 Local backup connection mode :
mysqldump -uroot -pxxx -S /tmp/mysql.sock

 Connection mode of remote backup :
mysqldump -uroot -pxxx -h xxx -P 3306

 Basic backup parameters :
-A   Realize full database backup 
[[email protected] data]# mkdir -p /data/backup
[[email protected] data]# mysqldump -uroot -p123456 -A -S /tmp/mysql.sock > /data/backup/full.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] data]# echo $?
0

-B   Sub base backup 
 Backup liyu This library 
[[email protected] backup]# mysqldump -uroot -p123456 -B liyu -S /tmp/mysql.sock > /data/backup/db.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] backup]# echo $?
0

 Library name   Table name 
[[email protected] backup]# mysqldump -uroot -p123456 -B world city -S /tmp/mysql.sock > /data/backup/tab.sql

 Parameters are required :
-R  On backup , Back up stored procedures and functions at the same time , If not, it will be ignored automatically 
-E  On backup , Back up at the same time EVENT, Not automatically ignored 
--triggers  On backup , Also back up triggers , Not automatically ignored 
--master-data=2   Record the beginning of the backup position Number , Auto lock watch , coordination --single-transaction, Reduce the lock 
--single-transaction   about innodb Table of , Implement snapshot backup , Don't lock the watch 

3、 ... and 、 Build master-slave replication

1. Introduce

Binary log dependent ," real time " A multi node architecture for backup
binlog Logs are used for data recovery , Master slave copied

2. Premise of master-slave replication ( How to build )

A. At least two instances
B. Different server_id
C. The main database needs to open the binary log
D. The master database needs to authorize a dedicated replication user
E. Main database data backup
F. Start the dedicated replication thread

1. Prepare the multi instance environment , You can refer to <<mysql install , Architecture and management >> Multi instance configuration in
2. Check server_id

[[email protected]-master system]# mysql -S /data/3307/mysql.sock -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
|           7 |
+-------------+
[[email protected]-master system]# mysql -S /data/3308/mysql.sock -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
|           8 |
+-------------+
[[email protected]-master system]# mysql -S /data/3309/mysql.sock -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
|           9 |
+-------------+ 

3. choice 3307 Give priority to the library ,3308,3309 For from the library
Check 3307( Main library ) Binary log of

[email protected]-master system]# mysql -S /data/3307/mysql.sock -e "show variables like '%log_bin%'"
+---------------------------------+----------------------------+
| Variable_name                   | Value                      |
+---------------------------------+----------------------------+
| log_bin                         | ON                         |

4. The main library creates a copy user

[[email protected]-master system]# mysql -S /data/3307/mysql.sock   Enter the main library 
mysql> grant replication slave on *.* to [email protected]'192.168.25.%' identified by '123456';

5. Backup the main database data

[[email protected]-master system]# mysqldump -S /data/3307/mysql.sock -A --master-data=2 -R -E --triggers --single-transaction > /tmp/full.sql

Restore data to the slave library (3308)

mysql -S /data/3308/mysql.sock
mysql> set sql_log_bin=0;  Close binary log 
mysql> source /tmp/full.sql;  take 3307 Import backed up files 

Tell the information copied from the library ( user name , password , Port number , Copy starting point ,IP,binlog relevant )

 Sure help change master to Check usage 
CHANGE MASTER TO
  MASTER_HOST='master2.example.com',
  MASTER_USER='replication',
  MASTER_PASSWORD='password',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='master2-bin.001',
  MASTER_LOG_POS=4,
  MASTER_CONNECT_RETRY=10;

Can be in the backup of /tmp/full.sql Check it out LOG_FILE,22 Row view

 CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=448;

From the library 3308 Up operation

mysql> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.25.131',
    ->   MASTER_USER='repl',
    ->   MASTER_PASSWORD='123456',
    ->   MASTER_PORT=3307,
    ->   MASTER_LOG_FILE='mysql-bin.000001',
    ->   MASTER_LOG_POS=448,
    ->   MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;              # Start replication thread 
Query OK, 0 rows affected (0.01 sec)

Problems encountered : If change master to Incorrect input , Sure

mysql> stop  slave;    # Stop the replication thread first 
mysql> reset slave all;    # Clean up input errors change master to The content of 
mysql>  Re input change master to
mysql> start slave;    # Restart  

View the connection status from the Library

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.25.131
                  Master_User: repl
                  Master_Port: 3307
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 448
               Relay_Log_File: keepalived-master-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes     # appear YES That is success 
            Slave_SQL_Running: Yes

3、 ... and 、 How master-slave replication works

1、 Noun recognition
file :

Main library :binlog
Slave Library : relay-log relay logs
master.info Main library information file
relay-log.info Relay log application information

Threads :

Main library : binlog_dump_thread Binary log delivery thread
You can use this command to see mysql -S /data/3307/mysql.sock -e "show processlist"
Slave Library : IO_Thread: From library IO Threads , Be responsible for requesting and accepting the data sent from the main database binlog
SQL_Thread: From library SQL Threads , Playback log

2、 working principle

1、 Execute... From the library change master to sentence , Will immediately record the master database information to master.info In the document
2、 Execute... From the library start slave The moment of , From the library will be generated immediately IO_Thread and SQL_Thread
3、IO_Thread Will read master.info Files in , Get the main database information
4、IO_Thread Connect to the main library , The master database will immediately assign a binlog_dump_thread, And IO_Thread Interact
5、IO_Thread according to master.info binlog Information , towards binlog_dump_thread Ask for the latest binlog
6、 Main library binlog_dump_thread, After inquiry , If you find something new , Intercepts and returns to the IO_Thread
7、 Slave Library IO_T Will receive binlog, Stored in TCP/IP In cache , Back at the bottom of the network ACK
8、 Slave Library IO_T Will update master.info, Reset binlog Location point information
9、 Slave Library IO_T Will binlog, Write to relaylog journal
10、 Slave Library SQL_T Read relay-log.info file , Get the last executed location
11、SQL_T Follow the location point down relay-log.info
12、SQL_T After execution , Renew relay-log.info
13、relaylog Regular automatic cleaning
details : The main database has been modified , After updating the binary log , Will send a " The signal " to binlog_dump_thread,binlog_dump_thread Notified to IO_T Threads

3. Master slave replication monitoring and fault handling
A. Master-slave monitoring
Main library : You will find a thread

[[email protected]-master data]# mysql -S /data/3307/mysql.sock
mysql> show processlist;
+----+------+-------------------------+------+-------------+-------+---------------------------------------------------------------+------------------+
| Id | User | Host                    | db   | Command     | Time  | State                                                         | Info             |
+----+------+-------------------------+------+-------------+-------+---------------------------------------------------------------+------------------+
|  6 | repl | keepalived-master:49296 | NULL | Binlog Dump | 19863 | Master has sent all binlog to slave; waiting for more updates | NULL           

Slave Library : Information of the monitored master database ( Namely master.info Information in )

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event    # The status value 
                  Master_Host: 192.168.25.131                      # Main library ip
                  Master_User: repl                                # Copy user name 
                  Master_Port: 3307
                Connect_Retry: 10   # If the master and slave are not connected, they will try again 10 Time 
              Master_Log_File: mysql-bin.000001                     # What has been obtained binlog file name 
          Read_Master_Log_Pos: 448                                  # What has been obtained binlog Location number              

From library relaylog Information about (relay-log.info)

  Relay_Log_File: keepalived-master-relay-bin.000002    # From the library has been run relaylog The name of the file 
                Relay_Log_Pos: 320                      # From the library has been run relaylog The location of 

Copy thread working state from library :

 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

Filter copy related states :

              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:

Time delay of master database from database , Seconds per unit

Seconds_Behind_Master: 0

Error details from library thread

Last_IO_Errno: 0   IO Wrong number 
Last_IO_Error:    IO The details of the error 
Last_SQL_Errno: 0   SQL Wrong number 
Last_SQL_Error:      SQL The details of the error 

Delay slave ( Take the initiative to ), What does the main library do , How long will it take to do it from the library , Prevent misoperation

SQL_Delay: 0
SQL_Remaining_Delay: NULL   # The remaining time of the delay operation 

GTID Copy information

Retrieved_Gtid_Set:   Received GTID The number of 
Executed_Gtid_Set:    Executed GTID The number of 

B、 Analysis and treatment of master-slave faults
Copy the working state of the thread from the library

 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

Error details from library thread

Last_IO_Errno: 0   IO Wrong number 
Last_IO_Error:    IO The details of the error 
Last_SQL_Errno: 0   SQL Wrong number 
Last_SQL_Error:      SQL The details of the error 

C、IO Thread failure
1、 Unable to connect to the main database

connecting state ,NO state

reason :

The Internet is not working , A firewall ,IP incorrect ,port incorrect , The password is wrong , The user is wrong (change master to The reason for writing ), Maximum number of connections ,server_id The problem of , Log corruption

Handling ideas :

Use mysql -urepl -P3307 -p123456 -h
192.168.25.131 Make manual connection , If the connection is wrong, an error will be prompted , You can locate your own mistakes

How to deal with it ?

stop slave;
reset slave all;
change master to;

Trouble shooting : Log corruption , The logs are not continuous
Main warehouse operation :

[[email protected]-master data]# mysql -S /data/3307/mysql.sock
mysql> flush logs;       # Refresh the log 
Query OK, 0 rows affected (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)
 View status from library 
mysql> show slave status\G
*************************** 1. row ***************************
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 154
 View the status in the main library 
mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000004
         Position: 154
 The two information correspond to 

 Execute... In the main library , Sabotage , Lead to inconsistency between master and slave 
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)

 Check out... From the library ,IO_T Status as NO
mysql> show slave status\G
*************************** 1. row ***************************           
             Slave_IO_Running: No    # here !
            Slave_SQL_Running: Yes
            Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'could not find next log; the first event 'mysql-bin.000001' at 448, the last event read from '/data/3307/mysql-bin.000004' at 154, the last byte read from '/data/3307/mysql-bin.000004' at 154.'
 Then create in the main database 2 Databases , At this point, the slave library is destroyed , Cannot back up newly created 2 Databases 
mysql> create database dd;
mysql> create database dd1;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dd                 |
| dd1                |
| liyu               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

 Processing from the library 
mysql> stop slave;
mysql> reset slave all;
mysql> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.25.131',
    ->   MASTER_USER='repl',
    ->   MASTER_PASSWORD='123456',
    ->   MASTER_PORT=3307,
    ->   MASTER_LOG_FILE='mysql-bin.000001',   # This is in the main database show master status\G You can see which file to operate 
    ->   MASTER_LOG_POS=154,            # Last time IO There is a record location in the error message 
    ->   MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;
mysql> show slave status \G     # Check again and you'll be back to normal 
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.25.131
                  Master_User: repl
                  Master_Port: 3307
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 463
               Relay_Log_File: keepalived-master-relay-bin.000002
                Relay_Log_Pos: 629
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
 Then view the library from the library , The library created in the main library has been restored 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dd                 |
| dd1                |
| liyu               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

原网站

版权声明
本文为[I love Qianxi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280515158578.html