NoSQL-Redis服务

一、概述

NoSQL(Not Only SQL)

  • 不仅仅时数据库
  • 泛指非关系型数据库
  • 不需要预先定义数据存储结构
  • 每条记录可以有不同的数据类型和字段个数

二、主流NoSQL软件

  • Memcached
  • Redis
  • MongoDB
  • CouchDB
  • Neo4j
  • FlockDB

三、Redis服务

3.1、Redis介绍

  • Remote Dictionary Server(远程字典服务器)
  • 是一款高性能的(key/Values)分布式内存数据库
  • 支持数据持久化(定期把内存里的数据存储到硬盘)
  • 支持多种数据类型string、list、hash…
  • 支持master-salve模式数据备份
  • 开源软件

3.2、安装Redis服务

准备源码包redis-4.0.8.tar.gz

  • 源码编译安装
[root@client ~]# rpm -q gcc
[root@client ~]# yum -y install gcc
[root@client ~]# tar -zxf redis-4.0.8.tar.gz 
[root@client ~]# cd redis-4.0.8/
[root@client redis-4.0.8]# make && make install

3.3、Redis初始配置

  • 初始化
    • 使用默认配置
[root@client redis-4.0.8]# ./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...
Installation successful!

[root@client redis-4.0.8]# netstat -utnlp |grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      5140/redis-server 1 
  • 端口——6379
  • 主配置文件——/etc/redis/6379.conf
  • 日志文件——/var/log/redis_6379.log
  • 数据库目录——/var/lib/redis/6379
  • 服务启动程序——/usr/local/bin/redis-server
  • 命令行连接命令——/usr/local/bin/redis-cli

3.4、管理Redis服务

  • 停止服务
[root@client ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
  • 启动服务
[root@client ~]# /etc/init.d/redis_6379 start
Starting Redis server...
  • 查看进程
[root@client ~]# ps -C redis-server
   PID TTY          TIME CMD
  5157 ?        00:00:00 redis-server
  • 查看端口
[root@client ~]# netstat -utnlp |grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      5157/redis-server 1 

3.5、连接Redis服务

[root@client redis-4.0.8]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name abc	//存数据
OK
127.0.0.1:6379> get name		//取数据
"abc"
127.0.0.1:6379> exit			
[root@client redis-4.0.8]# 

3.6、Redis常用命令

3.6.1、命令set 、 mset 、 get 、 mget

set key名 key值

  • 存单个数据
127.0.0.1:6379> set name abc	//存数据
OK
127.0.0.1:6379> get name		//取数据
"abc"

mset key名列表

  • 存多个数据
127.0.0.1:6379> MSET age 19 sex boy
OK

get key名

  • 获取单个数据
127.0.0.1:6379> get name 
"abc"

mget

  • 获取多个数据
127.0.0.1:6379> MGET age sex
1) "19"
2) "boy"

3.6.2、命令keys 、 type 、 exists 、 del

keys

  • 显示所有变量 keys *
127.0.0.1:6379> keys *
1) "sex"
2) "name"
3) "age"
  • 显示指定变量
127.0.0.1:6379> keys ???	//显示三个字符的
1) "sex"
2) "age"
127.0.0.1:6379> keys ????	//显示四个字符的
1) "name"
127.0.0.1:6379> keys a*		//显示以a开头的
1) "age"

type key名

  • 查看key类型
127.0.0.1:6379> type age	//使用set 命令存储的都是字符类型
string

del key名

  • 删除指定的key
127.0.0.1:6379> del age
(integer) 1

exists key名

  • 测试key名是否存在
127.0.0.1:6379> EXISTS age	//变量不存在返回值0
(integer) 0
127.0.0.1:6379> EXISTS sex 	//变量存在返回值1
(integer) 1

3.6.3、命令ttl 、 expire 、 move 、 flushdb 、flushall 、save、shutdown、select

ttl、expire

  • 查看key生存时间——ttl key名
  • 设置key有效时间——expire key名 数字
127.0.0.1:6379> TTL sex		//返回值 -1,表示变量永不过期
(integer) -1
127.0.0.1:6379> EXPIRE sex 20	//设置变量过期时间为20秒
(integer) 1
127.0.0.1:6379> TTL sex		//还剩14秒过期
(integer) 14
127.0.0.1:6379> ttl sex		//返回值 -2 表示已经过期
(integer) -2
127.0.0.1:6379> EXISTS sex	//变量已经不存在
(integer) 0

move key名 库编号

  • 移动key到指定库
127.0.0.1:6379> MOVE name 1		//把变量name移动到1号库
(integer) 1

select 数据库编号0-15

  • 切换库
127.0.0.1:6379> select 1	//切换到1号库
OK
127.0.0.1:6379[1]> keys *		//查看所有
1) "name"

flushdb

  • 删除所在库的所有key
127.0.0.1:6379[1]> FLUSHDB
OK
127.0.0.1:6379[1]> keys *
(empty list or set)

flushall

  • 删除内存里所有key
127.0.0.1:6379[1]> FLUSHALL
OK

save

  • 保存所有key到硬盘
127.0.0.1:6379[1]> SAVE
OK

shutdown

  • 停止服务
