liunx CentOs下安装redis并加入开机自启动
1.官网下载redis安装包
2. 将安装包上传到服务器
将下载到的 redis-5.0.5.tar.gz 压缩包上传到 /usr/local/redis 没有redis这个文件夹,就自己新建一个。
然后进行解压
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# tar -zxvf redis-5.0.5.tar.gz
解压后的文件目录
3.安装gc环境
由于redis是由c语言编写的,它的运行c环境,因此需要安装gcc。
[root@localhost redis]# yum install gcc-c++
已加载插件:fastestmirror
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock error was
12: Timeout on http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
* base: mirrors.huaweicloud.com
* extras: mirrors.aliyun.com
* updates: mirrors.cn99.com
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): extras/7/x86_64/primary_db | 205 kB 00:00:00
(2/2): updates/7/x86_64/primary_db | 6.4 MB 00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 gcc-c++.x86_64.0.4.8.5-36.el7_6.2 将被 安装
--> 正在处理依赖关系 libstdc++-devel = 4.8.5-36.el7_6.2,它被软件包 gcc-c++-4.8.5-36.el7_6.2.x86_64 需要
--> 正在处理依赖关系 gcc = 4.8.5-36.el7_6.2,它被软件包 gcc-c++-4.8.5-36.el7_6.2.x86_64 需要
--> 正在处理依赖关系 libmpfr.so.4()(64bit),它被软件包 gcc-c++-4.8.5-36.el7_6.2.x86_64 需要
--> 正在处理依赖关系 libmpc.so.3()(64bit),它被软件包 gcc-c++-4.8.5-36.el7_6.2.x86_64 需要
--> 正在检查事务
---> 软件包 gcc.x86_64.0.4.8.5-36.el7_6.2 将被 安装
--> 正在处理依赖关系 cpp = 4.8.5-36.el7_6.2,它被软件包 gcc-4.8.5-36.el7_6.2.x86_64 需要
--> 正在处理依赖关系 glibc-devel >= 2.2.90-12,它被软件包 gcc-4.8.5-36.el7_6.2.x86_64 需要
---> 软件包 libmpc.x86_64.0.1.0.1-3.el7 将被 安装
---> 软件包 libstdc++-devel.x86_64.0.4.8.5-36.el7_6.2 将被 安装
---> 软件包 mpfr.x86_64.0.3.1.1-4.el7 将被 安装
--> 正在检查事务
---> 软件包 cpp.x86_64.0.4.8.5-36.el7_6.2 将被 安装
---> 软件包 glibc-devel.x86_64.0.2.17-260.el7_6.5 将被 安装
--> 正在处理依赖关系 glibc-headers = 2.17-260.el7_6.5,它被软件包 glibc-devel-2.17-260.el7_6.5.x86_64 需要
--> 正在处理依赖关系 glibc-headers,它被软件包 glibc-devel-2.17-260.el7_6.5.x86_64 需要
--> 正在检查事务
---> 软件包 glibc-headers.x86_64.0.2.17-260.el7_6.5 将被 安装
--> 正在处理依赖关系 kernel-headers >= 2.2.1,它被软件包 glibc-headers-2.17-260.el7_6.5.x86_64 需要
--> 正在处理依赖关系 kernel-headers,它被软件包 glibc-headers-2.17-260.el7_6.5.x86_64 需要
--> 正在检查事务
---> 软件包 kernel-headers.x86_64.0.3.10.0-957.21.3.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
=====================================================================================================================================================
Package 架构 版本 源 大小
=====================================================================================================================================================
正在安装:
gcc-c++ x86_64 4.8.5-36.el7_6.2 updates 7.2 M
为依赖而安装:
cpp x86_64 4.8.5-36.el7_6.2 updates 5.9 M
gcc x86_64 4.8.5-36.el7_6.2 updates 16 M
glibc-devel x86_64 2.17-260.el7_6.5 updates 1.1 M
glibc-headers x86_64 2.17-260.el7_6.5 updates 683 k
kernel-headers x86_64 3.10.0-957.21.3.el7 updates 8.0 M
libmpc x86_64 1.0.1-3.el7 base 51 k
libstdc++-devel x86_64 4.8.5-36.el7_6.2 updates 1.5 M
mpfr x86_64 3.1.1-4.el7 base 203 k
事务概要
=====================================================================================================================================================
安装 1 软件包 (+8 依赖软件包)
总下载量:41 M
安装大小:84 M
Is this ok [y/d/N]: y
Downloading packages:
(1/9): gcc-4.8.5-36.el7_6.2.x86_64.rpm | 16 MB 00:00:01
(2/9): cpp-4.8.5-36.el7_6.2.x86_64.rpm | 5.9 MB 00:00:01
(3/9): gcc-c++-4.8.5-36.el7_6.2.x86_64.rpm | 7.2 MB 00:00:00
(4/9): glibc-headers-2.17-260.el7_6.5.x86_64.rpm | 683 kB 00:00:00
(5/9): glibc-devel-2.17-260.el7_6.5.x86_64.rpm | 1.1 MB 00:00:00
(6/9): libmpc-1.0.1-3.el7.x86_64.rpm | 51 kB 00:00:00
(7/9): kernel-headers-3.10.0-957.21.3.el7.x86_64.rpm | 8.0 MB 00:00:00
(8/9): libstdc++-devel-4.8.5-36.el7_6.2.x86_64.rpm | 1.5 MB 00:00:00
(9/9): mpfr-3.1.1-4.el7.x86_64.rpm | 203 kB 00:00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------
总计 13 MB/s | 41 MB 00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : mpfr-3.1.1-4.el7.x86_64 1/9
正在安装 : libmpc-1.0.1-3.el7.x86_64 2/9
正在安装 : cpp-4.8.5-36.el7_6.2.x86_64 3/9
正在安装 : kernel-headers-3.10.0-957.21.3.el7.x86_64 4/9
正在安装 : glibc-headers-2.17-260.el7_6.5.x86_64 5/9
正在安装 : glibc-devel-2.17-260.el7_6.5.x86_64 6/9
正在安装 : gcc-4.8.5-36.el7_6.2.x86_64 7/9
正在安装 : libstdc++-devel-4.8.5-36.el7_6.2.x86_64 8/9
正在安装 : gcc-c++-4.8.5-36.el7_6.2.x86_64 9/9
验证中 : gcc-c++-4.8.5-36.el7_6.2.x86_64 1/9
验证中 : mpfr-3.1.1-4.el7.x86_64 2/9
验证中 : glibc-devel-2.17-260.el7_6.5.x86_64 3/9
验证中 : glibc-headers-2.17-260.el7_6.5.x86_64 4/9
验证中 : libmpc-1.0.1-3.el7.x86_64 5/9
验证中 : cpp-4.8.5-36.el7_6.2.x86_64 6/9
验证中 : gcc-4.8.5-36.el7_6.2.x86_64 7/9
验证中 : libstdc++-devel-4.8.5-36.el7_6.2.x86_64 8/9
验证中 : kernel-headers-3.10.0-957.21.3.el7.x86_64 9/9
已安装:
gcc-c++.x86_64 0:4.8.5-36.el7_6.2
作为依赖被安装:
cpp.x86_64 0:4.8.5-36.el7_6.2 gcc.x86_64 0:4.8.5-36.el7_6.2 glibc-devel.x86_64 0:2.17-260.el7_6.5
glibc-headers.x86_64 0:2.17-260.el7_6.5 kernel-headers.x86_64 0:3.10.0-957.21.3.el7 libmpc.x86_64 0:1.0.1-3.el7
libstdc++-devel.x86_64 0:4.8.5-36.el7_6.2 mpfr.x86_64 0:3.1.1-4.el7
完毕!
[root@localhost redis]#
4.进行编译和安装
进入到 redis-5.0.5 目录,进行编译
[root@localhost redis]# cd redis-5.0.5
[root@localhost redis-5.0.5]# make
代码太长了就不贴了,等它跑完。
然后 进入到 src 目录,进行 redis 安装 make install
[root@localhost redis-5.0.5]# cd src
[root@localhost src]# make install
CC Makefile.dep
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
[root@localhost src]#
6.对目录结构进行调整
将一些常用的命令和配置文件统一整理到对应的文件夹
1.在redis-5.0.5 创建bin文件夹
[root@localhost src]# cd ../
[root@localhost redis-5.0.5]# mkdir bin
[root@localhost redis-5.0.5]# ls
00-RELEASENOTES BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src utils
bin CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf tests
[root@localhost redis-5.0.5]#
2.将 src 目录下的mkreleasehdr.sh、redis-benchmark、redis-check-aof、redis-cli、redis-server 移动到 /usr/local/redis/redis-5.0.5/bin/ 目录下
[root@localhost redis-5.0.5]# cd src
[root@localhost src]# mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server /usr/local/redis/redis-5.0.5/bin/
[root@localhost src]#
3.在/usr/local/redis 下创建文件夹 conf,我觉得这样比较方便管理redis配置文件,也有人喜欢建在刚才建的bin文件夹同个目录里,这个看个人吧
[root@localhost src]# cd ../
[root@localhost redis-5.0.5]# cd ../
[root@localhost redis]# mkdir conf
[root@localhost redis]# ls
conf redis-5.0.5 redis-5.0.5.tar.gz
[root@localhost redis]#
4.然后将redis-5.0.5目录下的 redis.conf 移动到刚创建的conf文件里
[root@localhost redis]# cd redis-5.0.5
[root@localhost redis-5.0.5]# mv redis.conf /usr/local/redis/conf
[root@localhost redis-5.0.5]#
5.修改redis.conf 配置文件
- 将文件中的daemonize=no 改成 daemonize=yes(表示在后台运行)
- 将文件中的 bind 127.0.0.1 这一行给注释掉,注释掉后,才能进行远程访问。
- 在配置文件中找到这一行 # requirepass foobared 将注释放开,然后将foobared换成你想要的密码
7.将redis加入开机自启动
1.编写开机自启动脚本
[root@localhost conf]# vi /etc/init.d/redis
将下面的脚本添加到redis文件中
#!/bin/bash
# chkconfig: 2345 10 90
# description: Start and Stop redis
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
# password
RESDISPASSWORD=kingkong
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/conf/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed."
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE exists, process is not running."
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$CLIEXEC -a $RESDISPASSWORD -p $REDISPORT shutdown
while [ -x $PIDFILE ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
2.设置权限
[root@localhost conf]# chmod 755 /etc/init.d/redis
3.启动测试
[root@localhost conf]# /etc/init.d/redis start
Starting Redis server...
16850:C 26 Jun 2019 18:28:03.628 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16850:C 26 Jun 2019 18:28:03.628 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=16850, just started
16850:C 26 Jun 2019 18:28:03.628 # Configuration loaded
Redis is running...
[root@localhost conf]#
看见这样就启动成功了。
4.设置开机自启动
[root@localhost conf]# chkconfig --add redis
[root@localhost conf]# chkconfig redis on
5.重启验证下
[root@localhost conf]# reboot
6.查找redis服务是否开启了
[root@localhost ~]# ps -ef|grep redis
root 5956 1 0 18:36 ? 00:00:00 /usr/local/bin/redis-server *:6379
root 7483 7467 0 18:37 pts/0 00:00:00 grep --color=auto redis
7.开启关闭redis
[root@localhost ~]# service redis stop
Stopping...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped
[root@localhost ~]# service redis start
Starting Redis server...
7514:C 26 Jun 2019 18:39:38.752 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7514:C 26 Jun 2019 18:39:38.752 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7514, just started
7514:C 26 Jun 2019 18:39:38.752 # Configuration loaded
Redis is running...