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"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章