mysql management statement and related backup and recovery

1. Database backup statement
1.1 Full backup
xtrabackup --defaults-file=/usr/local/mysql/mysql6606.cnf --user=$user --password=$passwd --port=$port --socket=$socket - -use-memory=4G --parallel=4 --no-lock --backup --target-dir=$full_backup_dir
1.2 incremental backup
1.2.1 first incremental
innobackupex --defaults-file=/usr/local /mysql/mysql6606.cnf --user=root --password=123 --incremental --incremental-basedir=/backup/full /backup/inc1
1.2.2 second increment
innobackupex --defaults-file=/usr /local/mysql/mysql6606.cnf --user=root --password=123 --incremental --incremental-basedir=/backup/inc1 /backup/inc2
1.2.3 Logical backup
mysqldump -uroot -p -R -E --max-allowed-packet=256M --single-transaction --set-gtid-purged=OFF -P6606 -S/data/mysql_6606/mysql.sock --databases tybss_report >/data/tybss_report0509.sql
1.2.4 备份库,但忽略一些表
mysqldump -uroot -p -R -E --max-allowed-packet=256M --single-transaction --set-gtid-purged=OFF -P6606 -S/data/mysql_6606/mysql.sock panda_rcs --ignore-table=panda_rcs.match_event_info --ignore-table=panda_rcs.standard_sport_market_flowing --ignore-table=panda_rcs.rcs_standard_sport_market_sell_flowing --ignore-table=panda_rcs.bak_match_statistics_info_detail --ignore-table=panda_rcs.standard_sport_market_odds --ignore-table=panda_rcs.rcs_monitor_error_log --ignore-table=panda_rcs.standard_sport_market --ignore-table=panda_rcs.rcs_monitor_mq_info > ./panda_rcs.sql
1.2.5 只备份特定表的表结构
mysqldump -uroot -p -R -E --max-allowed-packet=256M --single-transaction --set-gtid-purged=OFF -P6606 -S/data/mysql_6606/mysql.sock -d panda_rcs match_event_info standard_sport_market_flowing rcs_standard_sport_market_sell_flowing bak_match_statistics_info_detail standard_sport_market_odds rcs_monitor_error_log standard_sport_market rcs_monitor_mq_info match_statistics_info_flowing rcs_monitor_garbage_collector > table.sql

2. Database recovery
2.1 Full backup recovery
xtrabackup --defaults-file=/usr/local/mysql/mysql6606.cnf --prepare --target-dir=$full_backup_dir
xtrabackup --defaults-file=/usr/local/mysql/ mysql6606.cnf --host=localhost --user=root --password=mysql123 --port=6606 --datadir=/data/mysql_6606 --copy-back --target-dir=/db_backup/pro_15/backup/6626 /full/20200618_053001
2.2 Incremental backup recovery
2.2.1 Full backup
innobackupex --apply-log --redo-only /backup/3306/full
2.2.2 First backup merge full
innobackupex --apply-log --redo -only --incremental-dir=/data/backup/inc1 /data/backup/full
2.2.3 The second backup merges the first
innobackupex --apply-log --incremental-dir=/data/backup/inc2 / data/backup/full
2.2.4 The last finishing
innobackupex --apply-log /data/backup/full

3. Binlog interception
show master status
show binlog events in 'mybinlog.****'
SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
3.1 Intercept a node's binlog, intercept this section The operation of the gtid group. --exclude-gtids exclude gtids. Here can be followed by multiple binlog files.
mysqlbinlog --skip-gtids --include-gtids='1afe8136-601d-11e9-9022-000c2928f5dd:7-9' --exclude-gtids='dff98809-55c3-11e9-a58b-000c2928f5dd:4' /data/binlog /mysql-bin.000009 >/data/backup/binlog.sql
3.2 Intercept
mysqlbinlog according to the location --start-position=219 --stop-position=1347 /data/binlog/mysql-bin.000003 >/tmp/bin. sql
3.3 Intercept
mysqlbinlog according to time --start-datetime='2019-05-06 17:00:00' --stop-datetime='2019-05-06 17:01:00' /data/binlog/mysql-bin .000004
3.4 View binary log
mysqlbinlog --defaults-file=/usr/local/mysql/mysql6606.cnf --start-datetime='2020-05-01 19:00:00' --stop-datetime='2020-05-01 19:10:00' --base64-output=decode-rows -v mybinlog.000009 > /tmp/8.sql

4. 主从复制
use mysql;select * from slave_master_info;
stop slave;
reset slave all;
change master to master_host='10.5.11.21',master_user='repl',master_port=6606,master_password='RwFdd6ZpoKcfD07VgyHr',master_auto_position=1;
start slave;

5. ceph挂载db_backup
mount -t ceph 10.5.17.202:6789:/ /db_backup -o name=admin,secretfile=./admin.key

