redis数据库基础

一 redis 介绍:
–Remote Dictionary Server(远程字典服务器)
–是一款高性能的(key/values)分布式内存数据库
–支持数据持久化,可以把内存里的数据保存的硬盘中
–也支持list(列表),hash(散列),set(集合),zset(有序集合) 数据类型
–支持master-slave 模式数据备份

1 装包:

[root@mysql50 ~]# yum -y install gcc
[root@mysql50 ~]# yum -y install make pcre-devel
[root@mysql50 ~]# yum -y install make openssl-devel

[root@mysql50 ~]# tar -xf redis-4.0.8.tar.gz 
[root@mysql50 ~]# systemctl stop mysqld
[root@mysql50 ~]# cd redis-4.0.8/
[root@mysql50 redis-4.0.8]# make    && make install                  
 //tar包解压后,当前目录下已经有了makefile文件了,只需要编译安装就行

2 初始化配置:

[root@mysql50 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]       //默认端口号为6379
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]       // 默认redis主配置文件
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]      //启动redis服务的命令的绝对路径
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.

查看端口号,服务有没有起来

[root@mysql50 utils]# ss -antulp | grep 6379
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=19686,fd=6))

3 如何启动 停止服务

[root@mysql50 utils]# /etc/init.d/redis_6379 stop
[root@mysql50 utils]# /etc/init.d/redis_6379 start
[root@mysql50 utils]# /etc/init.d/redis_6379 status    //当查看状态时,如果服务没有开启,查询结果后面没有pid号

4 连接数据库:

[root@mysql50 utils]# which redis-cli                        //查看命令的位置
/usr/local/bin/redis-cli
[root@mysql50 utils]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@mysql50 utils]# redis-cli --help

[root@mysql50 utils]# redis-cli                             //连接数据库   //默认为本机127.0.0.1  :6379
127.0.0.1:6379> set name tom                        //设置一个变量name 值为tom
OK
127.0.0.1:6379> get name                                 //获取变量name的值
"tom"
127.0.0.1:6379> keys *                                 //获取所有的变量
1) "name"
127.0.0.1:6379> exit

[root@mysql50 utils]# ls /var/lib/redis/6379/                   //存储数据的文件
dump.rdb

5 常用操作指令:

127.0.0.1:6379> select 10  //切换到第11个库(默认有16个库,编号从0开始)
OK

127.0.0.1:6379> keys ????        //查找有4个字符的变量
1) "name"
127.0.0.1:6379> EXISTS name           //测试一个变量是否存在,存在返回1,不存在返回0
(integer) 1
127.0.0.1:6379> type name                    //查看一个变量的数据类型(用set设置变量默认类型都是字符)
string
127.0.0.1:6379> ttl name                //查看一个变量有没有设置过期时间,返回值为-1时表示用不过期
(integer) -1
127.0.0.1:6379> EXPIRE name 20           //设置变量的有效期为20s 
(integer) 1
127.0.0.1:6379> ttl name                //查看变量的过期时间
(integer) 9
127.0.0.1:6379> ttl name
(integer) 1
127.0.0.1:6379> ttl name                     // 返回值为-2时代表已经过期,过期后会在内存中删除
(integer) -2
127.0.0.1:6379> del a                         //手动删除变量a
(integer) 1
127.0.0.1:6379> FLUSHALL                     //清楚内存里的所有数据
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> flushdb                       // 删除当前库里的所有数据
127.0.0.1:6379> save                    //手动将内存的数据写入硬盘 (立刻执行)
127.0.0.1:6379> shutdown              //停止redis服务 ,查看不到端口6379 ,停止后,内存中的数据会自动写入硬盘
127.0.0.1:6379> move age 1    //将当前库的变量age移动到1库里去,返回值为1表示移动成功。
(integer) 1

[root@mysql50 utils]# ln -s /etc/init.d/redis_6379 /sbin  制作软链接
[root@mysql50 utils]# redis_6379 start      开启服务
[root@mysql50 utils]# redis-cli shutdown   用命令停止redis服务

二 redis 配置文件 解析:vim /etc/redis/6379.conf
1 修改端口号

 93 port 6350              //修改端口号6379为6350

注意:修改配置文件端口号后,关于开启和关闭服务

[root@mysql50 utils]# /etc/init.d/redis_6379 stop
[root@mysql50 utils]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@mysql50 utils]# ss -antulp | grep 6379
[root@mysql50 utils]# ss -antulp | grep 6350
tcp    LISTEN     0      128    127.0.0.1:6350  

[root@mysql50 utils]# /etc/init.d/redis_6379 stop    //再次用脚本关闭程序时会报错,因为脚本默认的端口为6379
Stopping ...
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...

[root@mysql50 utils]# redis-cli -p 6350 shutdown    //利用命令行关闭服务
[root@mysql50 utils]# ss -antulp | grep 6350                   //服务已关闭
[root@mysql50 utils]# /etc/init.d/redis_6379 start   //开启服务
[root@mysql50 utils]# ss -antulp | grep 6350                  //查看服务已开启
tcp    LISTEN     0      128    127.0.0.1:6350                  *:*                   users:(("redis-server",pid=21157,fd=6))
############################################################################################

2 修改提供redis服务的IP地址:

