Centos7 安装 redis 6

准备

升级gcc

1、因为redis-5以上要使用gcc5.3以上版本进行编译,但是centos7默认安装的gcc版本是4.8.5,所以需要升级gcc版本:image.png
如果gcc版本过低,编译的时候会出错:
image.png

  1. #安装gcc
  2. yum -y install gcc tcl
  3. # 查看gcc版本是否在5.3以上,centos7.6默认安装4.8.5
  4. gcc -v

2、升级gcc的操作

  1. # 升级到gcc 9.3:
  2. yum -y install centos-release-scl
  3. yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
  4. scl enable devtoolset-9 bash
  5. # 需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
  6. # 如果要长期使用gcc 9.3的话:
  7. echo -e "\nsource /opt/rh/devtoolset-9/enable" >>/etc/profile
  8. # 查看gcc版本
  9. # [root@node01 ~]# gcc -v
  10. Using built-in specs.
  11. COLLECT_GCC=gcc
  12. COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
  13. Target: x86_64-redhat-linux
  14. 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
  15. Thread model: posix
  16. gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)

安装redis

下载安装

  1. # 下载redis安装包
  2. wget http://download.redis.io/releases/redis-6.2.5.tar.gz
  3. # 解压安装包
  4. [root@node01 modules]# tar -zxvf redis-6.2.5.tar.gz
  5. # 进入安装包
  6. [root@node01 modules]# cd redis-6.2.5/
  7. # 编译和安装
  8. make && make test && make install

安装出错的话执行:

  1. # 编译出错时,清出编译生成的文件
  2. make distclean
  3. # 卸载
  4. make uninstall

配置

  1. 将服务端脚本和客户端脚本移动到新建的文件夹下面 ```shell

    在安装目录下创建一个 bin 目录

    [root@node01 redis-6.2.5]# mkdir bin [root@node01 redis-6.2.5]# cd src/

    将 redis-server 和 redis-cli 移动到 bin 目录中

    [root@node01 src]# cp redis-server ../bin/ [root@node01 src]# cp redis-cli ../bin/

[root@node01 src]# cd .. [root@node01 redis-6.2.5]# ls 00-RELEASENOTES BUGS CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf tests utils bin CONDUCT COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src TLS.md

将 redis 的配置文件 redis.conf 移动到 bin 目录下

[root@node01 redis-6.2.5]# cp redis.conf bin/ [root@node01 redis-6.2.5]# cd bin/ [root@node01 bin]# ls redis-cli redis.conf redis-server

修改配置文件,修改的内容在下面

[root@node01 bin]# vim redis.conf [root@node01 bin]# ./redis-server redis.conf [root@node01 bin]# ./redis-cli 127.0.0.1:6379> set test hello OK 127.0.0.1:6379> get test “hello”

  1. 2. 配置redis.conf 文件
  2. ```nginx
  3. # 设置可以访问redis服务的IP
  4. bind 0.0.0.0
  5. # 设置redis的访问端口
  6. port 6379
  7. # 设置访问redis的密码
  8. requirepass 123456
  9. # 设置 redis-server 以守护线程方式启动
  10. daemonize yes
  1. 启动redis, 登录数据库 ```shell

    启动 redis 服务, 后面加 & 参数后台表示启动

    $REDIS_HOME/bin/redis-server redis.conf &

查看 redis 进程是否启动

[root@localhost bin]# ps -ef | grep redis root 85823 62546 0 23:54 pts/1 00:00:00 ./redis-server 0.0.0.0:6379 root 85839 83082 0 23:55 pts/2 00:00:00 grep —color=auto redis

启动 redis 的客户端

$REDIS_HOME/bin/redis-cli

登录 redis

auth [yourpassword]

  1. 4. 开启自启动
  2. ```shell
  3. # 创建开机自启动脚本的文件
  4. vim /etc/init.d/redis

编写开机自启动的脚本, 注意替换自己的redis的启动命令的位置以及配置文件的位置。

  1. #!/bin/sh
  2. # Configurations injected by install_server below....
  3. EXEC=/home/redis-6.2.6/bin/redis-server
  4. CLIEXEC=/home/redis-6.2.6/bin/redis-cli
  5. PIDFILE=/var/run/redis_6379.pid
  6. CONF="/home/redis-6.2.6/bin/redis.conf"
  7. REDISPORT="6379"
  8. ###############
  9. # SysV Init Information
  10. # chkconfig: - 58 74
  11. # description: redis_6379 is the redis daemon.
  12. ### BEGIN INIT INFO
  13. # Provides: redis_6379
  14. # Required-Start: $network $local_fs $remote_fs
  15. # Required-Stop: $network $local_fs $remote_fs
  16. # Default-Start: 2 3 4 5
  17. # Default-Stop: 0 1 6
  18. # Should-Start: $syslog $named
  19. # Should-Stop: $syslog $named
  20. # Short-Description: start and stop redis_6379
  21. # Description: Redis daemon
  22. ### END INIT INFO
  23. case "$1" in
  24. start)
  25. if [ -f $PIDFILE ]
  26. then
  27. echo "$PIDFILE exists, process is already running or crashed"
  28. else
  29. echo "Starting Redis server..."
  30. # 启动 Redis
  31. $EXEC $CONF
  32. fi
  33. ;;
  34. stop)
  35. if [ ! -f $PIDFILE ]
  36. then
  37. echo "$PIDFILE does not exist, process is not running"
  38. else
  39. PID=$(cat $PIDFILE)
  40. echo "Stopping ..."
  41. # $CLIEXEC -p $REDISPORT shutdown
  42. # 带有密码的redis关闭方式: redis-cli -a 123456 shutdown
  43. $CLIEXEC -a 123456 shutdown
  44. while [ -x /proc/${PID} ]
  45. do
  46. echo "Waiting for Redis to shutdown ..."
  47. sleep 1
  48. done
  49. echo "Redis stopped"
  50. fi
  51. ;;
  52. status)
  53. if [ ! -f $PIDFILE ]
  54. then
  55. echo 'Redis is not running'
  56. else
  57. PID=$(cat $PIDFILE)
  58. if [ ! -x /proc/${PID} ]
  59. then
  60. echo 'Redis is not running'
  61. else
  62. echo "Redis is running ($PID)"
  63. fi
  64. fi
  65. ;;
  66. restart)
  67. $0 stop
  68. $0 start
  69. ;;
  70. *)
  71. echo "Please use start, stop, restart or status as first argument"
  72. ;;
  73. esac

设置开机自启动

  1. # 为redis开机启动脚本赋予执行权限
  2. chmod +x /etc/init.d/redis
  3. # 开启redis服务
  4. systemctl start redis
  5. # 查看redis的状态
  6. systemctl status redis
  7. # 停止redis服务
  8. systemctl stop redis
  9. # 查看redis服务的状态
  10. systemctl status redis
  11. # 设置redis开机自启动
  12. systemctl enable redis
  13. # 设置禁止redis开机自启动
  14. systemctl disable redis

ERROR

server.c:5171:176: 错误:‘struct redisServer’没有名为‘maxmemory’的成员

出现这个错误是因为gcc版本过低,升级gcc到9.3后重新编译即可。