在本篇文章中将给大家讲解下msyql中now()、sysdate()、curdate()区别:
SELECT date_sub(date_sub(date_format(now(),'%y-%m-%d '),interval extract( day from now())-1 day),interval 1 month);
select date_sub('2019-06-01',interval 1 month);
上个月第一天
select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract( day from now()) day),interval 0 month)
上个月最后一天
select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract( day from now())-1 day),interval 0 month)
select date_format(now(),'%Y-%m-01 ');
这个月第一天
Mysql日志管理
error log 错误日志 排错 /var/log/mysqld.log【默认开启】
bin log 二进制日志 备份 增量备份 DDL DML DCL
Relay log 中继日志 复制 接收 replication master
slow log 慢查询日志 调优 查询时间超过指定值
Error Log
log-error=/var/log/mysqld.log
Binary Log(用于备份恢复数据)
产生binlog日志:
log-bin=/var/log/mysql-bin/slave2
serve-id=2
mkdir /var/log/mysql-bin/slave2
#chmod mysql.mysql /var/log/mysql-bin/slave
#systemctl restart mysqld
-
重启mysqld 会截断旧日志产生新的日志
- 刷新日志会截断旧日志产生新的日志
mysql> flush logs
- 删除所有binlog(禁用)
mysql> reset master
- 删除部分日志
mysql> PURGE BINARY LOGS TO 'mysql-bin.010';
mysql> PURGE BINARY LOGS BEFORE '2016-04-02 22:46:26';
- 暂停binlog日志功能(仅对当前会话生效)
mysql> SET SQL_LOG_BIN=0;
mysql> SET SQL_LOG_BIN=1;
读取binlog日志:
mysqlbinlog mysql.000002
按datetime读取:
mysqlbinlog mysql.000002 --start-datetime="2018-12-05 10:02:56"
mysqlbinlog mysql.000002 --stop-datetime="2018-12-05 11:02:54"
mysqlbinlog mysql.000002 --start-datetime="2018-12-05 10:02:56" --stop-datetime="2018-12-05 11:02:54"
按position读取:
mysqlbinlog mysql.000002 --start-position=260
mysqlbinlog mysql.000002 --stop-position=260
mysqlbinlog mysql.000002 --start-position=260 --stop-position=930
查看带加密的binlong日志
mysqlbinlog ---output=decode-rows -v 日志文件
根据binlog恢复数据:
根据时间点恢复数据
mysqlbinlog --start-datetime='2014-11-25 11:56:54' --stop-datetime='2014-11-25 11:57:41' tiger-bin.000001 | mysql -u root -p1
根据位置点恢复数据
mysqlbinlog --start-position 106 --stop-position 527 tiger-bin.000001 | mysql -u root -p1
刷新bin-log日志:
#mysqladmin flush-logs
去除binlog加密:
transaction_isolation=repeatable-read
binlog_format=mixed
慢查询:
slow_query_log=1
slow_query_log_file=/var/log/mysql-slow/slow.log
long_query_time=3
mkdir /var/log/mysql-slow/
chown mysql.mysql /var/log/mysql-slow/
systemctl restart mysqld
查看慢查询日志
测试:BENCHMARK(count,expr)
SELECT BENCHMARK(50000000,2*3)