Organize the Python connection Mysql/mongodb/redis/memcache database tutorial

Source: Internet
Author: User
Tags memcached mongodb redis

How to consolidate Redis mongodb MySQL

1. Relationship of business systems with MySQL organization
2. Cache data with memcached, Redis access
3. Large text data into MongoDB access
4. Data statistics count add threshold function

combines memcached, Redis, and MongoDB with MySQL,

to see what is appropriate for memcached, Redis cache access based on the actual business needs, Which is suitable for mongodb access. The

Core architecture:

1, the application framework schema
2, and the database system Business Architecture

are key factors that affect the success of a project.



One, Python operations MySQL: See details:

"Apt-get install python-mysqldb" #!/bin/env python # -*- encoding: utf-8 -*-# -------------------------------------------------------------------------------# purpose:      example for python_to_mysql # author:       Zhoujy # created:     2013-06-14 # update:       2013-06-14 #-------------------------------------------------------------------------------import  MySQLdb import os #建立和数据库系统的连接, Format #conn    = mysqldb.connect (host= ' localhost ', user= ' root ') , passwd= ' 123456 ', db= ' test ', port=3306,charset= ' UTF8 ') #指定配置文件, determine the directory, or write the absolute path Cwd = os.path.realpath ( Os.path.dirname (__file__)) Db_conf = os.path.join (cwd,  ' db.conf ') conn   =  MySQLdb.connect (read_default_file=db_conf,host= ' localhost ', db= ' test ', port=3306,charset= ' UTF8 ') #要执行的sql语句 query   =&nbSP; ' Select id  from t1 ' #获取操作游标 cursor = conn.cursor () #执行SQL cursor.execute (query) #
Gets a record, each record as a tuple returns, returns 3, and the cursor refers to the 2nd record. Result1 = cursor.fetchone () FOR I IN RESULT1:     print i #
Returns the number of rows affected     print cursor.rowcount #获取指定数量记录, each record as a tuple returns, returns 1, 2, the cursor begins with the 2nd record, and the cursor refers to the 4th record. Result2 = cursor.fetchmany (2) FOR I IN RESULT2:     for ii  in i:         print ii #获取所有记录, each record as a tuple returns, returns 3,4,7,6,
The cursor starts at the 4th record to the last. Result3 = cursor.fetchall () FOR I IN RESULT3:     for ii  In i:         print ii #获取所有记录, each record as a tuple returns, returns 3,4,7,6, The cursor starts at the 1th record #重置游标位置, 0 is offset, mode=absolute | relative, default is relative Cursor.scroll (0,mode= ' absolute ') RESULT3  = cursor.fetchall () FOR I IN RESULT3:     for ii in i:         print ii # Here are 2 ways to insert data into a database: # (one) for i in range  (10,20):     query2 =  ' Insert into t1 values ('%d ', now ()) '  %i     cursor.execute (query2)       #提交     conn.rollback () # (two) rows = [] for i in  range  (10,20):     rows.append (i) query2 =  ' insert into t1 
VALUES ("%s", now ()) ' #executemany  2 a parameter, and the 2nd argument is a variable. Cursor.executemany (query2,rows) #提交 conn.commit () #选择数据库 query3 =  ' Select id from dba_ Hospital ' #重新选择数据库 conn.select_db (' Chushihua ') cursor.execute (query3) Result4 = cursor.fetchall () for  I IN RESULT4:     for ii in i:          print ii #不定义query, direct execution: Cursor.exEcute ("set session binlog_format= ' Mixed '") #关闭游标, releasing Resources cursor.close () "" 


+------+---------------------+
| ID | Modifyt |
+------+---------------------+
| 3 | 2010-01-01 00:00:00 |
| 1 | 2010-01-01 00:00:00 |
| 2 | 2010-01-01 00:00:00 |
| 3 | 2010-01-01 00:00:00 |
| 4 | 2013-06-04 17:04:54 |
| 7 | 2013-06-04 17:05:36 |
| 6 | 2013-06-04 17:05:17 |
+------+---------------------+

'''

Note: In the script, the password is easily exposed in the script, so you can use a configuration file to save the password, such as db.conf:



[Client]
User=root
password=123456


Second, python operation MongoDB:



#!/bin/env python # -*- encoding: utf-8 -*-#------------------------------------------ -------------------------------------# purpose:     example for python _to_mongodb # author:      zhoujy # created:      2013-06-14 # update:      2013-06-14 #--------------------------------- ----------------------------------------------Import pymongo Import os #建立和数据库系统的连接, when you create a connection, Specifies the host and port parameter Conn   = pymongo. Connection (host= ' 127.0.0.1 ', port=27017) #admin   Database has account number, connection-authentication-switch Library db_auth = conn.admin db_
Auth.authenticate (' sa ', ' sa ') #连接数据库 db = conn.abc #连接表 collection = db.stu #查看全部表名称 Db.collection_names () #print  db.collection_names () #访问表的数据, specify column item = collection.find ({},{"sname" : 1, "course": 1, "_id": 0}) For rows in item:    &NBSp;print rows.values () #访问表的一行数据 print collection.find_one () #得到所有的列 for rows in  Collection.find_one ():     print rows #插入 Collection.insert ({"Sno": "Sname": "JL", ""
Course ": {" D ": u = dict," S ":}}) #或 (sno=102,sname= ' zjjj ', course={" D ":," S ":}) Collection.insert (U) #得到行数 Print collection.find (). Count () Print collection.find ({"sno":) #排序, according to the value of a column. Pymongo. Descending: Reverse; Pymongo. Ascending: Ascending. Follow Sno Reverse Item = collection.find (). Sort (' Sno ', Pymongo. Descending) For rows in item:     print rows.values () #多列排序 item =  collection.find (). Sort ([' Sno ', Pymongo. Descending), (' A ', Pymongo. Ascending)] #更新, the first parameter is the condition, the second parameter is the update operation, $set,%inc, $push, $ne, $addToSet, $rename   etc collection.update ({"Sno": 100},
{"$set": {"sno": 101}})
#更新多行和多列 collection.update ({"Sno": 102},{"$set": {"sno":, "sname": "SSSS"}},multi=true) #删除, the first argument is a condition, and the second parameter is a delete operation. Collection.remove ({"Sno": "}") ' Sno: School number; sName: Course: Subject Db.stu.insert ({"Sno": 1, "sname": "John", "course": {"A": "," B ":", "C": "," "D": "," E ": 100}}) Db.stu.insert ({"Sno": 2, "sname": "Dick", "course": {"A": "The", "B": "," X ":", "Y": "," Z ":", "({" Sno ": 3," Sname ":" Zhao Five "," course ": {" A ":", "B": "," F ":", "G": "," H ":" {"}}" Db.stu.insert ({"Sno": 4, "sname": "Zhoujy", "course": {" A ":", "B": "C": "," T ":" ("Y") Db.stu.insert ({"Sno": 5, "sname": "abc", "Course": {"A": "," "B": "," Z ":", "G": 54 , "H": [}}] Db.stu.insert ({"Sno": 6, "sname": "Yang Six", "course": {"A": "", "U": "," C ":", "" R ":", "N": "" "({)}) Db.stu.insert ({" Sno ": 7," sname ":" Chen II "," course ": {" A ":", "M": "," N ":", "S": "," "K": "," "Db.stu.insert" ({"Sno": 8, "sname": "Zhoujj", " Course ": {" P ":", "B":, "J": "," K ":", "L": "Db.stu.insert" ({"Sno": 9, "sname": "CCC", "course": {"Q": "," B ":", "C" : "V": "85}}", "U":



Calculates the number of collections in a MongoDB document:



Import pymongo Conn   = pymongo. Connection (host= ' 127.0.0.1 ', port=27017) db = conn.abc     #abc文档 for tb_name  in db.collection_names ():      #循环出各集合名     count =
 db[tb_name].count ()              #计算各集合的数量     if Count > 2:                                    #过滤条件         print tb_name  +  ': '  + str (Count) ' Conn   = pymongo. Connection (host= ' 127.0.0.1 ', port=27017) db = conn.abc For tb_name in db.collection_ Names ():     print tb_name +  ': '     exec (' print  '  +  ' db. ' +tb_name+ '. Count () ')        #变量当集合的处理方式 OR Conn   = pymongo. Connection (host= ' 127.0.0.1 ', port=27017) db = conn.abc For tb_name in db.collection_ Names ():     mon_dic=db.command ("Collstats",  tb_name)         #以字典形式返回     print mon_dic.get (' ns '), Mon_dic.get (' count ') '


Three, Python operations Redis:


#!/bin/env python # -*- encoding: utf-8 -*-#------------------------------------------ -------------------------------------# purpose:     example for python _to_mongodb # author:      zhoujy # created:      2013-06-14 # update:      2013-06-14 #---------------------------------
----------------------------------------------Import redis f = open (' Aa.txt ') While true:     line = f.readline (). Strip (). Split ('  #  ')     if  line == [']:         break     
Username,pwd,email = line #    print name.strip (), Pwd.strip (), Email.strip ()     rc = redis. Strictredis (host= ' 127.0.0.1 ', port=6379,db=15)     rC.hset (' Name: '  + username, ' email ', email)     rc.hset (' Name: '  + username , ' Password ', PWD) f.close () Alluser = rc.keys (' * ') #print  alluser print  "====================== ============= read the stored data =================================== "For user in alluser:      print  '  #  '. Join ((User.split (': ') [1],rc.hget (User, ' Password '), rc.hget (user, ' Email '))



Four, python operation Memcache:

Import memcache Mc = memcache. Client ([' 127.0.0.1:11211 '],debug=1) #!/usr/bin/env python #coding =utf-8 import mysqldb import  Memcache import sys import time Def get_data (mysql_conn): #    nn =  Raw_input ("Press string name:")     mc = memcache. Client ([' 127.0.0.1:11211 '],debug=1)     t1 =time.time ()     value  = mc.get (' Zhoujinyia ')     if value == none:          t1 = time.time ()         print  t1         query =  "Select company,email,sex, address from uc_user_offline where realname =  ' Zhoujinyia ' "         cursor= mysql_conn.cursor ()       &Nbsp; cursor.execute (query)         item =  Cursor.fetchone ()         t2 = time.time ()          print t2         t = round (T2-T1)         print  "From mysql cost %s sec"  %t         print item          mc.set (' Zhoujinyia ', item,60)     else :      
   t2 = time.time ()         t=round (T2-T1)         print  "From memcache cost %s sec"
 %t         print value if __name__ == ' __main__ ':     mysql_conn = mysqldb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' 123456 ', db= ' member ', port=3306,charset= ') UTF8 ')     get_data (mysql_conn)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.