mysql常用命令

常用命令

修改mysql时区

1
2
3
4
5
6
7
8
9
# 查看时区
show variables like "%time_zone%";

# 设置时区为UTC+8:00
set global time_zone = '+8:00';
set time_zone = '+8:00';

# 刷新立即生效
flush privileges;

使用mysql binlog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 查看日志是否开启
show variables like 'log_%';

# 查看binlog配置
show variables like 'binlog_%';

# 查看数据库当前日志情况
show master status;

# 查看所有日志文件
show binary logs;
show master logs;

# 根据事件查看文件
show binlog events in '${binlogname}';

# 查看binlog日志文件内容
mysqlbinlog ${binlogname}

# 以人类能看懂的方式查看binlog文件内容
mysqlbinlog --base64-output=decode-rows -vvvvvv ${binlogname}

# 使用-r命令将日志转写进sql
mysqlbinlog --base64-output=decode-rows -vvvvvv ${binlogname} -r ${sqlname.sql}

# 还原数据库
mysqlbinlog --start-positoin=${number} --stop-position=${number} ${binlogname} -d ${databasename} | mysql -u${username} -p${password}

  转写进文件的日志不能直接执行,不过我们可以从其中提取有用信息。
  因为binlog日志里面会将删除等操作全部保存下来,所以再还原数据库信息时一定不能全部解析,需要指定确切位置。
  指定位置,数据库,时间,端口,host的命令都可以跟在mysqlbinlog后面,不只只用来恢复,转储,查看都可以。这些命令可以排列组合,放在日志名称前即可。

mysqlbinlog其他参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 起始位置
--start-position=${positiom number}
# 结束位置
--stop-position=${position number}

# 开始时间
--start-datetime=${starttime}
# 结束时间
--stop-datetime=${stoptime}

# 数据库
-d ${databasename}

# 端口号(大写的P)
-P ${port}

# ip地址
-h ${hosts}

# serverId
--server-id=${server-id}