본문으로 바로가기

MySQL binlog 설정 및 삭제, 복구

category 서버 & 시스템/Linux 2017. 10. 28. 11:51
반응형

1. 명령어 경로

1. bin로그 설정


1) my.cnf 수정을 통한 설정

# vi /etc/my.cnf

log-bin=[bin로그 파일명]

max_binlog_size=[bin로그 하나 당 설정한 용량만큼 저장 후 로테이션]

expire_logs_days=[보관기간]


ex)

log-bin=mysql-bin

max_binlog_size=1G

expire_logs_days=30 



2) mysql 콘솔에서 설정

# mysql -uroot -p

> set global expire_logs_days=3;

> show variables like '%expire%';




2. bin로그 삭제

# mysql -uroot -p

> show binary logs; \\ 쌓여있는  binary log 확인


> purge master logs to 'mysql-bin.***'; \\ 지정된 binary 를 제외한 "이전"의 바이너리로그 파일 모두 삭제

> show binary logs; \\ 쌓여있는  binary log 확인


cf) binarylog 옵션 확인

# /usr/local/mysql/bin/mysqlbinlog 

/usr/local/mysql/bin/mysqlbinlog Ver 3.3 for unknown-linux-gnu at x86_64

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Dumps a MySQL binary log in a format usable for viewing or for piping to

the mysql command line client.


Usage: /usr/local/mysql/bin/mysqlbinlog [options] log-files

  -?, --help          Display this help and exit.

  --base64-output[=name] 

                      Determine when the output statements should be

                      base64-encoded BINLOG statements: 'never' disables it and

                      works only for binlogs without row-based events;

                      'decode-rows' decodes row events into commented SQL

                      statements if the --verbose option is also given; 'auto'

                      prints base64 only when necessary (i.e., for row-based

                      events and format description events); 'always' prints

                      base64 whenever possible. 'always' is for debugging only

                      and should not be used in a production system. If this

                      argument is not given, the default is 'auto'; if it is

                      given with no argument, 'always' is used.

  --character-sets-dir=name 

                      Directory for character set files.

  -d, --database=name List entries for just this database (local log only).

  --debug-check       Check memory and open file usage at exit .

  --debug-info        Print some debug info at exit.

  -D, --disable-log-bin 

                      Disable binary log. This is useful, if you enabled

                      --to-last-log and are sending the output to the same

                      MySQL server. This way you could avoid an endless loop.

                      You would also like to use it when restoring after a

                      crash to avoid duplication of the statements you already

                      have. NOTE: you will need a SUPER privilege to use this

                      option.

  -F, --force-if-open Force if binlog was not closed properly.

  -f, --force-read    Force reading unknown binlog events.

  -H, --hexdump       Augment output with hexadecimal and ASCII event dump.

  -h, --host=name     Get the binlog from server.

  -l, --local-load=name 

                      Prepare local temporary files for LOAD DATA INFILE in the

                      specified directory.

  -o, --offset=#      Skip the first N entries.

  -p, --password[=name] 

                      Password to connect to remote server.

  -P, --port=#        Port number to use for connection or 0 for default to, in

                      order of preference, my.cnf, $MYSQL_TCP_PORT,

                      /etc/services, built-in default (3306).

  --position=#        Deprecated. Use --start-position instead.

  --protocol=name     The protocol to use for connection (tcp, socket, pipe,

                      memory).

  -R, --read-from-remote-server 

                      Read binary logs from a MySQL server.

  -r, --result-file=name 

                      Direct output to a given file.

  --server-id=#       Extract only binlog entries created by the server having

                      the given id.

  --set-charset=name  Add 'SET NAMES character_set' to the output.

  -s, --short-form    Just show regular queries: no extra info and no row-based

                      events. This is for testing only, and should not be used

                      in production systems. If you want to suppress

                      base64-output, consider using --base64-output=never

                      instead.

  -S, --socket=name   The socket file to use for connection.

  --start-datetime=name 

                      Start reading the binlog at first event having a datetime

                      equal or posterior to the argument; the argument must be

                      a date and time in the local time zone, in any format

                      accepted by the MySQL server for DATETIME and TIMESTAMP

                      types, for example: 2004-12-25 11:25:56 (you should

                      probably use quotes for your shell to set it properly).

  -j, --start-position=# 

                      Start reading the binlog at position N. Applies to the

                      first binlog passed on the command line.

  --stop-datetime=name 

                      Stop reading the binlog at first event having a datetime

                      equal or posterior to the argument; the argument must be

                      a date and time in the local time zone, in any format

                      accepted by the MySQL server for DATETIME and TIMESTAMP

                      types, for example: 2004-12-25 11:25:56 (you should

                      probably use quotes for your shell to set it properly).

  --stop-position=#   Stop reading the binlog at position N. Applies to the

                      last binlog passed on the command line.

  -t, --to-last-log   Requires -R. Will not stop at the end of the requested

                      binlog but rather continue printing until the end of the

                      last binlog of the MySQL server. If you send the output

                      to the same MySQL server, that may lead to an endless

                      loop.

  -u, --user=name     Connect to the remote server as username.

  -v, --verbose       Reconstruct SQL statements out of row events. -v -v adds

                      comments on column data types.

  -V, --version       Print version and exit.

  --open_files_limit=# 

                      Used to reserve file descriptors for use by this program.

 




3. binarylog 쿼리 저장


1) binlog 텍스트로 변환

# mysqlbinlog [binlog 경로] > [파일이름]. sql


ex)

# mysql-bin.000001 > [파일명].sql



2) 특정 db 의 지정된 날짜동안의 binlog 텍스트 변환

# mysqlbinlog --database=[db명] --start-date="[yyyy-mm-dd 시간:분:초]" --stop-date="[yyyy-mm-dd 시간:분:초] [binlog 경로] > [파일명].sql


ex)

# mysqlbinlog --database=itsyou --start-date="2019-01-01 12:00:00" --stop-date="2019-01-03 19:00:00" mysql-bin.000002 > test.sql



3) 특정 db의 지정된 포지션 사이의 binlog 텍스트 변환

* start position 은 정확한 포지션을 지정해주어야함

# mysqlbinlog --databse=[db명] --start-position=[num] --stop-position=[num]  [binlog 경로] > 파일명.sql


ex)

# mysqlbinlog --databse=itsyou --start-position=1 --stop-position=10000 /usr/local/mysql/data/mysql-bin.000003 > recover.sql




4. binlog 를 통한 복구

* 복구 전 기존 데이터는 백업 후 진행할 것

* 복구할 binlog 의 sql 파일이 db나 table 을 생성하는것부터 시작할 경우 (ex. create table / create database) 해당 table 이나 database 삭제 후 진행해야함

* 삭제된 db 나 table 을 복구할 경우 sql 파일 내에 불필요한 쿼리는 주석 후 복구할 것





반응형

'서버 & 시스템 > Linux' 카테고리의 다른 글

Mysql 언어셋 변경 (utf8 → euckr)  (0) 2018.01.11
vsftpd 설치 및 설정  (1) 2017.11.27
Java & Tomcat & Apache 연동  (0) 2017.10.15
iptables 설정  (0) 2017.10.11
WordPress 설치  (0) 2017.10.10