zoukankan      html  css  js  c++  java
  • Ubuntu18.04,安装Redis配置远程连接访问和简单使用Redis

    前言

    Redis是常用基于内存的Key-Value数据库,比Memcache更先进,支持多种数据结构,高效,快速。用Redis可以很轻松解决高并发的数据访问问题;作为实时监控信号处理也非常不错。

    环境

    Ubuntu 18.04

    安装Redis服务器端

    ~ sudo apt-get install redis-server

    安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序

    检查Redis服务器系统进程

    ~ ps -agx|grep redis

    1 10382 10382 10382 ? -1 Ssl 124 0:07 /usr/bin/redis-server 127.0.0.1:6379
    2677 10618 10617 2677 pts/0 10617 S+ 1000 0:00 grep --color=auto redis

    通过启动命令检查Redis服务器状态

    ~ netstat -nlt|grep 6379

    tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 
    tcp6 0 0 ::1:6379 :::* LISTEN

    通过启动命令检查Redis服务器状态

    复制代码
    ~$ sudo /etc/init.d/redis-server status

    ● redis-server.service - Advanced key-value store
    Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2018-08-10 19:50:13 CST; 36min ago
    Docs: http://redis.io/documentation,
    man:redis-server(1)
    Main PID: 10382 (redis-server)
    Tasks: 4 (limit: 2294)
    CGroup: /system.slice/redis-server.service
    └─10382 /usr/bin/redis-server 127.0.0.1:6379

    8月 10 19:50:12 zhangkun-virtual-machine systemd[1]: Starting Advanced key-v…..
    8月 10 19:50:13 zhangkun-virtual-machine systemd[1]: redis-server.service: C…ry
    8月 10 19:50:13 zhangkun-virtual-machine systemd[1]: Started Advanced key-va…e.
    Hint: Some lines were ellipsized, use -l to show in full.

    复制代码

    通过命令行客户端访问Redis

    安装Redis服务器,会自动地一起安装Redis命令行客户端程序。

    在本机输入redis-cli命令就可以启动,客户端程序访问Redis服务器。

    复制代码
    ~ redis-cli

    127.0.0.1:6379> help
    redis-cli 4.0.9
    To get help about Redis commands type:
    "help @<group>" to get a list of commands in <group>
    "help <command>" for help on <command>
    "help <tab>" to get a list of possible help topics
    "quit" to exit

    To set redis-cli preferences:
    ":set hints" enable online hints
    ":set nohints" disable online hints
    Set your preferences in ~/.redisclirc


    127.0.0.1:6379> set key1 "hello"
    OK
    127.0.0.1:6379> get key1
    "hello"

    复制代码

    基本的Redis客户端命令操作

    增加一条记录key1

    复制代码
    redis 127.0.0.1:6379> set key1 "hello"
    OK
    
    # 打印记录
    redis 127.0.0.1:6379> get key1
    "hello"
    复制代码

    增加一条数字记录

    复制代码
     
    set key2 1
    OK
    
    # 让数字自增
    redis 127.0.0.1:6379> INCR key2
    (integer) 2
    redis 127.0.0.1:6379> INCR key2
    (integer) 3
    
    # 打印记录
    redis 127.0.0.1:6379> get key2
    "3"
     
    复制代码

    增加一个列表记录key3

    复制代码
     
    redis 127.0.0.1:6379> LPUSH key3 a
    (integer) 1
    
    # 从左边插入列表
    redis 127.0.0.1:6379> LPUSH key3 b
    (integer) 2
    
    # 从右边插入列表
    redis 127.0.0.1:6379> RPUSH key3 c
    (integer) 3
    
    # 打印列表记录,按从左到右的顺序
    redis 127.0.0.1:6379> LRANGE key3 0 3
    1) "b"
    2) "a"
    3) "c"
    复制代码
    复制代码

    增加一个哈希记表录key4

    复制代码
    复制代码
    redis 127.0.0.1:6379> HSET key4 name "John Smith"
    (integer) 1
    
    # 在哈希表中插入,email的Key和Value的值
    redis 127.0.0.1:6379> HSET key4 email "abc@gmail.com"
    (integer) 1
    
    # 打印哈希表中,name为key的值
    redis 127.0.0.1:6379> HGET key4 name
    "John Smith"
    
    # 打印整个哈希表
    redis 127.0.0.1:6379> HGETALL key4
    1) "name"
    2) "John Smith"
    3) "email"
    4) "abc@gmail.com"
    复制代码
    复制代码

    增加一条哈希表记录key5

    复制代码
    复制代码
    # 增加一条哈希表记录key5,一次插入多个Key和value的值
    redis 127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3
    OK
    
    # 打印哈希表中,username和age为key的值
    redis 127.0.0.1:6379> HMGET key5 username age
    1) "antirez"
    2) "3"
    
    # 打印完整的哈希表记录
    redis 127.0.0.1:6379> HGETALL key5
    1) "username"
    2) "antirez"
    3) "password"
    4) "P1pp0"
    5) "age"
    6) "3"
    复制代码
    复制代码

    删除记录

    复制代码
    复制代码
    # 查看所有的key列表
    redis 127.0.0.1:6379> keys *
    1) "key2"
    2) "key3"
    3) "key4"
    4) "key5"
    5) "key1"
    
    # 删除key1,key5
    redis 127.0.0.1:6379> del key1
    (integer) 1
    redis 127.0.0.1:6379> del key5
    (integer) 1
    
    # 查看所有的key列表
    redis 127.0.0.1:6379> keys *
    1) "key2"
    2) "key3"
    3) "key4"
    复制代码
    复制代码

    修改Redis的配置

    使用Redis的访问账号

    默认情况下,访问Redis服务器是不需要密码的,为了增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为redisredis。

    用vi打开Redis服务器的配置文件redis.conf

    ~ sudo vi /etc/redis/redis.conf
    
    #取消注释requirepass
    requirepass redisredis

    让Redis服务器被远程访问

    默认情况下,Redis服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。

    用vi打开Redis服务器的配置文件redis.conf

    ~ sudo vi /etc/redis/redis.conf
    
    #注释bind
    #bind 127.0.0.1

    修改后,重启Redis服务器。

    ~ sudo /etc/init.d/redis-server restart
    Stopping redis-server: redis-server.
    Starting redis-server: redis-server.

    未使用密码登陆Redis服务器

    ~ redis-cli
    
    redis 127.0.0.1:6379> keys *
    (error) ERR operation not permitted

    发现可以登陆,但无法执行命令了。

    登陆Redis服务器,输入密码

    复制代码
    ~  redis-cli -a redisredis
    
    redis 127.0.0.1:6379> keys *
    1) "key2"
    2) "key3"
    3) "key4"
    复制代码

    登陆后,一切正常。

    我们检查Redis的网络监听端口

    检查Redis服务器占用端口

    ~ netstat -nlt|grep 6379
    tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN

    我们看到网络监听从之前的 127.0.0.1:6379 变成 0 0.0.0.0:6379,表示Redis已经允许远程登陆访问。

    我们在远程的另一台Linux访问Redis服务器

    复制代码
    ~ redis-cli -a redisredis -h 192.168.1.199
    
    redis 192.168.1.199:6379> keys *
    1) "key2"
    2) "key3"
    3) "key4"
    复制代码

    远程访问正常。通过上面的操作,我们就把Redis数据库服务器,在Linux Ubuntu中的系统安装完成。

  • 相关阅读:
    java中equals和==的区别 (转)
    LoadRunner常见问题整理(转)
    python 遇到 syntaxerror: non-ascii character '/xd6' in file 我 教你解决 (python问题)(转)
    Linux查看系统性能命令
    android权限大全
    StringTokenizer类的使用
    web_reg_find()查询信息为变量
    Loadrunner执行Java脚本
    LoadRunner调用Java程序—性能测试
    Request Connection: Remote Server @ 192.229.145.200:80
  • 原文地址:https://www.cnblogs.com/yunlongaimeng/p/10260573.html
Copyright © 2011-2022 走看看