1、介绍
Redis是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。 Redis是一个key-value存储系统。 和Memcached缓存类似,Redis支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、hash(哈希类型)。并且支持在服务器端计算集合的并、交和补集(difference)等,还支持多种排序功能。Redis也被看成是一个数据结构服务器。
Redis支持主从同步,数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制,由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录,同步对读取操作的可扩展性和数据冗余很有帮助。
2、安装
2.1 yum安装
yum install redis -y
#配置文件
/etc/redis.conf
#启动命令
systemctl start redis
2.2 源码安装
[root@localhost src]# wget http://download.redis.io/releases/redis-5.0.5.tar.gz
[root@localhost src]# tar xf redis-5.0.5.tar.gz
[root@localhost src]# cd redis-5.0.5
#编译安装
[root@localhost redis-5.0.5]# make PREFIX=/usr/local/redis install
2.2.1 启动
前台运行,这种方式当退出shell时redis服务也会同时被关闭
[root@localhost redis-5.0.5]# /usr/local/redis/bin/redis-server /usr/src/redis-5.0.5/redis.conf
后端启动
[root@localhost redis-5.0.5]# nohup /usr/local/redis/bin/redis-server /usr/src/redis-5.0.5/redis.conf &
上面的启动方式较麻烦,建议配置脚本进行启动
[root@localhost redis-5.0.5]# cp redis.conf /usr/local/redis/6379.conf
[root@localhost redis-5.0.5]# /usr/src/redis-5.0.5/utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
#配置redis的端口,可以默认为6379,直接回车
Please select the redis port for this instance: [6379]
Selecting default: 6379
#配置redis的配置文件,放到redis的安装目录,加上文件名:/usr/local/redis/6379.conf
Please select the redis config file name [/etc/redis/6379.conf] /usr/local/redis/6379.conf
#配置redis的日志,放到redis的安装目录,加上文件名: /usr/local/redis/redis_6379.log
Please select the redis log file name [/var/log/redis_6379.log] /usr/local/redis/redis_6379.log
#配置redis的数据目录,也放在安装目录,加上端口号区分: /usr/local/redis/6379
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis/6379
#选择redis的启动程序,加上文件名:/usr/local/redis/bin/redis-server
Please select the redis executable path [] /usr/local/redis/bin/redis-server
Selected config:
Port : 6379
Config file : /usr/local/redis/6379.conf
Log file : /usr/local/redis/redis_6379.log
Data dir : /usr/local/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!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
启动
/etc/init.d/redis_6379 start
3、客户端连接
#本地连接:
[root@localhost redis]# /usr/local/redis/bin/redis-cli
#远程连接:
[root@localhost redis]# /usr/local/redis/bin/redis-cli -h host -p port -a password
127.0.0.1:6379> AUTH 123456
AUTH password 验证密码是否正确,password为服务器设置得requirepass,config set requirepass "123456"
PING 查看服务是否运行
QUIT 关闭当前连接
SELECT index 切换到指定的数据库,redis默认16个数据库,0-15