Redis下载安装与Redis初步认识

目录

1、Redis官网下载地址:Redis,下载方法就不必多说什么了。

2、Redis安装

    2.1、Redis解压:tar -zxvf redis-4.0.8.tar.gz -C /usr/local/

    2.2、安装

    2.3、redis启停,切换到 src 目录下:

3、Redis客户端

    3.1、redis命令行客户端

    3.2、Redis远程客户端

    3.3、redis编程客户端

4、设置Redis开机启动

4.1、复制配置文件

4.2、修改文件名在

4.3、修改配置文件

4.5、设置开机启动

5、远程授权

5.1、修改 bind配置,以下是默认配置

5.2、修改 protecte-mode

5.3、修改 daemonize

6、Redis命令常用

7、Redis的发布和订阅,参考Redis命令大全;

8、Redis数据持久化,参考Redis命令大全;

9、Redis事务,参考Redis命令大全。


1、Redis官网下载地址:Redis,下载方法就不必多说什么了。

2、Redis安装

    2.1、Redis解压:tar -zxvf redis-4.0.8.tar.gz -C /usr/local/

参数解读:

    tar:xxx.tar 或者 xxx.tar.gz 格式的压缩包的解压命令;

    -zxvf:z 代表gzip的压缩包;x 代表解压;v 代表显示过程信息;f 代表后面接的是文件 

    -C:解压到指定的目录。

    2.2、安装

切换目录到 Redis根目录下,执行 make 命令。

执行 make 命令可能会报错:

    错误一:gcc命令找不到,是由于没有安装gcc导致,直接使用 yum 进行安装,命令:yum -y install gcc; -y 是一个参数,表示在安装的过程中总是执行 yes;

    错误二:error: jemalloc/jemalloc.h: No such file or directory,是因为没有分配内存,内存分配器使用 libc ,执行命令:make MALLOC=libc;

    错误解决参考连接:初学者安装Redis时报错server.c:5170:31: error: ‘struct redisServer’,解决就是这门简单-CSDN博客

错误解决之后,先执行 make distclean 清理一下上次 make 之后产生的文件,再执行 make。

附加操作(可以不执行):执行完 make 之后,再执行 make install,该操作是将 src 目录下的可执行文件复制到 、usr/local/bin 目录下。

    2.3、redis启停,切换到 src 目录下:

2.3.1、启动:

    a、后台启动: ./redis-server & 

    b、后台启动并输出日志到nohup.out文件:nohup /usr/local/redis-4.0.8/src/redis-server &

2.3.2、关闭: ./redis-cli shutdown 或者通过杀死进程的方式直接简单粗暴的关闭(kill pid 或者 kill -9 pid,其中 pid 代表的是进程号)。

3、Redis客户端

    3.1、redis命令行客户端

redis-cli(Redis Command Line Interface)是Redis自带的基于命令行的Redis客户端,用于与服务端交互,我们可以使用该客户端来执行redis的各种命令,连接客户端有两种方式:

3.1.1、直接连接 Redis(默认ip127.0.0.1,端口6379):./redis-cli 

3.1.2、指定 IP 和 端口 port 连接 Redis:./redis-cli -h 127.0.0.1 -p 6379

    3.2、Redis远程客户端

Redis Desktop Manager 下载地址:RedisInsight | The Best Redis GUI,傻瓜式安装。

    3.3、redis编程客户端

3.3.1、什么是redis编程客户端:redis以键值对的方式存储数据在服务器上,那么我们Java程序如何读取键值对中的值内容呢?所以有人编写了一套程序,专门去连接redis并读取其中的键值对内容,这套程序就像驱动程序一样,我们使用它提供的API就能访问服务器上的redis并对它进行各种操作。

3.3.2、redis的Java编程客户端:Jedis:redis的Java编程客户端,Redis官方首选推荐使用Jedis,jedis是一个很小但很健全的redis的java客户端;Jedis 源码:https://github.com/xetorthio/jedis;api 文档:http://xetorthio.github.io/jedis/

4、设置Redis开机启动

4.1、复制配置文件

复制 redis_init_script 文件到 /etc/init.d/ 目录下,执行命令:cp /usr/local/redis-x.x.x/utils/redis_init_script /etc/init.d/

4.2、修改文件名在

改文件名字为redis,切换目录到 /etc/init.d/,执行命令:mv redis_init_script redis

4.1和4.2可以合并为一步,执行命令:cp /usr/local/redis-x.x.x/utils/redis_init_script /etc/init.d/redis

4.3、修改配置文件

如图,更改前文件内容为:

另外加上端口号,只需要改5处,初学者首次安装不用更改端口号,所以,此文只改图中标出的4处。如果在2.2 make之后执行过 make install,则 第①和第②两处也可以不改,其实第③处也可以不改,是自动生成的进程文件地址。最重要的是第④步,redis启动所需要的配置文件地址,指定到redis的配置文件地址就可以。

更改后文件内容为:

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/redis/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

4.5、设置开机启动

4.5.1、对文件赋予执行权限,执行命令:

chmod +x /etc/init.d/redis 或 chmod 777 /etc/init.d/redis

或者切换到 /etc/init.d/ 目录下,执行命令

chmod +x redis 或 chmod 777 redis

4.5.2、增加redis服务

执行命令:chkconfig --add redis

4.5.3、查询redis服务情况

执行命令:chkconfig --list redis,redis服务运行状态如下:

redis0:off 1:off 2:on 3:on 4:on 5:on 6:off

默认的运行级别为2,3,4,5为on,如果3,4,5 为off:则执行命令:chkconfig --level 345 redis on

5、远程授权

修改配置文件redis.conf,默认位置在redis根目录下

5.1、修改 bind配置,以下是默认配置

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
# Each address can be prefixed by "-", which means that redis will not fail to
# start if the address is not available. Being not available only refers to
# addresses that does not correspond to any network interface. Addresses that
# are already in use will always fail, and unsupported protocols will always BE
# silently skipped.
# 默认情况下,如果没有指定“bind”配置指令,Redis将监听主机上所有可用网络接口的连接。
# 使用“bind”配置指令,可以只监听一个或多个选定的接口,后面跟着一个或多个IP地址。
# 每个地址都可以用“-”作为前缀,这意味着如果地址不可用,redis不会启动失败。
# 不可用是指地址不对应于任何网络接口。已经在使用的地址总是会失败,不支持的协议总是会
# 被静默跳过。
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses
# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6
# bind * -::*                     # like the default, all available interfaces
#
# ~~~ 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 on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# COMMENT OUT THE FOLLOWING LINE.
#
# You will also need to set a password unless you explicitly disable protected
# mode.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 -::1

所以,修改方式有三种

5.1.1、指定固定ip

5.1.2、指定匹配规则的ip

5.1.3、不指定bind

5.2、修改 protecte-mode

以下是默认配置

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and the default user has no password, the server
# only accepts local connections from the IPv4 address (127.0.0.1), IPv6 address
# (::1) or Unix domain sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured.
protected-mode yes

指定方式有两种

5.2.1、修改 protected-mode 为 no

5.2.2、不修改protected-mode,设置username 和 password(未测试)

5.3、修改 daemonize

以下是默认配置

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no

修改 daemonize 为 yes 

6、Redis命令常用

redis中文版命令大全:www.redisdoc.com-官网首页;redis英文版命令大全:Commands | Redis

7、Redis的发布和订阅,参考Redis命令大全;

8、Redis数据持久化,参考Redis命令大全;

9、Redis事务,参考Redis命令大全。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值