redis

环境如下:

[root@mcw01 ~/msRedis]$ ps -ef|grep -v grep|grep redis
root 46061 1 0 14:28 ? 00:00:45 redis-sentinel *:26379 [sentinel]
root 46066 1 0 14:28 ? 00:00:44 redis-sentinel *:26380 [sentinel]
root 46071 1 0 14:28 ? 00:00:44 redis-sentinel *:26381 [sentinel]
root 46293 1 0 15:17 ? 00:00:14 redis-server *:7000 [cluster]
root 46298 1 0 15:18 ? 00:00:14 redis-server *:7001 [cluster]
root 46303 1 0 15:18 ? 00:00:13 redis-server *:7002 [cluster]
root 46308 1 0 15:18 ? 00:00:13 redis-server *:7003 [cluster]
root 46313 1 0 15:18 ? 00:00:13 redis-server *:7004 [cluster]
root 46318 1 0 15:18 ? 00:00:14 redis-server *:7005 [cluster]
root 54613 1 0 18:57 ? 00:00:00 redis-server *:6380
root 54620 1 0 18:57 ? 00:00:00 redis-server *:6381
[root@mcw01 ~/msRedis]$
[root@mcw01 ~/msRedis]$ redis-cli -p 6381 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=50805,lag=1
master_replid:e795be2a39f50a18c95c77196bcb56780a062113
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:50933
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:50933
[root@mcw01 ~/msRedis]$
[root@mcw01 ~/msRedis]$
[root@mcw01 ~/msRedis]$ redis-cli -p 6381
127.0.0.1:6381> keys *
1) "name1"
127.0.0.1:6381>
[root@mcw01 ~/msRedis]$

安装了redis模块。

from redis import Redis
redis_cli= Redis(host="10.0.0.11",port=6381)
res=redis_cli.get("name1")
print(res)

连接报错:
redis.exceptions.ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

查看redis配置如下:
[root@mcw01 ~/msRedis]$ cat 6381.conf
port 6381
daemonize yes
pidfile "/data/6381/redis.pid"
loglevel notice
logfile "/data/6381/redis.log"
dbfilename "dump.rdb"
dir "/data/6381" [root@mcw01 ~/msRedis]$
添加bind ip
[root@mcw01 ~/msRedis]$ cat 6379.conf
bind 10.0.0.11
port 6379
daemonize yes
pidfile /data/6379/redis.pid
loglevel notice
logfile "/data/6379/redis.log"
dbfilename dump.rdb
dir /data/6379
重新启动后,就可以正常连接

查看redis服务

设置一个值:

查看,添加数据成功

from redis import Redis
import json
redis_cli= Redis(host="10.0.0.11",port=6379)
res1=redis_cli.hset('stu1','name','mcw')
res2=redis_cli.hset('stu2','mcw2',json.dumps({'name':'mcw','age':'18','gender':'man'}))
res3=redis_cli.hmset('stu3',{'name':'mcw','age':'18','gender':'man'})
student={
'xiaoma':json.dumps({
'name':'xiaoma',
'age':18,
'hobby:':['football','sing','jump'],
'jiaren':{
'爷爷':{
'name':'马有才',
'hobby':['xiangqi','paobu']
}
}
}),
'xiaohong':json.dumps({
'name':'xiaohong'
})
}
res4=redis_cli.hmset('student_info',student)
print(res1,res2,res3,res4)

刚刚插入的数据

如下图,用程序获取出来的话,需要不停的做些操作。

哨兵好像没有这些命令

mogodb

环境:

