数据库服务软件分为2类:
关系型数据库服务软件 简称 RDBMS
非关系型数据库服务软件 简称 NoSQL
redis是一个开源的、使用C语言编写的、支持网络交互的、可基于内存也可持久化的Key-Value数据库。
redis 软件介绍
Remote Dictionary Server(远程字典服务器)
是一款高性能的(Key/Values)分布式内存数据库
支持数据持久化(定期把内存里数据存储到硬盘)
支持多种数据类型:字符、列表、散列、集合
支持 master-salve 模式数据备份
中文网站www.redis.cn
特征:
(1) 支持数据的持久化,可以将数据保存在磁盘中,重启之后可以再次加载到内存中使用
(2) 支持多种数据类型,除了KV类型的数据,还支持list、set、hash等数据结构
(3) 支持master-slave模式的数据备份
Redis应用场景
- 热点数据加速查询(主要场景),如热点商品、热点信息等访问量较高的数据
- 即时信息查询,如公交到站信息、在线人数信息等
- 时效性信息控制,如验证码控制、投票控制等
- 分布式数据共享,如分布式集群架构中的session分离消息队列
搭建Redis服务器
要求:在 ip地址 192.168.4.51 部署 redis服务,
具体操作如下:
安装软件
[root@host51 ~]# which gcc || yum -y install gcc 安装编译工具gcc
[root@host51 ~]#tar -xf redis-4.0.8.tar.gz 解压源码
[root@host51 ~]#cd redis-4.0.8/ 进入源码目录
[root@host51 ~]#make 编译
[root@host51 ~]#make install 安装
初始配置
[root@host51 ~]#./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/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 连接服务命令
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... 初始化配置后 会自动启动redis服务 并设置开机运行
Installation successful!
[root@host51 redis-4.0.8]#
查看服务的端口号
[root@host51 redis-4.0.8]# netstat -utnalp | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 4108/redis-server 1
[root@host51 redis-4.0.8]#
查看进程
[root@host51 redis-4.0.8]# ps -C redis-server
PID TTY TIME CMD
4108 ? 00:00:00 redis-server
[root@host51 redis-4.0.8]#
停止redis服务
[root@host51 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
启动redis服务
[root@host51 redis-4.0.8]# /etc/init.d/redis_6379 start
Stopping ...
连接服务存/取数据
说明:默认只能在本机连接redis服务 (只能自己访问自己)
[root@host51 ~]# redis-cli 连接服务 存取数据
127.0.0.1:6379> ping #测试连接是否正常
PONG
127.0.0.1:6379> set school tarena #存储数据 set 变量名 值
OK
127.0.0.1:6379> keys * #查看所有变量名
1) "school"
127.0.0.1:6379> get school #查看变量的值 get 变量名
"tarena"
127.0.0.1:6379> exit #断开连接
[root@host51 ~]#
Redis服务常用管理命令
存储数据 查看数据
删除数据 移动数据
设置数据的存储时间 查看数据类型
清空数据 停止redis服务
127.0.0.1:6379> mset name plj age 80 classs nsd2108
OK
127.0.0.1:6379> keys *
1) "age"
2) "classs"
3) "name"
4) "school"
127.0.0.1:6379> mget name age class
1) "plj"
2) "80"
3) (nil) 说明变量没有定义
127.0.0.1:6379> get classs
"nsd2108"
127.0.0.1:6379>
keys 使用统配符查看变量
* 匹配所有变量名
? 一个字符
127.0.0.1:6379> keys *
1) "age"
2) "classs"
3) "name"
4) "school"
127.0.0.1:6379> keys ???
1) "age"
127.0.0.1:6379> keys a*
1) "age"
127.0.0.1:6379>
127.0.0.1:6379> keys gender
(empty list or set) 说明变量没有定义
127.0.0.1:6379> keys name
1) "name"
127.0.0.1:6379>
select 切换库 默认库编号 0-15
127.0.0.1:6379> select 1 切换到 1号库里
OK
127.0.0.1:6379[1]> keys * 查看数据没有数据
(empty list or set)
127.0.0.1:6379[1]>
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> select 16
(error) ERR DB index is out of range
move 命令 移动变量到其他库里
127.0.0.1:6379> keys *
1) "age"
2) "classs"
3) "name"
4) "school"
127.0.0.1:6379> move age 1
(integer) 1
127.0.0.1:6379> keys *
1) "classs"
2) "name"
3) "school"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "age"
exists 检查变量是否存储 返回值1 变量存储 返回值是0 变量不存在
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> EXISTS name
(integer) 1
127.0.0.1:6379> get name
"plj"
127.0.0.1:6379> set name bob
OK
127.0.0.1:6379> get name
"bob"
127.0.0.1:6379>
EXPIRE 命令设置变量的过期时间 不设置变量永不过期
ttl 检查变量可以在内存里存多久
127.0.0.1:6379> set sex girl
OK
127.0.0.1:6379> ttl sex
(integer) -1 表示永不过期
127.0.0.1:6379> EXPIRE sex 15 设置过期时间15 秒
(integer) 1
127.0.0.1:6379> keys sex
1) "sex"
127.0.0.1:6379> ttl sex
(integer) 8 还剩下8秒时间过期
127.0.0.1:6379> ttl sex
(integer) -2 表示已经过期被删除
127.0.0.1:6379> keys sex
(empty list or set)
127.0.0.1:6379>
type 命令检查变量存储数据的类型
使用set mset命令存储的数据都字符类型。
数据的类型不同 管理的命令也不同
127.0.0.1:6379> set x 99
OK
127.0.0.1:6379> mset y 108
OK
127.0.0.1:6379> type x
string
127.0.0.1:6379> type y
string
127.0.0.1:6379> lpush tea nb wk zzg plj lx
127.0.0.1:6379> type tea 查看变量类型 list 列表类型的数据
list
del 删除内存里的变量
127.0.0.1:6379> keys *
1) "y"
2) "tea"
3) "name"
4) "school"
5) "x"
6) "classs"
127.0.0.1:6379> del tea y school
(integer) 3
127.0.0.1:6379> keys *
1) "name"
2) "x"
3) "classs"
127.0.0.1:6379>
flushdb 删除当前所在库的所有数据
127.0.0.1:6379> keys *
1) "name"
2) "x"
3) "classs"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> keys *
1) "age"
127.0.0.1:6379[1]>
flushall 删除内存里的所有内存里所有数据 (慎用)
save 把内存了数据马上存储到硬盘(存储到数据库目录下的文件 )
127.0.0.1:6379[1]> mset x 1 y 2 z 3 c 4
OK
127.0.0.1:6379[1]> keys *
1) "y"
2) "x"
3) "c"
4) "z"
127.0.0.1:6379[1]> save
OK
127.0.0.1:6379[1]> exit
[root@host51 ~]# ls /var/lib/redis/6379/
dump.rdb
[root@host51 ~]#
shutdown 停止redis 和 执行脚本停止服务效果一样
[root@host51 ~]# redis-cli shutdown
[root@host51 ~]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host51 ~]# redis-cli
127.0.0.1:6379> keys *
配置文件解析 文件里常用配置项说明
通过修改配置项改变redis服务的运行配置,需要重启redis服务才能生效
注意:修改服务使用的ip地址、端口号 、连接密码 三项中的任意一项
都无法再使用脚本停止服务,
解决办法1 使用命令停止服务
解决办法2 修改脚本
修改主机host51 Redis服务
使用的Ip地址192.168.4.51 端口号6351 服务连接密码123456
[root@host51 ~]# vim /etc/redis/6379.conf
70 bind 192.168.4.51 地址
93 port 6351 端口
501 requirepass 123456 密码
:wq
[root@host51 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host51 ~]# /etc/init.d/redis_6379 start
[root@host51 ~]# netstat -utnalp | grep redis-server
tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 4405/redis-server 1
[root@host51 ~]#
默认连接127.0.0.1 地址 和端口 6379 但当前Redis服务的地址和端口都改变了
[root@host51 ~]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Could not connect to Redis at 127.0.0.1:6379: Connection refused 连接失败提示
not connected> exit 退出
[root@host51 ~]#
-h 指定ip地址 -p(小写p) 端口号
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> ping
(error) NOAUTH Authentication required. 没有输入报错
192.168.4.51:6351>
192.168.4.51:6351> keys *
(error) NOAUTH Authentication required. 报错
192.168.4.51:6351>
192.168.4.51:6351> auth 123456 输入密码
OK
192.168.4.51:6351> ping 可以正常访问
PONG
192.168.4.51:6351> keys *
1) "b"
2) "c"
3) "a"
4) "d"
5) "site"
192.168.4.51:6351> exit
连接时直接指定密码
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
192.168.4.51:6351> ping
PONG
192.168.4.51:6351> keys *
1) "b"
2) "c"
3) "a"
4) "d"
5) "site"
192.168.4.51:6351>
使用命令停止服务
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 -a 123456 shutdown
[root@host51 ~]#
[root@host51 ~]# netstat -utnalp | grep redis-server #服务被停止了 查看不到进程了
[root@host51 ~]#
[root@host51 ~]# /etc/init.d/redis_6379 start 启动服务
Starting Redis server...
[root@host51 ~]# netstat -utnalp | grep redis-server 可以查看到进程了
tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 4464/redis-server 1
[root@host51 ~]#
修改脚本使其也可以使用脚本停止服务(启动脚本是使用shell语法编写)
[root@host51 ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.51 -p 6351 -a 123456 shutdown
:wq
#执行脚本停止服务
[root@host51 ~]# /etc/init.d/redis_6379 stop
#查看不到进程说明服务被停止了 脚本改成功了
[root@host51 ~]# netstat -utnalp | grep redis-server
[root@host51 ~]#
LNP+Redis (把网站的数据存储在redis服务器里)
生产环境下会被网站的热点数据存放在内存存储服务器里,这样的好处是可以加快存取数据的速度,能够实现网站访问加速通常网站会把 频繁被访问的数据(热点数据)、数据小的数据 、可再生的数据 存储在内存存储服务器里
部署网站运行环境LNP环境 统一使用host50做网站服务器
具体配置步骤如下:
1) 安装源码的nginx软件
2) 安装php软件
3) 修改nginx服务的配置文件实现动静分离
4) 启动服务
启动nginx服务 和 php-fpm服务
查看服务对应的端口80 和 9000
5) 测试Nginx服务能否解释PHP代码
[root@host50~]# yum -y install gcc pcre-devel zlib-devel
[root@host50~]# tar -xf nginx-1.12.2.tar.gz
[root@host50~]# cd nginx-1.12.2/
[root@host50~]#./configure
[root@host50~]# make
[root@host50~]# make install
[root@host50 nginx-1.12.2]# ls /usr/local/nginx/
conf html logs sbin
[root@host50 nginx-1.12.2]#
[root@host50 ~]# yum -y install php php-fpm php-devel
[root@host50 ~]# vim +65 /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
:wq
[root@host50 ~]# /usr/local/nginx/sbin/nginx -t 检查文件的修改
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@host50 ~]#
如果有httpd服务的话要停止
[root@host50 ~]# systemctl stop httpd
[root@host50 ~]# systemctl disable httpd
Removedsymlink /etc/systemd/system/multi-user.target.wants/httpd.service.
启动nginx服务
[root@host50 ~]# /usr/local/nginx/sbin/nginx
[root@host50 ~]# netstat -utnalp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9853/nginx: master
[root@host50 ~]# systemctl start php-fpm 启动php-fpm服务
[root@host50 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@host50 ~]# netstat -utnalp | grep 9000 查看端口
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 9863/php-fpm: maste
[root@host50 ~]#
编写php脚本
[root@host50 ~]# vim /usr/local/nginx/html/test.php
<?php
$i=99;
echo $i;
?>
:wq
命令行访问脚本
[root@host50 ~]# curl http://localhost/test.php
99
配置php支持redis
思就是在在网站服务器编写php 脚本 可以连接redis服务存储数据和查询数据
默认PHP不支持redis (也就是不能连接redis服务)
在网站服务器主机上做如下配置:
1) 安装软件提供连接redis服务的功能模块
2) 让PHP进程在运行时 调用redis模块
3) 查看是否支持redis服务
4) 测试配置
4.1 在网站服务器编写PHP脚本 存储数据 和查询数据
#编写存储数据的脚本
#编写查询数据的脚本
4.2 在客户端访问网站服务器php脚本
4.3 在Redis服务器本机查看内存里的数据
能够看到PHP存储的数据为成功
[root@host50~]#tar -xf redis-cluster-4.3.0.tgz
[root@host50~]#cd redis-4.3.0/
创建配置命令configure 和生产PHP配置信息文件/usr/bin/php-config
[root@host50 redis-4.3.0]# phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
#配置
[root@host50redis-4.3.0]# ./configure --with-php-config=/usr/bin/php-config
[root@host50 redis-4.3.0]# make 编译
[root@host50 redis-4.3.0]# make install 安装
#提示模块的安装目录
Installing shared extensions: /usr/lib64/php/modules/
[root@host50 redis-4.3.0]#
#查看目录下的模块列表 有redis.so 模块文件即可
[root@host50 redis-4.3.0]# ls /usr/lib64/php/modules/
curl.so json.so mysql.so pdo.so phar.so sqlite3.so
fileinfo.so mysqli.so pdo_mysql.so pdo_sqlite.so redis.so zip.so
[root@host50 redis-4.3.0]#
[root@host50 redis-4.3.0]# vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/" 模块文件所在的目录
730 extension = "redis.so" 模块名
:wq
[root@host50 redis-4.3.0]# systemctl restart php-fpm
#查看支持的模块
[root@host50 ~]# php -m | grep -i redis
redis
[root@host50 ~]#
测试配置
PHP脚本由网站的程序员 运维不需要编写,这里编写时为了测试部署的环境
说明: 要检查脚本连接的redis服务器的redis 服务开启了没有
在网站服务器编写PHP脚本 存储数据 和查询数据
1、连接redis服务(要指定Ip 、 端口 、连接密码(如果有密码的话))
2、执行存储数据或查询数据的命令
编写存储数据的脚本 set.php
[root@host50 ~]# vim /usr/local/nginx/html/set.php
<?php
$redis = new redis(); 定义连接命令
$redis->connect("192.168.4.51",6351); 指定服务器的ip和端口
$redis->auth("123456"); 指定连接密码
$redis->set("redistest","666666"); 存储数据
echo "save ok";
?>
:wq
编写查询数据的脚本 get.php
[root@host50 ~]# vim /usr/local/nginx/html/get.php
<?php
$redis = new redis(); 定义连接命令
$redis->connect("192.168.4.51",6351); 指定服务器的ip和端口
$redis->auth("123456"); 指定连接密码
echo $redis->get("redistest"); 输出查询结果
?>
:wq
在客户端访问网站服务器php脚本
[root@host50 ~]# curl http://localhost/set.php #访问存储数据的脚本
save ok
[root@host50 ~]# curl http://localhost/get.php #访问查询数据的脚本
666666
在Redis服务器本机查看内存里的数据
能够看到PHP存储的数据为成功
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
192.168.4.51:6351> keys redistest
1) "redistest"
192.168.4.51:6351> get redistest
"666666"
192.168.4.51:6351> exit
[root@host51 ~]#