redis的安装及lnmp+redis配置

3 篇文章 0 订阅

关系型数据库(RDBMS):
在这里插入图片描述
在这里插入图片描述
非关系型数据库(NoSQL):
在这里插入图片描述
在这里插入图片描述
一、Redis
在这里插入图片描述
1、安装redis及初始化配置

[root@host50 ~]# ls
redis-4.0.8.tar.gz
[root@host50 ~]# yum -y install gcc  gcc-c++ make
[root@host50 ~]# tar -zxvf redis-4.0.8.tar.gz
[root@host50 ~]# cd redis-4.0.8/
[root@host50 redis-4.0.8]# make
[root@host50 redis-4.0.8]# make install
[root@host50 redis-4.0.8]# ./utils/install_server.sh		//初始化配置(一路回车默认配置)
...
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...
Installation successful!
[root@host50 redis-4.0.8]# netstat -ntulap |grep :6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      4772/redis-server 1

2、连接redis、启动与停止redis

[root@host50 redis-4.0.8]# redis-cli		//连接数据库
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
[root@host50 redis-4.0.8]# redis-cli
127.0.0.1:6379> set name bob
OK
127.0.0.1:6379> get name
"bob"
127.0.0.1:6379> 
[root@host50 redis-4.0.8]# /etc/init.d/redis_6379 stop		//停止redis,停止前数据自动从内存写入硬盘,实现数据持久化
Stopping ...
Redis stopped
[root@host50 redis-4.0.8]# /etc/init.d/redis_6379 start		//启动redis
Starting Redis server...
[root@host50 redis-4.0.8]# netstat -ntulp |grep :6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      4791/redis-server 1
[root@host50 redis-4.0.8]# redis-cli 
127.0.0.1:6379> get name		//重启redis后之前存放的数据仍然存在
"bob"
[root@host50 ~]# ls /var/lib/redis/6379		//数据存放路径

准备一台51主机安装redis,创建变量test,赋值123
3、常用操作指令
在这里插入图片描述
在这里插入图片描述

[root@host50 ~]# redis-cli 
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> get name
"bob"
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> keys *
(empty list or set)
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> EXISTS name		//若 key 存在返回 1 ,否则返回 0 
(integer) 1
127.0.0.1:6379> ttl name		//当 key 存在但没有设置剩余生存时间时,返回 -1
(integer) -1
127.0.0.1:6379> type name
string
127.0.0.1:6379> expire name 20
(integer) 1
127.0.0.1:6379> ttl name
(integer) 14
127.0.0.1:6379> ttl name
(integer) 12
127.0.0.1:6379> ttl name
(integer) 4
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> set x 12
OK
127.0.0.1:6379> set y 13
OK
127.0.0.1:6379> move y 2		//将当前数据库的 key 移动到给定的数据库2当中
(integer) 1
127.0.0.1:6379> select 2		//共16个库(0-15)
OK
127.0.0.1:6379[2]> keys y
1) "y"
127.0.0.1:6379[2]> keys *
1) "y"
127.0.0.1:6379[2]> set xx 123
OK
127.0.0.1:6379[2]> keys ??
1) "xx"
127.0.0.1:6379[2]> keys *
1) "xx"
2) "y"
127.0.0.1:6379[2]> del x
(integer) 0
127.0.0.1:6379[2]> keys *
1) "xx"
2) "y"
127.0.0.1:6379[2]> del xx
(integer) 1
127.0.0.1:6379[2]> keys *
1) "y"
127.0.0.1:6379[2]> save
OK
127.0.0.1:6379[2]> flushall
OK
127.0.0.1:6379[2]> keys *
(empty list or set)
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> SHUTDOWN		//关闭redis
not connected> 

4、配置文件解析(/etc/redis/6379.conf)

[root@host50 ~]# cp /etc/redis/6379.conf /root		//修改配置文件前备份

常用配置选项:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 设置连接密码和端口、登录ip
[root@host50 ~]# vim /etc/redis/6379.conf
501 requirepass 123456
[root@host50 ~]# /etc/init.d/redis_6379 stop
[root@host50 ~]# /etc/init.d/redis_6379 start		//重启服务,是密码生效
[root@host50 ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>exit
[root@host50 ~]# redis-cli -a 123456 shutdown		//修改密码后停止redis命令
[root@host50 ~]# /etc/init.d/redis_6379 start
[root@host50 ~]# vim /etc/redis/6379.conf
93 port 6350
[root@host50 ~]# redis-cli -a 123456 shutdown
[root@host50 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host50 ~]# netstat -ntulp |grep :6350
tcp        0      0 127.0.0.1:6350          0.0.0.0:*               LISTEN      5039/redis-server 1
[root@host50 ~]# redis-cli -p 6350 -a 123456 shutdown		//修改密码和端口后停止redis命令
[root@host50 ~]# vim /etc/redis/6379.conf
70 bind 192.168.8.50		//修改redis登录ip
[root@host50 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host50 ~]# netstat -ntulap |grep redis
tcp        0      0 192.168.8.50:6350       0.0.0.0:*               LISTEN      915/redis-server 19
[root@host50 ~]# redis-cli -h 192.168.8.50 -a 123456 -p 6350		//修改登录ip、端口和密码后连接redis命令
192.168.8.50:6350> ping
PONG
[root@host50 ~]# redis-cli -h 192.168.8.50 -a 123456 -p 6350 shutdown		//修改登录ip、端口和密码后停止redis命令
[root@host50 ~]# netstat -ntulp |grep :6350
[root@host50 ~]# vim /etc/init.d/redis_6379		//修改启动脚本,使修改配置文件(登录ip、端口、密码)后,可以使用启动脚本开启和关闭服务	
43             $CLIEXEC -h 192.168.8.50 -p 6350 -a 123456 shutdown
[root@host50 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host50 ~]# netstat -ntulp |grep :6350
[root@host50 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host50 ~]# netstat -ntulp |grep :6350
tcp        0      0 192.168.8.50:6350       0.0.0.0:*               LISTEN      980/redis-server 19
[root@host50 ~]# redis-cli -h 192.168.8.50 -p 6350 -a 123456
192.168.8.50:6350> ping
PONG
192.168.8.50:6350> set name tom
OK
192.168.8.50:6350> set nianlin 56
OK
192.168.8.50:6350> keys *
1) "nianlin"
2) "name"
192.168.8.50:6350> exit

