Redis介绍及基本使用

数据库类型:
关系型RDBMS, 按照预先设置的组织结构(建表),将数据存储在物理介质上;数据之间可以做关联操作(MYSQL,MARIADB,ORACLE,DB2,SQL SERVER)
非关系型NOSQL,NOT ONLY SQL,无需预先定义存储结构,每条记录可以有不同的数据类型和字段个数(Memcached,Redis,MongoDB,CouchDB,Neo4j,FlockDB)

Redis(Remote Dictionary Server)
是一款高性能(key/value)分布式内存数据库
支持数据持久化(定期把内存里的数据存储到硬盘)
支持多种数据类型string,list,hash
支持master-slave模式数据备份
中文网站www.redis.cn

搭建Redis
安装软件
yum -y install gcc
tar -xf redis-4.0.8.tar.gz
cd redis-4.0.8
make&& make install

初始化配置
[root@node50 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!

默认使用lo口,本地还回接口

[root@node50 redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务
Stopping …
Redis stopped
[root@node50 redis-4.0.8]# /etc/init.d/redis_6379 start //启动服务
Starting Redis server…
[root@node50 redis-4.0.8]# /etc/init.d/redis_6379 status //查询运行状态
Redis is running (4619)

常用操作
[root@node50 redis-4.0.8]# redis-cli //登录本机redis
127.0.0.1:6379> ping //测试
PONG
127.0.0.1:6379> keys * //查询所有变量
(empty list or set)
127.0.0.1:6379> set name bob //设置变量值, name是变量名,bob是值
OK
127.0.0.1:6379> get name //查询变量
“bob”
127.0.0.1:6379> exit //退出登录

[root@node50 redis-4.0.8]# ls /var/lib/redis/6379/dump.rdb //定期会把内存中的数据写到这个路径,关闭服务也会写入这个位置

127.0.0.1:6379> mset age 19 sex male //同时建立多个变量
OK
127.0.0.1:6379> keys * //*表示所有

  1. “sex”
  2. “age”
  3. “name”
    127.0.0.1:6379> mget sex age name //同时查询多个变量
  4. “male”
  5. “19”
  6. “bob”
    127.0.0.1:6379> select 1 //切换库,0~15
    OK
    127.0.0.1:6379[1]> keys *
    (empty list or set)
    127.0.0.1:6379[1]> select 0
    OK
    127.0.0.1:6379> keys a?? //?代表通配符
  7. “age”
    127.0.0.1:6379> exists age //变量是否存在
    (integer) 1
    127.0.0.1:6379> ttl age //查询变量有效期,-1代表永久有效,-2代表过期,其他数字表示有效期剩余时间,单位是秒
    (integer) -1
    127.0.0.1:6379> type age //查询变量类型
    string
    127.0.0.1:6379> move age 1 //移动变量到其他库中
    (integer) 1
    127.0.0.1:6379> expire sex 10 //设置变量有效期,单位是秒
    (integer) 1
    127.0.0.1:6379> ttl sex
    (integer) 4
    127.0.0.1:6379> ttl sex
    (integer) 2
    127.0.0.1:6379> ttl sex
    (integer) -2
    127.0.0.1:6379> keys *
  8. “name”
    127.0.0.1:6379> set site bj
    OK
    127.0.0.1:6379> set sex female
    OK
    127.0.0.1:6379> keys *
  9. “sex”
  10. “site”
  11. “name”
    127.0.0.1:6379> del sex site //删除多个变量
    (integer) 2
    127.0.0.1:6379> flushdb //清除当前库
    OK
    127.0.0.1:6379> flushall //清除所有库
    OK
    127.0.0.1:6379> save //保存数据到硬盘
    OK
    127.0.0.1:6379> shutdown //在程序中结束进程
    not connected>

配置文件/etc/redis/6379.conf
常见配置

  • 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 //数据库目录

内存清除策略

  • volatile-lru //最近最少使用的(针对使用ttl设置的key)
  • allkeys-lru //删除最少使用的key(针对所有key)
  • allkeys-lfu //从所有key中删除使用频率最低的key
  • volatile-lfu //从所有配置了ttl的key中删除使用频率最低的key
  • volatile-random //在设置了ttl的key中随机删除
  • allkeys-random //在所有key中随机删除
  • volatile-ttl //移除最近过期的key
  • noeviction //不删除

[root@node50 redis-4.0.8]# /etc/init.d/redis_6379 stop
[root@node50 redis-4.0.8]# vim +501 /etc/redis/6379.conf //修改配置文件,密码,端口,IP
requirepass 123456
port 6350
bind 192.168.4.50
:wq
/etc/init.d/redis_6379 start
redis-cli -h 192.168.4.50 -p 6350 -a 123456 //登录redis
redis-cli -h 192.168.4.50 -p 6350 -a 123456 shutdown //关闭服务

部署LNMP+Redis
安装nginx,php,mariadb
yum -y install gcc pcre-devel zlib-devel
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure
make && make install
yum -y install php-fpm
yum -y install mariadb-server mariadb
yum -y install php php-mysql

修改nginx配置文件
[root@node57 ~]# 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;
}
wq
[root@node57 ~]# /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@node57 ~]# /usr/local/nginx/sbin/nginx //启动程序
[root@node57 ~]# systemctl start php-fpm
[root@node57 ~]# systemctl start mariadb

编写网页文件
[root@node57 ~]# vim /usr/local/nginx/html/test.php

:wq
[root@node57 ~]# firefox 192.168.4.57/test.php //测试

配置支持redis
[root@node57 ~]# yum -y install php-devel autoconf automake
[root@node57 phpredis-2.2.4]# tar -xf php-redis-2.2.4.tar.gz
[root@node57 phpredis-2.2.4]# cd phpredis-2.2.4/
[root@node57 phpredis-2.2.4]# phpize //生成配置文件php-config及configure命令
[root@node57 phpredis-2.2.4]# ./configure --with-php-config=/usr/bin/php-config
[root@node57 phpredis-2.2.4]# make
[root@node57 phpredis-2.2.4]# make install
Installing shared extensions: /usr/lib64/php/modules/

[root@node57 phpredis-2.2.4]# vim +728 /etc/php.ini //修改配置文件,支持redis模块
extension_dir = “/usr/lib64/php/modules/”
; On windows:
extension = “redis.so”
:wq
[root@node57 phpredis-2.2.4]# systemctl restart php-fpm
[root@node57 phpredis-2.2.4]# php -m | grep redis //查看php启动模块
redis

编写测试页面
vim /usr/local/nginx/html/redis.php

connect("192.168.4.50","6350"); $redis->auth("123456"); $redis->set("linux","redhat"); echo $redis->get("linux"); ?>

:wq
[root@node57 phpredis-2.2.4]# curl 192.168.4.57/redis.php //测试
redhat

你可能感兴趣的:(学习笔记)