在MySQL数据库中,mysql-bin.000001、mysql- bin.000002等文件是数据库的操作日志,例如UPDATE一个表,或者DELETE一些数据,即使该语句没有匹配的数据,这个命令也会存储到日志文件中,还包括每个语句执行的时间,也会记录进去的。
这样做主要有以下两个目的:
1:数据恢复
如果你的数据库出问题了,而你之前有过备份,那么可以看日志文件,找出是哪个命令导致你的数据库出问题了,想办法挽回损失。
2:主从服务器之间同步数据
主服务器上所有的操作都在记录日志中,从服务器可以根据该日志来进行,以确保两个同步。
处理方法分两种情况:
1:只有一个mysql服务器,那么可以简单的注释掉这个选项就行了。
vi /etc/my.cnf把里面的log-bin这一行注释掉,重启mysql服务即可。
2:如果你的环境是主从服务器,那么就需要做以下操作了。
A:在每个从属服务器上,使用SHOW SLAVE STATUS来检查它正在读取哪个日志。
B:使用SHOW MASTER LOGS获得主服务器上的一系列日志。
C:在所有的从属服务器中判定最早的日志,这个是目标日志,如果所有的从属服务器是更新的,就是清单上的最后一个日志。
D:清理所有的日志,但是不包括目标日志,因为从服务器还要跟它同步。
清理日志方法为:
PURGE MASTER LOGS TO 'mysql-bin.010';
PURGE MASTER LOGS BEFORE '2008-12-19 21:00:00';
如果你确定从服务器已经同步过了,跟主服务器一样了,那么可以直接RESET MASTER将这些文件删除。
[root@jiucool var]# /usr/local/mysql/bin/mysql -u root -p Enter password: (输入密码) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 264001 Server version: 5.1.35-log Source distribution Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement. mysql> (清除日志文件) Query OK, 0 rows affected (8.51 sec) mysql>
好了,我们再来查看下mysql文件夹占用多少空间?
[root@jiucool var]# /usr/local/mysql/ 37M /usr/local/mysql/var 70M /usr/local/mysql/mysql-test 15M /usr/local/mysql/lib 448K /usr/local/mysql/include 2.9M /usr/local/mysql/share 7.6M /usr/local/mysql/libexec 17M /usr/local/mysql/bin 11M /usr/local/mysql/docs 2.9M /usr/local/mysql/sql-bench 163M /usr/local/mysql/
好了,看一下,整个mysql目录才占用163M大小!OK,没问题,既然mysql-bin.0000X日志文件占用这么大空间,存在的意义又不是特别大,那么我们就不让它生成吧。
[root@jiucool var]#
找到了my.cnf 即mysql配置文件,我们将log-bin=mysql-bin 这条注释掉即可.
# Replication Master Server (default) # binary logging is required for replication # log-bin=mysql-bin
在Mysql中,如果启用mysql-bin日志记录,则Mysql的变更修改都会被记录到日志文件中:
mysql> show global variables like '%bin%';
+---------------------------------+----------------------+
| Variable_name | Value |
+---------------------------------+----------------------+
| binlog_cache_size | 32768 |
| innodb_locks_unsafe_for_binlog | OFF |
| log_bin | ON |
| log_bin_trust_function_creators | OFF |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 104857600 |
| sync_binlog | 0 |
+---------------------------------+----------------------+
7 rows in set (0.00 sec)
如果使用了配置文件,则可以修改 /etc/my.cnf 把里面的log-bin这一行注释掉,重启mysql服务即可关闭bin日志的记录。
一个客户的数据库,积累了大量的bin日志:
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000064 | 3175623 |
| mysql-bin.000065 | 104857696 |
| mysql-bin.000066 | 30124812 |
| mysql-bin.000067 | 104857882 |
| mysql-bin.000068 | 104857829 |
| mysql-bin.000069 | 104857910 |
| mysql-bin.000070 | 29627738 |
| mysql-bin.000071 | 104857777 |
| mysql-bin.000072 | 104857857 |
| mysql-bin.000073 | 104857728 |
| mysql-bin.000074 | 104857739 |
| mysql-bin.000075 | 104857775 |
| mysql-bin.000076 | 104857686 |
| mysql-bin.000077 | 104857875 |
| mysql-bin.000078 | 104857927 |
| mysql-bin.000079 | 104857827 |
| mysql-bin.000080 | 104857856 |
| mysql-bin.000081 | 104857762 |
| mysql-bin.000082 | 104857797 |
| mysql-bin.000083 | 104857860 |
| mysql-bin.000084 | 104857751 |
| mysql-bin.000085 | 104857925 |
| mysql-bin.000086 | 104857791 |
| mysql-bin.000087 | 104857838 |
| mysql-bin.000088 | 104857749 |
| mysql-bin.000089 | 104857836 |
| mysql-bin.000090 | 104857988 |
| mysql-bin.000091 | 104857807 |
| mysql-bin.000092 | 104857691 |
| mysql-bin.000093 | 104857834 |
| mysql-bin.000094 | 104857647 |
| mysql-bin.000095 | 104857814 |
| mysql-bin.000096 | 104857830 |
| mysql-bin.000097 | 104858049 |
| mysql-bin.000098 | 104857862 |
| mysql-bin.000099 | 104858089 |
| mysql-bin.000100 | 104857760 |
| mysql-bin.000101 | 104857650 |
| mysql-bin.000102 | 104857794 |
| mysql-bin.000103 | 104857656 |
| mysql-bin.000104 | 104857961 |
| mysql-bin.000105 | 104857725 |
| mysql-bin.000106 | 104857804 |
| mysql-bin.000107 | 104857925 |
| mysql-bin.000108 | 104857717 |
| mysql-bin.000109 | 104857678 |
| mysql-bin.000110 | 104857675 |
| mysql-bin.000111 | 104857897 |
| mysql-bin.000112 | 104857947 |
| mysql-bin.000113 | 104857651 |
| mysql-bin.000114 | 104858066 |
| mysql-bin.000115 | 104857766 |
| mysql-bin.000116 | 104857719 |
| mysql-bin.000117 | 104857732 |
| mysql-bin.000118 | 104857733 |
| mysql-bin.000119 | 104857814 |
| mysql-bin.000120 | 104857763 |
| mysql-bin.000121 | 104857680 |
| mysql-bin.000122 | 104857742 |
| mysql-bin.000123 | 104858011 |
| mysql-bin.000124 | 104857708 |
| mysql-bin.000125 | 104857821 |
| mysql-bin.000126 | 104857877 |
| mysql-bin.000127 | 104857735 |
| mysql-bin.000128 | 104857781 |
| mysql-bin.000129 | 104857743 |
| mysql-bin.000130 | 104857858 |
| mysql-bin.000131 | 104857860 |
| mysql-bin.000132 | 104857809 |
| mysql-bin.000133 | 104857711 |
| mysql-bin.000134 | 104857744 |
| mysql-bin.000135 | 104857802 |
| mysql-bin.000136 | 104857672 |
| mysql-bin.000137 | 104857933 |
| mysql-bin.000138 | 104857758 |
| mysql-bin.000139 | 104857657 |
| mysql-bin.000140 | 104857664 |
| mysql-bin.000141 | 104857715 |
| mysql-bin.000142 | 104857881 |
| mysql-bin.000143 | 104857958 |
| mysql-bin.000144 | 104857917 |
| mysql-bin.000145 | 104857771 |
| mysql-bin.000146 | 104857731 |
| mysql-bin.000147 | 104857770 |
| mysql-bin.000148 | 104857864 |
| mysql-bin.000149 | 104857726 |
| mysql-bin.000150 | 104857885 |
| mysql-bin.000151 | 104857817 |
| mysql-bin.000152 | 104857909 |
| mysql-bin.000153 | 104857665 |
| mysql-bin.000154 | 104857647 |
| mysql-bin.000155 | 104857880 |
| mysql-bin.000156 | 104858010 |
| mysql-bin.000157 | 104857821 |
| mysql-bin.000158 | 104857649 |
| mysql-bin.000159 | 104857930 |
| mysql-bin.000160 | 104857856 |
| mysql-bin.000161 | 104857673 |
| mysql-bin.000162 | 104857969 |
| mysql-bin.000163 | 104857848 |
| mysql-bin.000164 | 104857656 |
| mysql-bin.000165 | 104857721 |
| mysql-bin.000166 | 104857758 |
| mysql-bin.000167 | 104857934 |
| mysql-bin.000168 | 104857753 |
| mysql-bin.000169 | 104857833 |
| mysql-bin.000170 | 104858059 |
| mysql-bin.000171 | 104857766 |
| mysql-bin.000172 | 104857865 |
| mysql-bin.000173 | 104857741 |
| mysql-bin.000174 | 104857922 |
| mysql-bin.000175 | 104857776 |
| mysql-bin.000176 | 104857720 |
| mysql-bin.000177 | 104857647 |
| mysql-bin.000178 | 104857977 |
| mysql-bin.000179 | 104857736 |
| mysql-bin.000180 | 104857725 |
| mysql-bin.000181 | 104857883 |
| mysql-bin.000182 | 104857810 |
| mysql-bin.000183 | 104857707 |
| mysql-bin.000184 | 104857799 |
| mysql-bin.000185 | 104857977 |
| mysql-bin.000186 | 104857804 |
| mysql-bin.000187 | 104857861 |
| mysql-bin.000188 | 104857665 |
| mysql-bin.000189 | 104857872 |
| mysql-bin.000190 | 104857870 |
| mysql-bin.000191 | 104857891 |
| mysql-bin.000192 | 104857742 |
| mysql-bin.000193 | 104857830 |
| mysql-bin.000194 | 104857873 |
| mysql-bin.000195 | 104857750 |
| mysql-bin.000196 | 104857669 |
| mysql-bin.000197 | 104857867 |
| mysql-bin.000198 | 104857702 |
| mysql-bin.000199 | 104857974 |
| mysql-bin.000200 | 104857866 |
| mysql-bin.000201 | 104857742 |
| mysql-bin.000202 | 104857688 |
| mysql-bin.000203 | 104857654 |
| mysql-bin.000204 | 104857895 |
| mysql-bin.000205 | 104857709 |
| mysql-bin.000206 | 104857922 |
| mysql-bin.000207 | 104858019 |
| mysql-bin.000208 | 104857790 |
| mysql-bin.000209 | 104857655 |
| mysql-bin.000210 | 104857996 |
| mysql-bin.000211 | 104857956 |
| mysql-bin.000212 | 104857659 |
| mysql-bin.000213 | 104857835 |
| mysql-bin.000214 | 104857643 |
| mysql-bin.000215 | 104857999 |
| mysql-bin.000216 | 104857770 |
| mysql-bin.000217 | 104857672 |
| mysql-bin.000218 | 104857753 |
| mysql-bin.000219 | 104857795 |
| mysql-bin.000220 | 104858032 |
| mysql-bin.000221 | 104857861 |
| mysql-bin.000222 | 104857896 |
| mysql-bin.000223 | 104857694 |
| mysql-bin.000224 | 104857747 |
| mysql-bin.000225 | 104857861 |
| mysql-bin.000226 | 104857783 |
| mysql-bin.000227 | 104857680 |
| mysql-bin.000228 | 104857882 |
| mysql-bin.000229 | 104857688 |
| mysql-bin.000230 | 104857756 |
| mysql-bin.000231 | 104857744 |
| mysql-bin.000232 | 104857759 |
| mysql-bin.000233 | 104857794 |
| mysql-bin.000234 | 104857863 |
| mysql-bin.000235 | 104858031 |
| mysql-bin.000236 | 104857902 |
| mysql-bin.000237 | 104857715 |
| mysql-bin.000238 | 104857746 |
| mysql-bin.000239 | 104857839 |
| mysql-bin.000240 | 104857845 |
| mysql-bin.000241 | 104857771 |
| mysql-bin.000242 | 104857692 |
| mysql-bin.000243 | 104857806 |
| mysql-bin.000244 | 104857927 |
| mysql-bin.000245 | 104857729 |
| mysql-bin.000246 | 104857961 |
| mysql-bin.000247 | 104857687 |
| mysql-bin.000248 | 104857872 |
| mysql-bin.000249 | 104857881 |
| mysql-bin.000250 | 104858045 |
| mysql-bin.000251 | 104857817 |
| mysql-bin.000252 | 104857901 |
| mysql-bin.000253 | 104858075 |
| mysql-bin.000254 | 104857730 |
| mysql-bin.000255 | 104857756 |
| mysql-bin.000256 | 104857927 |
| mysql-bin.000257 | 104857892 |
| mysql-bin.000258 | 104857738 |
| mysql-bin.000259 | 104857914 |
| mysql-bin.000260 | 104857774 |
| mysql-bin.000261 | 104857892 |
| mysql-bin.000262 | 104857941 |
| mysql-bin.000263 | 104857845 |
| mysql-bin.000264 | 104857732 |
| mysql-bin.000265 | 104857881 |
| mysql-bin.000266 | 104857782 |
| mysql-bin.000267 | 104857927 |
| mysql-bin.000268 | 104857881 |
| mysql-bin.000269 | 104857791 |
| mysql-bin.000270 | 104857749 |
| mysql-bin.000271 | 104857777 |
| mysql-bin.000272 | 104857879 |
| mysql-bin.000273 | 104857767 |
| mysql-bin.000274 | 104858094 |
| mysql-bin.000275 | 104858023 |
| mysql-bin.000276 | 104857726 |
| mysql-bin.000277 | 104857810 |
| mysql-bin.000278 | 104857868 |
| mysql-bin.000279 | 104857861 |
| mysql-bin.000280 | 104857695 |
| mysql-bin.000281 | 104857974 |
| mysql-bin.000282 | 104857873 |
| mysql-bin.000283 | 104857787 |
| mysql-bin.000284 | 104857778 |
| mysql-bin.000285 | 104857901 |
| mysql-bin.000286 | 104857999 |
| mysql-bin.000287 | 104857860 |
| mysql-bin.000288 | 104857698 |
| mysql-bin.000289 | 104857773 |
| mysql-bin.000290 | 104857751 |
| mysql-bin.000291 | 104857741 |
| mysql-bin.000292 | 104857926 |
| mysql-bin.000293 | 104857693 |
| mysql-bin.000294 | 104857684 |
| mysql-bin.000295 | 104857892 |
| mysql-bin.000296 | 104857697 |
| mysql-bin.000297 | 104857985 |
| mysql-bin.000298 | 104857656 |
| mysql-bin.000299 | 104857683 |
| mysql-bin.000300 | 104857695 |
| mysql-bin.000301 | 104857646 |
| mysql-bin.000302 | 104857812 |
| mysql-bin.000303 | 92771646 |
+------------------+-----------+
240 rows in set (0.00 sec)
如果没有主从复制,可以通过reset master的方式,重置数据库日志,清除之前的日志文件:
mysql> reset master;
Query OK, 0 rows affected (8.51 sec)
但是如果存在复制关系,应当通过PURGE的方式来清理bin日志:
语法如下:
PURGE {MASTER | BINARY} LOGS TO 'log_name'
PURGE {MASTER | BINARY} LOGS BEFORE 'date'
用于删除列于在指定的日志或日期之前的日志索引中的所有二进制日志。这些日志也会从记录在日志索引文件中的清单中被删除,这样被给定的日志成为第一个。
例如:
PURGE MASTER LOGS TO 'mysql-bin.010';
PURGE MASTER LOGS BEFORE '2008-06-23 15:00:00';
清除3天前的 binlog
PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY);
BEFORE变量的 date自变量可以为'YYYY-MM-DD hh:mm:ss'格式。MASTER和BINARY是同义词。
如果您有一个活性的从属服务器,该服务器当前正在读取您正在试图删除的日志之一,则本语句不会起作用,而是会失败,并伴随一个错误。不过, 如果从属服务器是休止的,并且您碰巧清理了其想要读取的日志之一,则从属服务器启动后不能复制。当从属服务器正在复制时,本语句可以安全运行。您不需要停止它们。
要清理日志,需按照以下步骤:
1. 在每个从属服务器上, 使用SHOW SLAVE STATUS来检查它正在读取哪个日志。
2. 使用SHOW MASTER LOGS获得主服务器上的一系列日志。
3. 在所有的从属服务器中判定最早的日志。这个是目标日志。如果所有的从属服务器是更新的,这是清单上的最后一个日志。
4. 制作您将要删除的所有日志的备份。(这个步骤是自选的,但是建议采用。)
5. 清理所有的日志,但是不包括目标日志,因为从服务器还要跟它同步
常用的命令:
mysql> show slave hosts -- 查看所有连接到Master的Slave信息
mysql> show master status -- 查看Master状态信息
mysql>show slave status -- 查看Slave状态信息
mysql> show binary logs -- 查看所有二进制日志
mysql>show binlog events [IN log_file] -- 查看二进制日志中的事件
供参考。
mysqlbinlog 查看mysql bin 日志
mysqlbinlog mysql-bin.000036 | less
查询包含几个字段的语句:
mysqlbinlog mysql-bin.000036| egrep '(2011030610002460|2011030310001730|2011030410011680|2011030410006280)'
mysqlbinlog查看二进制日志的问题 在使用mysqlbinlog查看日志的时候碰到了一个问题, 错误提示如下: /usr/local/mysql/bin/mysqlbinlog: unknown variable 'default-character-set=utf8' 产生这个问题的原因是因为我在my.cnf中的client选项组中添加了 default-character-set=utf8 要解决这个bug的方法还是有的, 一种方法是使用:--no-defaults ./mysqlbinlog --no-defaults mysql-bin.000063 另一种方法:使用mysqlbinlog工具查看二进制日志时会重新读取的mysql的配置文件my.cnf(windows下是my.ini),而不是服务器已经加载进内存的配置文件。也就是说只要修改并保存了my.cnf文件,而不需要重起mysql服务器,则使用mysqlbinlog查看时修改后的my.cnf配置文件对mysqlbinlog而言已经生效。所以这里可以使用此方法:把client选项组中default-character-set=utf8选项屏蔽掉,如 #default-character-set=utf8 然后运行mysqlbinlog工具,则不会产生任何问题了。当然记得在不在使用mysqlbinlog工具时,把#default-character-set=utf8 选项恢复为default-character-set=utf8 |