一、数据库类型
- 关系数据库管理系统(RDBMS)
- 非关系数据库管理系统(NoSQL)
按照预先设置的组织机构,将数据存储在物理介质上(即:硬盘上)
数据之间可以做无关联操作 (例如: 多表查询,嵌套查询,外键等)
主流的RDBMS软件:MySQL、MariaDB、Oracle、DB2、SQL Server;要存储的数据是有固定格式的(例如:要向银行存现金,需要录入:姓名,年龄,金额,家庭住址等),并且是永久存储的,类似这种对于同一个业务,录入数据的方式一样的采用关系型数据库。
二、NoSQL(NoSQL = Not Only SQL)
意思是“不仅仅是SQL”
泛指非关系型数据库,不需要预先定义数据存储结构,每条记录可以有不同的 数据类型 和 字段个数
NoSQL主流软件:Memcached、Redis、MongoDB、Neo4j、FlockDB
三、Redis介绍
Remote Dictionary Server(远程字段服务器)是一款高性能的(Key/Values)分布式内存数据库
支持数据持久化(定期把内存里数据存储到硬盘)
支持多种数据类型 string、list、hash
支持 master-slave 模式数据备份
中文网站 www.redis.cn
四、部署Redis服务
环境准备,创建template主机,ip地址为192.168.11.10
PS:官网稳定版6的版本,编译的时候一直报错找不到src目录
换成4版本成功的截图:

