linux安装部署redis
介绍
本文主要介绍linux安装部署redis的一般步骤,并介绍了一个部署redis的脚本。完整版脚本在文末给出。
脚本应该不能完全执行,脚本执行的对象,是我编译、整理后的redis安装包,不是官网直接下载的,可以参考一下步骤对官网下载安装包进行编译、整理,然后在参考我的脚本进行部署。
下载安装包
安装包安装需要,下载对应版本。
解压
把redis解压部署到指定目录下
function r_untar(){ if [ -d $redis_home ];then echo 'redis exists' else mkdir -p $redis_deploy_path tar -xf $redis_name -C $redis_deploy_path fi }
编译
切换到redis根目录下 /xx/xx/xx/redis-4.0.11/ ,执行 make test 命令。
安装
编译结束后,切换到 /xx/xx/xx/redis-4.0.11/src 目录下,执行 make install 命令。
整理文件
编译后的redis目录,产生很多可执行命令,还有很多.o结尾的中间文件,可以执行一下步骤进行整理。
1. 删除中间文件
在src目录下,执行 rm -rf *.o ,就可以删除中间文件。
2. 整理文件
创建 /xx/xx/xx/redis-4.0.11/bin 目录,用于放置常用的可执行文件,创建 /xx/xx/xx/redis-4.0.11/conf 用于放置配置文件,
复制文件到创建的目录:
mv /xx/xx/xx/redis-4.0.11/redis.conf /xx/xx/xx/redis-4.0.11/conf cd /xx/xx/xx/redis-4.0.11/src mv mkreleasdhdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server /xx/xx/xx/redis-4.0.11/bin
以上步骤参考文章:Linux下Redis的安装和部署
修改配置文件
1. 修改redis的绑定ip,默认是127.0.0.1,只能本机访问Redis。添加上Redis部署的服务器ip后,其他服务器才能根据Redis服务器ip访问到Redis。bind的值,就是外界访问Redis的ip。
2. 指定访问密码
3. 修改Redis快照存储目录,默认是“./”,会放置到启动命令的目录,可以修改为Redis的根目录下,默认不开启AOF.
function r_modify_conf(){ conf_path="$redis_deploy_path/redis-4.0.11/etc/redis.conf" localip=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:") if [ -d ${redis_home} ];then #Modify listening ip # sed -i "s@bind 127.0.0.1@bind 127.0.0.1 ${localip}@g" $conf_path sed -i "/^bind 127.0.0.1/cbind 127.0.0.1 ${localip}" $conf_path # sed -i "500 arequirepass Hik12345" $conf_path sed -i "s@# requirepass foobared@requirepass Hik12345@g" $conf_path touch $redis_logfile sed -i "s@logfile \"\"@logfile $redis_logfile@g" $conf_path #sed -i "s@^dir@dir $redis_home@g" $conf_path sed -i "/^dir/cdir $redis_home" $conf_path else echo 'Redis not installed' fi }
启动
function r_start(){ $redis_deploy_path/redis-4.0.11/bin/redis-server $redis_deploy_path/redis-4.0.11/etc/redis.conf }
开放端口
#Open 6379 port function r_open_port(){ port='6379' isopen=$(firewall-cmd --query-port=${port}/tcp) if [ 'no' == $isopen ];then firewall-cmd --add-port=${port}/tcp --permanent>/dev/null firewall-cmd --reload>/dev/null else echo "port ${port} already opened" fi }