准备篇
一、防火墙配置
CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙。
1、关闭firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
systemctl mask firewalld
systemctl stop firewalld
yum remove firewalld
2、安装iptables防火墙
yum install iptables-services #安装
vi /etc/sysconfig/iptables #编辑防火墙配置文件
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq! #保存退出
systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
/usr/libexec/iptables/iptables.init restart #重启防火墙
二、关闭SELINUX
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
安装篇
1、下载安装包
下载地址:https://download.redis.io/releases/redis-5.0.14.tar.gz
上传安装包到服务器/usr/local/src目录
2、安装Redis
服务器需要有编译环境,如果没有需要安装
gcc -v
yum install gcc gcc-c++ #安装gcc
mkdir -p /usr/local/redis_db #创建数据存放目录
mkdir -p /usr/local/redis-5.0.14 #创建安装目录
mkdir -p /usr/local/redis-5.0.14/log #创建日志目录
cd /usr/local/src
tar -zxvf redis-5.0.14.tar.gz
cd redis-5.0.14
make
make install PREFIX=/usr/local/redis-5.0.14
3、配置Redis
cp /usr/local/src/redis-5.0.14/redis.conf /usr/local/redis-5.0.14/redis.conf
vi /usr/local/redis-5.0.14/redis.conf
daemonize yes #以后台daemon方式运行redis
pidfile /usr/local/redis-5.0.14/redis_6379.pid
port 6379
bind 127.0.0.1
timeout 300 #客户端超时设置,单位为秒
loglevel notice #设置日志级别,支持四个级别:debug、verbose、notice、warning
logfile "/usr/local/redis-5.0.14/log/redis.log" #日志记录方式,默认为标准输出,logs不写文件,输出到空设备/deb/null
databases 16 #开启数据库的数量
save 900 1
save 300 10
save 60 10000
rdbcompression yes #启用数据库lzf压缩
dbfilename dump.rdb
dir "/usr/local/redis_db"
requirepass 123456 #设置redis数据库连接密码
maxclients 10000 #同一时间最大客户端连接数,0为无限制
maxmemory 4096MB #设定redis最大使用内存,值要小于物理内存,必须设置
appendonly yes #开启日志记录,相当于MySQL的binlog
appendfilename "appendonly.aof" #日志文件名,注意:不是目录路径
appendfsync everysec #每秒执行同步,还有两个参数always、no一般设置为everysec,相当于MySQL事物日志的写方式
:wq! #保存退出
4、启动redis
4.1手动启动
/usr/local/redis-5.0.14/bin/redis-server /usr/local/redis-5.0.14/redis.conf
#查看进程
ps -ef|grep redis
#端口测试
telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
#进入控制台
/usr/local/redis-5.0.14/bin/redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123456"
127.0.0.1:6379>
#关闭数据库,需要进入控制台操作
/usr/local/redis-5.0.14/bin/redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> shutdown
not connected>
4.2 配置脚本启动
vi /usr/local/redis-5.0.14/redis.sh
#!/bin/bash
#应用名称
APP_NAME=redis-5.0.14
#Redis端口
REDISPORT=6379
#Redis安装目录
DIR=/usr/local/redis-5.0.14
#Redis进程文件
PIDFILE=/usr/local/redis-5.0.14/redis_6379.pid
#Redis配置文件
CONF="/usr/local/redis-5.0.14/redis.conf"
#Redis密码
AUTH='123456'
#使用说明,用来提示输入参数
usage() {
echo "Usage: ./redis.sh [start|stop|restart|status]"
exit 1
}
#检查程序是否在运行
is_exist() {
if [ -f $PIDFILE ]
then
pid=$(cat $PIDFILE)
else pid=
fi
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
echo "Starting Redis server..."
$DIR/bin/redis-server $CONF
fi
}
#停止方法
stop() {
is_exist
if [ $? -eq "0" ]; then
$DIR/bin/redis-cli -p $REDISPORT -a $AUTH shutdown 2>/dev/null
sleep 2
while [ -x $PIDFILE ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is not running."
fi
}
#重启
restart() {
stop
sleep 2
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
:wq! #保存退出
#添加脚本执行权限
chmod +x /usr/local/redis-5.0.14/redis.sh
#执行脚本
sh /usr/local/redis-5.0.14/redis.sh start|stop|restart|status
4.3、设置开机启动Redis
cp /usr/local/src/redis-5.0.14/utils/redis_init_script /etc/init.d/redis
vi /etc/init.d/redis #添加修改
AUTH=123456
REDISPORT=6379
EXEC=/usr/local/redis-5.0.14/bin/redis-server
CLIEXEC=/usr/local/redis-5.0.14/bin/redis-cli
PIDFILE=/usr/local/redis-5.0.14/redis_${REDISPORT}.pid
CONF="/usr/local/redis-5.0.14/redis.conf"
$CLIEXEC -p $REDISPORT -a $AUTH shutdown 2>/dev/null
:wq! #保存退出
#设置开机启动
chkconfig redis on
#启动|关闭
service redis start|stop
标签:5.0,14,redis,Redis,usr,local From: https://www.cnblogs.com/gaoyanbing/p/16716763.html