Linux学习之Redis使用

搭建Redis服务器

在主机redis64运行redis服务

#安装redis服务
[root@redis64 ~]# yum install -y redis
# 启动redis服务并开机启动
[root@redis64 ~]# systemctl enable redis --now
# 查看redis端口
[root@redis64 ~]# ss -tnlp | grep redis-server
LISTEN 0      128        127.0.0.1:6379      0.0.0.0:*    users:(("redis-server",pid=1114,fd=6))
# 通过redis-cli连接redis服务
[root@redis64 ~]# redis-cli
127.0.0.1:6379> ping         # 检查能否正在访问
PONG
127.0.0.1:6379> set school nsd2306        # 存储变量
OK
127.0.0.1:6379> get school             # 查看变量 
"nsd2306"
127.0.0.1:6379> exit

修改服务运行参数

# 停止redis服务
[root@redis64 ~]# systemctl stop redis
# 修改redis配置文件 
bind 192.168.88.64
port 6364
requirepass tye      # 执行命令前需要先认证才可以执行
# 启动redis服务,查看端口
[root@redis64 ~]# ss -tnlp | grep redis-server
LISTEN 0      128    192.168.88.64:6364      0.0.0.0:*    users:(("redis-server",pid=1183,fd=6))
# 连接,通过认证才可以执行命令
[root@redis64 ~]# redis-cli  -h 192.168.88.64 -p 6364
192.168.88.64:6364> ping
(error) NOAUTH Authentication required.
192.168.88.64:6364> auth tye
OK
192.168.88.64:6364> ping
PONG
192.168.88.64:6364> keys *
1) "school"
192.168.88.64:6364> get school
"nsd2306"

常用命令

- mset mget keys type
- exists ttl expire move select
- del flushdb flushall

# 通过服务器地址和端口连接
[root@redis64 ~]# redis-cli -h 192.168.88.64 -p 6364
192.168.88.64:6364> mset name plj age 80 class nsd2023
OK
192.168.88.64:6364> keys *
1) "school"
2) "age"
3) "name"
4) "class"
192.168.88.64:6364> mget name age
1) "plj"
2) "80"
# keys使用通配符查看变量
	* 匹配所有变量名
	? 一个字符
192.168.88.64:6364> keys *
1) "school"
2) "age"
3) "name"
4) "class"
192.168.88.64:6364> keys p?
(empty list or set)
192.168.88.64:6364> keys a??
1) "age"
# select 切换库 默认库编号 0-15[默认为0号库] 
192.168.88.64:6364> select 1
OK
192.168.88.64:6364[1]> select 16
(error) ERR DB index is out of range
192.168.88.64:6364[1]> select 15
OK
# move 命令 移动变量到其他库里
192.168.88.64:6364> move age 1
(integer) 1
192.168.88.64:6364> keys *
1) "school"
2) "name"
3) "class"
192.168.88.64:6364> select 1
OK
192.168.88.64:6364[1]> keys *
1) "age"

# exists 检查变量是否存储  返回值1  变量存储 返回值是0 变量不存在
192.168.88.64:6364> exists name
(integer) 1
192.168.88.64:6364> get name
"plj"
192.168.88.64:6364> set name bob
OK
192.168.88.64:6364> get name
"bob"
# EXPIRE 命令设置变量的过期时间 不设置变量永不过期
# ttl   检查变量可以在内存里存多久
192.168.88.64:6364> set sex girl
OK
192.168.88.64:6364> ttl sex
(integer) -1	# -1表示永不过期
192.168.88.64:6364> expire sex 15    # 过期时间15秒
(integer) 1
192.168.88.64:6364> ttl sex
(integer) 10
192.168.88.64:6364> ttl sex
(integer) 5
192.168.88.64:6364> ttl sex
(integer) -2				# 表示已经过期被删除
192.168.88.64:6364> keys sex
(empty list or set)
# type 命令检查变量存储数据的类型  
# 使用set  mset命令存储的数据都字符类型。
# 数据的类型不同 管理的命令也不同 
192.168.88.64:6364> set x 99
OK
192.168.88.64:6364> mset y 108
OK
192.168.88.64:6364> type x
string
192.168.88.64:6364> type y
string
192.168.88.64:6364> lpush tea nb wk zzg plj lx
(integer) 5
192.168.88.64:6364> type tea
list
# del 删除内存里的变量
192.168.88.64:6364> keys *
1) "tea"
2) "x"
3) "class"
4) "school"
5) "name"
6) "y"
192.168.88.64:6364> del x y
(integer) 2
192.168.88.64:6364> keys *
1) "tea"
2) "class"
3) "school"
4) "name"
# flushdb  删除当前所在库的所有数据
192.168.88.64:6364> keys *
1) "tea"
2) "class"
3) "school"
4) "name"
192.168.88.64:6364> flushdb
OK
192.168.88.64:6364> keys *
(empty list or set)
192.168.88.64:6364> select 1
OK
192.168.88.64:6364[1]> keys *
1) "age"
# 删除内存里所有内存里所有数据(慎用)
192.168.88.64:6364[1]> flushall
OK
192.168.88.64:6364[1]> keys *
(empty list or set)