[mongod@mcw01 ~]$ mongo --port 30000
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:30000/test
[mongod@mcw01 ~]$ ps -ef|grep mongo
mongod 20138 1 0 Mar05 ? 00:11:59 mongod --dbpath /mongodb/m20 --logpath /mongodb/mlog/m20.log --logappend --fork --port 27020 --configsvr
mongod 20158 1 0 Mar05 ? 00:05:03 mongos --logpath /mongodb/mlog/m30.log --logappend --fork --port 30000 --configdb 10.0.0.11:27020
root 61265 56474 0 21:08 pts/0 00:00:00 su - mongod
mongod 61266 61265 0 21:08 pts/0 00:00:00 -bash
mongod 61338 61266 0 21:09 pts/0 00:00:00 ps -ef
mongod 61339 61266 0 21:09 pts/0 00:00:00 grep --color=auto mongo
[mongod@mcw01 ~]$ mongo --port 30000
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:30000/test
mongos> show dbs;
config 0.001GB
shop 0.000GB
mongos> use shop
switched to db shop
mongos> show tables;
user
mongos> db.user.find().limit(2);
{ "_id" : ObjectId("6223149d46d8d9b89fc9df41"), "userid" : 2000, "intro" : "i am mcw, from china" }
{ "_id" : ObjectId("6223149d46d8d9b89fc9df42"), "userid" : 2001, "intro" : "i am mcw, from china" }
mongos>
提前部署好两个复制集,部署脚本如上
现在部署情况如下
rs3:
10.0.0.12 27017
10.0.0.12 27018
10.0.0.12 27019 rs4:
10.0.0.13 27017
10.0.0.13 27018
10.0.0.13 27019 configsvr :
10.0.0.11 27020 mongos:
10.0.0.11 30000

下载pymongo。下面是连接程序

find_one:

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.user.find_one({"_id" : ObjectId("6223149d46d8d9b89fc9df41")}); print(res)

成功查出数据

find:

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.user.find(); #<pymongo.cursor.Cursor object at 0x02D0B550> 生成器
for row in res:
print(row)
print(res)

如下,4000条都收到了

查询前四条,括号里放的跟mongodb类似

插入一条数据insert_one:

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.zoo.insert_one({"name":"cat","age":1})
print(res.inserted_id,type(res.inserted_id))
print(res)
charuChaxun=MongoDB.zoo.find({"_id" : ObjectId(res.inserted_id)})
for row in charuChaxun:
print("查询刚刚插入的数据",row)

多条数据插入insert_many

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.zoo.insert_many([{"name":"dog","age":1},{"name":"pig","age":1}])
print(res.inserted_ids,type(res.inserted_ids))
print(res)
for i in res.inserted_ids:
charuChaxun = MongoDB.zoo.find({"_id":i})
for row in charuChaxun:
print("查询刚刚插入的数据",row)

只修改一条数据update_one:

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.zoo.insert_many([{"name":"cat","age":1},{"name":"dog","age":1},{"name":"pig","age":1}])
for i in res.inserted_ids:
charuChaxun = MongoDB.zoo.find({"_id":i})
for row in charuChaxun:
print("查询刚刚插入的数据",row)
MongoDB.zoo.update_one({},{'$inc':{'age':2}})
# MongoDB.zoo.update_many({},{'$inc':{'age':2}})
res2=MongoDB.zoo.find(); #<pymongo.cursor.Cursor object at 0x02D0B550> 生成器
for row in res2:
print('更新后数据查询:',row)

修改多条数据updata_many:

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.zoo.insert_many([{"name":"cat","age":1},{"name":"dog","age":1},{"name":"pig","age":1}])
for i in res.inserted_ids:
charuChaxun = MongoDB.zoo.find({"_id":i})
for row in charuChaxun:
print("查询刚刚插入的数据",row)
MongoDB.zoo.update_one({},{'$inc':{'age':2}}) #修改一条
MongoDB.zoo.update_many({"age":1},{'$inc':{'age':10}}) #修改多条
res2=MongoDB.zoo.find(); #<pymongo.cursor.Cursor object at 0x02D0B550> 生成器
for row in res2:
print('更新后数据查询:',row)

只删除一个匹配的delete_one

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.zoo.insert_many([{"name":"cat","age":1},{"name":"dog","age":1},{"name":"pig","age":1}])
for i in res.inserted_ids:
charuChaxun = MongoDB.zoo.find({"_id":i})
for row in charuChaxun:
print("查询刚刚插入的数据",row)
MongoDB.zoo.update_one({},{'$inc':{'age':2}})
MongoDB.zoo.update_many({"age":1},{'$inc':{'age':10}})
res2=MongoDB.zoo.find(); #<pymongo.cursor.Cursor object at 0x02D0B550> 生成器
for row in res2:
print('更新后数据查询:',row)
MongoDB.zoo.delete_one({"age":11}) #删除一个匹配的
res3=MongoDB.zoo.find(); #<pymongo.cursor.Cursor object at 0x02D0B550> 生成器
for row in res3:
print('删除后数据查询:',row)

