Monday, May 24, 2010

mysqlbinlog Ver 3.3和3.1版本之间的输出差异

mysqlbinlog Ver 3.3中多了一个--base64-output参数,默认值为auto:


--base64-output[=name]
Determine when the output statements should be
base64-encoded BINLOG statements: 'never' disables it and
works only for binlogs without row-based events; 'auto'
is the default and prints base64 only when necessary
(i.e., for row-based events and format description
events); 'always' prints base64 whenever possible.
'always' is for debugging only and should not be used in
a production system. The default is 'auto'.
--base64-output is a short form for
--base64-output=always.

在原来的Ver3.1版本中没有此参数,对比mysql5.0生成的日志文件的输出结果:
Ver3.1版本中使用mysqlbinlog输出结果:

shell> mysqlbinlog mysql-bin.000182|less
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#100515 1:38:58 server id 25 end_log_pos 98 Start: binlog v 4, server v 5.0.37-log created 100515 1:38:58
# at 98

Ver3.3版本中使用mysqlbinlog输出结果:

shell> mysqlbinlog --base64-output=never mysql-bin.000182|less
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#100515 1:38:58 server id 25 end_log_pos 98 Start: binlog v 4, server v 5.0.37-log created 100515 1:38:58
# at 98

shell> mysqlbinlog --base64-output=auto mysql-bin.000182|less
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#100515 1:38:58 server id 25 end_log_pos 98 Start: binlog v 4, server v 5.0.37-log created 100515 1:38:58
BINLOG '
sortSw8ZAAAAXgAAAGIAAAAAAAQANS4wLjM3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAASwAEGg==
'/*!*/;
# at 98

从上面结果比较而言,相当于3.1版本的mysqlbinlog的base64-output参数默认为never。
在mysql 5.0中带的mysqlbinlog一般为3.1版本的,而mysql5.1版本中带的mysqlbinlog则为3.3版本,如果需要将mysql5.0生成的日志文件导入mysql5.1时,需要将mysqlbinlog的base64-output值设置为never。如果不设置此参数,导入日志时,会在
BINLOG 'sortSw8ZAAAAXgAAAGIAAAAAAAQANS4wLjM3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAASwAEGg=='

处报SQL语法错误。

MySQL5 slave同步报错(server_errno=1236)

100519 19:35:33 [Note] Slave I/O thread: connected to master 'repl@master:3306', replication started in log 'mysql-bin.000176' at position 615525147

100519 19:35:33 [ERROR] Error reading packet from server: Could not find first log file name in binary log index file ( server_errno=1236)

从日志信息上可知,slave已经连接到master,并且准备从指定的binlog文件指定位置开始同步,但后面错误提示日志文件找不到,而master服务器上日志文件是存在的。
这种情况有二种处理方法:
一、重启主数据库(master)之后,然后slave上stop slave;start slave,再检查同步的状态。
二、不重启主服务器,则用mysqlbinlog根据start-position或者start-datetime,将日志分析出来后,将分析结果在slave上用mysql命令导入,导入完成后,再用CHANGE MASTER TO语句,从下一个日志文件MASTER_LOG_POSITION=98开始同步。

另外在使用mysqlbinlog工具进行日志导入时,需要注意以下问题,下面内容转自MySQL官方手册:
如果MySQL服务器上有多个要执行的二进制日志,安全的方法是在一个连接中处理它们。下面是一个说明什么是不安全的例子:

shell> mysqlbinlog hostname-bin.000001 | mysql # DANGER!!
shell> mysqlbinlog hostname-bin.000002 | mysql # DANGER!!

使用与服务器的不同连接来处理二进制日志时,如果第1个日志文件包含一个CREATE TEMPORARY TABLE语句,第2个日志包含一个使用该临时表的语句,则会造成问题。当第1个mysql进程结束时,服务器撤销临时表。当第2个mysql进程想使用该表时,服务器报告 “不知道该表”。

要想避免此类问题,使用一个连接来执行想要处理的所有二进制日志中的内容。下面提供了一种方法:

shell> mysqlbinlog hostname-bin.000001 hostname-bin.000002 | mysql -u root -ppassword

