Redis数据库
Redis数据库
数据库类型: 关系型数据库 MySQL, mariadb-server, oracle 非关系型数据库 NoSQL 以键值key-value对方式的存储数据 redis, mongodb, memcached, memcachedDB NoSQL(Not Only SQL)数据库 高性能、高并发 NoSQL数据库类型: 键值(key-value)存储数据库, redis 面向列存储数据库 HBase 面向文档存储数据库 MongoDB 图形数据库 开源数据库 配置简单 支持内存存储数据 支持持久化存储数据 datafile 数据文件 *.rdb aof(append only file)文件,日志文件 支持多实例部署 支持主从复制、集群 支持事务transaction 以key-value键值对的方式存储 value类型:string字符串、list列表、set集合、sorted_set有序集合、hash值
Redis数据库安装及启动
[01 ~]# yum install -y gcc
[ ~]# tar xf redis-3.2.9.tar.gz
[ ~]# cd redis-3.2.9/
[ redis-3.2.9]# make
[ redis-3.2.9]# make PREFIX=/usr/local/redis install
[ ~]# ls /usr/local/redis/
bin
命令解释:
1、redis-server 启动redis服务
2、redis-cli redis客户端工具
3、redis-benchmark redis性能测试
4、redis-check-rdb 检测rdb文件
5、redis-check-aof 检测aof日志文件
[ ~]# mkdir /usr/local/redis/conf
[ ~]# cd redis-3.2.9/
[ redis-3.2.9]# cp redis.conf /usr/local/redis/conf/
[ ~]# /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf &
[ ~]# ss -antp | grep :6379
LISTEN 0 128 127.0.0.1:6379 : users:(("redis-server",pid=9612,fd=4))
[ ~]#
启动服务时内核参数说明:
1、/proc/sys/net/core/somaxconn
定义了系统中每一个端口最大的监听队列的长度,默认为128. 对于大多数需要处理高并发连接的应用来说,该值太小,建议修改为1024或者更大
2、overcommit_memory
规定决定是否接受超大内存请求的条件。这个参数有三个可能的值: 0 — 默认设置。内核执行启发式内存过量使用处理,方法是估算可用内存量,并拒绝明显无效的请求。遗憾的是因为内存是使用启发式而非准确算法计算进行部署,这个设置有时可能会造成系统中的可用内存超载。 1 — 内核执行无内存过量使用处理。使用这个设置会增大内存超载的可能性,但也可以增强大量使用内存任务的性能。 2 — 内存拒绝等于或者大于总可用 swap 大小以及 overcommit_ratio 指定的物理 RAM 比例的内存请求。如果您希望减小内存过度使用的风险,这个设置就是最好的。
3、/sys/kernel/mm/transparent_hugepage/enabled
禁止使用huge page
[ redis]# tail -n 1 /etc/profile
export PATH=$PATH:/usr/local/redis/bin
[ redis]#
[ redis]# source /etc/profile
测试redis存储、读取数据:
[ ~]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set name martin
OK
127.0.0.1:6379> get name
"martin"
127.0.0.1:6379>
Redis服务的启动与关闭
- 启动服务
[ ~]# redis-server /usr/local/redis/conf/redis.conf &
编辑rc.local文件,实现开机自启动
- 关闭服务
[ ~]# redis-cli shutdown
Redis常规操作:
[ ~]# redis-cli
127.0.0.1:6379> set age 16
OK
127.0.0.1:6379> get age
"16"
127.0.0.1:6379> set name martin
OK
127.0.0.1:6379> get name
"martin"
127.0.0.1:6379>
127.0.0.1:6379> keys *
- "age"
- "name"
127.0.0.1:6379> del age
(integer) 1
127.0.0.1:6379> get age
(nil)
127.0.0.1:6379>
字符串string数据类型操作
127.0.0.1:6379> set name martin
OK
127.0.0.1:6379> get name
"martin"
127.0.0.1:6379> mset name tom age 15 sex male
OK
127.0.0.1:6379> mget name age sex
- "tom"
- "15"
- "male"
127.0.0.1:6379>
127.0.0.1:6379> strlen name
(integer) 3
127.0.0.1:6379>
127.0.0.1:6379> set count 1
OK
127.0.0.1:6379> incr count
(integer) 2
127.0.0.1:6379> incr count
(integer) 3
127.0.0.1:6379> incr count
(integer) 4
127.0.0.1:6379> get count
"4"
127.0.0.1:6379> decr count
(integer) 3
127.0.0.1:6379> get count
"3"
127.0.0.1:6379>
127.0.0.1:6379> set count 1
OK
127.0.0.1:6379> incrby count 3
(integer) 4
127.0.0.1:6379> get count
"4"
127.0.0.1:6379> decrby count 2
(integer) 2
127.0.0.1:6379> get count
"2"
127.0.0.1:6379>
Redis安全设置:
- 设置密码
[ ~]# grep "^require" /usr/local/redis/conf/redis.conf
requirepass redhat
重启redis
使用密码连接操作redis
方法1)
[ ~]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set name jerry
(error) NOAUTH Authentication required.
127.0.0.1:6379>
127.0.0.1:6379> AUTH redhat
OK
127.0.0.1:6379> set name jerry
OK
127.0.0.1:6379> get name
"jerry"
127.0.0.1:6379>
方法2)
[ ~]# redis-cli -a redhat
127.0.0.1:6379> set age 10
OK
127.0.0.1:6379> get age
"10"
127.0.0.1:6379>
2、设置命令别名
[ ~]# grep "^rename" /usr/local/redis/conf/redis.conf
rename-command set uplooking
[ ~]#
[ ~]# redis-cli -a redhat
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> set age 10
(error) ERR unknown command ‘set‘
127.0.0.1:6379>
127.0.0.1:6379> uplooking age 10
OK
127.0.0.1:6379> get age
"10"
127.0.0.1:6379>
redis事务
保证多个操作同时成功执行,在一个事务中,任何一个操作发生error,所有操作都会回滚
192.168.122.102:6379> set count 1
OK
192.168.122.102:6379> multi
OK
192.168.122.102:6379> incr count
QUEUED
192.168.122.102:6379> incr count
QUEUED
192.168.122.102:6379> exec
- (integer) 2
- (integer) 3
192.168.122.102:6379>
192.168.122.102:6379> get count
"3"
192.168.122.102:6379>
192.168.122.102:6379> set number 1
OK
192.168.122.102:6379> MULTI
OK
192.168.122.102:6379> incr number
QUEUED
192.168.122.102:6379> incre number
(error) ERR unknown command ‘incre‘
192.168.122.102:6379>
192.168.122.102:6379> exec
(error) EXECABORT Transaction discarded because of previous errors.
192.168.122.102:6379>
192.168.122.102:6379>
192.168.122.102:6379>
192.168.122.102:6379> get number
"1"
192.168.122.102:6379>
redis配置文件 redis.conf
1、指定额外的配置文件
include /path/to/local.conf
include /path/to/other.conf
2、设置redis监听的IP地址
bind 192.168.122.101
3、设置redis进程接收的TCP队列长度
tcp-backlog 511
同时修改内核参数/proc/sys/net/core/somaxconn
4、设置客户端空闲连接超时时间, 0表示不设置
timeout 0
5、设置redis启动时作为后台服务运行
daemonize yes
6、指定pid文件
pidfile /var/run/redis_6379.pid
7、设置日志级别及日志文件
loglevel notice
logfile ""
8、设置数据库的数量 0--15
databases 16
9、设置redis保存数据的频率
save 900 1
save 300 10
save 60 10000
redis保存数据的方式: save, 会阻塞(block)客户端连接请求 bgsave
10、当bgsave发生error时,停止写操作
stop-writes-on-bgsave-error yes
11、启用压缩
rdbcompression yes
12、设置rdb数据文件名称及存放目录
dbfilename dump.rdb
dir /usr/local/redis/data
13、设置并发连接
maxclients 10000
14、设置redis最大使用的内存
maxmemory //建议为物理内存的3/5
15、启用redis aof功能
appendonly yes
appendfilename "appendonly.aof"
一、为PHP添加redis模块
- 安装lamp平台
[ ~]# yum install -y httpd mariadb-server php php-gd php-mysql gd php-devel
[ ~]# systemctl start httpd
[ ~]# systemctl enable httpd
[ ~]# systemctl start mariadb
[ ~]# systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
- 安装php模块
[ ~]# tar xf redis-3.1.2.tgz
[ ~]# cd redis-3.1.2/
[ redis-3.1.2]# /usr/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[ redis-3.1.2]#
[ redis-3.1.2]# ./configure --enable-redis --with-php-config=/usr/bin/php-config
[ redis-3.1.2]# make && make install
安装完成后,提示redis.so模块的安装路径 Installing shared extensions: /usr/lib64/php/modules/
- 编辑php配置文件,加载redis.so模块
vim /etc/php.ini
extension = /usr/lib64/php/modules/redis.so
[ redis-3.1.2]# systemctl restart httpd
- 编辑php页面,测试php连接redis的效果
[ html]# cat /var/www/html/b.php
<?php $redis = new Redis(); $redis -> connect("192.168.122.101",6379); $redis -> auth("redhat"); $redis -> set("name","Tome"); $var = $redis -> get("name"); echo "$var";?>二、为Python添加redis模块
[ ~]# tar xf redis-2.9.1.tar.gz
[ ~]# cd redis-2.9.1/
[ redis-2.9.1]# python setup.py install
通过python测试连接redis
import redis
r = redis.Redis(host=‘192.168.122.101‘,port=6379,password=‘redhat‘)
r.set(‘foo‘,‘bar‘)
Truevalue = r.get(‘foo‘)
print value
bar
三、配置redis多实例
Instance01 6380端口 Instance02 6381端口
- 为每个实例创建目录
[ ~]# mkdir /app/{6380,6381} -p
[ ~]# mkdir /app/6380/data
[ ~]# mkdir /app/6381/data
- 为每个实例准备配置文件
[ ~]# cp /usr/local/redis/conf/redis.conf /app/6380/
[ ~]# cp /usr/local/redis/conf/redis.conf /app/6381/
- 编辑实例6380的配置文件
[ ~]# vim /app/6380/redis.conf
bind 192.168.122.101
daemonize yes
port 6380
pidfile /var/run/redis_6380.pid
dir /app/redis/instance/6380/data
同样的方法编辑另一个实例的配置文件
- 分别启动实例
[ ~]# redis-server /app/6380/redis.conf
[ ~]# redis-server /app/6381/redis.conf
[ ~]# ss -antp | grep redis
LISTEN 0 128 192.168.122.101:6380 : users:(("redis-server",pid=23687,fd=4))
LISTEN 0 128 192.168.122.101:6381 : users:(("redis-server",pid=23695,fd=4))
[ ~]#
- 分别连接两个实例,进行数据读写验证
[ ~]# redis-cli -a redhat -h 192.168.122.101 -p 6380
192.168.122.101:6380> set name martin
OK
192.168.122.101:6380> get name
"martin"
192.168.122.101:6380> quit
[ ~]# redis-cli -a redhat -h 192.168.122.101 -p 6381
192.168.122.101:6381> set age 18
OK
192.168.122.101:6381> get age
"18"
192.168.122.101:6381> get name
(nil)
192.168.122.101:6381>
Redis主从复制
1、采用异步的方式进行数据同步 3、支持级联复制, 某一个slave服务器可以作为另外slave服务器的主 4、进行主从复制时,master/slave不会阻塞客户端访问请求 5、自redis 2.6版本后,slave服务器默认工作在只读模式
配置案例
环境描述:
node02 192.168.122.102 Master
node03 192.168.122.103 Slave
分别在两个节点安装redis,并启动
编辑slave服务器的配置文件
[ redis-3.2.9]# vim /usr/local/redis/conf/redis.conf
slaveof 192.168.122.102 6379 masterauth redhat
- 启动slave
[ ~]# redis-server /usr/local/redis/conf/redis.conf &
- 查看复制状态
192.168.122.103:6379> info replication
Replication
role:slave
master_host:192.168.122.102
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:127
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.122.103:6379>
- 测试复制
[ ~]# redis-cli -h 192.168.122.102
192.168.122.102:6379>
192.168.122.102:6379> AUTH redhat
OK
192.168.122.102:6379>
192.168.122.102:6379> set name martin
OK
192.168.122.102:6379> get name
"martin"
192.168.122.102:6379>
192.168.122.103:6379> get name
"martin"
192.168.122.103:6379>
Redis集群cluster
Redis 集群是一个提供在多个Redis间节点间共享数据 Redis集群通过分区shared来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下继续处理命令. Redis 集群的优势: 自动分割数据到不同的节点上。 整个集群的部分节点失败或者不可达的情况下能够继续处理命令。
Redis集群的数据分片
Redis 集群使用哈希槽slot进行数据分片 Redis集群有16384个哈希槽,每个key通过CRC16校验后对16384取模来决定放置哪个槽.集群的每个节点负责一部分hash槽,举个例子,比如当前集群有3个节点,那么: 节点 A 包含 0 到 5500号哈希槽. 节点 B 包含5501 到 11000 号哈希槽. 节点 C 包含11001 到 16384号哈希槽. 数据究竟存放到哪个槽上? 数据做hash运算除以16384除余
部署redis集群
环境描述: 192.168.122.101 node01 3个集群模式的实例 7000, 7001, 7002 192.168.122.102 node02 3个集群模式的实例 7003, 7004, 7005
1、分别在两个节点安装redis
2、为了方便使用创建集群命令,将其复制/usr/local/bin
[ src]# cp redis-trib.rb /usr/local/bin/
[ ~]# cd redis-4.0.2/src/
[ src]# cp redis-trib.rb /usr/local/bin/
3、在node01上创建3个集群模式的实例
[ ~]# mkdir -p /app/redis/cluster/{7000,7001,7002}
[ ~]# cd redis-4.0.2/
[ redis-4.0.2]# cp redis.conf /app/redis/cluster/7000/
[ redis-4.0.2]# cp redis.conf /app/redis/cluster/7001/
[ redis-4.0.2]# cp redis.conf /app/redis/cluster/7002/
[ ~]# vim /app/redis/cluster/7000/redis.conf
bind 192.168.122.101 port 7000 daemonize yes pidfile /var/run/redis_7000.pid cluster-enabled yes cluster-config-file nodes-7000.conf cluster-node-timeout 5000 appendonly yes
其他实例参照以上修改
4、分别启动实例
[ ~]# redis-server /app/redis/cluster/7000/redis.conf
[ ~]# redis-server /app/redis/cluster/7001/redis.conf
[ ~]# redis-server /app/redis/cluster/7002/redis.conf
[ ~]# redis-server /app/redis/cluster/7003/redis.conf
[ ~]# redis-server /app/redis/cluster/7004/redis.conf
[ ~]# redis-server /app/redis/cluster/7005/redis.conf
[ ~]# netstat -antp | grep redis
tcp 0 0 192.168.122.101:17000 0.0.0.0:* LISTEN 14045/redis-server
tcp 0 0 192.168.122.101:17001 0.0.0.0:* LISTEN 14050/redis-server
tcp 0 0 192.168.122.101:17002 0.0.0.0:* LISTEN 14055/redis-server
tcp 0 0 192.168.122.101:7000 0.0.0.0:* LISTEN 14045/redis-server
tcp 0 0 192.168.122.101:7001 0.0.0.0:* LISTEN 14050/redis-server
tcp 0 0 192.168.122.101:7002 0.0.0.0:* LISTEN 14055/redis-server
[ ~]#
[ ~]# ps aux | grep redis
root 14045 0.1 0.4 147308 9608 ? Ssl 14:24 0:00 redis-server 192.168.122.101:7000 [cluster]
root 14050 0.1 0.4 147308 9608 ? Ssl 14:24 0:00 redis-server 192.168.122.101:7001 [cluster]
root 14055 0.1 0.4 147308 9608 ? Ssl 14:24 0:00 redis-server 192.168.122.101:7002 [cluster]
5、创建集群
创建集群时需要使用redis-trib.rb工具,该工具是由ruby语言编写,因此需要机器有ruby环境
[ ~]# yum install -y ruby ruby-devel rubygems rpm-build
[ ~]# gem install redis
Fetching: redis-4.0.0.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
[ ~]#
[ ~]# rpm -q ruby
ruby-2.0.0.648-30.el7.x86_64
redis需要ruby 2.2版本以上的支持,使用工具rvm(ruby version manager)安装ruby
以下是使用rvm安装ruby的过程
- 安装rvm
[ ~]# gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
[ ~]# curl -L get.rvm.io | bash -s stable
[ ~]# source /usr/local/rvm/scripts/rvm
- 查看可用的ruby版本
[ ~]# rvm list known
- 安装2.3.4版本的ruby
[ ~]# rvm install 2.3.4
- 配置系统使用ruby 2.3.4
[ ~]# rvm use 2.3.4
Using /usr/local/rvm/gems/ruby-2.3.4
[ ~]# rvm use 2.3.4 --default
Using /usr/local/rvm/gems/ruby-2.3.4
[ ~]#
[ ~]# rvm remove 2.0.0
ruby-2.0.0-p648 - #already gone
Using /usr/local/rvm/gems/ruby-2.3.4
[ ~]#
[ ~]# ruby --version
ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux]
[ ~]#
- 再次安装redis gem
[ ~]# gem install redis
创建集群
[ ~]# redis-trib.rb create --replicas 1 \
192.168.122.101:7000
192.168.122.101:7001
192.168.122.101:7002
192.168.122.102:7003
192.168.122.102:7004
192.168.122.102:7005 \
集群创建完毕后会有以下提示
[OK] All 16384 slots covered.
选项:
--replicas 1 为集群里的每个master节点配备一个slave节点
6、向集群当中储存数据
[ ~]# redis-cli -h 192.168.122.101 -p 7000 -c
192.168.122.101:7000> set a 10
-> Redirected to slot [15495] located at 192.168.122.101:7001
OK
192.168.122.101:7001> set b 10
-> Redirected to slot [3300] located at 192.168.122.101:7000
OK
192.168.122.101:7000>
[ ~]# redis-cli -h 192.168.122.102 -p 7003 -c
192.168.122.102:7003>
192.168.122.102:7003>
192.168.122.102:7003> get a
-> Redirected to slot [15495] located at 192.168.122.101:7001
"10"
192.168.122.101:7001> get b
-> Redirected to slot [3300] located at 192.168.122.101:7000
"10"
192.168.122.101:7000> set name martin
-> Redirected to slot [5798] located at 192.168.122.102:7003
OK
192.168.122.102:7003> set age 10
-> Redirected to slot [741] located at 192.168.122.101:7000
OK
192.168.122.101:7000>