6. Database startup and shutdown
6.1 Start service
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/mysql6606.cnf &
6.2 Close service
mysqladmin -uroot -p -S /data/mysql_6606 /mysql.sock shutdown

6. Master-slave inconsistency needs to skip gtid operation
slave:
stop slave;
set @@session.gtid_next='ed41df0a-05dc-11ea-a4b0-000c293a9237:520485'
begin;commit;
set @@session.gtid_next='automatic';

7. Slow log, sort by query number and query row number
mysqldumpslow -s cr slow.log

8. User management:
8.1 Create user
create user test@'%' identified by '123';
grant select on *.* to test@'%';
8.2 Change password
alter user test@'%' identified by '456'
8.3 View user permissions
show grants for root@'localhost';
8.4 Cancel permissions
revoke select on *.* from test@'%';

9. Deadlock management command:
9.1 View deadlock status
show status like 'innodb_row_lock%';
9.2 View lock source
use sys
select locked_table,waiting_trx_id,waiting_pid,blocking_trx_id,blocking_pid from innodb_lock_waits\G
9.3 Find lock source id
select thread_id from performance _schema. threads where processlist_id=25;
9.4 Find the sql statement of the lock source
select * from performance_schema.events_statements_current where thread_id=65\G

10. cetus management
10.1 start
/usr/local/cetus/bin/cetus --defaults-file=/usr/local/cetus/conf/proxy.conf &
10.2 management interface
/usr/bin/mysql -h10.5.22.42 -uadmin -P9606 -pRt57qHm8Xp91aFo3uExD
select * from backends
10.3 Manually switch between writing and reading
/usr/bin/mysql -h10.5.22.42 -uadmin -P9606 -pRt57qHm8Xp91aFo3uExD -e " update backends set state='up' , type='rw' where address='10.5.22.41:6616';"

11. Delete the binary log
mysql> show master logs;
+-----------------+------------+------- ----+
| Log_name | File_size | Encrypted |
+-----------------+------------+------ -----+
| mybinlog.004356 | 1073742346 | No |
| mybinlog.004357 |
1073749103 |
No |
| mybinlog.004358 | 0 | 554989663 | No |
| mybinlog.004361 | 1073741982 | No |
| mybinlog.004362 | 1073743396 | No | | mybinlog.004363
| 1073769737 | No | | 1073755841 | No | | mybinlog.004366 | 1073742882 | No | | mybinlog.004367 | 1073780415 | No |




| mybinlog.004368 | 1073743509 | No |
| mybinlog.004369 | 1073741875 | No |

View the current binlog log file
mysql> show master status;'
+-----------------+-----------+------- -------+------------------+----------------------- ----------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------- --------+-----------+--------------+-------------- ----+--------------------------------------------- ------+
| mybinlog.004385 | 115387258 | | | 31654e9c-1197-11ea-8f74-000c29d32f03:1-4466952829 |
+-----------------+- ----------+--------------+------------------+----- ----------------------------------------------+

Delete binary logs
mysql> purge binary logs to 'mybinlog.004383';


12. Initialize the database
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/opt/app1/data/mysql_6666

1. Log in to mysql to view the binlog
and only view the contents of the first binlog file
mysql> show binlog events;
view the contents of the specified binlog file
mysql> show binlog events in 'mysql-bin.000002';
get the list of binlog files
mysql> show binary logs;
If you need to filter, only query insert, update, delete statements, you can write like this:
mysqlbinlog --no-defaults --database=raceEnroll binlogs.000078 |grep update |more
If you need to query 2017-09-17 07:21 :09 to 2017-09-19 07:59:50 The database is the operation log of geeRunner, enter the following command to write the data to a spare txt.
mysqlbinlog --no-defaults --database=geeRunner --start-datetime="2017-09-17 07:21:09" --stop-datetime="2017-09-19 07:59:50" binlogs.000080 > sanjiaomao.txt
--insecure-registry 192.168.111.143:8088
View the execution frequency of the database
show [session|global] status;
query the database operations since this login, mainly concerned with the acquisition of values
show session status like 'Com_insert%';
show session status like 'Com_select%'
show session status like 'Com_update%';
show session status like 'Com_delete%';
query the database operations since the start of the database service, mainly concerned with the acquisition of values
show global status like 'Com_delete%';
show global status like 'Com_insert%';
show global status like 'Com_update%';
show global status like 'Com_select%';
query the database operation from the database innodb engine, mainly concerned with the acquisition of values Show session status
like 'InnoDB_rows%';
show global status like 'InnoDB_rows%';
view the number of database connections including successful and unsuccessful
show global status like 'connections';
show session status like 'connections';
View the working time of the database in seconds
show session status like 'Uptime';
show global status like 'Uptime';
check the number of full queries in the database
show global status like 'Slow_queries';

Guess you like

Origin blog.csdn.net/eagle89/article/details/129686228