1.获取资源路径
redis官网:https://redis.io/download
在这里插入图片描述
获得资源路径:http://download.redis.io/releases/redis-6.0.6.tar.gz
2.服务器安装wget
yum install wget
3.解压
#我选择下载到/usr/local目录下
cd /usr/local
#下载
wget http://download.redis.io/releases/redis-6.0.6.tar.gz
#解压
tar -zxvf redis-6.0.6.tar.gz
4.编译
cd redis-6.0.6
make
make这步大概率会报错
1)如果报错 cc:command not found,是因为缺少gcc
请执行以下操作:
安装gcc
yum install gcc
清理一下
make distclean
再次编译
make
2)如果报错:zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
#include <jemalloc/jemalloc.h>,是编译需要使用jemalloc,安装jemalloc
请执行以下操作:
#使用yum安装得添加一下仓库源。改完仓库源,这时候就可以使用
yum install jemalloc
#下面这种方式是使用jemalloc源码包来进行编译。
(1)下载源码安装包。
wget https:///jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2
(2) 解压jemalloc-5.2.1.tar.bz2
tar -jxvf jemalloc-5.2.1.tar.bz2
如果解压提示错误,解压不成功,执行yum -y install bzip2以支持bzip2
$ cd emalloc-5.2.1
#(在非root用户下--prefix设置路径需要手动创建)
$ ./configure --prefix=/usr/local/jemalloc
$ make && make install
$ cd redis-6.0.6
#(刚才上面--prefix设置路径下的lib)
$ make MALLOC=/usr/local/jemalloc/lib
#(在非root用户下PREFIX设置路径需要手动创建,make install是将可执行命令bin目录移动到制定目录)
$ make install PREFIX=/usr/local/redis
3)如果报错:make[1]: *** [server.o] Error 1 …,是因为gcc默认安装的gcc是4.8.5,版本过低,需要升级
请执行以下操作:
#6以上要求gcc版本号在5以上
#Centos7 gcc版本默认4.8.3,Red Hat 为了软件的稳定和版本支持,yum 上版本也是4.8.3,所以无法使用yum进行软件更新,所以使用scl。
scl软件集(Software Collections),是为了给 RHEL/CentOS 用户提供一种以方便、安全地安装和使用应用程序和运行时环境的多个(而且可能是更新的)版本的方式,同时避免把系统搞乱
yum -y install centos-release-scl scl-utils-build
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
#查看版本号
gcc -v
清理一下
cd /usr/local/redis-6.0.6
make distclean
再次编译
#(刚才上面--prefix设置路径下的lib)
$ make MALLOC=/usr/local/jemalloc/lib
#(在非root用户下PREFIX设置路径需要手动创建,make install是将可执行命令bin目录移动到制定目录)
$ make install PREFIX=/usr/local/redis
5.配置环境变量
从源码中把可执行文件迁出
make install PREFIX=/usr/local/redis
vi /etc/profile
增加以下内容:
export REDIS_HOME=/user/local/redis
export PATH=$PATH:$REDIS_HOME/bin
#使配置生效
source /etc/profile
#验证
echo $PATH
6.创建redis实例
cd /home/test/redis-6.0.6/utils/
./install_server.sh
这步有可能会报错:
This systems seems to use systemd. Please take a look at the provided example service unit files in this directory, and adapt and install t hem. Sorry!
解决方案:
vi ./install_server.sh
注释下面的代码即可
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
然后重新运行 ./install_server.sh
即可。
#查看该实例的运行状态
service redis_6379 status
#停止
service redis_6379 stop
#启动
service redis_6379 start
7.启动redis的三种方式
先切换到redis src目录下
1、直接启动redis
./redis-server
如上图:redis启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。
按 ctrl + c可以关闭窗口。
2、以后台进程方式启动redis
第一步:修改redis.conf文件
将
daemonize no
修改为
daemonize yes
第二步:指定redis.conf文件启动
./redis-server /usr/local/redis-6.0.6/redis.conf
第三步:关闭redis进程
首先使用ps -aux | grep redis查看redis进程
使用kill命令杀死进程
3、设置redis开机自启动
1、在/etc目录下新建redis目录
2、将/usr/local/redis-6.0.6/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf
cp /usr/local/redis-6.0.6/redis.conf /etc/redis/6379.conf
3、将redis的启动脚本复制一份放到/etc/init.d目录下
cp /usr/local/redis-6.0.6/utils/redis_init_script /etc/init.d/redis
4、设置redis开机自启动
先切换到/etc/init.d目录下
然后执行自启命令
chkconfig redis on
现在可以直接已服务的形式启动和关闭redis了
启动:
service redis start
关闭:
service redisd stop
外网访问设置:
1.确认配置文件/etc/redis/6379.conf
bind的ip是否正确,一般想要外网能访问,需要填写为0.0.0.0,表示监听任何ip
注意任何人都能访问,一定要开启密码 requirepass 你的密码
2.确认protected-mode 是否为 no
3.修改完配置文件后重启Redis
service redis restart