部署LNP+Redis

1.在主机192.168.88.64部署LNP 环境
2.配置PHP支持redis
3.编写网站脚本,把数据存储到本机的内存里
# 编译安装nginx,先安装依赖
[root@redis64 ~]# yum install -y gcc pcre-devel zlib-devel make
[root@redis64 ~]# tar xf nginx-1.22.1.tar.gz 
[root@redis64 nginx-1.22.1]# ./configure
[root@redis64 nginx-1.22.1]# make && make install
[root@redis64 nginx-1.22.1]# ls /usr/local/nginx
conf  html  logs  sbin
# 安装php所需软件包
[root@redis64 nginx-1.22.1]# yum install -y php php-fpm php-devel
# 配置动静分离,修改nginx配置文件
 65         location ~ .php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 70             include        fastcgi.conf;
 71         }
 [root@redis64 nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
[root@redis64 nginx-1.22.1]# /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@redis64 nginx-1.22.1]# /usr/local/nginx/sbin/nginx
[root@redis64 nginx-1.22.1]# ss -tnlp | grep 80
LISTEN 0      128          0.0.0.0:80        0.0.0.0:*    users:(("nginx",pid=7679,fd=6),("nginx",pid=7678,fd=6))
# 启动php-fpm服务
[root@redis64 nginx-1.22.1]# vim /etc/php-fpm.d/www.conf 
listen = 127.0.0.1:9000  # 非sock方式运行 
[root@redis64 nginx-1.22.1]# systemctl enable php-fpm --now
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@redis64 nginx-1.22.1]# ss -tnlp | grep 9000
LISTEN 0      128        127.0.0.1:9000      0.0.0.0:*    users:(("php-fpm",pid=7751,fd=8),("php-fpm",pid=7750,fd=8),("php-fpm",pid=7749,fd=8),("php-fpm",pid=7748,fd=8),("php-fpm",pid=7747,fd=8),("php-fpm",pid=7746,fd=6))
# 测试php配置
[root@redis64 nginx-1.22.1]# vim /usr/local/nginx/html/test.php
<?php
  phpinfo();
?>

在这里插入图片描述

# 配置PHP支持redis
# 1.安装软件提供模块 --with-php-config=/usr/bin/php-config获取版本信息
[root@redis64 ~]# tar xf redis-cluster-4.3.0.tgz 
[root@redis64 pub]# cd redis-4.3.0/ 进源码目录
[root@redis64 redis-4.3.0]# phpize  获取php版本信息
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
[root@redis64 redis-4.3.0]# ./configure --with-php-config=/usr/bin/php-config
[root@redis64 redis-4.3.0]# make && make install       # 编译安装
# 2.调用模块
[root@redis64 redis-4.3.0]# vim /etc/php.ini 
extension_dir = "/usr/lib64/php/modules/"
extension = "redis.so"
# 3.加载模块
[root@redis64 redis-4.3.0]# systemctl restart php-fpm
# 4.查看模块
[root@redis64 redis-4.3.0]# php -m | grep redis
redis

# 编写网站脚本,把数据存储到本机的内存里
# 1.允许通过lo口连接本机redis服务
[root@redis64 redis-4.3.0]# systemctl stop redis
[root@redis64 redis-4.3.0]# vim /etc/redis.conf
bind 127.0.0.1 192.168.88.64
[root@redis64 redis-4.3.0]# systemctl start redis
[root@redis64 redis-4.3.0]# ss -tnlp | grep redis
LISTEN 0      128    192.168.88.64:6364      0.0.0.0:*    users:(("redis-server",pid=10067,fd=7))                
LISTEN 0      128        127.0.0.1:6364      0.0.0.0:*    users:(("redis-server",pid=10067,fd=6)) 
# 2.编写php脚本
[root@redis64 redis-4.3.0]# vim /usr/local/nginx/html/redis.php
<?php
$redis = new redis();
$redis->connect("127.0.0.1", "6364");
$redis->auth("tarenaplj");
$redis->set("class","nsd");
echo "save okn";
?>
# 3.访问脚本
通过浏览器访问:http://192.168.88.64/redis.php
save ok
# 4.连接redis查看变量
[root@redis64 redis-4.3.0]# redis-cli -h 192.168.88.64 -p 6364
192.168.88.64:6364> keys *
1) "class"
192.168.88.64:6364> get class
"nsd2023"