1、安装依赖
由于 redis 是用 C 语言开发,安装之前必先确认是否安装 gcc 环境(gcc -v),如果没有安装,执行以下命令进行安装
yum install -y gcc
2、下载并解压安装包
或从官网下载
wget http://download.redis.io/releases/redis-5.0.3.tar.gztar -zxvf redis-5.0.3.tar.gz
3、编译安装
cd redis-5.0.3makemake install PREFIX=/usr/local/redis
4、启动服务
1)前台启动
cd /usr/local/redis/bin/./redis-server
2)后台启动
a.方法一(推荐)
从 redis 的源码目录中复制 redis.conf 到 redis 的安装目录
cp /usr/local/redis-5.0.3/redis.conf /usr/local/redis/bin/
修改 redis.conf 文件,把 daemonize no 改为 daemonize yes
vi redis.conf
执行一下命令配置Redis配置之后Redis能随系统启动
./utils/install_server.sh
安装过程
./install_server.sh #执行这个文件他会默认生成一些redis的配置文件######如果执行这脚本的时候发生下面的错误###########################[root@localhost utils]# ./install_server.sh Welcome to the redis service installerThis script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] Selecting default: 6379Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.confPlease select the redis log file name [/var/log/redis_6379.log] Selected default - /var/log/redis_6379.logPlease select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379Please select the redis executable path [/usr/local/bin/redis-server] s#^port [0-9]{4}$#port 6379#;s#^logfile .+$#logfile /var/log/redis_6379.log#;s#^dir .+$#dir /var/lib/redis/6379#;s#^pidfile .+$#pidfile /var/run/redis_6379.pid#;s#^daemonize no$#daemonize yes#;Copied /tmp/6379.conf => /etc/init.d/redis_6379Installing service..../install_server.sh: line 178: update-rc.d: command not found exists, process is already running or crashed
解析
Welcome to the redis service installerThis script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] #redis 配置的端口Selecting default: 6379 #默认为6379Please select the redis config file name [/etc/redis/6379.conf] #redis默认文件存放路径Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] #redis 日志文件路径Selected default - /var/log/redis_6379.logPlease select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379Please select the redis executable path [/opt/bin/redis-server]
注意,你安装的时候,有可能出现的bug
解决方案一:
按错误提示,我们对/etc/init.d/redis_6379进行修改,只有要“\n”删除并且输入回车,修改完毕后,保存
解决方案二:
#######因为install_server.sh这个安装脚本有bug#####################################vim install_server.sh##第一个点###修改前-if [ !`which chkconfig` ] ; then ###修改后+if [ ! `which chkconfig` ] ; then #combine the header and the template (which is actually a static footer) echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE" else##第二个点###修改前-if [ !`which chkconfig` ] ; then ###修改后+if [ ! `which chkconfig` ] ; then #if we're not a chkconfig box assume we're able to use update-rc.d update-rc.d redis_$REDIS_PORT defaults && echo "Success!" else###修改完成后,再次执行,应该没问题的了
开启关闭服务
/etc/init.d/redis_6379 start #或 service redis_6379 start 开启/etc/init.d/redis_6379 stop # 或 service redis_6379 stop 关闭
b. 方法二
启动命令
./redis-server redis.conf
查看服务运行进程
ps -ef|grep redis
停止redis(关闭指定端口):
./redis-cli -p6380 shutdown
查找进程号并杀死进程:
ps -aux|grep redis kill -9 2530
5、设置开机启动
1)运行脚本(适用于安装方法二)
添加开机启动服务
vi /etc/systemd/system/redis.service
[Unit]Description=redis-serverAfter=network.target[Service]Type=forkingExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.confPrivateTmp=true[Install]WantedBy=multi-user.target
注意:ExecStart配置成自己的路径
设置开机启动
systemctl daemon-reload #重置服务systemctl start redis.servicesystemctl enable redis.service
服务操作命令
systemctl start redis.service #启动redis服务systemctl stop redis.service #停止redis服务systemctl restart redis.service #重新启动服务systemctl status redis.service #查看服务当前状态systemctl enable redis.service #设置开机自启动systemctl disable redis.service #停止开机自启动
附录:
1)基本操作 redis-cli
redis-cli # 连接redis服务
redis 127.0.0.1:6379> info #查看server版本内存使用连接等信息redis 127.0.0.1:6379> client list # 获取客户连接列表redis 127.0.0.1:6379> client kill 127.0.0.1:33441 #终止某个客户端连接redis 127.0.0.1:6379> dbsize #当前保存key的数量redis 127.0.0.1:6379> save #立即保存数据到硬盘redis 127.0.0.1:6379> bgsave #异步保存数据到硬盘redis 127.0.0.1:6379> flushdb #当前库中移除所有keyredis 127.0.0.1:6379> flushall #移除所有key从所有库中redis 127.0.0.1:6379> lastsave #获取上次成功保存到硬盘的unix时间戳redis 127.0.0.1:6379> monitor #实时监测服务器接收到的请求redis 127.0.0.1:6379> slowlog len #查询慢查询日志条数(integer) 3redis 127.0.0.1:6379> slowlog get #返回所有的慢查询日志,最大值取决于slowlog-max-len配置redis 127.0.0.1:6379> slowlog get 2 #打印两条慢查询日志redis 127.0.0.1:6379> slowlog reset #清空慢查询日志信息
2)centos7查看、打开和关闭防火墙
centos7的防火墙改用firewalld,而不再使用iptables了。
sudo systemctl status firewalld #查看 active(running)运行状态sudo systemctl stop firewalld #关闭 inactive(dead) sudo systemctl start firewalld #打开# 上面打开和关闭防火墙都是临时的,重启电脑后又会恢复到默认的状态(我的默认状态是打开)sudo systemctl disable firewalld #永久关闭sudo systemctl enable firewalld #永久开启