Linux下redis最完善使用教程

1.下载

用wget命令从官网下载压缩包:wget http://download.redis.io/releases/redis-5.0.2.tar.g

Linux下redis最完善使用教程_第1张图片

2.解压

tar -xvf redis-5.0.2.tar.gz

Linux下redis最完善使用教程_第2张图片

3.进入到redis目录下编译redis

cd redis-5.0.2

make

Linux下redis最完善使用教程_第3张图片

4.进入到redis-5.0.2/src目录下测试是否安装成功:

./redis-server

Linux下redis最完善使用教程_第4张图片

 

2.分析配置文件

分析配置文件有助于我们以后碰到问题可以有效及时的解决,当然除了下面的一些,配置文件还有其他一些重要的内容,如:主从复制,日志等,如果感兴趣可以去redis目录下查看。

1、ctrl+c退出redis

2.返回上级目录进入到redis.con

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#这段将的是网络配置,bind 127.0.0.1绑定本机IP地址,但是如果你要redis监听到所有的网络借口,那么你需要注释掉最后一行。

bind 127.0.0.1


# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.

#连接端口号为6379,如果设置为0表示不监听tcp连接
port 6379


# Close the connection after a client is idle for N seconds (0 to disable)

#超时连接,0就是一直尝试连接
timeout 0

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT where
# dbid is a number between 0 and 'databases'-1

#数据库最大值
databases 16

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

 

#生成快照,将内存的数据存储到磁盘上,

#格式:save <间隔时间(秒)> <写入次数>

save 900 1        # 900 秒内如果至少有 1 个 key 的值变化,则保存
save 300 10      # 300 秒内如果至少有 10 个 key 的值变化,则保存
save 60 10000  # 60 秒内如果至少有 10000 个 key 的值变化,则保存

除了这种方式持久化之外,我们还可以用aof方式,就是将每次写操作追到到文件中

appendonly yes #启用aof 持久化方式 

# appendfsync always //收到写命令就立即写入磁盘,最慢,但是保证完全的持久化 。

appendfsync everysec //每秒钟写入磁盘一次,性能和可靠度折中选择。

# appendfsync no //完全依赖os,性能最好,但是如果操作系统没有及时写入,出现意外情况,如断电等,数据可能会丢失。

两者对比:

1. 因为RDB每次都是将数据完整加到写到磁盘,所以RDB 在恢复大数据集时的速度比 AOF 的恢复速度要快。。

2. RDB会丢失几分钟的数据,AOF是否丢失数据取决于模式的选择。

3. AOF每次碰到写操作都要追加到文件中会导致文件过大。

 

################################## SECURITY ###################################

# Require clients to issue AUTH before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#设置登录密码,可以不需要。设置方式:将# requirepass foobared 变成 requirepass xxx
# requirepass foobared

# Command renaming.

 

################################### CLIENTS ####################################

# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#设置最大的客户端连接数10000
# maxclients 10000

 

 

 

3.语法操作

redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。

对String就是set key value,get key

对list常见操作有lpush, rpush, lrange等.

set有常见操作有sadd,smember,求交集,并集,差集。

zset常见操作有zrange, zadd, zrevrange等。

hash常见操作有hget, hset, hmget, hmset等。

 

 

你可能感兴趣的:(redis)