redis入门简介及linux下安装配置
redis是一种基于键值对 KEY-VALUE 的nosql数据库
redis的值可以是字符串,hash, list列表, set集合, zset有序集合
一. redis特点:
- 速度快
原因: 1. 所有数据存放在内存中
单线程,避免锁竞争
c语言实现 - 键值对的数据结构
- 稳定简单
4.支持多种语言 - 支持持久化,防止数据丢失
- 主从复制
- 高可用,支持cluster
二. redis常见使用场景:
缓存
排行系统
计数(redis支持计数功能)
消息队列(虽然相比其他MQ比较菜,但是一般场景足够满足要求了)
三.版本说明:
redis版本命名采用跟linux类似:
版本号第二位如果是奇数则代表的是非稳定版本
如果是偶数则代表稳定版本
一个大的版本号,说明有非常大的改变
目前在版本列表中可以看到
3版本目前在3.2版本
4版本在4.0
5版本也是5.0
而且时间跨度不是很大,说明redis最近更新非常快
四. redis安装
redis可用通过yum进行安装,但是yum安装版本相对比较旧
yum install redis
另外一种使用源码进行安装, 源码安装也很简单,建议使用该种方式
在官网上选择合适版本进行下载:
http://download.redis.io/releases/
目前最新版本是5.0.4
这里选择3.2.12进行安装
wget http://download.redis.io/releases/redis-3.2.13.tar.gz
tar -zxvf redis-3.2.13.tar.gz
ln -s redis-3.2.13 redis (建立软连接,这个在升级redis的时候可以不用考虑版本的目录影响)
cd redis
make
make 完成后在src目录下会有一堆执行文件:
[root@dev src]# ll | grep -i redis
-rw-rw-r-- 1 root root 2276 Mar 18 12:25 redisassert.h
-rwxr-xr-x 1 root root 2432088 Apr 21 09:20 redis-benchmark
-rw-rw-r-- 1 root root 29559 Mar 18 12:25 redis-benchmark.c
-rw-r--r-- 1 root root 108440 Apr 21 09:20 redis-benchmark.o
-rwxr-xr-x 1 root root 25205 Apr 21 09:20 redis-check-aof
-rw-rw-r-- 1 root root 6328 Mar 18 12:25 redis-check-aof.c
-rw-r--r-- 1 root root 33680 Apr 21 09:20 redis-check-aof.o
-rwxr-xr-x 1 root root 5191997 Apr 21 09:20 redis-check-rdb
-rw-rw-r-- 1 root root 12789 Mar 18 12:25 redis-check-rdb.c
-rw-r--r-- 1 root root 54488 Apr 21 09:20 redis-check-rdb.o
-rwxr-xr-x 1 root root 2585163 Apr 21 09:20 redis-cli
-rw-rw-r-- 1 root root 90746 Mar 18 12:25 redis-cli.c
-rw-r--r-- 1 root root 367104 Apr 21 09:20 redis-cli.o
-rwxr-xr-x 1 root root 5191997 Apr 21 09:20 redis-sentinel
-rwxr-xr-x 1 root root 5191997 Apr 21 09:20 redis-server
-rwxrwxr-x 1 root root 60852 Mar 18 12:25 redis-trib.rb
在src目录下执行,检查版本:
[root@dev src]# ./redis-cli -v
redis-cli 3.2.13
安装成功
其中几个执行文件说明及作用如下:
redis-server – 启动redis服务器
redis-cli – Redis命令客户端
redis-benchmark – Redis测试工具
redis-check-aof – AOF文件修复工具
redis-check-dump – RDB文件检查工具
redis-sentinel – Sentinel服务器
redis启动方法:
redis-server,按照默认配置启动
启动中会提示一些默认参数,如下:
端口: 6379
进程号6809
如果要使用配置文件,可以使用./redis-server /path/to/redis.conf 形式[root@dev src]# ./redis-server
6809:C 21 Apr 09:25:51.847 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.13 (00000000/0) 64 bit
.-
.-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.
-.|’` .-‘| Port: 6379
|-._
. / .-‘ | PID: 6809
-._
-. `-./ .-‘ .-‘
|`-.-._
-..-‘ .-‘.-‘|
|-._
-. .-‘.-‘ | http://redis.io
`-.-._
-..-‘.-‘ .-‘
|-._
-. `-.__.-‘ .-‘.-‘|
| `-.-._ _.-'_.-' |
-. `-.`-._.-‘.-‘ _.-‘`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
6809:M 21 Apr 09:25:51.848 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6809:M 21 Apr 09:25:51.848 # Server started, Redis version 3.2.13
6809:M 21 Apr 09:25:51.848 # 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.
6809:M 21 Apr 09:25:51.848 # 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.
6809:M 21 Apr 09:25:51.848 * The server is now ready to accept connections on port 6379
参数启动:
查看帮助[root@dev src]# ./redis-server -h
Usage: ./redis-server [/path/to/redis.conf] [options]./redis-server - (read config from stdin)
./redis-server -v or --version
./redis-server -h or --help
./redis-server --test-memory <megabytes>
Examples:
./redis-server (run the server with default conf)
./redis-server /etc/redis/6379.conf
./redis-server --port 7777
./redis-server --port 7777 --slaveof 127.0.0.1 8888
./redis-server /etc/myredis.conf --loglevel verbose
Sentinel mode:
./redis-server /etc/sentinel.conf --sentinel
例如:
./redis-server --port 6888
配置文件启动:redis-server configPath(配置文件)
新建一个配置文件/root/redis.confbind 127.0.0.1
daemonize yes
pidfile “/root/redis.pid”
logfile “/var/log/redis/redis.log”./redis-server /root/redis.conf
这里采用的是守护进程启动,所以不会有任何提示
查看进程
[root@dev src]# ps -ef | grep -i redis
root 35758 1 0 09:32 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 35764 2321 0 09:33 pts/0 00:00:00 grep --color=auto -i redis
停止redis:
./redis-cli shutdown
redis 停止最好不要使用kill -9 命令, 就算要使用一般使用kill命令即可
由于退出也没有任何提示, 可以在日志中查看:
[root@dev src]# tail /var/log/redis/redis.log
35758:M 21 Apr 09:32:58.897 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
35758:M 21 Apr 09:32:58.897 # Server started, Redis version 3.2.13
35758:M 21 Apr 09:32:58.897 # 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.
35758:M 21 Apr 09:32:58.897 # 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.
35758:M 21 Apr 09:32:58.897 * DB loaded from disk: 0.000 seconds
35758:M 21 Apr 09:32:58.897 * The server is now ready to accept connections on port 6379
35758:M 21 Apr 09:36:16.623 # User requested shutdown...
35758:M 21 Apr 09:36:16.624 * Removing the pid file.
35758:M 21 Apr 09:36:16.624 # Redis is now ready to exit, bye bye.
检查进程:
[root@dev src]# ps -ef | grep -i redis
root 35770 2321 0 09:36 pts/0 00:00:00 grep --color=auto -i redis
还没有评论,来说两句吧...