CentOS7下安装Redis6.0.3
简介
Redis,全称 Remote Dictionary Server(远程字典服务器) ,全开源基于C语言开发,是高性能的(key/value)分布式内存数据库,基于内存运行并支持持久化的NoSQL数据库,是当前最热门的NoSQL数据库之一,也被人们称为数据结构服务器。
在线测试:http://try.redis.io/
使用文档:http://doc.redisfans.com/
特点
1、Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
2、Redis不仅仅支持简单的key - value类型的数据,同时还提供list、set、zset、hash等数据结构的存储。
3、Redis支持数据的备份,即master - slave模式的数据备份。
环境准备
用gcc -v 命令查看当前版本号
gcc -v
CentOS7默认安装的是4.8.5,而redis6.0只支持5.3以上版本,这里将gcc升级到9
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
#scl命令启用只是临时的切换,新开的会话默认还是原gcc版本。
scl enable devtoolset-9 bash
#如果要长期使用gcc 9.1的话执行下面的命令即可:
echo -e "\nsource /opt/rh/devtoolset-9/enable" >>/etc/profile
编译安装
#解压
tar -zxvf redis-6.0.3.tar.gz
#编译
cd redis-6.0.3/
make
make install PREFIX=/opt/app/redis6
#如果编译出错之后再编译可以先执行命令删除之前的编译文件
make distclean
编译完了可以执行命令测试:
#测试需要依赖tcl
yum install tcl -y
#测试
make test
配置启动
进入到安装的目录/opt/app/redis6下 将源码中的redis.conf文件 复制到redis6的bin目录下并修改配置:
#创建data文件夹保存数据
mkdir /opt/app/redis6/data
#赋值redis.conf到指定文件
cp /home/wyk/redis-6.0.3/redis.conf /opt/app/redis6/bin/
修改redis.conf
vim /opt/app/redis6/bin/redis.conf
修改以下几个参数:
#daemonize no 改为yes,开启后台运行,默认是前台运行
daemonize yes
#把这一行注释,监听所有IP
#bind 127.0.0.1
#protected-mode yes 如果改为no,则是关闭保护模式,这种模式下不能配置系统服务,建议还是开启
protected-mode yes
#requirpass,保护模式开启的时候要配置密码或者bind ip
requirepass 123456
#修改本参数,指定数据目录
dir /opt/app/redis6/data
#修改本参数,指定日志目录
logfile /opt/app/redis6/redis_6379.log
执行下面的命令后台启动redis:
cd /opt/app/redis6/bin
./redis-server redis.conf
开机自启:
注册服务 vim /lib/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/opt/app/redis6/bin/redis-server /opt/app/redis6/bin/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
使用systemctl命令:
# 重载服务
systemctl daemon-reload
# 开机自启
systemctl enable redis
# 启动
systemctl start redis
# 重启
systemctl restart redis
# 停止
systemctl stop redis
# 查看状态
systemctl status redis
# 关闭开机启动
systemctl disable redis
使用Redis Desktop Manager连接Redis
1.首先使用命令查看iptable是否为空
vi /etc/sysconfig/iptables
打开文件情况:
系统配置文件中没有这个文件,安装防火墙服务:
yum install iptables-services
修改iptables文件的信息:
vi /etc/sysconfig/iptables
复制一下22的端口的规则,修改成6379端口
启动iptables: systemctl enable iptables
打开iptables:使用systemctl start iptables
然后去修改redis配置,注释bing ,把保护模式关闭
#把这一行注释,监听所有IP
#bind 127.0.0.1
#关闭保护模式
protected-mode yes 改为 no
配置防火墙,嫌麻烦直接关闭即可:
#查看防火墙状态
systemctl status firewalld
#关闭防火墙
service firewalld stop
#开启防火墙
service firewalld start
#单独开6379端口
firewall-cmd --permanent --add-port=6379
tcpfirewall-cmd --reload
redis常用的命令:
1). 查看redis进程:ps -ef|grep redis 和lsof –i :6379
2). 查看6379端口情况:ps -ef | grep 6379
3). 查看redis版本:redis-server –v
4). 查看redis客户端版本:redis-cli –v
5). 关闭redis服务:pkill redis-server
6). 关闭redis客户端:redis-cli shutdown
7). 启动redis服务:redis-server /uer/local/redis/redis/etc/redis.conf
8). 启动redis客户端:redis-cli –p 6379