删除所有匹配的delete_many

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.zoo.insert_many([{"name":"cat","age":1},{"name":"dog","age":1},{"name":"pig","age":1}])
for i in res.inserted_ids:
charuChaxun = MongoDB.zoo.find({"_id":i})
for row in charuChaxun:
print("查询刚刚插入的数据",row)
MongoDB.zoo.update_one({},{'$inc':{'age':2}})
MongoDB.zoo.update_many({"age":1},{'$inc':{'age':10}})
res2=MongoDB.zoo.find(); #<pymongo.cursor.Cursor object at 0x02D0B550> 生成器
for row in res2:
print('更新后数据查询:',row)
MongoDB.zoo.delete_many({"age":11}) #删除所有匹配的
res3=MongoDB.zoo.find(); #<pymongo.cursor.Cursor object at 0x02D0B550> 生成器
for row in res3:
print('删除后数据查询:',row)

skip和limit使用:

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.zoo.insert_many([{"name":"cat","age":1},{"name":"dog","age":2},{"name":"pig","age":5},{"name":"monkey","age":3}])
res2=MongoDB.zoo.find().skip(1).limit(2); #<pymongo.cursor.Cursor object at 0x02D0B550> 生成器
for row in res2:
print('插入后数据查询:',row)

原本是这样:

使用skip跳过2条数据:

使用limit限制前多少行

跳过第一行,后面的取前两行。取中间的区间

指定字段升序和降序查询asc,desc:

from pymongo import MongoClient
from bson import ObjectId
MC = MongoClient("10.0.0.11",30000)
MongoDB = MC["shop"]
res=MongoDB.zoo.insert_many([{"name":"cat","age":1},{"name":"dog","age":2},{"name":"pig","age":5},{"name":"monkey","age":3}])
from pymongo import DESCENDING,ASCENDING #需要导入正序和倒序
res2=MongoDB.zoo.find().sort('age',ASCENDING); #<pymongo.cursor.Cursor object at 0x02D0B550> 生成器
for row in res2:
print('插入和排序后数据查询:',row)

按指定字段降序查询

按指定字段升序查询

使用1和-1也可以的

