Centos7 安装 redis 6
准备
升级gcc
1、因为redis-5以上要使用gcc5.3以上版本进行编译,但是centos7默认安装的gcc版本是4.8.5,所以需要升级gcc版本:
如果gcc版本过低,编译的时候会出错:
#安装gccyum -y install gcc tcl# 查看gcc版本是否在5.3以上,centos7.6默认安装4.8.5gcc -v
2、升级gcc的操作
# 升级到gcc 9.3:yum -y install centos-release-sclyum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutilsscl enable devtoolset-9 bash# 需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。# 如果要长期使用gcc 9.3的话:echo -e "\nsource /opt/rh/devtoolset-9/enable" >>/etc/profile# 查看gcc版本# [root@node01 ~]# gcc -vUsing built-in specs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapperTarget: x86_64-redhat-linuxConfigured 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-linuxThread model: posixgcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)
安装redis
下载安装
# 下载redis安装包wget http://download.redis.io/releases/redis-6.2.5.tar.gz# 解压安装包[root@node01 modules]# tar -zxvf redis-6.2.5.tar.gz# 进入安装包[root@node01 modules]# cd redis-6.2.5/# 编译和安装make && make test && make install
安装出错的话执行:
# 编译出错时,清出编译生成的文件make distclean# 卸载make uninstall
配置
- 将服务端脚本和客户端脚本移动到新建的文件夹下面 
```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”
2. 配置redis.conf 文件```nginx# 设置可以访问redis服务的IPbind 0.0.0.0# 设置redis的访问端口port 6379# 设置访问redis的密码requirepass 123456# 设置 redis-server 以守护线程方式启动daemonize yes
查看 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]
4. 开启自启动```shell# 创建开机自启动脚本的文件vim /etc/init.d/redis
编写开机自启动的脚本, 注意替换自己的redis的启动命令的位置以及配置文件的位置。
#!/bin/sh# Configurations injected by install_server below....EXEC=/home/redis-6.2.6/bin/redis-serverCLIEXEC=/home/redis-6.2.6/bin/redis-cliPIDFILE=/var/run/redis_6379.pidCONF="/home/redis-6.2.6/bin/redis.conf"REDISPORT="6379"################ SysV Init Information# chkconfig: - 58 74# description: redis_6379 is the redis daemon.### BEGIN INIT INFO# Provides: redis_6379# Required-Start: $network $local_fs $remote_fs# Required-Stop: $network $local_fs $remote_fs# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Should-Start: $syslog $named# Should-Stop: $syslog $named# Short-Description: start and stop redis_6379# Description: Redis daemon### END INIT INFOcase "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."# 启动 Redis$EXEC $CONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."# $CLIEXEC -p $REDISPORT shutdown# 带有密码的redis关闭方式: redis-cli -a 123456 shutdown$CLIEXEC -a 123456 shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;status)if [ ! -f $PIDFILE ]thenecho 'Redis is not running'elsePID=$(cat $PIDFILE)if [ ! -x /proc/${PID} ]thenecho 'Redis is not running'elseecho "Redis is running ($PID)"fifi;;restart)$0 stop$0 start;;*)echo "Please use start, stop, restart or status as first argument";;esac
设置开机自启动
# 为redis开机启动脚本赋予执行权限chmod +x /etc/init.d/redis# 开启redis服务systemctl start redis# 查看redis的状态systemctl status redis# 停止redis服务systemctl stop redis# 查看redis服务的状态systemctl status redis# 设置redis开机自启动systemctl enable redis# 设置禁止redis开机自启动systemctl disable redis
ERROR
server.c:5171:176: 错误:‘struct redisServer’没有名为‘maxmemory’的成员
出现这个错误是因为gcc版本过低,升级gcc到9.3后重新编译即可。
