Linux连接redis数据库

  • Post author:
  • Post category:linux


redis至少开两个窗口,一个服务器,一个客户端

服务器




解压redis
    chu@root:~$ cd Desktop/
    chu@root:~/Desktop$ tar -zxvf redis-4.0.2.tar.gz -C soft
    chu@root:~/Desktop$ cd soft
    chu@root:~/Desktop/soft$ cd redis-4.0.2/

查看日志介绍
    chu@root:~/Desktop/soft/redis-4.0.2$ vim README.md

make
    chu@root:~/Desktop/soft/redis-4.0.2$ make

测试make配置
    chu@root:~/Desktop/soft/redis-4.0.2$ make test

查看文件
    chu@root:~/Desktop/soft/redis-4.0.2$ ls

进入src
    chu@root:~/Desktop/soft/redis-4.0.2$ cd src/

进入服务器
chu@root:~/Desktop/soft/redis-4.0.2/src$ ./redis-server


客户端


客户端

进入redis的src

    chu@root:~$ cd Desktop/
    chu@root:~/Desktop$ cd soft
    chu@root:~/Desktop/soft$ ls
    jdk-9.0.1  pycharm-2017.2.4  Python-3.6.3  redis-4.0.2
    chu@root:~/Desktop/soft$ cd redis-4.0.2/
    chu@root:~/Desktop/soft/redis-4.0.2$ ls
    chu@root:~/Desktop/soft/redis-4.0.2$ cd src/

进入客户端
    chu@root:~/Desktop/soft/redis-4.0.2/src$ ./redis-cli

    ping一下,链接服务器
        127.0.0.1:6379> ping
        PONG
    测试,set发送信息,get接受信息,set是字典类型
        127.0.0.1:6379> set username 'chu'
        OK
        127.0.0.1:6379> get username
        "chu"
        127.0.0.1:6379> 

安装驱动

1, 退出src,进入conf
    chu@root:~/Desktop/soft/redis-4.0.2$ vim redis.conf
2, 关掉保护模式(可以让所有人都连接这台主机)
    在普通模式下,关闭bind

这里写图片描述

这里写图片描述

修改密码

在普通模式下,然后/requirepass搜索,修改密码

这里写图片描述

这里写图片描述

这里写图片描述

完成之后,重启服务器

首先关闭

ctrl+c

然后重启

./redis-server ../redis.conf

链接数据库

chu@root:~/Desktop/soft/redis-4.0.2/src$ ./redis-cli -h 192.168.241.132 -p 6379

密码

auto rootroot

python操作Redis

1. 创建项目
2. 安装驱动程序
   - pip install  redis
   - pip install python-redis

依赖解决

1. 缺少gcc,安装发送的三条依赖

    sudo apt-get install libxml2-dev libxslt1-dev python-dev
    sudo apt-get install zlib1g-dev
    sudo apt-get install libevent-dev

2. python3的依赖
    sudo apt install python3-dev

3. 如果还是继续gcc,安装所有依赖
    sudo apt-get install build-essential autoconf libtool pkg-config python-opengl python-imaging python-pyrex python-pyside.qtopengl idle-python2.7 qt4-dev-tools qt4-designer libqtgui4 libqtcore4 libqt4-xml libqt4-test libqt4-script libqt4-network libqt4-dbus python-qt4 python-qt4-gl libgle3 python-dev python3-dev

4. 使用过程中尽量别升级,容易出现版本不匹配情况

'''
    链接redis
        1,导入redis
        2,创建redis客户端
            redis.redis
            redis.strictredis  推荐使用 和sql一样操作基本一样

'''

#导入库
import redis


#创建链接
client = redis.StrictRedis(host = 'chuhuan',passwd = 'rootroot')

print(client.get('name').decode('utf-8'))



版权声明:本文为weixin_40688956原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。