存档

2009年3月 的存档

python use mysql

2009年3月31日 评论已被关闭

python连接mysql中我遇到了这样的问题

Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
  File “build/bdist.linux-i686/egg/MySQLdb/__init__.py”, line 81, in Connect
  File “build/bdist.linux-i686/egg/MySQLdb/connections.py”, line 188, in __init__
_mysql_exceptions.OperationalError: (2002, “Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)”)

mysql启动是参数设定的比较多,我的mysql启动脚本:
/home/2hei/mysql/bin/mysqld –defaults-file=/home/2hei/mysql/etc/my.cnf –basedir=/home/2hei/mysql –datadir=/home/2hei/mysql/data –log-error=/home/2hei/mysql/data/2hei.net.err –pid-file=/home/2hei/mysql/data/mysqld.pid –socket=/tmp/mysql.sock –port=13306

#!/usr/bin/env python
# -*-coding:UTF-8-*
import MySQLdb
conn = MySQLdb.Connection(host=’localhost’,port=13306,user=’2hei’,passwd=’123456′,db=’pytest’)
cur = conn.cursor()
cur.execute(‘select * from test’)
row=cur.fetchall()
print row

一直提示我mysql.scok出错。看了MySQLdb的源码后发现可以设定的参数还有很多
————————————
class Connection(_mysql.connection):

    “””MySQL Database Connection Object”””

    default_cursor = cursors.Cursor
   
    def __init__(self, *args, **kwargs):
        “””

        Create a connection to the database. It is strongly recommended
        that you only use keyword parameters. Consult the MySQL C API
        documentation for more information.

        host
          string, host to connect
         
        user
          string, user to connect as

        passwd
          string, password to use

        db
          string, database to use

        port
          integer, TCP/IP port to connect to

        unix_socket
          string, location of unix_socket to use

        conv
          conversion dictionary, see MySQLdb.converters

        connect_timeout
          number of seconds to wait before the connection attempt
          fails.

        compress
          if set, compression is enabled

        named_pipe
          if set, a named pipe is used to connect (Windows only)

        init_command
          command which is run once the connection is created

        read_default_file
          file from which default client values are read

        read_default_group
          configuration group to use from the default file

        cursorclass
          class object, used to create cursors (keyword only)

        use_unicode
          If True, text-like columns are returned as unicode objects
          using the connection’s character set.  Otherwise, text-like
          columns are returned as strings.  columns are returned as
          normal strings. Unicode objects will always be encoded to
          the connection’s character set regardless of this setting.

        charset
          If supplied, the connection character set will be changed
          to this character set (MySQL-4.1 and newer). This implies
          use_unicode=True.

        sql_mode
          If supplied, the session SQL mode will be changed to this
          setting (MySQL-4.1 and newer). For more details and legal
          values, see the MySQL documentation.
         
        client_flag
          integer, flags to use or 0
          (see MySQL docs or constants/CLIENTS.py)

        ssl
          dictionary or mapping, contains SSL connection parameters;
          see the MySQL documentation for more details
          (mysql_ssl_set()).  If this is set, and the client does not
          support SSL, NotSupportedError will be raised.

        local_infile
          integer, non-zero enables LOAD LOCAL INFILE; zero disables
    ————————–

于是修改源码为:
conn = MySQLdb.Connection(host=’localhost’,port=13306,user=’2hei’,passwd=’123456′,db=’pytest’,unix_socket=’/tmp/mysql.sock’)
这样执行后可以得到正确的结果:
$python link_mysql.py
((1L, ‘ronaldo’, 23L), (2L, ‘figo’, 30L))

问题得以解决!

分类: python 标签:

use mod_gnutls replace mod_ssl in apache VirtualHost

2009年3月11日 评论已被关闭

use mod_gnutls replace mod_ssl in apache
mod_gnutls一个替换apache mod_ssl的模块。

mod_gnutls uses the GnuTLS library to provide SSL 3.0, TLS 1.0 and TLS 1.1 encryption for Apache HTTPD.
It is similar to mod_ssl in purpose, but does not use OpenSSL.
作者很有意思,写了为啥自己要写mod_gnutls模块,据说是厌倦了mod_ssl的bug,汗!
—————————————-
This module started back in September of 2004 because I was tired of trying to
fix bugs in mod_ssl.  mod_ssl is a giant beast of a module — no offense to it’s
authors is intended — but I believe it has fallen prey to massive feature bloat.

When I started hacking on httpd, mod_ssl remained a great mystery to me, and
when I actually looked at it, I ran away.  The shear ammount code is huge, and it
does not conform to the style guidelines.  It was painful to read, and even harder
to debug.  I wanted to understand how it worked, and I had recently heard about
GnuTLS, so long story short, I decided to implement a mod_gnutls.
—————————————-

