1.主从
##复制用户
grant replication slave on *.* to repl@'10.186.63.%' identified by '123';
##pos主从
change master to
master_host='10.31.1.112',
master_user='repl',
master_password='test',
MASTER_LOG_FILE='mysql-bin.027774',
MASTER_LOG_POS=643608336 ;
##建立主从复制
CHANGE MASTER TO
MASTER_HOST='10.186.63.105',
MASTER_USER='repl',
MASTER_PASSWORD='Lhh@1234',
MASTER_PORT=3333,
MASTER_AUTO_POSITION=1,
MASTER_CONNECT_RETRY=10;
##设置复制过滤
CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE = ('test.sbtest1');
##pos模式跳过同步
set global sql_slave_skip_counter = 1;
##gtid模式注入空事务
stop slave;
set gtid_next='c3e416a9-8b9b-11ef-bddc-02000aba3f56:27';
begin;commit;
set gtid_next='AUTOMATIC';
start slave;
show slave status\G
2.binlog管理
--手动清除binlog
purge master logs before '2023-04-17 16:53:36';
--查看事件
show binlog events in 'db01.000001';
--mysqlbinlog截取二进制日志 pos
mysqlbinlog --start-position=219 --stop-position=335 mysql-bin.000003
--mysqlbinlog截取二进制日志 gtid
mysqlbinlog --include-gtids='f641804e-ae98-11ed-8825-02000aba3f69:1' --base64-output=decode-rows -vv db01.000001|grep -v 'at #'
--判断事务中DML数量,是否为大事务
mysqlbinlog -vv --include-gtids='6c5a5ba4-e3f9-11ed-bc17-02000aba3faf:1082828' mysql-bin.000001|egrep "### UPDATE|### DELETE|### INSERT"|wc -l
--统计binlog中DML操作的数量
mysqlbinlog --no-defaults --base64-output=decode-rows -vv mysql-bin.000001| awk '/###/ {if($0~/UPDATE|INSERT|DELETE/)count[$2" "$NF]++}END{for(i in count) print i,"\t",count[i]}' | column -t | sort -k2nr
--查找binlog中大事务
mysqlbinlog binlog.000001 | grep "last_committed=" -B 1| awk '/^# at/&&NR==1 {tmp=$NF} /^# at/&&NR>1 {print($NF-tmp,"--start-position="tmp,"--stop-position="$NF);tmp=$NF}' | sort -n -r | head -n 10
mysqlbinlog -vv --start-position=23225462 --stop-position=436888581 mysql-bin.038662|less
--binlog2sql解析二进制日志
python /soft/binlog2sql-master/binlog2sql/binlog2sql.py -ubinlog2sql -p'Lhh@1234'
-P3333 -dtest -tsbtest1 --start-file='db02.000001' --start-position=474 > test.sql
--设置gtid_purged,/*! '+'*/ 可将新的purged gtid加到集合里
set @@global.gtid_purged=/*! '+'*/ 'asfgahwegwegweh:1-10'
3.对表循环插入
DELIMITER //
CREATE PROCEDURE sdata()
BEGIN
DECLARE num INT;
SET num = 1;
WHILE
num < 10000000 DO
INSERT INTO buyer
VALUES(num,SUBSTRING(MD5(RAND()) FROM 1 FOR 5));
SET num = num + 1;
END WHILE;
END;
//
DELIMITER ;
call sdata();
4.索引
--建索引
create index xxx on tablename(column)
alter table tablename add index xxx(column)
create table tablename(id int(20) primary key)
--删索引
alter table city drop index idx_name;
drop index idx_name on city;
--查索引
show index from sbtest1;
--查找外键约束父子表
select CONSTRAINT_SCHEMA,CONSTRAINT_NAME,UNIQUE_CONSTRAINT_SCHEMA,TABLE_NAME,REFERENCED_TABLE_NAME from information_schema.REFERENTIAL_CONSTRAINTS;
5.备份恢复
--mydumper备份
mydumper --host=127.0.0.1 --port=3306 --user=backup --password=backup --chunk-filesize=1024 --triggers --events --routines --verbose=3 --compress --build-empty-files --regex '^(?!(mysql\.|sys\.))' --threads=8 --set-names=utf8mb4 --outputdir=./mydumper_`date +%F_%H_%M` --logfile=./mydumper_`date +%F_%H_%M`.log
--mydumper恢复
myloader -u backup -p backup -P 6667 -S /opt/mysql/data/6667/mysqld.sock -t 8 -d /data/recovery/mydumper_2022-09-07_01_44
--xtrabackup备份
xtrabackup --defaults-file=/etc/my.cnf --socket=/usr/local/mysql3306/mysql.sock --user=dba_find --password --no-version-check --parallel=4 --compress --chunk-size=256K --compress-threads=4 --backup --target-dir=/backup/
--解流
xbstream --verbose --extract < ./"$full_path".qp.xbstream --directory ./
--解压
/home/dmp/urman-agent/bin/xtrabackup --parallel=8 --decompress --remove-original --target-dir=./
--prepare
/home/dmp/urman-agent/bin/xtrabackup --use-memory=4096MB --prepare --target-dir=./
--copy
/home/dmp/urman-agent/bin/xtrabackup --defaults-file=/data/mysql/etc/4913/my.cnf --copy-back --target-dir=./
6.找出消耗cpu语句
--找出消耗cpu最高的sql
top -p 76816 -H -b -n 1|cat -A|grep mysqld|head -n5 |awk '{print "mysql -S /data/mysql/data/3333/mysqld.sock -pLhh@1234 -e \"select * from performance_schema.threads,(SELECT "$9" as PERCENTAGE_OS_CPU) A where THREAD_OS_ID="$1"\\G\" 2>/dev/null"|"/bin/bash"}'
//-p 后面加的是mysql的pid
7.排查锁
--直接查看被锁语句与锁源
5.7
SELECT r.trx_mysql_thread_id waiting_thread,w.requesting_trx_id waiting_trx_id,r.trx_query waiting_query,
concat(timestampdiff(SECOND,r.trx_wait_started,CURRENT_TIMESTAMP()),'s') AS duration,
b.trx_mysql_thread_id blocking_thread,w.blocking_trx_id blocking_trx_id,t.processlist_command state,b.trx_query blocking_current_query,e.sql_text blocking_last_query
FROM information_schema.INNODB_LOCK_WAITS w
JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id
JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id
JOIN performance_schema.threads t on t.processlist_id = b.trx_mysql_thread_id
LEFT JOIN performance_schema.events_statements_current e USING(thread_id);
8.0
SELECT r.trx_mysql_thread_id waiting_thread,w.REQUESTING_ENGINE_TRANSACTION_ID waiting_trx_id,r.trx_query waiting_query,
concat(timestampdiff(SECOND,r.trx_wait_started,CURRENT_TIMESTAMP()),'s') AS duration,
b.trx_mysql_thread_id blocking_thread,w.BLOCKING_ENGINE_TRANSACTION_ID blocking_trx_id,t.processlist_command state,b.trx_query blocking_current_query,e.sql_text blocking_last_query
FROM performance_schema.data_lock_waits w
JOIN information_schema.innodb_trx b ON b.trx_id = w.BLOCKING_ENGINE_TRANSACTION_ID
JOIN information_schema.innodb_trx r ON r.trx_id = w.REQUESTING_ENGINE_TRANSACTION_ID
JOIN performance_schema.threads t on t.processlist_id = b.trx_mysql_thread_id
LEFT JOIN performance_schema.events_statements_current e USING(thread_id);
--分步查出锁源头
(1)如果top命令显示的sys跟wait很高,排查锁
show status like 'innodb_row_lock%';
use information_schema
select
(2)查看哪个事务在等待(被阻塞)
mysql
use information_schema
SELECT trx_id,trx_state,trx_mysql_thread_id,trx_query FROM information_schema.INNODB_TRX WHERE trx_state='LOCK WAIT';
trx_id : 事务ID号
trx_state : 当前事务的状态
trx_mysql_thread_id:连接层的,连接线程ID(SHOW PROCESSLIST ===>Id或trx_id )
trx_query : 当前被阻塞的操作(一般是要丢给开发的)
(3)查看锁源
mysql
SELECT locked_table,waiting_trx_id,waiting_pid,blocking_trx_id,blocking_pid FROM sys.innodb_lock_waits;
locked_table : 哪张表出现的等待
waiting_trx_id: 等待的事务(与上个视图trx_id 对应)
waiting_pid : 等待的线程号(与上个视图trx_mysql_thread_id)
blocking_trx_id : 锁源的事务ID
blocking_pid : 锁源的线程号
(4)查看锁源sql层的thread_id
mysql
SELECT THREAD_ID,NAME,TYPE,PROCESSLIST_ID,PROCESSLIST_USER,PROCESSLIST_HOST,PROCESSLIST_DB,PROCESSLIST_COMMAND,PROCESSLIST_INFO FROM performance_schema.threads WHERE processlist_id=18;
//processlist_id是连接层的id,找出来的是sql层的线程id
(5)找到锁源的sql语句(根据上一步查出来的sql层id)
mysql
-- 当前在执行的语句
SELECT * FROM performance_schema.`events_statements_current` WHERE thread_id=131;
-- 执行语句的历史
SELECT * FROM performance_schema.`events_statements_history` WHERE thread_id=131;
//where是sql层的线程id
8.连接数
--打堆栈查看线程数
pstack $mysqlpid > mysql.log
cat mysql.log|grep Thread |wc -l
9.碎片
--查看某个库的碎片
SELECT * from ( SELECT CONCAT(table_schema,'.',table_name) AS 'table_name', table_rows AS 'Number of Rows', CONCAT(ROUND(data_length/(1024*1024),6),' M') AS 'data_size', CONCAT(ROUND(index_length/(1024*1024),6),' M') AS 'index_size' , CONCAT(ROUND(data_free/(1024*1024),6),' M') AS'data_free', ENGINE as 'engine' FROM information_schema.TABLES WHERE table_schema = 'test' ) t ORDER BY data_free DESC;
10.内存
--buffer_pool使用百分比
SELECT CONCAT(FORMAT(A.num * 100.0 / B.num,2),"%") BufferPoolFullPct FROM
(SELECT variable_value num FROM performance_schema.global_status
WHERE variable_name = 'Innodb_buffer_pool_pages_data') A,
(SELECT variable_value num FROM performance_schema.global_status
WHERE variable_name = 'Innodb_buffer_pool_pages_total') B;
--快速判断buffer pool够不够用,
show global status like 'innodb_buffer_pool_read%s';select sleep(60); show global status like 'innodb_buffer_pool_read%s';
--线程级占用内存计算
SELECT ( ( @@read_buffer_size + @@read_rnd_buffer_size + @@sort_buffer_size + @@join_buffer_size + @@binlog_cache_size + @@thread_stack + @@tmp_table_size + @@bulk_insert_buffer_size ) ) / (1024*1024) AS MEMORY_MB;
--全局内存使用
SELECT (@@innodb_buffer_pool_size +@@innodb_log_buffer_size +@@key_buffer_size) / 1024 /1024 AS MEMORY_MB;
--查看performance_schema
SELECT SUBSTRING_INDEX(event_name,'/',2) AS code_area, sys.format_bytes(SUM(current_alloc)) AS current_alloc FROM sys.x$memory_global_by_current_bytes GROUP BY SUBSTRING_INDEX(event_name,'/',2) ORDER BY SUM(current_alloc) DESC;
--根据主机分组查看内存使用
select host,current_count_used/1024/1024 as used_MB ,current_allocated/1024/1024 as allocated_MB,current_avg_alloc/1024/1024 as avg_alloc_MB,current_max_alloc/1024/1024 as max_alloc_MB,total_allocated/1024/1024 as total_all_MB from sys.x$memory_by_host_by_current_bytes limit 5;
--根据线程分组
select thread_id,user,current_count_used/1024/1024 as used_MB ,current_allocated/1024/1024 as allocated_MB,current_avg_alloc/1024/1024 as avg_alloc_MB,current_max_alloc/1024/1024 as max_alloc_MB,total_allocated/1024/1024 as total_all_MB from sys.x$memory_by_thread_by_current_bytes limit 5;
--根据用户分组
select user,current_count_used/1024/1024 as used_MB ,current_allocated/1024/1024 as allocated_MB,current_avg_alloc/1024/1024 as avg_alloc_MB,current_max_alloc/1024/1024 as max_alloc_MB,total_allocated/1024/1024 as total_all_MB from sys.x$memory_by_user_by_current_bytes;
--根据事件分组
select event_name,current_alloc/1024/1024 as current_all_MB,current_avg_alloc/1024/1024 as avg_all_MB,high_alloc/1024/1024 as high_all_MB,high_avg_alloc/1024/1024 as high_avg_all_MB from sys.x$memory_global_by_current_bytes limit 5;
--
11.内存激增模拟
12.授权
--授权表名开头为stmp_的语句
select concat('grant select on ',TABLE_SCHEMA,'.',TABLE_NAME,' to spms@\'%\';') from information_schema.tables where TABLE_SCHEMA='mdm' and TABLE_NAME like 'spms_%' into outfile '/tmp/grant.sql';
13.ps库存储过程应用
--清除ps库中consumer 记录,参数FALSE对所有表执行
call sys.ps_truncate_all_tables(FALSE);
--开启wait相关consumer
call sys.ps_setup_enable_consumer('waits');
--查看存储过程
select routine_schema,routine_type,routine_name from information_schema.routines;
14.MySQL监控
--监控MySQL当前活跃线程刷盘io
call sys.ps_setup_enable_consumer('waits');
select *,latency/1000/1000/1000 as latency_ms from sys.x$latest_file_io order by latency desc limit 20;
15. 长事务
select trx_id,trx_mysql_thread_id,unix_timestamp() - unix_timestamp(trx_started) as time,trx_query from information_schema.INNODB_TRX where unix_timestamp() - unix_timestamp(trx_started) > 600;
select evt.event_id as event_id ,evt.DIGEST_TEXT as digest_text,MYSQL_ERRNO,ERRORS
from
performance_schema.threads thd left join performance_schema.events_statements_history evt
on
thd.THREAD_ID = evt.THREAD_ID
where
thd.processlist_id = 3
order by evt.EVENT_ID DESC limit 100;
16.innodb buffer pool
--获取互斥锁信息
/mysqldata/mysql_base/8.0.24/bin/mysql -pmsandbox -S /tmp/mysql_sandbox8025.sock -BNe "SHOW ENGINE INNODB MUTEX" | awk -F'\t' '{split($3, waits, "="); out[$2]+=waits[2];} END { for(el in out) printf "%s\t%d\n", el, out[el] } '
17.压测
## mysqlslap
/data01/mysql/mysql_base/5.7.32/bin/mysqlslap --delimiter=";" \
--create-schema=test \
--query=" " \
--concurrency=10 --iterations=2 --number-of-queries=20\
--host=127.0.0.1 --port=8025 \
--user=test --password=Lhh@1234 \
--commit=1
//concurrency:客户端连接个数
//iterations:迭代遍数,同样环境下整体运行几遍,1遍时max avg min结果相同
//number-of-queries:指定总的查询次数
## sysbench
sysbench /usr/local/share/sysbench/oltp_read_write.lua --db-driver=mysql --threads=128 --time=120 --mysql-host=127.0.0.1 --mysql-port=2333 --mysql-user=test --mysql-password=Lhh@1234 --tables=1 --table-size=500000 --report-interval=1 prepare
18.数据一致性校验
mysql [localhost:8032] {root} (test) > checksum table t1;
+---------+------------+
| Table | Checksum |
+---------+------------+
| test.t1 | 1528270017 |
+---------+------------+
1 row in set (0.01 sec)
19.optimizer trace
##开启optimize trace,直对本session开启
set optimizer_trace="enabled=on";
##执行我们要分析的SQL
##查看上述SQL的trace信息
SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE\G
##关闭optimize trace
set optimizer_trace="enabled=off";
20.查看虚拟列(debug版本)
--查询表有多少个列(包括虚拟列)
select count(*) from mysql.columns where table_id = ( select id from mysql.tables where name = 'test') and name not in ('DB_ROLL_PTR', 'DB_TRX_ID');
-- 查看普通字段和隐藏字段各有多少个select hidden, count(*) from mysql.columns where table_id = ( select id from mysql.tables where name = 'test') and name not in ('DB_ROLL_PTR', 'DB_TRX_ID') group by hidden;
20.dbdepolyer部署
dbdeployer deploy replication 5.7.27 --repl-crash-safe --gtid --my-cnf-options="character_set_server=utf8mb4" --sandbox-directory=test-delay
21.执行过的sql
select thd.processlist_host,evt.SQL_TEXT from performance_schema.threads thd left join performance_schema.events_statements_history evt on thd.THREAD_ID=evt.THREAD_ID where evt.SQL_TEXT like '%delete%' limit 10;
22.重启清空buffer_pool配置
重启 mysqld ,清空 innodb buffer pool,注意参数:
innodb_buffer_pool_size = 64M
innodb_buffer_pool_load_at_startup = 0
innodb_buffer_pool_dump_at_shutdown = 0
innodb_buffer_pool_dump_pct = 0
23.关注脏页比例,合理设置innodb_io_capacity
select VARIABLE_VALUE into @a from global_status where VARIABLE_NAME = 'Innodb_buffer_pool_pages_dirty';
select VARIABLE_VALUE into @b from global_status where VARIABLE_NAME = 'Innodb_buffer_pool_pages_total';
select @a/@b; //尽量小于75