Redis installation and use basis

A redis official website installation         online demo

 1 wget http://download.redis.io/releases/redis-4.0.9.tar.gz

 2 tar xzf redis-4.0.9.tar.gz

***ubuntu  --->1 sudo mv ./redis-4.0.9 /usr/local/redis/

                       2 cd /usr/local/redis/

                       3 sudo make  4  sudo make test    5  sudo make install

                       6 sudo cp /usr/local/redis/redis.conf /etc/redis/ mobile configuration file

3 redis configuration information http://blog.csdn.net/ljphilp/article/details/52934933

Successful installation diagram

Install and run redis under windows (compressed package method) to   add environment variables

1. Although redis is started above, as long as the cmd window is closed, the redis service will stop. So redis must be set as a service under windows.

2. Set service commands

redis-server --service-install redis.windows-service.conf --loglevel verbose

Two  Redis server-side and client-side commands

  • The server-side command is redis-server

  • You can use help to view the help file

  • redis-server --help

  • ps aux | grep redis View the redis server process
    sudo kill -9 pid Kill the redis server
    sudo redis-server /etc/redis/redis.conf Specify the loaded configuration file
  • The client's command is redis-cli
  • You can use help to view the help file
  • redis-cli --help
  • Run test command ping
  • Connect to redis redis-cli select database select 10

  • The database has no name, there are 16 by default, which are identified by 0-15, and the first data is selected by default when connecting to redis

Check whether redis is started

Then click服务与应用 --> 服务 --> 查找redis的服务----- windows

Type  ps -ef |grep redis-------------->linux

netstat -lntp | grep 6379

./etc/init.d/redis-server start

./etc/init.d/redis-server stop

./etc/init.d/redis-server restart

Open the path to install redis in cmd

1. Start the redis server in the local path and specify the configuration file
redis-server.exe redis-windows.conf
2. Start the redis client, specify the ip, port, and password

Run the redis-cli.exe in the folder in the local path to start the client
redis-cli.exe redis-cli after startup
redis-cli.exe shutdown
 

Three Click on the official website to view the command file http://redis.cn/commands.html

There are five types of values:

  • String, hash, list
  • Set set, ordered set zset

Four interact with python

  There are 3 ways to install Redis https://github.com/andymccurdy/redis-py

  • The first type: enter the virtual environment pip install redis
  • The second type: enter the virtual environment easy_install redis
  • The third type: download the source code of the redis package from the official website-client, and install it using the source code
  • Execute wget step by step  https://github.com/andymccurdy/redis-py/archive/master.zip
    unzip master.zip
    cd redis-py-master
    sudo python setup.py install
  • from redis import *
    """这个模块中提供了StrictRedis对象(Strict严格),⽤于连接redis服务器,并按照不同类型提供 了不同⽅法,进⾏交互操作
    """
    
    sr = StrictRedis(host='localhost', port=6379, db=0)
    
    """
    通过init创建对象,指定参数host、port与指定的服务器和端⼝连接,host默认为localhost,port默认为6379,db默认为0
    """
    简写
    sr=StrictRedis()
    
    
    # 测试
    from redis import *
    if __name__=="__main__":
        try:
            #创建StrictRedis对象,与redis服务器建⽴连接
            sr=StrictRedis()
            #添加键name,值为test
            result=sr.set('name','test')
            #输出响应结果,如果添加成功则返回True,否则返回False
            print(result)
      
            result = sr.get('name')
            #输出键的值,如果键不存在则返回None
            print(result)
    
            result = sr.delete('name')
            #输出响应结果,如果删除成功则返回受影响的键数,否则则返回0
            print(result)
    
            #获取所有的键
            result=sr.keys()
            #输出响应结果,所有的键构成⼀个列表,如果没有键则返回空列表
            print(result)
        except Exception as e:
            print(e)

     

Guess you like

Origin blog.csdn.net/weixin_42322206/article/details/105258893