NoSQL数据库 redis数据库服务的使用
 链接:https://pan.baidu.com/s/1cCANl-XFsZ0n0XUid4NBXA
 提取码:tty8nosql_day01
一、 搭建redis服务器
 1.1 相关概念
 数据库服务软件2种类型: 关系型 非关系型
 1.2 搭建Redis服务器
 1.2.1 安装软件
 #rpm -q gcc || yum -y install gcc
 #tar -zxvf redis-4.0.8.tar.gz
 #cd redis-4.0.8
 #make
 #make install1.2.2 初始化配置redis-4.0.8]# ./utils/install_server.sh
 一路回车
 1.2.3 查看服务状态
 ]# ss -utnlp | grep 6379 #看端口号
 ]# ps -C redis-server #看进程1.2.4 连接服务存储数据1. [root@host50 ~]# redis-cli
 127.0.0.1:6379> ping
 PONG
 127.0.0.1:6379> set school tarena
 OK
 127.0.0.1:6379> keys *1. “school”
 127.0.0.1:6379> get school
 “tarena”
 127.0.0.1:6379> exit
 [root@host50 ~]#1. 
1.3 常用命令使用
 set x 99(设置一个变量和值,这个变量类型是string)
 mset i 7 j 9 k 99 ax 99 af 43 age 68(设置多个变量和值)get x (获取一个变量的值)
 mget i j k (获取多个变量的值)keys * (查看所有变量)
 keys ? (查看变量是一个字母的)
 keys ??? (查看变量是三个字母的)
 keys a? (查看变量是a开头两个字母的)type i (查看变量类型)
 type j
 lpush tea plj nb dmy (设置变量和值 这个变量的类型为list)
 type tea (查看变量类型)
 get tea (get是获取不到lpush创建的变量的值的)127.0.0.1:6379> get name
 127.0.0.1:6379> EXISTS name(查看变量是否存在,存在返回1,不存在返回0)
 (127.0.0.1:6379> EXISTS username127.0.0.1:6379> ttl i (查看过期时间)
 127.0.0.1:6379> EXPIRE i 20 (设置变量过期时间为20秒)127.0.0.1:6379> ttl i
127.0.0.1:6379> keys i (查看变量)
127.0.0.1:6379> select 2 (切换到2号库)
 127.0.0.1:6379[2]> keys *127.0.0.1:6379[2]> select 0 (切换到0号库)
 127.0.0.1:6379[2]> move tea 2 (把变量tea移动到2号库里)
 127.0.0.1:6379[2]> move age 2127.0.0.1:6379> select 2
 OK
 127.0.0.1:6379[2]> keys *1. “tea”
2. “age”
 127.0.0.1:6379[2]>127.0.0.1:6379[2]> keys *
1. “tea”
2. “age”
 127.0.0.1:6379[2]> del tea (删除变量tea)
 select 0
 127.0.0.1:6379> del x y ax127.0.0.1:6379> keys *
 127.0.0.1:6379> flushdb 删除所在库的所有变量127.0.0.1:6379[2]> flushall 清空内存
127.0.0.1:6379> shutdown (关闭服务)
 not connected> ping
 Could not connect to Redis at 127.0.0.1:6379: Connection refused
 not connected> exit配置文件解析(修改配置文件可以改变服务的运行方式,要修改生效并重启服务)
服务的运行方式有2种
	守护进程(常驻内存)
	非守护进程(非常驻内存)[root@host50 ~]# /etc/init.d/redis_6379 stop
 [root@host50 ~]# vim /etc/redis/6379.conf
 bind 192.168.4.50
 port 6350
 requirepass 123456
 :wq[root@host50 ~]# sed -n ‘93p;70p;501p’ /etc/redis/6379.conf
 bind 192.168.4.50
 port 6350
 requirepass 123456[root@host50 ~]# /etc/init.d/redis_6379 start
 [root@host50 ~]# ss -utnlp | grep redis-server连接服务
 [root@host50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
 192.168.4.50:6350> ping
 PONG二、 LNMP+Redis
 2.1 部署lnmp环境
 36 rpm -q gcc || yum -y install gcc
 37 yum -y install pcre-devel zlib-devel
 38 tar -zxvf /var/ftp/pub/nginx-1.12.2.tar.gz
 39 cd nginx-1.12.2/
 40 ./configure
 41 make && make install[root@host52 nginx-1.12.2]# ls /usr/local/nginx/
 conf html logs sbin45 yum -y install php php-devel php-fpm
 46 systemctl start php-fpm
 47 systemctl enable php-fpm
 48 ss -utnlp | grep 9000
 ]# vim +65 /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 }
 :wq
 [root@host52 ~]# /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@host52 ~]#[root@host52 ~]# ss -utnlp |grep httpd 有的要停止httpd服务并设置开机不运行
[root@host52 ~]# ss -utnlp |grep 80
编脚本测试配置
 [root@host52 ~]# vim /usr/local/nginx/html/test.php <?php echo "hello wrold!!!" ; ?> 
:wq
[root@host52 ~]#
 #curl http://localhost/test.php
 hello wrold!!!2.2 配置网站可以把数据存储在redis服务器里
	检查依赖的软件是否安装[root@host52 ~]# rpm -q php
 [root@host52 ~]# rpm -q php-devel
 [root@host52 ~]# rpm -q autoconf
 [root@host52 ~]# rpm -q automake
 2.2.1 在网站服务器安装提供链接模块的软件包
 73 tar -zxvf /var/ftp/pub/php-redis-2.2.4.tar.gz
 75 cd phpredis-2.2.4/
 78 phpize
 81 ./configure -with-php-config=/usr/bin/php-config
 82 make
 83 make install
 84 ls /usr/lib64/php/modules/redis.*
 2.2.2 调用模块
 ]# vim /etc/php.ini
 728 extension_dir = “/usr/lib64/php/modules/”
 730 extension = “redis.so”
 :wq
 [root@host52 ~]# systemctl restart php-fpm
 [root@host52 ~]# php -m | grep redis
 redis2.2.3 测试配置[root@host52 html]# cd /usr/local/nginx/html/
 [root@host52 html]# vim s.php <?php $redis = new redis(); $redis->connect("192.168.4.50","6350"); $redis->auth("123456"); $redis->set("address","beijing"); $redis->set("mail","plj@tedu.cn"); echo "save ok"; ?> 
:wq
[root@host52 html]# cp s.php g.php
 [root@host52 html]# vim g.php <?php $redis = new redis(); $redis->connect("192.168.4.50","6350"); $redis->auth("123456"); echo $redis->get("address"); echo $redis->get("mail"); ?> 
:w
[root@host52 html]# cp s.php sg.php
[root@host52 html]# vim sg.php
 <?php $redis = new redis(); $redis->connect("192.168.4.50","6350"); $redis->auth("123456"); $redis->set("username","panglijing"); echo $redis->get("username"); ?> 
测试配置
 [root@host50 ~]# curl http://192.168.4.52/s.php
 save ok[root@host50 ~]#[root@host50 ~]# curl http://192.168.4.52/g.php
 beijingplj@tedu.cn[root@host50 ~]#[root@host50 ~]# curl http://192.168.4.52/sg.php
 panglijing[root@host50 ~]#三.内存清除策略
 volatile-lru //删除最近最少使用的存在生存周期的key
 allkeys-lru //删除最少使用的所有keyvolatile-lfu //删除使用频率最小的存在生存周期的key
 allkeys-lfu //删除使用频率最小的所有keyvolatile-random //在设置了生存周期的key里随即删除
 allkeys-random //随机删除keyvolatile-ttl //删除即将过期的key
noeviction //不删除