一、python远程操作 redis
1.1 前提准备
1、安装redis第三方库
pip install redis
2、修改配置文件
vim redis.conf
注释掉/bind中:bind 127.0.0.1 (ip)
修改/protected:protected-mode no (关闭安全模式)
1、创建一个产品表 mysql -h localhost -u root -p use mydb; create table products( id int primary key auto_increment, name varchar(20) not null, price double not null ); #插入记录 insert into products(name,price)values('洗衣粉',12.5),('旺旺雪饼',15),('辣条',5); #查看表 select * from products; +----+--------------+-------+ | id | name | price | +----+--------------+-------+ | 1 | 洗衣粉 | 12.5 | | 2 | 旺旺雪饼 | 15 | | 3 | 辣条 | 5 | +----+--------------+-------+ 2、启动redis ywh@ywh:~$ redis-server redis.conf ywh@ywh:~$ redis-cli 127.0.0.1:6379>
1.2 python代码演示
# cache_tool.py import pymysql,redis def selectFromMySQL(_id): # 根据产品id从MySQL数据库查询产品 conn = pymysql.Connect( host='192.168.0.111:22',#虚拟机地址 port=3306, user='root', password='rock1204', database='mydb', charset='utf8', ) cursor = conn.cursor() sql = "select id,name,price from products where id=%d" try: cursor.execute(sql%_id) product = cursor.fetchone() except Exception as e: print("操作MySQL查询数据发生异常了:",e) finally: cursor.close() conn.close() return product def saveToRedis(product): # 将MySQL中查询的产品保存到Redis中 r = redis.Redis("192.168.0.111:22",6379) # 创建操作Redis的实例 r.hmset("product:"+str(product[0]),{'id':product[0],'name':product[1],'price':product[2]}) def selectFromRedis(_id): # 从Redis中查询数据,并返回 r = redis.Redis("192.168.0.111:22", 6379) # 创建操作Redis的实例 product_dict = r.hgetall("product:"+str(_id)) # 以字典形式获取,字节序列 show_result = {} for k,v in product_dict.items(): show_result[k.decode()] = v.decode() # 解码字节序列 return show_result
#client.py from cache_tool import * _id = 1 print("先从Redis中查询") data = selectFromRedis(_id) # 从Redis中查 print("从Redis中查询的数据是:",data) if data: print("恭喜,Redis命中啦~~~") print("从Redis中查询的数据是:",data) else: print("Redis中没有,只能从MySQL中查询了。。。。。。") product = selectFromMySQL(_id) if product: saveToRedis(product) print('已经将产品信息保存到Redis了') print("从MySQL中查询的信息是:",product) else: print("Sorry,系统暂无此信息!!!")
二、mangoDB
查看镜像来源
vim /etc/apt/sources.list
vim /etc/apt/sources.list # deb cdrom:[Ubuntu 16.04.2 LTS _Xenial Xerus_ - Release amd64 (20170215.2)]/ xenial main restricted # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to # newer versions of the distribution. deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted ## Major bug fix updates produced after the final release of the ## distribution. deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu ## team. Also, please note that software in universe WILL NOT receive any ## review or updates from the Ubuntu security team. deb http://mirrors.aliyun.com/ubuntu/ xenial universe # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu ## team, and may not be under a free licence. Please satisfy yourself as to ## your rights to use the software. Also, please note that software in ## multiverse WILL NOT receive any review or updates from the Ubuntu ## security team. deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial multiverse "/etc/apt/sources.list" [只读] 51L, 2885C
2.1 MongoDB简介
MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库管理系统
安装方式:
sudo apt-get install mongodb
进入mongodb环境:mongo
数据库(db)------>集合(collections)---->文档
2.2 MongoDB的操作
查看所有数据库 : show dbs
查看当前数据库: db
进入指定数据库: use 数据库名
显示当前库中所有的集合: show collections
创建一个集合: db.createCollection("集合名称”)
向集合中插入文档:
db.集合称.insert({name:'tom',age:20,score:65})
db.集合名称.save(数据) # 若存在,则修改
查询指定集合的所有数据
db.集合名称.find()
db.集合名称.find().pretty()
删除集合
db.集合名.drop()
删除当前数据库
db.dropDatabase()
例一:查询students集合中成绩大于80分的文档
db.students.find({score:{$gt:80}})
例二:查询students集合中成绩大于80分且小于90分的文档
db.students.find({score:{lt:90}})
例三:从students集合中删除名字为‘alice’的文 档
db.students.remove({name:'alice'})
2.3 代码演示
ywh@ywh:~$ mongo #进入mongoDB
MongoDB shell version: 2.6.10
connecting to: test
> db #查看当前数据库
test
> show dbs #查看所有数据库
admin (empty)
local 0.078GB
> db #查看当前数据库
test
> use mydb #进入指定数据库
switched to db mydb
> show collections #查看所有集合
> db #查看当前数据库
mydb
> show dbs #查看所有数据库
admin (empty)
local 0.078GB
mydb (empty)
> db.createCollection('students') #创建students集合
{ "ok" : 1 }
> show collections #查看所有集合
students
system.indexes
> db.students.insert({name:'tom',age:20,score:83.5}) #向students集合中插入文档
WriteResult({ "nInserted" : 1 })
> db.students.save({name:'jerry',age:28,score:63.5,sex:'man'}) #向students集合中插入文档,save:如果存在该文档则表示修改
WriteResult({ "nInserted" : 1 })
> db.students.find() #查看数据库所有文档
{ "_id" : ObjectId("5bb329c0a9f09b8ba5efd5f1"), "name" : "tom", "age" : 20, "score" : 83.5 }
{ "_id" : ObjectId("5bb32a86a9f09b8ba5efd5f2"), "name" : "jerry", "age" : 28, "score" : 63.5, "sex" : "man" }
> db.students.find().pretty() #查看数据库所有文档并格式化
{
"_id" : ObjectId("5bb329c0a9f09b8ba5efd5f1"),
"name" : "tom",
"age" : 20,
"score" : 83.5
}
{
"_id" : ObjectId("5bb32a86a9f09b8ba5efd5f2"),
"name" : "jerry",
"age" : 28,
"score" : 63.5,
"sex" : "man"
}
> db.students.find({score:{$gt:80}}) #查询students集合中成绩大于80分的文档
{ "_id" : ObjectId("5bb329c0a9f09b8ba5efd5f1"), "name" : "tom", "age" : 20, "score" : 83.5 }
> db.students.find({score:{$gt:60,$lt:80}}) 查询students集合中成绩大于60分且小于80分的文档
{ "_id" : ObjectId("5bb32a86a9f09b8ba5efd5f2"), "name" : "jerry", "age" : 28, "score" : 63.5, "sex" : "man" }
> db.students.remove({name:'jerry'}) #删除文档
WriteResult({ "nRemoved" : 1 })
> db.students.save({name:'jerry',age:28,score:63.5,sex:'man'})
WriteResult({ "nInserted" : 1 })
> db.students.find().sort({score:1}) #升序排序
{ "_id" : ObjectId("5bb32e60a9f09b8ba5efd5f3"), "name" : "jerry", "age" : 28, "score" : 63.5, "sex" : "man" }
{ "_id" : ObjectId("5bb329c0a9f09b8ba5efd5f1"), "name" : "tom", "age" : 20, "score" : 83.5 }
> db.students.find().sort({score:-1}) #降序排序
{ "_id" : ObjectId("5bb329c0a9f09b8ba5efd5f1"), "name" : "tom", "age" : 20, "score" : 83.5 }
{ "_id" : ObjectId("5bb32e60a9f09b8ba5efd5f3"), "name" : "jerry", "age" : 28, "score" : 63.5, "sex" : "man" }
> db.students.drop() #删除集合
true
> show dbs
admin (empty)
local 0.078GB
mydb 0.078GB
> db.dropDatabase() #删除数据库
{ "dropped" : "mydb", "ok" : 1 }
> show dbs
admin (empty)
local 0.078GB