python连接redis,mongodb以及简单命令使用的更多相关文章

  1. Python连接Redis连接配置

    1. 测试连接: Python 2.7.8 (default, Oct 20 2014, 15:05:19) [GCC 4.9.1] on linux2 Type "help", ...

  2. python mysql redis mongodb selneium requests二次封装为什么大都是使用类的原因,一点见解

    1.python mysql  redis mongodb selneium requests举得这5个库里面的主要被用户使用的东西全都是面向对象的,包括requests.get函数是里面每次都是实例 ...

  3. python连接redis哨兵集群

    一.redis集群模式有多种, 哨兵模式只是其中的一种实现方式, 其原理请自行谷歌或者百度 二.python 连接 redis 哨兵集群 1. 安装redis包 pip install redis 2 ...

  4. python连接redis、redis字符串操作、hash操作、列表操作、其他通用操作、管道、django中使用redis

    今日内容概要 python连接redis redis字符串操作 redis之hash操作 redis之列表操作 redis其他 通用操作,管道 django中使用redis 内容详细 1.python ...

  5. python连接redis,redis集群

    python连接redis: import redis r = redis.Redis(host='192.168.50.181',port=6002) r.set('user_phone_14900 ...

  6. Python使用Redis实现一个简单作业调度系统

    Python使用Redis实现一个简单作业调度系统 概述 Redis作为内存数据库的一个典型代表,已经在非常多应用场景中被使用,这里仅就Redis的pub/sub功能来说说如何通过此功能来实现一个简单 ...

  7. python 连接 redis cluster 集群

    一. redis集群模式有多种, cluster模式只是其中的一种实现方式, 其原理请自行谷歌或者百度, 这里只举例如何使用Python操作 redis cluster 集群 二. python 连接 ...

  8. 【python】Redis介绍及简单使用

    一.redis redis是一个key-value存储系统.和 Memcached类似,它支持存储的value类型相对更多,包括string(字符串). list(链表).set(集合).zset(s ...

  9. python连接redis

    一.首先,要下载redis pip3 install redis 二.连接redis import redis #拿到一个redis的链接 conn=redis.Redis('127.0.0.1',6 ...

  10. redis基础之python连接redis(五)

    前言 前面介绍了在数据库命令行直接操作redis,现在学习使用python的redis包来操作redis,本人安装的是redis==2.10.6: 系列文章 redis安装和配置 redis命令行操作 ...

随机推荐

  1. 区块链从入门到放弃系列教程-涵盖密码学,超级账本,以太坊,Libra,比特币等持续更新

    目录 简介 什么是区块链 区块链不是什么 区块链的基础:密码学 区块链的基础:分布式系统和共识机制 超级账本Hyperledger 以太坊 Libra 比特币 总结 简介 区块链是一种防篡改的共享数字 ...

  2. 【Learning eBPF-3】一个 eBPF 程序的深入剖析

    从这一章开始,我们先放下 BCC 框架,来看仅通过 C 语言如何实现一个 eBPF.如此一来,你会更加理解 BCC 所做的底层工作. 在这一章中,我们会讨论一个 eBPF 程序被执行的完整流程,如下图 ...

  3. HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报

    当用户有跨语种交流或音频内容翻译的需求时,应用需要能自动检测语音内容再输出为用户需要的语言文字. HMS Core机器学习服务提供同声传译能力,同声传译实现将实时输入的长语音实时翻译为不同语种的文本以 ...

  4. 报名直达丨HarmonyOS开发者创新大赛线下城市交流会来了,约吗?

    HarmonyOS开发者创新大赛线下城市交流会 正式开启啦! 在这里,将有专业的讲师 与HarmonyOS开发者们面对面演示开发实操.交流开发心得 还等什么?赶快扫码报名吧~↓↓↓ 城市交流会报名链接 ...

  5. nginx重新整理——————nginx 的网络模型[九]

    前言 简单介绍一下nginx的网络模型. 正文 网络拓扑图: 数据流: 网络传输大概是这样传输的. nginx 事件循环: 事件处理过程: 上面两张图什么意思呢? 其实就是说,nginx 是通过事件驱 ...

  6. sql 语句系列(两个日期之间)[八百章之第十七章]

    前言 进入了日期章了. 年月日加减法 分别对原有的日期进行加减法. sql server select DATEADD(DAY,-5,HIREDATE) as hd_mimus_5D, DATEADD ...

  7. c# mvc 移除多于的视图引擎

    前言 在我们的mvc中,我们又很多视图引擎是不需要的.为什么这么说呢? 我们知道计算机玩的就是遍历. 上图中我们可以看到,首先找的是index.aspx,因为我们都是cshtml,那么去映射的时候呢每 ...

  8. 记一次 JMeter 压测 HTTPS 性能问题

    ​简介:在使用 JMeter 压测时,发现同一后端服务,在单机 500 并发下,HTTP 和 HTTPS 协议压测 RT 差距非常大.同时观测后端服务各监控指标水位都很低,因此怀疑性能瓶颈在 JMet ...

  9. 技术干货 | 阿里云数据库PostgreSQL 13大版本揭秘

    简介: 阿里云RDS PostgreSQL是一款兼容开源PostgreSQL的全托管云数据库产品,自2015年首次发布以来,根据用户需求不断升级迭代,已支持9.4.10.11.12等多个版本,覆盖了高 ...

  10. [FAQ] 清理 Docker 环境长期构建占用磁盘空间过大问题

      $ docker system df 长时间积累多次运行 docker 构建过程,Build Cache 缓存几乎占据了硬盘 1/3 的容量. $ docker system  prune 此命令 ...