数据库服务软件类型和配置redis

                                                    ql-day11     数据库服务软件类型和配置redis  

数据库服务软件类型

² RDBMS

• 关系数据库管理系统

Relational Database Management System

– 数据按照预先设置的组织结构,存储在物理存储介质上。

– 数据之间可以做关联操作

² RDBMS 服务软件

• 主流的 RDBMS 软件

Oracle

DB2

WS-sqlserver

MySQL

² NoSQL

NoSQL(NoSQL = Not Only SQL )

– 意思是 " 不仅仅是 SQL

– 泛指非关系型数据库

– 不需要预先定义数据存储结构

– 表的每条记录都可以有不同的类型和结构

² NoSQL 服务软件

• 主流软件

Redis

MongoDB

Memcached

CouchDB

Neo4j

FlockDB

² Redis 介绍

Redis

Remode DIctionary Server( 远程字典服务器 )

– 使用 C 语言编写的,遵守 BSD 的开源软件

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

– 并支持数据持久化的 NoSQL 数据库服务软件

– 中文网站 www.redis.cn

Redis 介绍(1 )

² Redis 特点:

– 支持数据持久化,可以把内存里数据保存到硬盘中

– 不仅仅支持 key/values 类型的数据,同时还支持 list

hash set zset 类型

– 支持 master-salve 模式数据备份

1)装包

# cd soft/redis

# tar -xzf redis-4.0.8.tar.gz

# yum -y install gcc-c++ gcc

# cd redis-4.0.8

# make         或      // make && make install

# make install

2)初始化

# ./utils/install_server.sh

3)启动redis服务(软件安装完成后服务自动运行)启动服务

• 启动服务

# /etc/init.d/redis_<portnumber> start

• 停止服务

# /etc/init.d/redis_<portnumber> stop

[root@localhost redis-4.0.8]# /etc/init.d/redis_6379 status

[root@localhost redis-4.0.8]# netstat -utnlp  | grep :6379  //redis

[root@localhost redis-4.0.8]# ps -C  redis-server

4)运行

[root@host51 redis-4.0.8]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

连接本机redis服务 

[root@localhost redis-4.0.8]# redis-cli

127.0.0.1:6379> ping

PONG

127.0.0.1:6379> set k1 v1    //再次附值,覆盖之前的值

OK

127.0.0.1:6379> get k1

"v1"

127.0.0.1:6379> SHUTDOWN

not connected> exit

常用操作指令

Set keyname keyvalue          // 存储

get keyname                       // 获取

Select 数据库编号 0-15      // 切换库

Keys *                                 // 打印所以变量

Keys a?                               // 打印指定变量

Exits keyname                     // 测试是否存在

ttl keyname                         // 查看生存时间

type keyname                      // 查看类型

连接本机redis服务

[root@localhost redis-4.0.8]# redis-cli

127.0.0.1:6379> ping

PONG

127.0.0.1:6379> set k1 v1     //k1附值v1

OK

127.0.0.1:6379> get k1          //查看值

"v1"

127.0.0.1:6379> keys *

1) "k1"

127.0.0.1:6379> SHUTDOWN

not connected> exit

常用操作指令 ( 1)

move keyname dbname    // 移动变量

expire keyname 10            // 设置有效时间

del keyname                     // 删除变量

flushall                             // 删除所有变量

save                                 // 保存变量

shutdown                         // 关闭服务

  

配置文件解析

  • 数据单位

• 数据单

  • 常用配置选项    vim  /etc/redis/6379.conf

        *模板*                                           *修改*

port 6379 // 端口                        // 93 port 6351       

bind 127.0.0.1 //IP 地址              // 70 bind 127.0.0.1 192.168.4.51

tcp-backlog 511 //tcp 连接总数            

timeout 0 // 连接超时时间

tcp-keepalive 300 // 长连接时间

daemonize yes // 守护进程方式运行

databases 16 // 数据库个数

logfile /var/log/redis_6379.log //pid 文件

maxclients 10000 // 并发连接数量

dir /var/lib/redis/6379 // 数据库目录

1) 51主机更改配置文件

[root@host52 redis-4.0.8]# vim  /etc/redis/6379.conf

:set nu

 70 bind 127.0.0.1 192.168.4.51

 93 port 6351

815 cluster-enabled yes                            //去掉注释

823 cluster-config-file nodes-6357.conf    //去掉注释。修改

829 cluster-node-timeout 5000                 //去掉注释。修改

2) 52 主机上连接51

[root@host52 redis-4.0.8]# redis-cli -h 192.168.4.51 -p 6351

  • 内存管理

• 内存清除策略

volatile-lru 最近最少使用 (针对设置了过期时间的 key )

keys-lru 删除最少使用的 key

volatile-random 在设置了过期的 key 里随机移除

allkeys-random 随机移除 key

volatile-ttl (minor TTL) 移除最近过期的 key

noeviction不删除 写满时报错

  • 内存管理 (1 )

• 选项默认设置

maxmemory <bytes> // 最大内存

maxmemory-policy noeviction // 定义使用的策略

maxmemory-samples 5 // 选取模板数据的个数

(针对 lru ttl 策略)

  • 设置连接密码

• 设置密码    配置文件:   vim  /etc/redis/6379.conf

grep -n requirepass /etc/redis/6379.conf

501:requirepass 123456

1)重起服务 (命令停止服务)   //刚开始要downredis-cli -p 6379 shutdown

2)[root@host51 redis-4.0.8]# redis-cli -p 6351 shutdown

                         ]# redis-cli -p 6351 -a 123456 shutdown

[root@host51 redis-4.0.8]# /etc/init.d/redis_6379 start