51主机远程连接到50主机redis数据库

[root@host51 redis-4.0.8]# redis-cli -h 192.168.8.50 -p 6350 -a 123456
192.168.8.50:6350> ping
PONG
192.168.8.50:6350> keys *
1) "nianlin"
2) "name"
192.168.8.50:6350> set dizhi hubei
OK
192.168.8.50:6350> keys *
1) "dizhi"
2) "nianlin"
3) "name"
192.168.8.50:6350>exit

二、部署LNMP+redis
1、安装源码nginx和php

[root@host50 ~]# ls
6379.conf                           php-fpm-5.4.16-42.el7.x86_64.rpm
linkredis.php                       php-redis-2.2.4.tar.gz
nginx-1.12.2                        redis-4.0.8
nginx-1.12.2.tar.gz                 redis-4.0.8.tar.gz
php-devel-5.4.16-42.el7.x86_64.rpm
[root@host50 ~]# yum -y install gcc-c++ pcre-devel zlib-devel
[root@host50 ~]# tar -zxvf nginx-1.12.2.tar.gz
[root@host50 ~]# cd nginx-1.12.2/
[root@host50 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@host50 nginx-1.12.2]# make
[root@host50 nginx-1.12.2]# make install
[root@host50 nginx-1.12.2]# yum -y install php-common
[root@host50 nginx-1.12.2]# cd
[root@host50 ~]# yum -y install php-fpm-5.4.16-42.el7.x86_64.rpm
[root@host50 ~]# systemctl start php-fpm		//启动php-fpm服务
[root@host50 ~]# netstat -ntulap |grep :9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      3550/php-fpm: maste

2、修改nginx配置文件,启动nginx

[root@host50 ~]# vim /usr/local/nginx/conf/nginx.conf
65         location ~ \.php$ {
66             root           html;
67             fastcgi_pass   127.0.0.1:9000;
68             fastcgi_index  index.php;
69             include        fastcgi.conf;
70         }
[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 ~]# /usr/local/nginx/sbin/nginx -V		//查看nginx配置选项
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
configure arguments: --prefix=/usr/local/nginx
[root@host50 ~]# /usr/local/nginx/sbin/nginx 		//启动nginx
[root@host50 ~]# netstat -ntulap |grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3590/nginx: master

3、创建php测试文件

[root@host50 ~]# vim /usr/local/nginx/html/test.php
<?php
    echo "hello world"
?>
[root@host51 redis-4.0.8]# curl http://192.168.8.50/test.php		//php页面解析成功
hello world

4、配置PHP支持redis

[root@host50 ~]# yum -y install php
[root@host50 ~]# yum -y install php-devel-5.4.16-42.el7.x86_64.rpm
[root@host50 ~]# tar -zxvf  php-redis-2.2.4.tar.gz
[root@host50 ~]# cd phpredis-2.2.4/
[root@host50 phpredis-2.2.4]# phpize
[root@host50 phpredis-2.2.4]# ./configure --with-php-config=/usr/bin/php-config
[root@host50 phpredis-2.2.4]# make
[root@host50 phpredis-2.2.4]# make install
[root@host50 phpredis-2.2.4]# ls /usr/lib64/php/modules/
curl.so  fileinfo.so  json.so  phar.so  redis.so  zip.so
[root@host50 phpredis-2.2.4]# vim /etc/php.ini		//修改php主配置文件
728 extension_dir = "/usr/lib64/php/modules"
730 extension = "redis.so"
[root@host50 phpredis-2.2.4]# systemctl start php-fpm
[root@host50 html]# netstat -ntulap |grep :9000		//查看php-fpm是否启动
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      12502/php-fpm: mast
[root@host50 phpredis-2.2.4]# php -m |grep redis		//查看php模块是否支持redis
redis

5、创建测试页面(php-redis)

[root@host50 html]# vim redis.php
<?php
$redis=new redis();
$redis->connect("192.168.8.50",6350);
$redis->auth("123456");
$redis->set("school","tarena");
echo $redis->get("school");
?>
[root@host51 ~]# curl http://192.168.8.50/redis.php		//测试访问成功
tarena
[root@host50 html]# redis-cli -h 192.168.8.50 -a 123456 -p 6350
192.168.8.50:6350> keys *
1) "school"		//变量已经写入redis数据库
2) "nianlin"
3) "dizhi"
4) "name"
192.168.8.50:6350> get school
"tarena"
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值