redis基本操作脚本

环境:redis已经下载和make好了,datadir是/data1,basedir是/data1/redis-4.0.6

     可直接通过中控机远程操作多台,将basedir和脚本scp过去就可以

 1 #!/bin/bash
 2 
 3 #author:hushi
 4 #date:2017-12-8
 5 
 6 log_file=/tmp/redis_install.log
 7 datadir=/data1
 8 redis_datadir=/usr/local/redis-4.0.6.tgz
 9 
10 function helpfunc()
11 {
12     echo "Please check your parameter"
13     echo
14     echo "-p        specify the port of redis to be installed"
15     echo "Example:
16             bash redis_install.sh -p 9000"
17 }
18 
19 if  [ $# -lt 1 ]
20 then
21     helpfunc && exit 1
22 else
23     while getopts "p:" Option
24     do
25         case $Option in
26            p) port=$OPTARG;;
27            *) helpfunc; exit 1; ;;
28         esac
29     done
30 fi
31 
32 function before_install_check()
33 {
34     if [ -z "$port" ]; then
35         helpfunc && exit 1
36     fi
37     port=`echo $port | egrep "^[0-9]{4}$"`
38     if [ -z "$port" ]; then
39         helpfunc && exit 1   
40     fi
41     [ -e ${datadir} ] || mkdir ${datadir}
42     [ -e "${datadir}/redis-4.0.6" ] || /bin/cp ${redis_datadir} ${datadir}
43     
44     if ! echo $PATH|grep redis &>>${log_file};then
45         sed -i "$a export PATH=/data1/redis/redis-${version}/src:$PATH" ~/.bashrc && . ~/.bashrc
46     fi
47     
48     if netstat -antlp|grep ${port} &>>${log_file} ;then
49         echo -e "       \e[43;31m port ${port} is be used,please check and input again\e[0m" && exit 1
50     fi
51     
52 
53 }
54 
55 
56 function install()
57 {
58     cd ${datadir} && mkdir redis${port}
59     cp ${datadir}/redis-4.0.6/redis.conf  redis${port}/redis${port}.conf
60 
61     sed -i "s/6379/${port}/g" redis${port}/redis${port}.conf
62 
63  }
64 
65 
66 function main()
67 {
68     before_install_check
69     install
70 #    after_install_check
71 }
72 
73 main

 

#!/bin/bash

#author:hushi
#date:2017-12-12

log_file=/tmp/redis_replication.log
datadir=/data1
basedir=/data1/redis-4.0.6

function helpfunc()
{
    echo "Please check your parameter"
    echo
    echo "-m        specify the port of redis master "
    echo "-s        specify the port of redis slave  "
    echo "Example:
            bash redis_replication.sh  -m 9000 -s 9001"
}


if  [ $# -lt 1 ]
then
    helpfunc && exit 1
else
    while getopts "m:s:" Option
    do
        case $Option in
            m) master_port=$OPTARG;;
            s) slave_port=$OPTARG;;
            *) helpfunc; exit 1; ;;
        esac
    done
fi

function before_replication_check()
{ 
    if [ ! -e "${datadir}/redis${master_port}" ] || [ ! -e "${datadir}/redis${slave_port}"  ]   &>>${log_file};then
        echo -e "      \e[43;31m master port or slave port is not be initialized,please check error log ${log_file} \e[0m" && exit 1 
    fi

    if ! netstat -antlp|grep ${master_port} &>>${log_file} ;then
        echo -e "       \e[43;31m master port ${master_port} is not be started up ,please check and input again\e[0m" && exit 1
    fi
    if ! netstat -antlp|grep ${slave_port} &>>${log_file} ;then
        echo -e "       \e[43;31m slave port ${slave_port} is not be started up ,please check and input again\e[0m" && exit 1
    fi

}

function replication()
{
    sed -i '/^appendonly/ s/no/yes/'  ${datadir}/redis${slave_port}/redis${slave_port}.conf
    sed -i '/^maxmemory/ s/8gb/70gb/' ${datadir}/redis${slave_port}/redis${slave_port}.conf

    if   ${datadir}/redis-4.0.6/src/redis-cli -p ${slave_port} slaveof 127.0.0.1 ${master_port} &>>${log_file}
    then
        echo -e  "      \e[42;30m Replication is established succeed \e[0m"
        sed -i "/^port/a slaveof                127.0.0.1 ${master_port}" ${datadir}/redis${slave_port}/redis${slave_port}.conf
    else
        echo -e  "      \e[43;31m Replicaion is establieshed failed,please check error log  ${log_file}\e[0m"
    fi
}

function after_replication_check()
{
   la 
        
}


function main()
{
    before_replication_check    
    replication
#    after_replication_check
}

main

 

 1 #!/bin/bash
 2 
 3 #author:hushi
 4 #date:2017-12-11
 5 
 6 log_file=/tmp/maintain.log
 7 com_dir=/data1/redis-4.0.6/src
 8 
 9 
10 function usage()
11 {
12     echo "Please check your parameter"
13     echo
14     echo -e "-T        specify the \e[31m type \e[0m of redis to be operate,\e[35m 0 is stop redis, 1 is start redis, 2 is restart, 3 is kill redis\e[0m"
15     echo -e "-P        specify the \e[31m port \e[0m of redis to be operate"
16     echo "Example:
17             bash redis_maintain.sh -T 1 -P 7000 "
18 }
19 
20 if  [ $# -lt 1 ]
21 then
22     usage && exit 1
23 else
24     while getopts "P:T:" Option
25     do
26         case $Option in
27             P) port=$OPTARG;;
28             T) type=$OPTARG;;
29             *) usage; exit 1; ;;
30         esac
31     done
32 fi
33 
34 function start()
35 {
36      ${com_dir}/redis-server /data1/redis${port}/redis${port}.conf  &>>${log_file} && echo -e "\e[32m       redis ${port} port has already start\e[0m"|| echo -e  "\e[31m  redis ${port} port start failed\e[0m"
37 }
38 
39 function stop()
40 {
41     ${com_dir}/redis-cli -p ${port} shutdown  &>>${log_file} && echo -e "\e[32m       redis ${port} port has already stopped\e[0m" || echo -e "\e[31m  redis ${port} port stop failed\e[0m"
42 }
43 
44 function kill()
45 {
46     ps -ef|grep redis|grep ${port}|awk '{print $2}'|xargs kill -9 &>>${log_file} && echo -e "\e[32m        redis ${port} port has already stopped\e[0m" || echo -e "\e[31m  redis ${port} port stop failed\e[0m"
47 }
48 case $type in
49     0) stop;;
50     1) start;;
51     2) stop;start ;;
52     3) kill;;
53     *) echo "   type input fault,please check and input again "exit; ;;
54 esac

 

转载于:https://www.cnblogs.com/hs2021/p/8072994.html

Redis的Lua脚本编写是一种强大的工具,可以在Redis服务器上执行一些复杂的操作,例如原子地执行多个命令、执行事务、执行复杂计算等。 以下是编写Redis Lua脚本的一些基本知识: 1. Lua脚本是一种文本文件,以.lua为扩展名。可以使用任何文本编辑器来创建Lua脚本。 2. Redis的Lua脚本可以包含任意数量的Redis命令,这些命令被称为Redis调用。 3. Redis的Lua脚本可以通过redis.call()和redis.pcall()函数来执行Redis调用。redis.call()函数是同步执行的,而redis.pcall()函数是异步执行的。 4. Redis的Lua脚本可以使用一些特殊的变量来访问Redis服务器的一些信息,例如KEYS和ARGV变量。KEYS变量是一个数组,包含了所有传递给脚本的键名,ARGV变量是一个数组,包含了所有传递给脚本的参数。 下面是一个简单的Redis Lua脚本示例: ``` -- 声明一个变量 local count = redis.call('incr', KEYS[1]) -- 判断变量是否超过了指定的值 if count > tonumber(ARGV[1]) then redis.call('set', KEYS[1], 0) return 1 else return 0 end ``` 这个脚本执行的操作是:自增一个指定的键值,如果自增的结果超过了指定的值,则将键值重置为0,并返回1;否则返回0。 在使用Redis Lua脚本时,需要注意以下几点: 1. Redis的Lua脚本是在服务器端执行的,因此需要将脚本上传到服务器上。 2. Redis的Lua脚本可以使用一些特殊的命令,例如eval和evalsha命令,用于执行脚本。 3. Redis的Lua脚本可以使用一些特殊的参数,例如0和1,用于指定脚本的执行模式。0表示同步执行,1表示异步执行。 4. Redis的Lua脚本可以使用一些特殊的返回值,例如OK、ERR、false、true等,用于指示脚本的执行结果。 总之,Redis的Lua脚本编写是一种非常有用的技术,可以在Redis服务器上执行一些复杂的操作。如果你想深入了解Redis的Lua脚本编写,可以参考Redis官方文档和Lua官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值