Redis 使用基础及配置文件详解(三)|学习笔记

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
日志服务 SLS,月写入数据量 50GB 1个月
简介: 快速学习Redis 使用基础及配置文件详解(三)

开发者学堂课程【Redis 入门实战演练Redis 使用基础及配置文件详解(三)】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/653/detail/10833


Redis 使用基础及配置文件详解(三)


七、python连接方式

#! /bin/env python

#Author : ZhangJie

import redis

import time

poo1 = redis .ConnectionPool (host="192.168.7.101",port=6379 , password="")

r = redis . Redis (connection_ pool=poo1)

for i in range(100):

r.set("k%d" % i,"v%d”% i)

time.sleep(1)

data=r .get("K%d"% i)

print (data)

Python 是模块化的,需要导入两个模块,一个是 Redis 模块,一个是 time 模块,python 是比较简单的,学起来难度小一些。

import redis

import

time

Pool=redis . ConnectionPool(host="172.31.7.101", port=6379,password= "linux39" )

r = redis . Redis (connection pool=pool)

for i in range(100) :

r.set( "k8d" % i,"v%d" % i)

#time .sleep(1 )

data=r .get("k%d" % i)

print ( data )

写完一个数据之后 sleep 多少秒,slepp 之后读出来,data=r .get("k%d" % i)就是读数据,每次写完一个数据就 get 一下,然后复制给 data,再打印出来。r.set 是进行增加数据的,r.get 是查数据的,保存之后运行看是否能用。

[ root@172 ~]# python

Python 2.7.5 (default, Aug 7 2019, 00:51:29)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2

Type "help","copyright", "credits" or " license"

for more info rmation.

>>> quit( )

H

[root@172 ~]# python

Python 2.7.5 (default, Aug 7 2019, 00:51:29)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on Linux2

Type "help", "copyright", "credits" or "license" for more info rmation.

>>> import redis

Traceback ( most recent call Last) :

File "<stdin>", Line 1, in <module>

ImportError: No module named redis

显示没有Redis模块,没有就需要装一个。

[root@172 ~]# yum intall python-pip

Loaded plugins: fastestmirror

No such command: intall. PLease use /usr/bin/yum --help

[ root@172 ~]#

[ root@172 ~]# yum intall python-pip^c

[ root@172 ~]# yum list

Loaded plugins: fastestmin ror

Loading mirror speeds from cached hostfile

[ root@172 ~]# pip install redis

Collecting redis

Downloading https://files . pythonhosted . org/ packages/ f0/05/ 1fc7feedc 19c 123e7a95c fc9e7892eb6c dd2e5df4e9e8af 6384349c 1cc3d/

redis-3.4.1-py2. py3-none-any .whl ( 71kB )

1111111111111111i11iiiiiii 1 1 1 1 | 71kB 18kB/s

Installing collected packages: redis

Successfully installed redis-3.4.1

[ root@172 ~]#

[root@172 ~]# python

[ root@172 ~]# pip install redis

Collecting redis

Downloading https://files . pythonhosted . org/ packages/ f0/05/ 1fc7feedc 19c 123e7a95c fc9e7892eb6c dd2e5df4e9e8af 6384349c 1cc3d/

redis-3.4.1-py2. py3-none-any .whl ( 71kB )

100% | 71kB 18kB/s

Installing collected packages: redis

Successfully installed redis-3.4.1

[ root@172 ~]#

[root@172 ~]# python

用 python 执行之前的脚本。

[root@172 ~]#python redis-client.py就可以执行了。

进去看一下是否写完了程序

127.0.0.1:6379> FLUSHALL

[root@redis-server1 redis-4.0.14]# / apps/ redis/bin/ redis-cli

127.0.0.1:6379> KEYS *

(error) NOAUTH Authentication required .

127.0.0.1:6379> AUTH Linux39

OK

127.0.0.1:6379> FLUSHDB

 

八、Redis 配置文件

把所有的命令做一个软连接,不用写一次命令就敲一次链接了。

[ root@redis-serverl redis-4.0.14]# Ln -sV /apps/ redis/bin/ redis-

redis- benchmark redis-check - aof  redis-check- rdb  redis-cli  redis- sentinel redis- server

[ root@redis-serverl redis-4.0.14]# Ln -SV /apps/ redis/bin/ redis-*/usr/bin/

1.Redis 主要配置项

1) bind 0.0.0.0 #监听地址,可以用空格隔开后多个监听IP

2) protected-mode yes #redis3.2 之后加入的新特性,在没有设置 bind IP 和密码的时候 redis 只允许访问127.0.0.1:6379,远程访问将提示警告信息并拒绝远程访问。

[ root@redis-serverl redis-4.0.14]# vim /apps/ redis/etc/ redis. conf

[ root@redis-server1 redis-4.0.14]# systemctl restart redis

[ root@redis-serverl redis-4.0.14 ]#

[ root@redis-serverl redis-4. 0.14]#

[ rootaredis-serverl redis-4.0.14]# Ss - tnl

这时候从远端去连接,可以不使用密码

[ root@172 ~]# redis-cli -h 172.31.7.101 -p 6379

172.31. 7.101:6379>get k1

输入这些以后,会发现远端连接会出现错误,会出现 error,安全模式已经打开,没有设置监听地址,系统给出了四个解决方案。

第一个方法是关闭安全模式。第二个关闭配置文件的安全模式。第三个是重启“protected-mode no”选项。第四个是设置监听地址或者设置一个密码。

3) port 6379 #监听端口