mod_gnutls代码很简洁,代码行数与mod_ssl相比如下:
Lines of code in mod_gnutls: 3,593
Lines of code in mod_ssl: 15,324
系统安装需求
GnuTLS    >=    2.4.0
Apache HTTPd    >=    2.0.42
APR Memcache Client >=    0.7.0 (Optional)

如下是安装过程,因为需要libgcrypt、gnutls等的支持,所以需要先安装其他关联模块:
ftp://ftp.gnupg.org/gcrypt/libgcrypt/
wget ftp://ftp.gnupg.org/gcrypt/libgcrypt/libgcrypt-1.4.4.tar.gz
./configure && make && make install

http://www.gnu.org/software/gnutls/download.html
wget http://ftp.gnu.org/pub/gnu/gnutls/gnutls-2.2.4.tar.bz2
./configure && make && make install

./configure –with-apxs=/home/webadm/bin/apxs –with-libgnutls=/usr/local/gnutils LDFLAGS=’-L/usr/lib64′
make && make install

64位的configure是需要加上LDFLAGS=’-L/usr/lib64′
文档:
http://www.outoforder.cc/projects/apache/mod_gnutls/docs/

基本配置,使用同一ip、同一端口配置多个虚拟主机:
# Load the module into Apache.
LoadModule gnutls_module modules/mod_gnutls.so

Listen 10.10.10.10:443
NameVirtualHost 10.10.10.10:443

<VirtualHost 10.10.10.10:443>
    GnuTLSEnable on
    GnuTLSPriorities NORMAL:!DHE-RSA:!DHE-DSS:!AES-256-CBC:%COMPAT
    DocumentRoot /home/apache2/htdocs/myhost
    ServerName myhost.com:443
    GnuTLSCertificateFile /home/apache2/conf/ssl/myhost.com.cer
    GnuTLSKeyFile /home/apache2/conf/ssl/myhost.com.key
</VirtualHost>

<VirtualHost 10.10.10.10:443>
    GnuTLSEnable on
    GnuTLSDHFile /etc/apache2/dh.params
    GnuTLSPriorities NORMAL:!AES-256-CBC:%COMPAT
    DocumentRoot /home/apache2/htdocs/2hei
    ServerName 2hei.net:443
    GnuTLSCertificateFile /home/apache2/conf/ssl/2hei.net.cer
    GnuTLSKeyFile /home/apache2/conf/ssl/2hei.net.key
</VirtualHost>

在具体使用中,我用mod_gnutls替换掉mod_ssl后,启动apache
遇到了这样的一个问题:
GnuTLS: Failed to Import Certificate (-34) Base64 decoding error.
说我的证书编码有问题,关于这个问题好像网上关联的比较少,还没人做出明确的回答,给作者发了邮件,希望能给我回复,呵呵。

分类: OpenSource 标签:

linux kernel: oom-killer

2009年3月8日 评论已被关闭

