CentOS 7.x 编译安装redis-6.0.16
准备篇
一、防火墙配置
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-6.0.16.tar.gz
上传安装包到服务器/usr/local/src目录
2、升级gcc版本
redis源码编译需要先gcc的版本大于等于5
gcc -v 查看gcc版本
yum -y install gcc gcc-c++ #安装gcc
yum -y install tcl
#升级到gcc版本,redis-6.0.x版本需要gcc 5.3以上,CentOS 7.x默认是gcc version 4.8.5
yum -y install centos-release-scl
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 #永久生效
3、安装Redis
mkdir -p /usr/local/redis_db #创建数据存放目录
cd /usr/local/src
tar -zxvf redis-6.0.16.tar.gz
cd redis-6.0.16
make
make install PREFIX=/usr/local/redis-6.0.16
4、配置Redis
cp /usr/local/src/redis-6.0.16/redis.conf /usr/local/redis-6.0.16/redis.conf
vi /usr/local/redis-6.0.16/redis.conf
daemonize yes #以后台daemon方式运行redis
pidfile /usr/local/redis-6.0.16/redis_6379.pid
port 6379
bind 127.0.0.1
timeout 300 #客户端超时设置,单位为秒
loglevel notice #设置日志级别,支持四个级别:debug、verbose、notice、warning
logfile "/usr/local/redis-6.0.16/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! #保存退出
5、启动redis
5.1手动启动
/usr/local/redis-6.0.16/bin/redis-server /usr/local/redis-6.0.16/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-6.0.16/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-6.0.16/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>
5.2 配置脚本启动
vi /usr/local/redis-6.0.16/redis.sh
#!/bin/bash
#应用名称
APP_NAME=redis-6.0.16
#Redis端口
REDISPORT=6379
#Redis安装目录
DIR=/usr/local/redis-6.0.16
#Redis进程文件
PIDFILE=/usr/local/redis-6.0.16/redis_6379.pid
#Redis配置文件
CONF="/usr/local/redis-6.0.16/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-6.0.16/redis.sh
#执行脚本
sh /usr/local/redis-6.0.16/redis.sh start|stop|restart|status
5.3、设置开机启动Redis
cp /usr/local/src/redis-6.0.16/utils/redis_init_script /etc/init.d/redis
vi /etc/init.d/redis #添加修改service redis start|stop
AUTH=123456
REDISPORT=6379
EXEC=/usr/local/redis-6.0.16/bin/redis-server
CLIEXEC=/usr/local/redis-6.0.16/bin/redis-cli
PIDFILE=/usr/local/redis-6.0.16/redis_${REDISPORT}.pid
CONF="/usr/local/redis-6.0.16/redis.conf"
$CLIEXEC -p $REDISPORT -a $AUTH shutdown 2>/dev/null
:wq! #保存退出
#设置开机启动
chkconfig redis on
#启动|关闭
service redis start|stop
至此,CentOS 7.x 安装redis-6.0.16完成。
转载来源:https://www.osyunwei.com/archives/12794.html
文章目录
关闭