Skip to content

Redis单机安装(Centos)

1. 下载Redis

访问https://redis.io/download, 下载redis-7.2.4.tar.gz Alt text

提示

不建议安装Windows版本Redis,🤗官网也不提供: Alt text

2. Linux环境准备

2.1 安装gcc

sh
[root@hadoop105 ~]# gcc -v
-bash: gcc: 未找到命令
## 安装gcc
[root@hadoop105 ~]# yum -y install gcc-c++
[root@hadoop105 ~]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

3. 上传Redis并解压

将redis-7.2.4.tar.gz上传到/opt/software/

sh
[root@hadoop105 software]# ll
总用量 3344
drwxr-xr-x. 2 root root     202 3月   5 15:30 hadoop_source
-rw-r--r--. 1 root root 3424072 3月   5 22:34 redis-7.2.4.tar.gz
[root@hadoop105 software]# tar -xvf redis-7.2.4.tar.gz -C /opt/module/

4. 编译安装

sh
[root@hadoop105 bin]# make PREFIX=/opt/module/redis-7.2.4/ install

5. Redis目录

sh
[root@hadoop105 bin]# ll
总用量 29240
-rwxr-xr-x. 1 root root  6899744 3月   6 01:17 redis-benchmark
lrwxrwxrwx. 1 root root       12 3月   6 01:17 redis-check-aof -> redis-server
lrwxrwxrwx. 1 root root       12 3月   6 01:17 redis-check-rdb -> redis-server
-rwxr-xr-x. 1 root root  7619616 3月   6 01:17 redis-cli
lrwxrwxrwx. 1 root root       12 3月   6 01:17 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 15415632 3月   6 01:17 redis-server
  • redis-benchmark:性能测试工具
  • redis-check-aof:修复有问题的AOF文件
  • redis-check-dump:修复有问题的dump.rdb文件
  • redis-cli:客户端,操作入口
  • redis-sentinel:redis集群使用
  • redis-server:Redis服务器启动命令

6. 配置redis

  1. 复制redis.conf文件到conf文件夹
sh
[root@hadoop105 bin]# cp /opt/module/redis-7.2.4/redis.conf /opt/module/redis-7.2.4/conf/
  1. 修改配置redis.conf
sh
[root@hadoop105 bin]#  vi redis.conf
## 默认daemonize no 改为 daemonize yes,支持后台启动
## 默认protected-mode  yes 改为 protected-mode no 保护模式会拒绝远程客户端的连接请求
## 默认bind 127.0.0.1 改为  直接注释掉(默认bind 127.0.0.1只能本机访问)或改成本机IP地址,否则影响远程IP连接
## 添加redis访问密码  改为 requirepass 你自己设置的密码  
## logfile "/opt/module/redis-7.2.4/logs/redis.log" 设置redis日志目录
## pidfile /opt/module/redis-7.2.4/logs/redis.pid 记录pid文件
## dir /opt/module/redis-7.2.4/ 设置工作目录

7. 启动redis

sh
[root@hadoop105 bin]# cd /opt/module/redis-7.2.4/bin
[root@hadoop105 bin]# ./redis-server ../conf/redis.conf 
43124:C 06 Mar 2024 01:40:49.129 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[root@hadoop105 bin]# sysctl vm.overcommit_memory=1
[root@hadoop105 bin]# ./redis-server ../conf/redis.conf

此时Redis服务器已经启动起来了。

8. 客户端连接

8.1 命令行连接

redis-cli是Redis命令行工具,是一个命令行客户端程序,可以将命令直接发送到Redis,并直接从终端读取服务器返回的应答。 它有两种主要模式:

  1. 参数模式
    将命令作为redis-cli的参数发送,并打印执行结果在标准输出上。
参数说明
-h主机名或IP地址
-p端口
--raw在终端上强制进行原始输出, 比如中文显示支持
-a身份验证密码,也可以配置REDISCLI_AUTH环境变量的方式,避免命令行-a 中出现明文密码
-n特定编号的数据库, 默认是0号数据库
-uuri, 比如redis://p%40ssw0rd@redis-16379.hosted.com
--tls启用SSL / TLS
-x从其他程序获取输入
-r <count> -i <delay>-r参数是运行命令的次数, 其中-1表示一直执行,-i 参数配置命令调用之间的延迟(以秒为单位⏱️) 比如redis-cli -r 5 incr foo
--csv快速将数据从Redis导出, 如redis-cli --csv lrange mylist 0 -1
  1. 交互模式
    其中存在一个REPL(Read Eval Print Loop),用户可以在其中键入命令并获得答复
    sh
    [root@hadoop105 bin]# ./redis-cli -a jack --raw
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    127.0.0.1:6379> ping
    PONG
    ## 此时进入交互模式
    127.0.0.1:6379>
    如果需要远程连接,可以使用connect指令:
    ## 本地没有redis,远程连接服务器Redis
    D:\redis-windows-7.2.4>redis-cli.exe
    Could not connect to Redis at 127.0.0.1:6379: 由于目标计算机积极拒绝,无法连接。
    not connected> connect 192.168.101.105 6379
    192.168.101.105:6379> auth jack
    OK
    192.168.101.105:6379> ping
    PONG
    后面的笔记操作都是以交互模式为主。

8.2 GUI客户端连接:

  1. redis官方UI工具RedisInsight:https://download.redisinsight.redis.com/latest/RedisInsight-win-installer.exeRedisInsight客户端
  2. 💯推荐AnotherRedisDesktopManager进行连接:https://github.com/qishibo/AnotherRedisDesktopManager/AnotherRedisDesktopManager客户端 新建Redis连接,点击确定: Alt text
  3. 使用TeamIDE进行连接:https://github.com/team-ide/teamide/releasesTeamIDE客户端 选择添加Redis连接,点击测试: TeamIDE进行Redis连接

9 关闭服务器

sh
## 关闭单机版redis
[root@hadoop105 bin]# ./redis-cli -a jack shutdown
## 指定关闭6379端口的redis
[root@hadoop105 bin]# ./redis-cli -a jack -p 6379 shutdown