Blog

  • python操作mysql

    yum安装

    yum install MySQL-python

    使用此模块

    import MySQLdb #注意大小写!!

    #建立和数据库系统的连接

     conn = MySQLdb.connect(host='localhost', user='root',passwd='root')

    #获取操作游标

     cursor = conn.cursor()

    #选择数据库

     conn.select_db('python')

    #执行SQL,创建一个数据库.

     cursor.execute("""select * from test""")

    #获取一条记录,每条记录做为一个元组返回

     result = cursor.fetchone();
     print result
     #print 'ID: %s info: %s' % (result[0],result[1])
     print 'ID: %s info: %s' % result

    #获取5条记录,注意由于之前执行有了fetchone(),所以游标已经指到第二条记录了,也就是从第二条开始的所有记录

     print "只获取5条记录:"
     results = cursor.fetchmany(5)
     for r in results:
     print r

    print “获取所有结果:”
    #重置游标位置,0,为偏移量,mode=absolute | relative,默认为relative,

     cursor.scroll(0,mode='absolute')
     #获取所有结果
     results = cursor.fetchall()
     for r in results:
     print r
     conn.close()

    #关闭连接,释放资源

     cursor.close();

    http://www.iteye.com/topic/573092

  • mysql连接数

    默认的MySQL数据库,其最大连接数max_connections= 100

    查看当前系统连接数设置
    MariaDB [(none)]> show variables like ‘max_connections’;

    max_connections代表的意义是

    查看当前连接详细

    show processlist;
    show full processlist;

    查看当前的连接数

    show status like “Threads%”;
    +—————————+——-+
    | Variable_name | Value |
    +—————————+——-+
    | Threads_cached | 4 |
    | Threads_connected | 3 |
    | Threads_connected_reserve | 1 |
    | Threads_created | 7 |
    | Threads_running | 3 |
    +—————————+——-+
    5 rows in set (0.00 sec)

    status也可以查看当前连接的id号,Threads也是连接数

    MariaDB [(none)]> status;
    ————–
    mysql Ver 15.1 Distrib 5.5.33a-MariaDB, for Linux (x86_64) using readline 5.1

    Connection id: 6213994
    Current database:
    Current user: root@localhost
    SSL: Not in use
    Current pager: stdout
    Using outfile: ”
    Using delimiter: ;
    Server: MariaDB
    Server version: 5.5.33a-MariaDB-log MariaDB Server, wsrep_23.7.6.rXXXX
    Protocol version: 10
    Connection: Localhost via UNIX socket
    Server characterset: latin1
    Db characterset: latin1
    Client characterset: utf8
    Conn. characterset: utf8
    UNIX socket: /tmp/mysql.sock
    Uptime: 236 days 16 hours 13 min 48 sec

    Threads: 95 Questions: 160672130 Slow queries: 2410 Opens: 102274 Flush tables: 2 Open tables: 400 Queries per second avg: 7.857
    ————–

  • time命令

    time命令用于计算一个命令执行的时间

    例如sleep 2运行时间是2秒钟

    /usr/bin/time sleep 2
    0.00user 0.00system 0:02.00elapsed 0%CPU (0avgtext+0avgdata 2656maxresident)k
    0inputs+0outputs (0major+211minor)pagefaults 0swaps

    time后面可以加很多参数,如-o output.txt将结果输出到文件

    /usr/bin/time -o output.txt sleep 2

    -f “%P” command 可以按需求输出cpu使用率

    /usr/bin/time -f "%P cpu percentage" find ./ -name abc

     -f “%M” command 可以按需求输出内存使用率,

    /usr/bin/time -f "%M Max Resident Set Size (Kb)" find ./ -name abc

    -f “%e” command 按秒输出命令执行的时间

    /usr/bin/time -f "%e running time (sec)" find ./ -name abc

    参考

    12 UNIX / Linux Time Command Output Format Option Examples

  • python安装simplejson

    没有安装simplejson时报错

    >>> import simplejson

    Traceback (most recent call last):

      File “<stdin>”, line 1, in <module>

    ImportError: No module named simplejson

    simplejson是ansible一个很重要的依赖,经测试在python 2.4.3及以上版本都可以用python setup.py install 安装成功。

    方法一:

    yum install python-simplejson -y

    方法二:

    wget https://pypi.python.org/packages/source/s/simplejson/simplejson-3.5.2.tar.gz#md5=10ff73aa857b01472a51acb4848fcf8b --no-check-certificate
    tar vxzf simplejson-3.5.2.tar.gz
    cd simplejson-3.5.2
    python setup.py install

    方法三:

    pip install simplejson

    方法四:

    easy_install simplejson
  • 番茄工作法笔记

    如果一项任务的估测值大于 5 到 7 个番茄,那么就打散它。
    如果一项任务的估测值小于一个番茄,就把几个小任务组合成一个大任务。

  • python查看已经安装的模块

    方法一:

    
    >>> help('modules')
    
    Please wait a moment while I gather a list of all available modules...
    
    ....
    
    Enter any module name to get more help.  Or, type "modules spam" to search
    
    for modules whose descriptions contain the word "spam".
    

    方法二:

    >>> import sys
    
    >>> sys.modules