apache 终于停止服务了,系统为2.6内核、64位操作系统、2G内存。
/var/log/messages 有这样的错误提示
Mar  8 20:18:24 2hei-net kernel: oom-killer: gfp_mask=0x1d2
Mar  8 20:18:24 2hei-net kernel: Mem-info:
Mar  8 20:18:24 2hei-net kernel: Node 0 DMA per-cpu:
Mar  8 20:18:24 2hei-net kernel: cpu 0 hot: low 2, high 6, batch 1
Mar  8 20:18:24 2hei-net kernel: cpu 0 cold: low 0, high 2, batch 1
Mar  8 20:18:24 2hei-net kernel: cpu 1 hot: low 2, high 6, batch 1
Mar  8 20:18:24 2hei-net kernel: cpu 1 cold: low 0, high 2, batch 1
Mar  8 20:18:24 2hei-net kernel: cpu 2 hot: low 2, high 6, batch 1
Mar  8 20:18:24 2hei-net kernel: cpu 2 cold: low 0, high 2, batch 1
Mar  8 20:18:24 2hei-net kernel: cpu 3 hot: low 2, high 6, batch 1
Mar  8 20:18:24 2hei-net kernel: cpu 3 cold: low 0, high 2, batch 1
Mar  8 20:18:24 2hei-net kernel: Node 0 Normal per-cpu:
Mar  8 20:18:26 2hei-net kernel: cpu 0 hot: low 32, high 96, batch 16
Mar  8 20:18:26 2hei-net kernel: cpu 0 cold: low 0, high 32, batch 16
Mar  8 20:18:26 2hei-net kernel: cpu 1 hot: low 32, high 96, batch 16
Mar  8 20:18:26 2hei-net kernel: cpu 1 cold: low 0, high 32, batch 16
Mar  8 20:18:26 2hei-net kernel: cpu 2 hot: low 32, high 96, batch 16
Mar  8 20:18:26 2hei-net kernel: cpu 2 cold: low 0, high 32, batch 16
Mar  8 20:18:26 2hei-net kernel: cpu 3 hot: low 32, high 96, batch 16
Mar  8 20:18:26 2hei-net kernel: cpu 3 cold: low 0, high 32, batch 16
Mar  8 20:18:26 2hei-net kernel: Node 0 HighMem per-cpu: empty
Mar  8 20:18:26 2hei-net kernel:
Mar  8 20:18:26 2hei-net kernel: Free pages:       17536kB (0kB HighMem)
Mar  8 20:18:26 2hei-net kernel: Active:257583 inactive:239023 dirty:0 writeback:0 unstable:0 free:4384 slab:3787 mapped:497010 pagetables:2846
Mar  8 20:18:26 2hei-net kernel: Node 0 DMA free:11832kB min:44kB low:88kB high:132kB active:0kB inactive:0kB present:16384kB pages_scanned:0 all_unreclaimable? yes
Mar  8 20:18:26 2hei-net kernel: protections[]: 0 0 0
Mar  8 20:18:26 2hei-net kernel: Node 0 Normal free:5704kB min:5720kB low:11440kB high:17160kB active:1029692kB inactive:956732kB present:2080416kB pages_scanned:3188856 all_unreclaimable? yes
Mar  8 20:18:26 2hei-net kernel: protections[]: 0 0 0
Mar  8 20:18:26 2hei-net kernel: Node 0 HighMem free:0kB min:128kB low:256kB high:384kB active:0kB inactive:0kB present:0kB pages_scanned:0 all_unreclaimable? no
Mar  8 20:18:26 2hei-net kernel: protections[]: 0 0 0
Mar  8 20:18:26 2hei-net kernel: Node 0 DMA: 6*4kB 4*8kB 2*16kB 3*32kB 2*64kB 2*128kB 2*256kB 1*512kB 0*1024kB 1*2048kB 2*4096kB = 11832kB
Mar  8 20:18:26 2hei-net kernel: Node 0 Normal: 0*4kB 1*8kB 4*16kB 0*32kB 0*64kB 0*128kB 0*256kB 1*512kB 1*1024kB 0*2048kB 1*4096kB = 5704kB
Mar  8 20:18:26 2hei-net kernel: Node 0 HighMem: empty
Mar  8 20:18:26 2hei-net kernel: Swap cache: add 1070476, delete 1070476, find 119872/179715, race 0+20
Mar  8 20:18:26 2hei-net kernel: Free swap:            0kB
Mar  8 20:18:26 2hei-net kernel: 524200 pages of RAM
Mar  8 20:18:27 2hei-net kernel: 10214 reserved pages
Mar  8 20:18:27 2hei-net kernel: 67015 pages shared
Mar  8 20:18:27 2hei-net kernel: 0 pages swap cached
Mar  8 20:18:27 2hei-net kernel: Out of Memory: Killed process 26079 (httpd).

终于发现了linux的OOM Killer(Out of Memory: Killed process)这个功能。当linux发现有进程占用内存过多时会触发OOM Killer功能,将占用内存最多的pid给杀掉,通过网上的一些惨痛的教训可以看到有mysql、oracle、apache给OOM kill掉的。

因为我的机器上只跑了apache服务,所以又看了一下我的apache配置
<IfModule worker.c>
StartServers         2
MaxClients         2048
MinSpareThreads     25
MaxSpareThreads     75
ThreadsPerChild     128
MaxRequests
本以为想让apache支持的连接数多一点,没想到MaxClients参数的设置影响了系统的稳定。

我看到以这种配置启动时VIRT  RES的值就已经显示为1702,RES从11m开始逐步升高。
 PID USER      PR  NI %CPU    TIME+  %MEM  VIRT  RES  SHR S COMMAND                                                                
 7009 apache    16   0    1   0:06.95  0.8  1702m  11m 2172 S /home/local/apache/bin/httpd                            
跑了几天以后VIRT已经到了2G多,RES也接近1G,终于今天挂掉了,httpd进程被linux给kill掉了。

于是尝试修改apache的httd.conf配置,发现MaxClients为1536时,启动VIRT可以达到1024m,如果设定为1024时 VIRT为776m,所以对于2G内存的机器不要超过1536为好。
参考文档:
http://lwn.net/Articles/104179/

分类: OpenSource 标签: