(一)Redis安装、配置、启动

基本

内存 + 持久化数据库
数据类型:字符串,列表,集合,散列表,有序集合
应用场景:发布/订阅,队列,主从复制,动态扩容,脚本操作,持久化
优点:没有Schame约束,数据结构变更容易,抗压能力强,性能极高
缺点:没有索引,没有外键,缺少int/date等基本数据类型,多条件查询需要通过内联集合

Redis安装

下载:

# 下载
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
# 解压
tar -zxvf redis-5.0.4.tar.gz -C /usr/local
# 编译
cd /usr/local/redis-5.0.4 
make MALLOC=libc && make install

如果不能编译,尝试安装依赖的库:yum -y install gcc automake autoconf libtool make

三种启动方式

1、直接启动

./src/redis-server    # 关闭会话后进程退出
[root@localhost src]# ./redis-server 
24725:C 21 Mar 2019 10:24:52.670 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24725:C 21 Mar 2019 10:24:52.671 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=24725, just started
24725:C 21 Mar 2019 10:24:52.671 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
24725:M 21 Mar 2019 10:24:52.672 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 24725
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

24725:M 21 Mar 2019 10:24:52.675 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
24725:M 21 Mar 2019 10:24:52.675 # Server initialized
24725:M 21 Mar 2019 10:24:52.675 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
24725:M 21 Mar 2019 10:24:52.676 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
24725:M 21 Mar 2019 10:24:52.676 * Ready to accept connections

2、守护进程启动

redis.conf

vim redis.conf
# daemonize 修改为 yes
daemonize yes
./src/redis-server redis.conf     
[root@localhost redis-5.0.4]# vim redis.conf 
[root@localhost redis-5.0.4]# ./src/redis-server redis.conf 
24731:C 21 Mar 2019 10:27:11.770 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24731:C 21 Mar 2019 10:27:11.770 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=24731, just started
24731:C 21 Mar 2019 10:27:11.770 # Configuration loaded

3、配置脚本开机启动

创建目录

mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf
#  开机启动脚本通常以d结尾
cp utils/redis_init_script /etc/init.d/redisd
cd /etc/init.d

编辑脚本

  • 添加代码 [chkconfig: 2345 90 10]
#!/bin/sh
#
# chkconfig: 2345 90 10
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
  • 执行命令
chkconfig redisd on
# 启动redis
service redisd start

出现的问题

/var/run/redis_6379.pid exists, process is already running or crashed

# 解决方法,删除对应pid
rm -rf /var/run/redis_6379.pid  

/etc/init.d/redisd: line 29: /usr/local/bin/redis-server: No such file or directory

# 解决1:将redis-server和redis-cli拷贝到/usr/local/bin目录下
# 解决2:修改脚本,指定命令的路径
EXEC=/usr/local/redis-5.0.4/src/redis-server
CLIEXEC=/usr/local/redis-5.0.4/src/redis-cli

测试启动

[root@localhost soft]# redis-cli
127.0.0.1:6379> ping
PONG

redis的通用命令

  • keys pattern:模式匹配key(慎用慎用慎用)
  • dbsize:key总数
  • exists key:key是否存在
  • del key [key...]:删除key
  • expire key seconds:设置过期时间
  • ttl key:查看还剩多长时间过期(-1为永久不过期;-2为已删除)
  • persist key:取消过期时间
  • type key:返回key的类型
127.0.0.1:6379> keys *
1) "name2"
2) "name"
127.0.0.1:6379> set age 12
OK
127.0.0.1:6379> keys nam*
1) "name2"
2) "name"

127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> del key a b c d e
(integer) 4

127.0.0.1:6379> exists name2
(integer) 1
127.0.0.1:6379> del name2
(integer) 1
127.0.0.1:6379> exists name2
(integer) 0

127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 5
127.0.0.1:6379> ttl name
(integer) 2
127.0.0.1:6379> ttl name
(integer) -2

127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 8
127.0.0.1:6379> persist name
(integer) 1
127.0.0.1:6379> ttl name
(integer) -1

127.0.0.1:6379> type name
string

转载于:https://www.cnblogs.com/zuier/p/10647573.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值