在一个服务器上开启多个 Redis 服务,每个 Redis 让它监听不同的端口,这个地方涉及到更改端口。第一个 Redis 是172.31.7.101:6379第二个 Redis 是172.31.7.101:6380第三个 Redis 是172.31.7.101.6381.只有两台服务器或者只有一个服务器开启 Redis,去让它监听不同的端口,就可以实现一个服务器或者两台服务器跑多个 Redis。生产是跑的 Redis 集群,主要目的是能够实现和生产环境同样架构的 Redis 集群。

4) tcp-backlog511 #三次握手的时候 server 端收到 client ack 确认号之后的队列值。默认值是511.

5) timeout 0 #客户端和 Redis 服务端的连接超时时间,默认是0,表示永不超时。客户端通常指的是程序,并非用户。一般来说都是0.在程序连接 Redis 的时候, timeout 为0的时候,几乎不会被中断,那么这个链接相当于是永远存在的,就会造成A程序和B程序连接一直存在。如果这两个程序都挂了的话,连接它还在。运行一段时间之后,就会发现 masql 会越来越慢,这个时候想办法把控量杀掉,或者把连接的时间修改的短一点。

6) tcp-keepalive 300 #tcp 会话保持时间

Redis 版本有2.8-->3.0-->3.2-->4.0-->5.0,版本的更新可能给会导致参数变化。

7) daemonize n #认情况下 redis 不是作为守护进程运行的,如果你想让它在后台运行,你就把它改成 yes,当 redis 作为守护进程运行的时候,它会写-个 pid到 /var/run/redis.pid 文件里面。daemonize 需要开启 PID 文件。

8) supervised no #和操作系统相关参数,可以设置通过 upstart 和 systemd 管理Redis 守护进程, centos 7 以后都使用 systemd。默认是 no。

 [Service]

ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf--supervised systemd

ExecReload=/bin/kill -s HUP$MAINPID

ExecStop=/bin/ kill -s QUIT $MAINPIDType=notify

user=redis

Group=redis

RuntimeDirectory=redis

RuntimeDirectoryMode=0755

[root@redis-server1 ~]# ll /usr/libexec / redis-shutdow

ls: cannot access /usr/libexec/redis-shutdow: No such file or directory[ root@redis-server1 ~]#/ apps/ redis/bin/redis-cli

127.0.0.1:6379>

[ root@redis-server1 ~]# ll / apps/ redis/ runy/total 0

[root@redis-server1 ~]# ll / apps/redis/ run/ -d

drwxr-xr-x 2 redis redis_6 Feb7 19:42 /apps/redis/run/

Redis考虑到有些公司要在一个服务器上去用多个实例,多个 PID 文件可以通过加端口的方式来区分 PID 是哪个 Redis 端口的文件。日志文件也可以这么区分。

9) pidfile /var/run/redis_ 6379.pid #pid 文件路径

10) loglevel notice #日志级别

级别分别是 debug,verbose,notice 和 warning。notice 级别相对折中一些,debug是最高的并且会保存所有的日志,warning 是最低的,它只会保存一些警告级别的日志。如果 Redis 运行正常,开一个 notice 就可以。

11) logfile " #日志路径

logfile " / apps/ redis/logs/ redis_6379.log"

12) databases 16 #设置 db 库数量,默认16个库

# Set the number of databases.The default database is DB 0,you can select#a different one on a per-connection basis using SELECTbid> where

#dbid is a number between 0 and ' databases ' -1

Detabases 相当于 masql 的数据库名称一样。detabases0 里面是不能设置两个 key 的,如果让 app1 是一个服务,app2 是一个服务,两个属于不同的服务,这两个都放到 Redis,假设 app1 设置k ey1 为 linux39,app2 不能再放到 key1了,否则会覆盖。如果设置 app2 的 key1值为 magedu,这时候就会出错。解决方式是 app1 写一个 key,app2 写一个 key,两个分开写。key 的名称可以相同,相互是不冲突的。尽量一个业务使用一个 databases。

17)alawys-show-logo yes #在启动 redis 时是否显示 log

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1月前
|
NoSQL Java Redis
redis-学习笔记(Jedis 通用命令)
redis-学习笔记(Jedis 通用命令)
31 1
|
8天前
|
负载均衡 NoSQL 关系型数据库
Redis分布式锁学习总结
Redis分布式锁学习总结
13 0
|
1月前
|
Java Redis
redis-学习笔记(Jedis zset 简单命令)
redis-学习笔记(Jedis zset 简单命令)
29 3
|
1月前
|
NoSQL 网络协议 关系型数据库
redis-学习笔记(redis 单线程模型)
redis-学习笔记(redis 单线程模型)
31 3
|
1月前
|
Java Redis
redis-学习笔记(Jedis string 简单命令)
redis-学习笔记(Jedis string 简单命令)
27 2
|
1月前
|
Java Redis
redis-学习笔记(Jedis hash简单命令)
redis-学习笔记(Jedis hash简单命令)
26 1
|
1月前
|
存储 Java Redis
redis-学习笔记(Jedis set 简单命令)
redis-学习笔记(Jedis set 简单命令)
28 1
|
1月前
|
Java Redis
redis-学习笔记(Jedis list简单命令)
redis-学习笔记(Jedis list简单命令)
26 1
|
1月前
|
NoSQL Java Redis
redis-学习笔记(Jedis 前置知识)
redis-学习笔记(Jedis 前置知识)
24 1
|
1月前
|
NoSQL Java Redis
redis-学习笔记(string , hash , list , set , zset 前置知识)
redis-学习笔记(string , hash , list , set , zset 前置知识)
13 0
redis-学习笔记(string , hash , list , set , zset 前置知识)