Database operation and maintenance methods

Database operation and maintenance methods

  1.  Percona Toolkit installation

Download the installation package

wget https://www.percona.com/downloads/percona-toolkit/2.2.19/tarball/percona-toolkit-2.2.19.tar.gz

installation steps

 

tar zxvf percona-toolkit-2.2.19.tar.gz

 cd percona-toolkit-2.2.19

 perl Makefile.PL

 make

 make install

The default installation location

/usr/local/bin/

 

  1. Shell Batch

Built sh file

We pt.sh

 

document content

#!/bin/bash

table=$1

alter_conment = $ 2

 

cnn_host='127.0.0.1'

cnn_user='cctischoolyard'

cnn_pwd='cct123456'

cnn_db='bayg'

 

echo "$table"

echo "$ alter_conment"

/usr/local/bin/pt-online-schema-change --charset=utf8 --no-version-check --user=${cnn_user} --password=${cnn_pwd} --host=${cnn_host}  P=3306,D=${cnn_db},t=$table --alter "${alter_conment}" --no-check-replication-filters --alter-foreign-keys-method=rebuild_constraints --recursion-method=none   --print --execute

 

Change permissions

chmod +x pt.sh

 

Shell execution

pt.sh HONOR "ADD COLUMN column1 tinyint(4) DEFAULT NULL"

 

HONOR table name

  1. Online DDL operations

pt-online-schema-change Introduction

pt-online-schema-change is a tool percona developed, this feature can be found in percona-toolkit package inside, it can modify table structure online

principle:

  • First, it will create a new document exactly the same table, the table name suffix is ​​generally _new
  • Then make changes in this new field operation table
  • Then add three triggers, DELETE / UPDATE / INSERT on the original table, the table also perform a new original statement to be executed in the table
  • Finally, the original copy of the data table to the new table, and then replace the original table

 

Add field

 

    pt-online-schema-change --user=root --password=123456 --host=127.0.0.1  --alter "ADD COLUMN content text" D=bayg,t=tmp_test --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --execute

 

Delete field

 

    pt-online-schema-change --user=root --password=123456 --host=127.0.0.1  --alter "DROP COLUMN content text" D=bayg,t=tmp_test --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --execute

 

Modify the field

 

    pt-online-schema-change --user=root --password=123456 --host=127.0.0.1  --alter "MODIFY COLUMN age TINYINT NOT NULL DEFAULT 0" D=bayg,t=tmp_test --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --execute

 

Fields renamed

 

    pt-online-schema-change --user=root --password=123456 --host=127.0.0.1  --alter "CHANGE COLUMN age address varchar(30)" D=bayg,t=tmp_test --no-check-alter --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --execute

 

Adding indexes

 

    pt-online-schema-change --user=root --password=123456 --host=127.0.0.1  --alter "ADD INDEX idx_address(address)" D=bayg,t=tmp_test --no-check-alter --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --execute

 

Delete Index

 

    pt-online-schema-change --user=root --password=123456 --host=127.0.0.1  --alter "DROP INDEX idx_address" D=bayg,t=tmp_test --no-check-alter --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --execute

 

Contraction table space

pt-online-schema-change --user=root --password=123456 --host=127.0.0.1 --alter "ENGINE=InnoDB" D=bayg,t=tmp_test --no-check-alter --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --execute

 

  1. Mysqldump

Export the database table structure

mysqldump -hlocalhost -ucctischoolyard -P3306 -pcct123456 -d db_stydy>/home/dba/db_stydy.sql

 

 

Export the database table structure and data

mysqldump -hlocalhost -ucctischoolyard -P3306 -pcct123456  db_stydy>/home/dba/db_stydy_data.sql

 

Export specific table structure and data

mysqldump -hlocalhost -ucctischoolyard -pcct123456 -P3306 db_stydy test1 > /home/dba/test1.sql

 

Export specific table structure (no data)

mysqldump -ucctischoolyard -hlocalhost -pcct123456  -P3306 -d db_stydy test1 > /home/dba/test1_nodata.sql

 

Export specific table data (without structure)

mysqldump -ucctischoolyard -hlocalhost -pcct123456  -P3306 -t db_stydy test1 > /home/dba/test1_data.sql

 

  1. XtraBackup 2.4

Download Package

wget https://www.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.15/binary/tarball/percona-xtrabackup-2.4.15-Linux-x86_64.libgcrypt153.tar.gz

 

Extracting package

tar  -zxvf  percona-xtrabackup-2.4.15-Linux-x86_64.libgcrypt153.tar.gz

 

backup database

./innobackupex --defaults-file=/etc/my.cnf --user=cctischoolyard --password=cct123456 --host=127.0.0.1 --databases=db_stydy  /home/dba/xtrabackup/

 

Part of the backup file

xtrabackup_binlog_info: Record the latest current LOG Position

xtrabackup_binlog_pos_innodb:innodb log postion

xtrabackup_checkpoints: backup storage start position and end position beginlsn endlsn, the incremental backup LSN [incremental backup can be seen from and to changes in the inside two values

xtrabackup_info: some concrete information log backup

 

Published 10 original articles · won praise 0 · Views 282

Guess you like

Origin blog.csdn.net/qq_40992737/article/details/103480210