Linux—redis的部署
一.redis简介
1.redis是一个开源的使用ANSI C语言编写、遵守BSD协议,支持网络,可基于内存可持久化的日志型、key-Value的数据库,并提供多种语言的API。
2.redis是完全开源免费的,是一个高性能的key-value数据库
3.redis的特点:
1)redis支持数据的持久化,可以将内存中的数据库保存在磁盘中,重启的时候可以再次加载进行使用
2)redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储
3)redis支持数据的备份,即master-slave模式的数据备份
4.redis优点:
1)redis的读写速度极高
2)redis的所有操作都具有原子性
3)支持很多二进制数据类型
二.redis的部署
1.准备安装包:
yum install gcc gcc-c++ make -y
2.准备redis安装包,并解压:
tar xzvf redis-5.0.7.tar.gz -C /opt
3.编译安装:
[root@localhost ~]# cd /opt/redis-5.0.7/
[root@localhost redis-5.0.7]# make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis install
4.开启服务,并设置:
[root@localhost redis-5.0.7]# cd /opt/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
5.查看端口:
[root@localhost utils]# netstat -natp | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 8145/redis-server 1
6.创建软链接
ln -s /usr/local/redis/bin/* /usr/local/bin/
7.修改配置文件,添加本地地址:
vim /etc/redis/6379.conf
bind 127.0.0.1 192.168.88.218
8.重启服务:
[root@localhost utils]# service redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
9.关闭防火墙:
[root@localhost utils]# systemctl stop firewalld.service
[root@localhost utils]# setenforce 0
10.登录redis数据库
[root@localhost utils]# redis-cli -h 192.168.88.218 -p 6379
192.168.88.218:6379>
三.redis数据库低一些基本操作命令:
1.help @list 查看列表
2.help set 查看set信息:
192.168.88.218:6379> help set
SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
3.创建
192.168.88.218:6379> set teacher zhangsan
OK
192.168.88.218:6379> set tea red
OK
4.keys * 查看所有
192.168.88.218:6379> keys *
1) "teacher"
2) "tea"
5.条件查找
192.168.88.218:6379> keys t??
1) "tea"
6.查看健的值:
192.168.88.218:6379> get tea
"red"
7.查看健是否存在
192.168.88.218:6379> EXISTS tea
(integer) 1
192.168.88.218:6379> EXISTS teas
(integer) 0
8.删除:
192.168.88.218:6379> del teacher
(integer) 1
192.168.88.218:6379> keys *
1) "tea"
9.查看健的类型:
192.168.88.218:6379> type tea
string
10.重命名:
192.168.88.218:6379> rename tea t1
OK
192.168.88.218:6379> keys *
1) "t1"
192.168.88.218:6379> get t1
"red"
11.切换库:
192.168.88.218:6379> SELECT 10
OK
192.168.88.218:6379[10]> keys *
(empty list or set)
12.移库:
192.168.88.218:6379[10]> SELECT 0
OK
192.168.88.218:6379> MOVE t1 10
(integer) 1
192.168.88.218:6379> SELECT 10
OK
192.168.88.218:6379[10]> keys *
1) "t1"
13.清空所有:
192.168.88.218:6379[10]> FLUSHDB
OK
192.168.88.218:6379[10]> keys *
(empty list or set)
四.压测redis的get和set
1.查看get和set
-h:指定服务器主机名
-p:指定服务端口
-c:并发连接数
-n:指定请求数
[root@localhost utils]# redis-benchmark -h 192.168.88.218 -p 6379 -c 100 -n 100000
====== SET ======
100000 requests completed in 0.68 seconds
100 parallel clients
3 bytes payload
keep alive: 1
99.39% <= 1 milliseconds
99.60% <= 2 milliseconds
99.80% <= 5 milliseconds
99.90% <= 13 milliseconds
100.00% <= 13 milliseconds
147058.83 requests per second
====== GET ======
100000 requests completed in 0.66 seconds
100 parallel clients
3 bytes payload
keep alive: 1
99.49% <= 1 milliseconds
100.00% <= 1 milliseconds
152439.02 requests per second
2.查看字节大小
-q:强制退出
-d:一字节的形式指定SET/GET值得数据大小
[root@localhost utils]# redis-benchmark -h 192.168.88.218 -p 6379 -q -d 100
PING_INLINE: 157232.70 requests per second
PING_BULK: 161812.31 requests per second
SET: 154559.50 requests per second
GET: 154798.75 requests per second
INCR: 154559.50 requests per second
LPUSH: 134770.89 requests per second
RPUSH: 130718.95 requests per second
LPOP: 142857.14 requests per second
RPOP: 149925.03 requests per second
SADD: 153374.23 requests per second
HSET: 150150.14 requests per second
SPOP: 154320.98 requests per second
LPUSH (needed to benchmark LRANGE): 133868.81 requests per second
LRANGE_100 (first 100 elements): 48520.13 requests per second
LRANGE_300 (first 300 elements): 15427.34 requests per second
LRANGE_500 (first 450 elements): 9593.25 requests per second
LRANGE_600 (first 600 elements): 6980.32 requests per second
MSET (10 keys): 110132.16 requests per second
五.配置文件优化
vim /etc/redis/6379.conf
1.RDB持久化
1.RDB核心规则配置 save <指定时间间隔> <执行指定次数更新操作>
save 900 1
save 300 10
save 60 10000
2.当RDB持久化出现错误后,是否依然进行继续进行工作
stop-writes-on-bgsave-error yes
3.压缩功能
rdbcompression yes
4.是否校验rdb文件
rdbchecksum yes
5.数据目录,数据库会写入这个目录
dir /var/lib/redis/6379
2.AOF持久化
1.Redis 默认不开启,开启它
appendonly yes
2.指定本地数据库文件名
appendfilename "appendonly.aof"
3.aof持久化策略的配置,总是开启
appendfsync always
#appendfsync everysec
#appendfsync no
4.重写功能
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
5.aof文件可能在尾部是不完整的,当redis启动的时候,aof文件的数据被载入内存
aof-load-truncated yes
6.加载redis时,可以识别AOF文件以“redis”开头
aof-use-rdb-preamble yes