步骤一:直接wget下载到/root目录下
- [root@template ~]# wget -c http://download.redis.io/releases/redis-4.0.8.tar.gz
复制代码 步骤二:源码编译安装
- # 安装编译环境gcc gcc-c++
- [root@template~]# yum -y install gcc
- # 解压到指定目录,个人习惯
- [root@template ~]# tar xf redis-4.0.8.tar.gz -C /usr/local/
- # 进入目录
- [root@template ~]# cd /usr/local/redis-4.0.8/
- [root@template redis-4.0.8]# ls
- 00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf tests utils
- BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src TLS.md
- # 编译安装
- [root@template redis-4.0.8]# make && make install
- # 测试
- [root@template redis-4.0.8]# redis-
- redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
复制代码 步骤三:初始配置,配置服务运行参数
- [root@template utils]# pwd
- /usr/local/redis-4.0.8/utils
- [root@template utils]# ./install_server.sh 执行源码目录下的初始化脚本
- 端口 6379
- 主配置文件 /etc/redis/6379.conf
- 日志文件 /var/log/redis_6379.log
- 数据库目录 /var/lib/redis/6379
- 服务启动程序 /usr/local/bin/redis-server
- 命令行连接命令 /usr/local/bin/redis-cli
- # 运行初始化脚本,一路回车即可
- 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主配置文件:【/etc/redis/6379.conf】 回车确认
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
选择redis日志文件:【/var/log/redis_6379.log】 回车确认
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
选择redis数据库目录:【/var/lib/redis/6379】 回车确认
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
选择redis启动程序:【/usr/local/bin/redis-server】 回车确认
Please select the redis executable path [/usr/local/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/bin/redis-server
- Cli Executable : /usr/local/bin/redis-cli
复制代码 确认,则选择回车即可;否则按 Ctrl + C 重新配置
- 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! #安装成功
复制代码 步骤四:查看redis的启动端口号
- [root@template utils]# ss -lntup | grep redis
- tcp LISTEN 0 128 127.0.0.1:6379 *:* users:(("redis-server",pid=9043,fd=6))
复制代码 步骤五:管理redis服务,通过脚本的方式停止redis的服务
- [root@template utils]# /etc/init.d/redis_6379 stop
- Stopping ...
- Redis stopped
- [root@template utils]#
- [root@template utils]#
- [root@template utils]# ss -lntup | grep redis
复制代码 步骤六:重新开启redis服务
- [root@template utils]# /etc/init.d/redis_6379 start
- Starting Redis server...
- [root@template utils]# ss -lntup | grep redis
- tcp LISTEN 0 128 127.0.0.1:6379 *:* users:(("redis-server",pid=10977,fd=6))
复制代码 步骤七:连接服务:redis-cli 默认连接本机的redis服务
- [root@template utils]# redis-cli
- # 使用ping命令,查看连接是否成功,结果是PONG,则代表redis正常连接
- 127.0.0.1:6379> ping
- PONG
- 127.0.0.1:6379>
- 127.0.0.1:6379> keys * #使用keys命令,查看当前库下的所有数据
- 127.0.0.1:6379> set school tarena #使用set命令,存入数据,school:tarena
- 127.0.0.1:6379> get school #使用get命令,从内存中,取出变量"school"对应的值
- 127.0.0.1:6379> keys * #使用keys命令,查看当前库下的所有数据
- 127.0.0.1:6379> exit #断开redis连接
复制代码 五、基本操作命令
PS:可以借助官方文档:https://docs.redis.com/六、配置文件解析
1、查看redis配置文件信息,修改配置时,在对应的模块下写即可
- [root@template ~]# vim /etc/redis/6379.conf
- .........
- ############################# INCLUDES ##############################
- .........
- ############################# MODULES ##############################
- .........
- ############################# NETWORK ##############################
- .........
- ############################# GENERAL ##############################
复制代码 2、数据单位,不区分字母大小写
- [root@template ~]# vim /etc/redis/6379.conf
- # 1k => 1000 bytes
- # 1kb => 1024 bytes
- # 1m => 1000000 bytes
- # 1mb => 1024*1024 bytes
- # 1g => 1000000000 bytes
- # 1gb => 1024*1024*1024 bytes
复制代码 3、查看redis常用配置信息
守护进程:指进程会一直存在,等待用户访问(耗资源,客户端访问速度快)
非守护进程:当服务运行后,如果一段时间内没有用户访问,服务会进入到休眠状态;当有用户访问时,服务会被唤醒,供用户去访问(节省资源,客户端访问速度慢一些)- 70 bind 127.0.0.1 #指定客户访问的IP地址,这里只允许本机访问
- 93 port 6379 #指定redis的访问端口
- 137 daemonize yes #以守护进程方式运行(进程一直存在,等待用户访问)
- 172 logfile /var/log/redis_6379.log #记录redis运行的启动和运行过程中的信息
- 187 databases 16 #数据库个数,默认16个,可以修改
- 264 dir /var/lib/redis/6379 #定义数据库目录
- 533 # maxclients 10000 #客户端同时访问redis的并发数量,默认10000
复制代码 4、内存管理,查看内存清除策略
- .........
- 562 # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
- 563 # is reached. You can select among five behaviors:
- 564 #
- 565 # volatile-lru -> 向redis中存入数据时,数据已满,则会在设置了TTL过期时间的变量中选择,删除最近最少使用的key,用于存放新的key;
-
- 566 # allkeys-lru -> 向redis中存入数据时,数据已满,则会在所有的变量中选择,删除最近最少使用的key,用于存放新的key;
- 567 # volatile-lfu -> 向redis中存入数据时,数据已满,则会在设置了TTL过期时间的变量中选择,删除使用频率最少的key,用于存放新的key;
- 568 # allkeys-lfu -> 向redis中存入数据时,数据已满,则会在所有的变量中选择,删除使用频率最少的key,用于存放新的key;
- 569 # volatile-random -> 向redis中存入数据时,数据已满,则会在设置了TTL过期时间的变量中选择,随机删除key,用于存放新的key;
-
- 570 # allkeys-random -> 向redis中存入数据时,数据已满,则会在所有的变量中选择,随机删除key,用于存放新的key;
-
- 571 # volatile-ttl -> 向redis中存入数据时,数据已满,删除最近过期的key;
- 572 # noeviction -> 向redis中存入数据时,数据已满,显示报错提示;
复制代码 5、内存优化设置,
从物理内存中划分多少内存给redis使用,这里没有指定,则代表将本机的所有物理内存交给redis去使用- 560 # maxmemory <bytes>
-
- #maxmemory-policy 定义当内存空间不足时,删除已存储数据的方式,策略为 noeviction,即,即使内存使用完了,也不删除已存储的数据
- 591 # maxmemory-policy noeviction
-
- #当使用lru,lfu,ttl 策略时,需要指定key模板的个数
- 602 # maxmemory-samples 5
复制代码 6、修改redis运行参数,修改密码,IP地址和端口号
- [root@template ~]# /etc/init.d/redis_6379 stop #停掉redis的服务
- [root@template ~]# vim /etc/redis/6379.conf
- 70 bind 192.168.4.50 #如果想让其他主机访问本机,修改监听地址为本机网卡地址
- 93 port 6350 #修改端口号为6350
- 501 requirepass 123456 #取消注释,修改用户连接redis的密码为123456
- 启动redis的服务
- [root@template ~]# /etc/init.d/redis_6379 start
- 查看redis服务的端口号
- [root@template ~]# ss -ntulp | grep redis
复制代码 七、连接redis服务
方法一:登录redis以后,输入连接密码
-h 指定要连接的主机,-p(小写) 指定连接端口号- [root@template ~]# redis-cli -h 192.168.4.50 -p 6350
- 192.168.4.50:6350> auth 123456 #auth 后跟上连接密码,否则无法正常使用
- 192.168.4.50:6350> ping
- 192.168.4.50:6350> exit
复制代码 方法二:连接redis时,输入连接密码
-a 指定连接密码- [root@template ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
- 192.168.4.50:6350> ping
- 192.168.4.50:6350> exit
复制代码 八、停止redis服务
当修改了redis服务的IP地址,密码和端口号以后,则无法通过脚本来停止redis服务
脚本停止服务针对的是redis服务默认的IP地址,密码和端口号
连接上redis, 使用shutdown来停止服务- [root@template ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 shutdown
- [root@template ~]# ss -ntulp | grep redis
- [root@template ~]# /etc/init.d/redis_6379 start #启动服务
复制代码 出处:http://www.cnblogs.com/sre-chan/-------------------------------------------
个性签名:今天做了别人不想做的事,明天你就做得到别人做不到的事,尝试你都不敢,你拿什么赢!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |