Linux系统Redis单机版安装(6.0.9)

一、下载redis安装包(稳定版)

      

二、上传文件并解压

[root@localhost tools]# rz                            //使用rz命令进行上传
[root@localhost tools]# tar -zxvf redis-6.0.9.tar.gz  //解压

三、进入redis解压目录并执行make命令进行编译

[root@centos102 tools]# cd redis-6.0.9/            //进入解压目录
[root@centos102 redis-6.0.9]#  make                //编译
cd src && make all
make[1]: Entering directory `/usr/tools/redis-6.0.9/src'
    CC Makefile.dep
    CC adlist.o
/bin/sh: cc: command not found                    //此时出现cc命令找不到
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/usr/tools/redis-6.0.9/src'
make: *** [all] Error 2

         此时报错说gcc或cc命令找不到,原因是redis是使用c语言编写的,所以需要添加gcc-c++依赖。

         这个时候就需要安装gcc。

         安装gcc命令

//这里必须有外网连接才可以
[root@centos11 tools]# [root@localhost redis-6.0.9]#  yum -y install gcc gcc-c++ libstdc++-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Dependency Installed:
  cpp.x86_64 0:4.8.5-39.el7      glibc-devel.x86_64 0:2.17-307.el7.1    glibc-headers.x86_64 0:2.17-307.el7.1    kernel-headers.x86_64 0:3.10.0-1127.19.1.el7   
  libmpc.x86_64 0:1.0.1-3.el7    mpfr.x86_64 0:3.1.1-4.el7             
Dependency Updated:
  glibc.x86_64 0:2.17-307.el7.1 glibc-common.x86_64 0:2.17-307.el7.1 libgcc.x86_64 0:4.8.5-39.el7 libgomp.x86_64 0:4.8.5-39.el7 libstdc++.x86_64 0:4.8.5-39.el7

Complete!

          此时继续使用make进行编译则报错

[root@localhost redis-6.0.9]#  make
cd src && make all
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
 #include <jemalloc/jemalloc.h>
                               ^
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/usr/tools/redis-6.0.9/src'
make: *** [all] Error 2

         这是因为第一次make时导致的一些不必要文件没有得到清除,此时需要清除不必要文件

[root@localhost redis-6.0.9]# make distclean               //清除命令   
cd src && make distclean
make[1]: Entering directory `/usr/tools/redis-6.0.9/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
rm -f adlist.d quicklist.d ae.d anet.d dict.d server.d sds.d zmalloc.d lzf_c.d lzf_d.d 
(rm -f .make-*)
make[2]: Leaving directory `/usr/tools/redis-6.0.9/deps'
(rm -f .make-*)
make[1]: Leaving directory `/usr/tools/redis-6.0.9/src'

         清除完之后则使用make命令继续编译,如果此时出现类似下面的错误信息,则是由于gcc版本太低导致的,此时则需要升级gcc版本

server.c:5342:39: error: ‘struct redisServer’ has no member named ‘maxmemory’
     if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {
                                       ^
server.c:5343:176: error: ‘struct redisServer’ has no member named ‘maxmemory’
         serverLog(LL_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?", server.maxmemory);
                                                                                                                                                                                ^
server.c:5346:31: error: ‘struct redisServer’ has no member named ‘server_cpulist’
     redisSetCpuAffinity(server.server_cpulist);

         升级gcc版本(这里也需要有外网支持才可以)

[root@centos102 redis-6.0.9]#  yum -y install centos-release-scl
[root@centos102 redis-6.0.9]#  yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@centos102 redis-6.0.9]# scl enable devtoolset-9 bash
需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
如果要长期使用gcc 9.3的话: 
[root@centos102 redis-6.0.9]# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

        查看gcc版本

[root@centos102 redis-6.0.9]# gcc -v      //查看版本命令
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)

       升级完gcc之后则继续进行make编译

[root@centos102 redis-6.0.9]#  make
cd src && make all
make[1]: Entering directory `/usr/tools/redis-6.0.9/src'
    CC Makefile.dep
make[1]: Leaving directory `/usr/tools/redis-6.0.9/src'
make[1]: Entering directory `/usr/tools/redis-6.0.9/src'
    CC server.o
    INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/tools/redis-6.0.9/src'

       编译完成后则需要执行make install进行安装

[root@centos102 redis-6.0.9]# make install      //安装redis
cd src && make install
make[1]: Entering directory `/usr/tools/redis-6.0.9/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/tools/redis-6.0.9/src'

          安装完成后,所有命令则默认存在与/usr/local/bin目录中

[root@centos102 bin]# ll
total 38076
-rwxr-xr-x. 1 root root 4741000 Nov  9 01:11 redis-benchmark
-rwxr-xr-x. 1 root root 9724328 Nov  9 01:11 redis-check-aof
-rwxr-xr-x. 1 root root 9724328 Nov  9 01:11 redis-check-rdb
-rwxr-xr-x. 1 root root 5061344 Nov  9 01:11 redis-cli
lrwxrwxrwx. 1 root root      12 Nov  9 01:11 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 9724328 Nov  9 01:11 redis-server
[root@centos102 bin]# pwd
/usr/local/bin

          至此则表示redis安装完成!

 

     redis启动

     redis启动前需要设置配置文件(redis.conf)中的参数

################################# GENERAL #####################################

# 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.
daemonize no                      //这里的no表示启动时占用当前窗口,更改为yes表示后台运行,不占用当前窗口

stop-writes-on-bgsave-error no    //设置为no,如果为yes时,则强制关闭Redis快照导致不能持久化

    redis启动服务端(执行配置文件进行启动)

[root@centos102 bin]# redis-server /myredis/redis.conf      //根据配置文件进行启动
[root@centos102 bin]# ps -ef |grep redis                    //查看端口号
root      10660      1  1 01:21 ?        00:00:00 redis-server 127.0.0.1:6379  //对应进程
root      10666   8922  0 01:21 pts/2    00:00:00 grep --color=auto redis

     redis启动客户端(此时是无密码访问)

[root@centos102 bin]#  redis-cli -p 6379  //redis默认端口号是6379
127.0.0.1:6379> ping 
PONG                                      //表示启动成功,此时是无密码访问

127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
127.0.0.1:6379>

     设置密码

//设置密码
127.0.0.1:6379> config set requirepass "123456"        //设置密码
OK 
127.0.0.1:6379> config get requirepass                 //查看密码
1) "requirepass"
2) "123456"

//这里可以把密码写入配置文件,防止重启后密码失效
127.0.0.1:6379> config rewrite                //密码写入配置文件,可加可不加;如果不加的话,进程结束后将恢复无密访问

//然后退出(这里把服务端进程杀死)发现无法进入
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456                   //使用 auth + 密码 进行验证
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

     查看进程

[root@centos102 bin]# ps -ef|grep redis
root      10660      1  0 01:21 ?     00:00:01 redis-server 127.0.0.1:6379  //服务端进程
root      10818   8922  0 01:33 pts/2    00:00:00 redis-cli -p 6379   //客户端进程
root      10831   3436  0 01:33 pts/0    00:00:00 grep --color=auto redis

    关闭

    同时关闭服务端和客户端

127.0.0.1:6379> shutdown                      //同时关闭服务端可客户端
not connected> exit

[root@cent102]# redis-cli -p 6379 shutdown    //多实例关闭,指定端口关闭
not connected> exit

    查看进程

[root@centos102 bin]# ps -ef|grep redis
root      10841   3436  0 01:35 pts/0    00:00:00 grep --color=auto redis
[root@centos102 bin]#