70 bind 192.168.4.50                    //通过本机的ip地址访问redis数据库
[root@mysql50 utils]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@mysql50 utils]# ss -antulp | grep 6350
tcp    LISTEN     0      128    192.168.4.50:6350                  *:*                   users:(("redis-server",pid=21261,fd=6))
[root@mysql50 utils]# redis-cli -h 192.168.4.50 -p 6350                  //访问时使用本机的Ip地址
192.168.4.50:6350> 

[root@mysql50 utils]# redis-cli -h 192.168.4.50 -p 6350 shutdown             //停止时需要加入ip地址,否则停止不了
[root@mysql50 utils]# ss -antulp | grep 6350

[root@mysql50 utils]# /etc/init.d/redis_6379 start                         //启动过程直接用脚本不需要修改
Starting Redis server...
[root@mysql50 utils]# ss -antulp | grep 6350
tcp    LISTEN     0      128    192.168.4.50:6350 

#############################################################################################

3 修改redis登录密码

 501 requirepass 123456   //将密码foobared修改为123456,打开注释
 [root@mysql50 utils]# redis-cli -h 192.168.4.50 -p 6350  -a 123456         //登录输入密码
192.168.4.50:6350> ping
PONG
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@mysql50 utils]# 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@mysql50 utils]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 shutdown  //关闭服务

[root@mysql50 utils]# vim /etc/init.d/redis_6379          //修改脚本使其可以被使用停止redis服务
 43             $CLIEXEC -p $REDISPORT shutdown  修改为
                  $CLIEXEC -h 192.168.4.50 -p 6350 -a 123456 shutdown

############################################################################

4 修改其他参数

102 tcp-backlog 511                //cp连接总数
114 timeout 0                           //连接超时时间
131 tcp-keepalive 300             //每隔300s检查一次是否保持连接,长连接时间
137 daemonize yes                  //守护进程方式运行
172 logfile /var/log/redis_6379.log           //pid文件
187 databases 16                     //默认16个库
533 # maxclients 10000          //并发连接数
264 dir /var/lib/redis/6379    //数据库目录

5 内存清除策略

565 # volatile-lru -> Evict using approximated LRU among the keys with an expir     e set.    // 删除最近最少使用(针对设置了TTL的key)
566 # allkeys-lru -> Evict any key using approximated LRU.                                                         //删除最少使用的key
569 # volatile-random -> Remove a random key among the ones with an expire set.             //在设置了TTL的key里随机删除
570 # allkeys-random -> Remove a random key, any key.                                                            //随机删除key
571 # volatile-ttl -> Remove the key with the nearest expire time (minor TTL)                         //移除最近过期的key
572 # noeviction -> Don't evict anything, just return an error on write operati     ons.           //不删除,写满时报错

6 内存选项设置

560 # maxmemory <bytes>                          //最大内存
591 # maxmemory-policy noeviction          // 定义使用策略
602 # maxmemory-samples 5                       //选取模板数据的个数(针对lru 和 ttl )

三 搭建LNMP + redis平台:
1 主机56上搭建LNMP平台
1)安装gcc openssl-devel pcre-devel 依赖包
2)安装nginx-1.12.2.tar.gz
3)安装php php-fpm-5.4.16-42.el7.x86_64.rpm php-common
4)安装php-redis-2.2.4.tar.gz
5)写入php测试页面,测试nginx解析php动态网页(修改配置文件,开启nginx ,开启php-fpm)

2 安装扩展软件支持php

1)[root@mysql56 ~]# yum -y install autoconf automake                  //安装依赖包

2)[root@mysql56 ~]# yum -y install php-devel-5.4.16-42.el7.x86_64.rpm             //安装依赖包

3)[root@mysql56 ~]# tar -xf php-redis-2.2.4.tar.gz                       //安装php扩展

4)[root@mysql56 phpredis-2.2.4]# /usr/bin/phpize                        // 获取当前php信息
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525

3 [root@mysql56 phpredis-2.2.4]# ./configure --with-php-config=/usr/bin/php-config //指定安装目录

 [root@mysql56 phpredis-2.2.4]# make
 [root@mysql56 phpredis-2.2.4]# make install
 Installing shared extensions:     /usr/lib64/php/modules/

4 确定连接redis数据库程序

[root@mysql56 phpredis-2.2.4]# ls /usr/lib64/php/modules/                  //redis.so为连接redis数据库的程序
curl.so  fileinfo.so  json.so  phar.so  redis.so  zip.so

5 修改配置文件

[root@mysql56 phpredis-2.2.4]# vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/"
729 ; On windows:
730 extension = "redis.so"

6 重启php-fpm服务

[root@mysql56 phpredis-2.2.4]# systemctl stop php-fpm
[root@mysql56 phpredis-2.2.4]# systemctl start php-fpm
[root@mysql56 phpredis-2.2.4]# php -m | grep -i redis
redis

7 编写连接redis数据库的php测试脚本

[root@mysql56 phpredis-2.2.4]# cat /usr/local/nginx/html/link.php 
<?php
$redis = new redis();
$redis->connect('192.168.4.50',6350);
$redis ->auth("123456");
$redis->set("nsd","1808");
echo $redis->get("nsd");
?>

8 测试:(同时登录50主机的redis数据库,即可查询到数据已被写入)

[root@room9pc01 ~]# firefox 192.168.4.56/link.php

如果需要取消密码限制,在配置文件 /etc/redis/6379.conf注释第501行 requirepass 123456 即可

猜你喜欢

转载自blog.csdn.net/weixin_42104231/article/details/84667616