Reference: http://yuweijun.blogspot.com/2009/12/mysql50.html

javascript中keyEvent按键事件说明(录自javascript权威指南第5版)

有3种按键类型,分别是keydown、keypress和keyup,它们分别对应onkeydown、onkeypress和onkeyup这几个事件处理器。
一个按键操作会产生这3个事件,依次是keydown、keypress,然后在按键释放的时候keyup。
这3个事件类型中,keypress事件是最为用户友好的:和它们相关的事件对象包含了所产生的实际字符的编码。keydown和keyup事件是较底层的,它们的按键事件包含一个和键盘所生成的硬件编码相关的“虚拟按键码”。对于ASCII字符集中的数字和字符,这些虚拟按键码和ASCII码相同。如果按下SHIFT键并按下数字2,keydown事件将通知发生了“SHITF-2”的按键事件。keypress事件会解释这一事件,说明这次按键产生了一个可打印的字符“@”。
对于不能打印的功能按键,如Backspace、Enter、Escape和箭头方向键、Page Up、Page Down以及F1到F12,它们会产生keydown和keyup事件。

在不同的浏览器中,按键事件的一些细节区别如下:


1、对于不能打印的功能按键,在Firefox中,也会产生keypress事件,在IE和Chrome中,则不会触发keypress事件,只有当按键有一个ASCII码的时候,即此字符为可打印字符或者一个控制字符的时候,keypress事件才会发生。对于这些不能打印的功能按键,可通过和keydown事件相关的keyCode来获取。
2、作为一条通用的规则,keydown事件对于功能按键来说是最有用的,而keypress事件对于可打印的按键来说是最有用的。
3、在IE中,Alt按键组合被认为是无法打印的,所以并不会触发keypress事件。
4、在Firefox中,按键事件定义有二个属性,keyCode存储了一个按键的较低层次的虚拟按键码,并且和keydown事件一起发送。charCode存储了按下一个键时所产生的可打印的字符的编码,并且和keypress事件一起发送。在Firefox中,功能按键会产生一个keypress事件,在这种情况下,charCode是0,而keyCode包含了虚拟按键码。在Firefox中,发生keydown事件时,charCode都为0,所以在keydown时获取charCode是无意义的。
5、在IE中,只有一个keyCode属性,并且它的解释也取决于事件的类型。对于keydown事件来说,keyCode是一个虚拟按键码,对于keypress事件来说,keyCode是一个字符码。
6、在Chrome中,功能键与IE中表现一样,不会触发keypress事件,对于keydown事件,也会在事件的keyCode中存储虚拟按键码,而charCode为0,与IE和Firefox表现一样,然而在发生可打印字符的keypress事件时,除了与Firefox一样,会在事件的charCode中存储实际按键编码之外,也会在keyCode中存储实际按键码,这二个值相同。
7、charCode字符码可以使用静态方数String.fromCharCode()转为字符。

rails session options

require 'action_controller/cgi_ext'
require 'action_controller/session/cookie_store'

module ActionController #:nodoc:
class Base
# Process a request extracted from a CGI object and return a response. Pass false as session_options to disable
# sessions (large performance increase if sessions are not needed). The session_options are the same as for CGI::Session:
#
# * :database_manager - standard options are CGI::Session::FileStore, CGI::Session::MemoryStore, and CGI::Session::PStore
# (default). Additionally, there is CGI::Session::DRbStore and CGI::Session::ActiveRecordStore. Read more about these in
# lib/action_controller/session.
# * :session_key - the parameter name used for the session id. Defaults to '_session_id'.
# * :session_id - the session id to use. If not provided, then it is retrieved from the +session_key+ cookie, or
# automatically generated for a new session.
# * :new_session - if true, force creation of a new session. If not set, a new session is only created if none currently
# exists. If false, a new session is never created, and if none currently exists and the +session_id+ option is not set,
# an ArgumentError is raised.
# * :session_expires - the time the current session expires, as a Time object. If not set, the session will continue
# indefinitely.
# * :session_domain - the hostname domain for which this session is valid. If not set, defaults to the hostname of the
# server.
# * :session_secure - if +true+, this session will only work over HTTPS.
# * :session_path - the path for which this session applies. Defaults to the directory of the CGI script.
# * :cookie_only - if +true+ (the default), session IDs will only be accepted from cookies and not from
# the query string or POST parameters. This protects against session fixation attacks.
def self.process_cgi(cgi = CGI.new, session_options = {})
new.process_cgi(cgi, session_options)
end

def process_cgi(cgi, session_options = {}) #:nodoc:
process(CgiRequest.new(cgi, session_options), CgiResponse.new(cgi)).out
end
end

end

Regexp Magic in vim

Some characters in the pattern are taken literally. They match with the same
character in the text. When preceded with a backslash however, these
characters get a special meaning.

Other characters have a special meaning without a backslash. They need to be
preceded with a backslash to match literally.

If a character is taken literally or not depends on the 'magic' option and the
items mentioned next.

*/\m* */\M*
Use of "\m" makes the pattern after it be interpreted as if 'magic' is set,
ignoring the actual value of the 'magic' option.
Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used.

*/\v* */\V*
Use of "\v" means that in the pattern after it all ASCII characters except
'0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning. "very magic"

Use of "\V" means that in the pattern after it only the backslash has a
special meaning. "very nomagic"

Examples:


after: \v \m \M \V matches ~
$ $ $ \$ matches end-of-line
. . \. \. matches any character
* * \* \* any number of the previous atom
() \(\) \(\) \(\) grouping into an atom
| \| \| \| separating alternatives
\a \a \a \a alphabetic character
\\ \\ \\ \\ literal backslash
\. \. . . literal dot
\{ { { { literal '{'
a a a a literal 'a'

{only Vim supports \m, \M, \v and \V}


It is recommended to always keep the 'magic' option at the default setting,
which is 'magic'. This avoids portability problems. To make a pattern immune
to the 'magic' option being set or not, put "\m" or "\M" at the start of the
pattern.

Without \v:
:%s/^\%(foo\)\{1,3}\(.\+\)bar$/\1/

With \v:
:%s/\v^%(foo){1,3}(.+)bar$/\1/

See also :h /\v

在CentOS 5中开启apache2的mod_ssl模块

在配置完/etc/httpd/conf.d/ssl.conf文件之后,进行配置文件语法正确性测试时,报语法错误如下:
$> httpd -t
Syntax error on line 5 of /etc/httpd/conf.d/ssl.conf:
Invalid command 'SSLPassPhraseDialog', perhaps misspelled or defined by a module not included in the server configuration

移除ssl.conf文件之后,语法检查正确,列出httpd的模块检查:
$> httpd -M
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
auth_basic_module (shared)
auth_digest_module (shared)
authn_file_module (shared)
authn_alias_module (shared)
authn_anon_module (shared)
authn_dbm_module (shared)
authn_default_module (shared)
authz_host_module (shared)
authz_user_module (shared)
authz_owner_module (shared)
authz_groupfile_module (shared)
authz_dbm_module (shared)
authz_default_module (shared)
ldap_module (shared)
authnz_ldap_module (shared)
include_module (shared)
log_config_module (shared)
logio_module (shared)
env_module (shared)
ext_filter_module (shared)
mime_magic_module (shared)
expires_module (shared)
deflate_module (shared)
headers_module (shared)
usertrack_module (shared)
setenvif_module (shared)
mime_module (shared)
dav_module (shared)
status_module (shared)
autoindex_module (shared)
info_module (shared)
dav_fs_module (shared)
vhost_alias_module (shared)
negotiation_module (shared)
dir_module (shared)
actions_module (shared)
speling_module (shared)
userdir_module (shared)
alias_module (shared)
rewrite_module (shared)
proxy_module (shared)
proxy_balancer_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_connect_module (shared)
cache_module (shared)
suexec_module (shared)
disk_cache_module (shared)
file_cache_module (shared)
mem_cache_module (shared)
cgi_module (shared)
version_module (shared)
proxy_ajp_module (shared)
Syntax OK

发现apache没有加载ssl_module,并且/etc/httpd/modules目录中无mod_ssl.so文件,需要在线安装:
$> yum search mod_ssl
mod_ssl.x86_64 : SSL/TLS module for the Apache HTTP server

$> yum install mod_ssl
Installed: mod_ssl.x86_64 1:2.2.3-43.el5.centos
Dependency Installed: distcache.x86_64 0:1.4.5-14.1
Updated: httpd.x86_64 0:2.2.3-43.el5.centos
Complete!

安装mod_ssl会增加一个用户类型,所以会修改/etc/passwd文件。
安装完成之后,修改ssl.conf文件,在文件顶部添加以下一行代码,加载ssl_module:

LoadModule ssl_module modules/mod_ssl.so
Listen 443
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
SSLPassPhraseDialog builtin
SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout 300
SSLMutex default

<VirtualHost _default_:443>
DocumentRoot "/path/to/wwwroot"
ServerName www.test.com:443
ServerAdmin test.yu@gmail.com
SSLEngine on
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
SSLCertificateFile /usr/local/apache2/conf/ssl.crt/server.crt
SSLCertificateKeyFile /usr/local/apache2/conf/ssl.key/server.key
SSLCACertificateFile /usr/local/apache2/conf/ssl.crt/cacertificate.crt
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory "/usr/local/apache2/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
SetEnvIf User-Agent ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
</VirtualHost>
再测试httpd配置文件:
$> httpd -t
Syntax OK

MySQL: Many tables or many databases?

Question:
For a project we having a bunch of data that always have the same structure and is not linked together. There are two approaches to save the data:
* Creating a new database for every pool (about 15-25 tables)
* Creating all the tables in one database and differ the pools by table names.
Which one is easier and faster to handle for MySQL?

Answer:
There should be no significant performance difference between multiple tables in a single database versus multiple tables in separate databases.

In MySQL, databases (standard SQL uses the term "schema" for this) serve chiefly as a namespace for tables. A database has only a few attributes, e.g. the default character set and collation. And that usage of GRANT makes it convenient to control access privileges per database, but that has nothing to do with performance.

You can access tables in any database from a single connection (provided they are managed by the same instance of MySQL Server). You just have to qualify the table name:

SELECT * FROM database17.accounts_table;

This is purely a syntactical difference. It should have no effect on performance.

Regarding storage, you can't organize tables into a file-per-database as @Chris speculates. With the MyISAM storage engine, you always have a file per table. With the InnoDB storage engine, you either have a single set of storage files that amalgamate all tables, or else you have a file per table (this is configured for the whole MySQL server, not per database). In either case, there's no performance advantage or disadvantage to creating the tables in a single database versus many databases.

There aren't many MySQL configuration parameters that work per database. Most parameters that affect server performance are server-wide in scope.

Regarding backups, you can specify a subset of tables as arguments to the mysqldump command. It may be more convenient to back up logical sets of tables per database, without having to name all the tables on the command-line. But it should make no difference to performance, only convenience for you as you enter the backup command.

-- Bill Karwin (the author of SQL Antipatterns from Pragmatic Bookshelf)

linux命令行显示指定行号的内容

以第四行为例,要查询的文件名为list.txt:
方法1:

grep -n '^' list.txt |grep '^4:'|grep -o '[^4:].*'

方法2:
sed -n '4p' list.txt

sed -n '4,4p' list.txt
方法3:
awk '{if ( NR==4 ) print $0}' list.txt

方法4:
tac list.txt |tail -4|tac|tail -1
tac list.txt |tail -n 4|tac|tail -n 1

References: http://zhidao.baidu.com/question/91856742

Tuesday, May 11, 2010

利用document.write()和noscript标签做内文替换的小技巧

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>noscript tips</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<script>document.write("新页面内容" + "<nosc"+"ript>");</script>
原来的页面内容: 利用document.write()打印新的页面内容,并且打出一个noscript起始标签,在新页面内容想要替换的页面内容结尾处,添加一个noscript结束标签,从而利用document.write()和noscript标签对,用新页面内容替换原来的页面内容,这个小技巧是在看google做页面内容不同时的效果跟踪代码时发现的。
</noscript>
</body>
</html>