当前位置:首页 > 有云笔记 > NOSQL > 正文内容

NoSQL概述 、 部署Redis服务 、 部署LNMP+Redis

小白2年前 (2022-03-27)NOSQL43360

一、相关概念 (需要了解)

1.1 相关的概念

数据库服务软件分为2类:

关系型数据库服务软件 简称 RDBMS

存储数据的特点?

服务软件?MySQL

非关系型数据库服务软件 简称 NoSQL

存储数据的特点?

服务软件?

1.2redis 软件介绍

Remote Dictionary  Server(远程字典服务器)

是一款高性能的(Key/Values)分布式内存数据库

支持数据持久化(定期把内存里数据存储到硬盘)

支持多种数据类型:字符、列表、散列、集合

支持 master-salve 模式数据备份

中文网站www.redis.cn

二、搭建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 ~]#

1.2 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命令存储的数据都字符类型。

数据的类型不同 管理的命令也不同

3天详细讲redis数据类型。

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 *

1.3  配置文件解析 文件里常用配置项说明

通过修改配置项改变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 ~]# netstat  -utnalp  | grep redis-server  #服务被停止了 查看不到进程了
[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

#修改脚本使其也可以使用脚本停止服务(启动脚本是使用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服务器里)

生产环境下会被网站的热点数据存放在内存存储服务器里,这样的好处是可以加快存取数据的速度,能够实现网站访问加速通常网站会把 频繁被访问的数据(热点数据)、数据小的数据 、可再生的数据 存储在内存存储服务器里

2.1 部署网站运行环境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

2.2 配置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 ~]#

4) 测试配置

PHP脚本由网站的程序员 运维不需要编写,这里编写时为了测试部署的环境

说明: 要检查脚本连接的redis服务器的redis 服务开启了没有

4.1 在网站服务器编写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

 4.2 在客户端访问网站服务器php脚本

[root@host50 ~]# curl http://localhost/set.php   #访问存储数据的脚本
save ok

[root@host50 ~]# curl http://localhost/get.php  #访问查询数据的脚本
666666

4.3 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 ~]#

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

思考主机host51 使用内存给网站服务提供存储数据的空间:

   时间久了,Redis服务器host51 会产生那些问题?

   1 存储空间不够用 (解决办法1 多台服务器一起提供数据存储服务

  解决办法2 删除内存里已经存储的数据 腾出空间存储新数据)

   2 单独故障问题

   3 数据备份问题

   4 访问多的时候 1Redis服务处理不过来

 问题2-4 可以通过部署Redis集群存储架构解决问题

试想2GU盘     存放喜欢看的小电影

10个小电影  共占用了  2G      下载新小电影没地方存储了  怎么办????

五、Redis服务的内存清除策略

当内存空间不足,删除内存里已经存储的数据的方式

内存清除策略是软件的开发者写功能程序并定义的名称方便运维调用。

根据需要选择使用那种内存清除策略即可。

我们运维要知道策略名称 和清除数据的方式!!!  

redis服务提供的内存清除策略如下:(在/etc/redis/6379.conf 有说明信息)

1) LRU  删除最近最少使用的,有2种清除方式:

volatile-lru 针对设置了TTLkey

allkeys-lru     针对所有的key

2LFU  删除使用频率最少的 有2种清除方式:

volatile-lfu 针对设置了TTLkey

allkeys-lfu     针对所有的key

3) RANDOM  随机删除 有2种清除方式:

volatile-random 针对设置了TTLkey

allkeys-random 针对所有的key

4) volatile-ttl  删除快要过期的

5noeviction 不删除 存储空间不够时 服务直接报错

与内存相关的配置项 vim /etc/redis/6379.conf

maxmemory    //最大内存

maxmemory-policy //定义使用策略

maxmemory-samples  20 //选取key模板的个数 (针对lru ttl volatile-ttl 策略)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

NoSQL_day02 部署redis 集群架构提供数据存储服务

需要机器如下:

当前host50 做客户端

准备6台新虚拟机机Ip地址 51-56

把今天做实验的host51 删除 重新创建虚拟机配置ip地址是192.168.4.51

在主机51-56 做如下配置

1) 安装Redis软件  初始化配置

2) 停止redis服务

3) 修改服务使用的ip地址是eth0接口的地址 (不需要设置连接密码和修改服务使用的端口号)

4) 启动redis服务

创建新虚拟机

配置Ip地址  192.168.4.57 redis-3.2.1.gem redis-4.0.8.tar.gz 只拷贝软件不需要安装  

over

NoSQL概述 、 部署Redis服务 、 部署LNMP+Redis  您阅读本篇文章共花了: 

分享到:

    扫描二维码推送至手机访问。

    版权声明:本文由有云转晴发布,如需转载请注明出处。

    本文链接:https://www.yyzq.cf/?id=152

    分享给朋友:
    返回列表

    没有更早的文章了...

    下一篇:创建集群 、 管理集群

    相关文章

    发表评论

    访客

    ◎欢迎参与讨论,请在这里发表您的看法和观点。