127.0.0.1:6379[1]> SHUTDOWN
not connected> exit
[root@client redis-4.0.8]# netstat -utnlp | grep redis-server	//没有进程信息
[root@client redis-4.0.8]# 
[root@client ~]# /etc/init.d/redis_6379  start
Starting Redis server...
[root@client ~]# netstat -utnlp | grep redis-server
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      1570/redis-server 1

3.7、配置文件解析

3.7.1、配置分类

名称 说明
NETWORK 网络
GENERAL 常规
SNAPSHOTTING 快照
REPLICATION 复制
SECURITY 安全
CLIENTS 客户端
MEMORY MANAGEMENT 内存管理

3.7.2、常用的配置项

  • port 6379
    • 端口
  • bind 127.0.0.1
    • IP地址
  • daemonize yes
    • 守护进程方式运行
  • databases 16
    • 数据库个数
  • logfile /var/log/redis_6379.log
    • 日志文件
  • maxclients 10000
    • 并发连接数量
  • dir /var/lib/redis/6379
    • 数据库目录

3.7.3、修改IP地址、端口、密码

[root@client ~]# sed -n '70p;93p;501p' /etc/redis/6379.conf	//修改前
bind 127.0.0.1
port 6379
# requirepass foobared
[root@client ~]# vim /etc/redis/6379.conf 
bind 192.168.4.50
port 6350
requirepass 123456

重启服务
[root@client ~]# /etc/init.d/redis_6379 stop	
Stopping ...
Redis stopped
[root@client ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@client ~]# netstat -utnlp | grep redis
tcp        0      0 192.168.4.50:6350       0.0.0.0:*               LISTEN      1702/redis-server 1

连接服务时需要指定IP地址和端口

[root@client ~]# redis-cli -h 192.168.4.50 -p 6350
192.168.4.50:6350> ping
(error) NOAUTH Authentication required.
192.168.4.50:6350> AUTH 123456	//输入密码
OK
192.168.4.50:6350> ping
PONG

[root@client ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> ping
PONG

3.7.4、内存管理

四、部署LNMP+Redis

两台虚拟机

4.1、部署LNMP网站环境

4.1.1、安装源码nginx、安装php-fpm

[root@lnmp ~]# yum -y install gcc pcre-devel zlib-devel
[root@lnmp ~]# tar -zxf nginx-1.12.2.tar.gz 
[root@lnmp ~]# ls
[root@lnmp ~]# cd nginx-1.12.2/
[root@lnmp nginx-1.12.2]# 
[root@lnmp nginx-1.12.2]# ./configure 
[root@lnmp nginx-1.12.2]# make && make install

[root@lnmp nginx-1.12.2]# yum -y install php-fpm

[root@lnmp ~]# 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;
        }

4.1.2、启动服务

[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx 
[root@lnmp ~]# netstat -utnlp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4022/nginx: master 

[root@lnmp ~]# systemctl start php-fpm.service 
[root@lnmp ~]# netstat -utnlp | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      4090/php-fpm: maste

4.1.3、测试配置

[root@lnmp ~]# vim /usr/local/nginx/html/test.php
<?php
    echo  "l love china" ;
?>

[root@lnmp ~]# curl http://127.0.0.1/test.php
l love china

4.2、配置PHP支持Redis

4.2.1、安装提供redis模块的软件

  1. 安装redis服务
[root@lnmp ~]# tar -zxf redis-4.0.8.tar.gz 
[root@lnmp ~]# cd redis-4.0.8/
[root@lnmp redis-4.0.8]# make && make install
[root@lnmp redis-4.0.8]# ./utils/install_server.sh
  1. 配置支持Redis
[root@lnmp ~]# yum -y  install php  php-devel automake autocon
[root@lnmp ~]# tar -zxf php-redis-2.2.4.tar.gz 
[root@lnmp ~]# ls
[root@lnmp ~]# cd phpredis-2.2.4/
[root@lnmp phpredis-2.2.4]# phpize 	//生成配置文件php-config及configure命令
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525

[root@lnmp phpredis-2.2.4]# ./configure  --with-php-config=/usr/bin/php-config
[root@lnmp phpredis-2.2.4]# make 
[root@lnmp phpredis-2.2.4]# make install
Installing shared extensions:     /usr/lib64/php/modules/

[root@lnmp phpredis-2.2.4]# ls /usr/lib64/php/modules/
curl.so  fileinfo.so  json.so  phar.so  redis.so  zip.so

4.2.2、加载redis模块

[root@lnmp ~]# vim +728 /etc/php.ini
extension_dir = "/usr/lib64/php/modules/"
; On windows:
extension = "redis.so"
[root@lnmp ~]# systemctl restart php-fpm.service

4.3、测试配置

4.3.1、查看模块

[root@lnmp ~]# php -m  | grep -i redis
redis

4.3.2、编写存取数据的php脚本并访问脚本

[root@lnmp ~]# vim /usr/local/nginx/html/x.php
<?php
$redis = new redis();
$redis->connect("192.168.4.50","6350");
$redis->auth("123456");
$redis->set("school","abc");
echo $redis->get("school");
?>

[root@lnmp ~]# curl http://127.0.0.1/x.php

4.3.2、在数据库服务器本机查看数据

192.168.4.50:6350> keys *
1) "school"
192.168.4.50:6350> get school
"abc"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章