[root@localhost redis-4.0.8]# redis-cli  -h192.168.4.51  -p 6351 -a 123456

127.0.0.1:6379> ping

(error) NOAUTH Authentication required.

127.0.0.1:6379> auth 123456 // 输入密码

OK

127.0.0.1:6379> ping

PONG

127.0.0.1:6379>

3)修改配置文件  (脚本停止服务配置)

[root@host51 redis-4.0.8]# vim  /etc/init.d/redis_6379

 43     $CLIEXEC -h 192.168.4.51 -p 6351 -a 123456 shutdown

[root@host51 redis-4.0.8]# /etc/init.d/redis_6379  stop

[root@host51 redis-4.0.8]# /etc/init.d/redis_6379  start

[root@host51 redis-4.0.8]# /etc/init.d/redis_6379  status


部署 LNMP

1)安装软件包

• 安装源码 nginx PHP

[root@host53 ~]# tar -xf lnmp_soft.tar.gz

]# yum -y install gcc gcc-c++ pcre-devel zlib-devel

]# tar -zxf nginx-1.12.2.tar.gz

]# cd nginx-1.12.2

]# ./configure --prefix=/usr/local/nginx

]# make

]# make install

[root@host53 lnmp_soft]# yum -y install php-common

[root@]host53 lnmp_soft# rpm -ivh php-fpm-5.4.16-42.el7.x86_64.rpm

[root@host53 lnmp_soft]# systemctl start php-fpm

[root@host53 lnmp_soft]# netstat -anput | grep :9000

[root@host53 nginx-1.12.2]# /usr/local/nginx/sbin/nginx

[root@host53 nginx-1.12.2]# netstat -anput | grep :80

[root@host53 nginx-1.12.2]# echo "123"  > /usr/local/nginx/html/index.html

[root@host53 nginx-1.12.2]# firefox 192.168.4.53/test.html

2)修改配置文件

• 修改 nginx 服务主配置文件

[root@bogon ~]# vim /usr/local/nginx/conf/nginx.conf

location ~ .php$ {

root

html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

include

fastcgi_params;

}

:wq

3)测试配置

• 测试文件

[root@bogon lnmp]# vim /usr/local/nginx/html/test.php

<?php

echo "hello world!!!";

?>

:wq

[root@bogon lnmp]#

• 访问 nginx 服务

[root@bogon lnmp]# elinks --dump http://localhost/test.php

hello world!!! 


运行 Redis 服务

• 装包并启动服务

[root@host53 ~]# cd soft/redis

# tar -xzf redis-4.0.8.tar.gz

# cd redis-4.0.8

#make

#make install

#./utils/install_server.sh // 初始化.全部回车

# /etc/init.d/redis_6379 start

配置 php 支持 Redis

• 安装 php 扩展

[root@host53 ~]# cd soft/redis

#yum -y install autoconf

#yum -y install automake

#rpm -ivh php-devel-5.4.16-42.el7.x86_64.rpm

# tar -zxf php-redis-2.2.4.tar.gz

# cd phpredis-2.2.4/

[root@bogon phpredis-2.2.4]# /usr/bin/phpize

# ./configure --with-php-config=/usr/bin/php-config

[root@bogon phpredis-2.2.4]# make

[root@bogon phpredis-2.2.4]# make install

[root@bogon ~]# vim /etc/php.ini

728    extension_dir = "/usr/lib64/php/modules/”

730    extension = "redis.so"测试配

测试配置

[root@host53 ~]# firefox 192.168.4.53/test.php

[root@host53 ~]# systemctl start php-fpm.service

• 查看是否支持模块

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

redis

• 编写测试文件

[root@bogon bin]# cat /usr/local/nginx/html/redis.php

<?php

$redis = new redis();

$redis->connect('127.0.0.1',6379);

$redis->set('redistest','666666');

echo $redis->get('redistest');

?>

• 访问 nginx 服务

[root@bogon bin]# elinks --dump http://localhost/redis.php

666666

访问和测试

[root@db54 redis-4.0.8]# redis-cli

127.0.0.1:6379> keys *

1) "redistest"

127.0.0.1:6379> get redistest

"666666"

127.0.0.1:6379>

配置页面

[root@db54 lnmp]# vim  /usr/local/nginx/html/test.php

<?php

phpinfo();

?>


快速配置redis服务

装包并启动服务

[root@host53 ~]# cd soft/redis

# tar -xzf redis-4.0.8.tar.gz

# cd redis-4.0.8

#make

#make install

#./utils/install_server.sh // 初始化

# /etc/init.d/redis_6379 start

修改配置    /etc/redis/6379.conf

[root@host52 redis-4.0.8]# vim  /etc/redis/6379.conf

:set nu

70 bind 127.0.0.1 192.168.4.51

93 port 6351

815 cluster-enabled yes                            //去掉注释

823 cluster-config-file nodes-6357.conf    //去掉注释。修改

829 cluster-node-timeout 5000                 //去掉注释。修改

修改配置(2/etc/init.d/redis_6379

[root@host51 redis-4.0.8]# vim  /etc/init.d/redis_6379

 43     $CLIEXEC -h 192.168.4.51 -p 6351 -a 123456 shutdown

停止服务

[root@host51 redis-4.0.8]#redis-cli -p 6379 shutdown

起服务

[root@host51 redis-4.0.8]# /etc/init.d/redis_6379  stop

[root@host51 redis-4.0.8]# /etc/init.d/redis_6379  start

[root@host51 redis-4.0.8]# /etc/init.d/redis_6379  status

查看端口

netstat -anput | grep redis

原文地址:https://www.cnblogs.com/qingbai/p/12015856.html