Basic introduction to Redis database and primary and resolve problems from building structures and clusters relationship

Copyright: Changan white https://blog.csdn.net/weixin_44074810/article/details/90722628

A, Redis Introduction

Redis is an open source use written in ANSI C, support network, based on the persistence of memory can log type, Key-Value database,
and provides multi-lingual API.

1. non-relational databases (nosql)
data are stored in 2.nosql Key Values form
3.NoSQL in a considerable number of product categories:
Redis
Mongodb
Hbase hadoop
Cassandra hadoop

  1. NoSQL and SQL database comparison:

    1. Different application scenarios: sql database suitable for particularly complex relational data query scenarios, nosql vice versa
    2. Transactional features: sql transaction support very well, and basically does not support transactions nosql
    3. Both constantly learn from each other, showing the fusion trend
  2. Redis properties

    1. Redis and other key - value caching product has the following three characteristics:
    2. Redis supports data persistence, data in memory can be saved to disk, reboot to load when you can be reused.
    3. Redis only supports simple key-value data types, while also providing a storage list, set, zset, hash and other data structures.
    4. Redis supports backup data, i.e., data backup master-slave mode.
  3. Redis advantage

    1. High performance - Redis can read as fast as 110,000 times / s, write speed is 81000 times / s.
    2. Rich data types - Redis supports binary case Strings, Lists, Hashes, Sets and Ordered Sets the data type of the operation.
    3. Atom - Redis All operations are atomic.
    4. Feature-rich - Redis also supports publish / subscribe, notification, key expiration and so on characteristics.

7.Redis application scenario
1 is used as cache (ehcache / memcached) - all data is placed in memory redis (the database memory)
2. alternative to traditional database in certain scenarios - such as social class application
3. in some large systems, skillfully achieve some specific features: session sharing, shopping cart

Two, redis configuration
under Ubuntu configuration Redis
# View configuration information
1. sudo vi /etc/redis/redis.conf
# Bind ip
2. the bind 127.0.0.1
# end-connector
3. Port 6379
# whether the daemon when running a
yes 4.daemonize
# find the configuration file
5. cd / etc / Redis /
# open Redis
6. Redis-Server redis.conf
# to see whether there will be help to check whether the open succeeded
7. redis-server --help

Third, the personal operation habits
server-side operations:
1. PS the AUX | grep view redis redis server processes
2. sudo kill -9 pid kill redis server
3. sudo redis-server /etc/redis/redis.conf specified load profile

Client Operating:

  1. Can use the help see the help documentation that
    redis-cli --help
  2. Connecting Redis
    Redis-CLI
  3. Switching database
    select the database name (0-15)

Four, of the type described redis
1. string type

Redis string type is the most basic type of data storage, it is binary safe in Redis, this would mean that the type can accept data in any format,
such as JPEG image data or Json object description information. Data type Value length of the string can accommodate up in Redis is 512M.

Setting key
set key value
set as the key name is itcast data: set name itcast

Increase: the SET
the SET name zhangsan

Append: the append
the append ---- Key value> is added to the value a1 bond is haha: append a1 haha

Increasing the plurality of values: MEST
MSET name Age 15 zhangsan

Review: when the existence of this data in the database is performed again set to modify the data tantamount

Delete: del
del name

Query: GET
GET name

Query multiple values: mget
query the current value of all library: keys *

Set an expiration time: SETEX
SETEX key seconds The value
set is the key value name nishi expiration time ten seconds
setex name 10 nishi
view the key value is the name nishi time expired
ttl nishi

setnx: when the command is written to the key value does not exist, a data can be written, if the key value already exists no operation

2. Key Commands

Find all keys: keys *

View name contains a key: keys a *

Determining whether there is a key, if present, returns 1 0 absent: key1 EXISTS
EXISTS key1: A1 EXISTS

See Types of value corresponding to the key: type key
see type key value of a1, five types of species ⼀ held in redis ⽀: type a1

Delete keys and corresponding values: del key1 key2 ...
delete key a2, a3: del a2 a3
delete key time value will also be deleted

Set expiration time, in seconds: expire key seconds
expiration time of 3 seconds key a1: expire a1 3

See effective time, in seconds: ttl key
to view the remaining effective time of a1: ttl a1

When viewing the display is -1 exists
when viewing the display is deleted -2

2. the hash type (somewhat similar to a dictionary corresponding to a key value)
1. Use the hash stored in the object structure of the object attribute, the value of
the type 2 is a string value

Providing a single property
hset key field value

Increased user setting key attribute name is itheima
HSET user name itheima

      设置多个属性
      hmset key field1 value1 field2 value2 ... 
      hset user name itheima age 12 gender 

      设置键u2的属性name为itcast、属性age为11
      hmset u2 name itcast age 11

There is the equivalent of a user and u2 in this class is the class name inside the increase: xxx, Age: xx, other property

View: View user key value is the attribute name
hget user name
See plurality of attribute value
hmget user name age
view all current information (corresponding to view all keys and key values corresponding to the current of this class)
hgetall user --- -> will all corresponding output value
Example: 127.0.0.1: 6379> hgetall Person
. 1) "name"
2) "a"
. 3) "Age"
. 4) "14"
Get all properties specified key (key) : hkeys user
to get all the value of the property (value): hvals user

Review: when the existence of this data in the database is performed again hset tantamount to modify the data

Delete: delete a key in this class: hdel user name
delete this category will delete all the data in this category: del user

3. list type
element 1. The list of type String
2. Insert sorted in the order

(This value can be added to add multiple data) increased

In the left insert data: lpush key value1 value2 ...
from the key to the left of a1 filling with the data list a, B, C
LPUSH Last a1 ABC is displayed: cba, so that a first insert is a first innermost b so that two insertion b is in the middle of the last inserted at the beginning of the c c so

Data inserted in the right START: rpush key value1 value2 ...
from the list on the right bond is filling with data a1 0,1
Last rpush a1 0 1 shows a cba 0 1
This is the original list of the right to add new data, and the first increase is 0 so 0 is next to a, so that 1 is added at the end of the rightmost 1

Before or after the specified element Insert new elements: linsert key before or after an existing element of the new element
before: Before what elements
after: after what elements
in the list of the key front of the elements a1 filling with b. 3
linsert before a1 b 3 shows the results of a: c 3 ba 0 1

See (Lrange)
Returns the list of elements in the specified range ⾥: Start Lrange STOP Key
1. Start, STOP is the index of the element index
from the left index 2, the first frame element 0
3. Indices may be negative, showing count from the end, such elements last frame -1 means
acquires a list of all key elements of a1:
this is somewhat similar to list some of the values may be used and the subscript index is from zero, is equivalent to 0 to -1 See all
lrange a1 0 -1

Setting element values ​​specified index

  1. Index from the left side, to the first frame element 0
  2. Indices may be negative, showing the tail counted as -1 for last frame elements

Review: lset key index value
modification keys in the list of subscripts a1 element value is 1, Z
LSET A 1 Z
original list is displayed as: c 3 ba 0 1
Last result is displayed: czba 0 1

Deleted (removed using a: remove)
Removes the specified elements: Key lrem count value
1. The value of the value list element count times before removal appears
2. count> 0: the end to remove the head
3. count < 0: to remove the head from the tail
4. count = 0: remove all
list the delete key on the left of a1 Z
lrem. 1 Z a1
final result is displayed: cba 0 1

  1. A2 START added to the list of elements A, B, A, B, A, B
    LPUSH a2 ABABAB
  2. Began to be removed from the list on the right side a2 2 b
    lrem a2 b -2
  3. View a list of all the elements a2
    lrange a2 0 -1

4. SET type
1. ⽆ ordered set
2. Elemental type string
3. ⼀ element has a unique, non-repeating
4. Definitions: For the set operation is not modified

Increase the
added element (also can be continuously added)
Sadd Key member1 member2 ...

   向键a3的集合中添加元素zhangsan、lisi、wangwu
   sadd a3 zhangsan sili wangwu

View
View all the elements
smembers key

  获取键a3的集合中所有元素
  smembers a3

Delete (also used to remove: remove)
Removes the specified element
srem key

   删除键a3的集合中元素wangwu
   srem a3 wangwu

5. zset Type
1. sorted set, the ordered set
2. Elemental a string type
has a unique property ⼀ 3. elements will not be repeated
are associated 4. Each element of type double ⼀ Score, indicates a weight by weight of the elements from the Sort ⼩ below approximately
5. Description: no modification operation

Increase
zadd key score1 member1 score2 member2 ... (there is a value similar to number score as the score, for example: 100 Zhang, 90 John Doe is there sorted according to the scores of small to large)

  向键a4的集合中添加元素lisi、wangwu、zhaoliu、zhangsan,权重分别为4、5、6、3
  zadd a4 4 lisi 5 wangwu 6 zhaoliu 3 zhangsan

See
1. Return element within a predetermined range
2. start, stop index element for the index
3. index from the left, 0 is the first frame element
4. Indices may be negative, indicating the start count from the end, such as -1 It represents the last frame elements

zrange key start stop

Obtaining a collection of all elements in the key a4
zrange a4 0 -1

See members score value between min and max of
zrangebyscore key min max

Get the value of the key member of the collection authority a4 is between 5 and 6
zrangebyscore a4 5 6

View member member of the score value
zscore key member

Get the right set of key elements of zhangsan a4 heavy
zscore a4 zhangsan

Delete (also equivalent to removing: remove)
Removes the specified element
zrem key member1 member2 ...

   删除集合a4中元素zhangsan
   zrem a4 zhangsan

   删除权重在指定范围的元素
   zremrangebyscore key min max

   删除集合a4中权限在5、6之间的元素
   zremrangebyscore a4 5 6

Five, python interact with
can be used StrictRedis string type data objects CRUD

在桌面上创建redis目录
使用pycharm打开 redis目录
创建redis_string.py文件
from redis import *
if __name__=="__main__":
    try:
        #创建StrictRedis对象,与redis服务器建⽴连接
        sr=StrictRedis()

    except Exception as e:
        print(e)

1. string- increase
Remedies set, add key, value added if successful return True, if you add it returns False fails
to write code as follows:

from redis import *
if __name__=="__main__":
    try:
        #创建StrictRedis对象,与redis服务器建⽴连接
        sr=StrictRedis()
        #添加键name,值为itheima
        result=sr.set('name','itheima')
        #输出响应结果,如果添加成功则返回True,否则返回False
        print(result)
    except Exception as e:
        print(e)

2. string- Get
Remedies get, adding a value corresponding to the key, if the key there is a corresponding value is returned, if the key does not exist is returned None
write code as follows:

from redis import *
if __name__=="__main__":
    try:
        #创建StrictRedis对象,与redis服务器建⽴连接
        sr=StrictRedis()
        #获取键name的值
        result = sr.get('name')
        #输出键的值,如果键不存在则返回None
        print(result)
    except Exception as e:
        print(e)

3. string- modify
Remedies SET, if the key already exists into ⾏ modification, if the key does not exist added into ⾏
write code as follows:

from redis import *
if __name__=="__main__":
    try:
        #创建StrictRedis对象,与redis服务器建⽴连接
        sr=StrictRedis()
        #设置键name的值,如果键已经存在则进⾏修改,如果键不存在则进⾏添加
        result = sr.set('name','itcast')
        #输出响应结果,如果操作成功则返回True,否则返回False
        print(result)
    except Exception as e:
        print(e)

4. string- delete
Remedies delete, delete key and the corresponding value, if successful delete key number affected is returned, otherwise 0 is returned
to write code as follows:

from redis import *
if __name__=="__main__":
    try:
        #创建StrictRedis对象,与redis服务器建⽴连接
        sr=StrictRedis()
        #设置键name的值,如果键已经存在则进⾏修改,如果键不存在则进⾏添加
        result = sr.delete('name')
        #输出响应结果,如果删除成功则返回受影响的键数,否则则返回0
        print(result)
    except Exception as e:
        print(e)

5. Obtain key
Remedies keys, key acquired according to the expression n
to write code as follows:

from redis import *
if __name__=="__main__":
    try:
        #创建StrictRedis对象,与redis服务器建⽴连接
        sr=StrictRedis()
        #获取所有的键
        result=sr.keys()
        #输出响应结果,所有的键构成⼀个列表,如果没有键则返回空列表
        print(result)
    except Exception as e:
        print(e)

Master-slave

  1. ⼀ a master can have more than one slave, a slave ⼀ separate warranty may have more than one slave, it goes on, forming a strong zoomed multi-level server cluster architecture
  2. master used to write data, slave to read data, the statistics: literacy ratio site is 10: 1
  3. The configuration may be implemented by separate read and write from the primary
  4. master and slave is a redis example (redis service)
搭建主从
1. 配置主:
	1. 查看主机的ip:ifconfig
	2. 修改/etc/redis/redis.conf文件
	    1. sudo vi redis.conf
	    2. bind 192.168.26.128
	3. 重启redis服务
	    1. sudo service redis stop
	    2. sudo redis-server redis.conf

2.配置从
	1. 复制/etc/redis/redis.conf文件
	    1. sudo cp redis.conf ./slave.conf
	2. 修改redis/slave.conf文件
	    2. sudo vi slave.conf
	3. 编辑内容
	    1. bind 192.168.26.128
	    2. slaveof 192.168.26.128 6379
	    3. port 6378
	4. redis服务
	    1. sudo redis-server slave.conf
	5. 查看主从关系
	    1. redis-cli -h 192.168.26.128 info Replication
3.数据操作 
	1. 在master和slave分别执⾏info命令,查看输出信息 进入主客户端
	    1. 在master和slave分别执⾏info命令,查看输出信息 进入主客户端
	2. 进入从的客户端
	    1. redis-cli -h 192.168.26.128 -p 6378
	3. 在master上写数据
	    1. set aa aa
	4. 在slave上读数据
	    1. get aa

搭建主从时遇到的一个问题:
zongwenbo@ubuntu:/etc/redis$ redis-cli -h 192.168.13.39 info replication
DENIED Redis is running in protected mode because protected mode is enabled,
no bind address was specified, no authentication password is requested to clients.
In this mode connections are only accepted from the loopback interface.
If you want to connect from external computers to Redis you may adopt one of the following solutions:

  1. Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
  2. Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no’, and then restarting the server.
  3. If you started the server manually just for testing, restart it with the '-protected-mode no' option 4) Setup a bind address or an authentication password NOTE:.. You only need to do one of the above things in order for the server to start accepting connections from the outside .
    translation: rejected Redis running in protected mode, because the protected mode is enabled, do not specify a bind address,
    no password authentication request to the client. In this mode, connected to only accept the loopback interface.
    If you want to connect from an external computer to repeat the solution you might take:
    1) just disable protected mode sends configuration setting protection mode command is not from the loopback interface to connect to repeat the same host server is running, however, to ensure that repeat, not public from If you access the Internet do so. Use the configuration rewrite this change permanent.
    2) Or, you can configure Redis by editing the file, the protected mode option is set to "no", then restart the server, thereby disabling protected mode.
    3) If you are just to test and manually start the server, use the "--protected-mode no" option to restart it. Set the binding address or password authentication. Note: In order for the server begins accepting connections from the outside, you only need to perform an action above.
解决方法:
zongwenbo@ubuntu:/etc/redis$ redis-cli
127.0.0.1:6379> config set protected-mode no
OK
127.0.0.1:6379> config get protected-mode
1) "protected-mode"
2) "no"

Create a cluster

  1. redis installation package contains redis-trib.rb, Use to create a cluster

  2. Next, operation of the machine in progress ⾏ 172.16.179.130

  3. To copy commands, so you can cut in any of Contents Use this command
    sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb / usr / local / bin /

  4. Install ruby environment, because redis-trib.rb Using the ruby is developed
    sudo apt-get install ruby

  5. redis-trib.rb create --replicas 1 172.16.179.130:7000 172.16.179.130:7001 172.16.179.130:7002 172.16.179.131:7003 172.16.179.131:7004 172.16.179.131:7005

  6. Perform this command on ⾯ on some machines may be error, mainly due to the latest version of the ruby is not installed!
    Heavenly anti-in fire wall download ⽆ law led to the latest version, so you need to set gem Source

  7. Solution:
    - First View Your Own gem What is the source address
    gem source -l - need to be replaced if it is https://rubygems.org/

    - change command to
    GEM Sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

    - mounting the depending redis by GEM
    the sudo GEM the install redis

    - Perform the command and then re
    And then if you add sudo prompt enough authority
    to verify the success of building
    the Using 3 Masters
    192.168.26.128:7000
    192.168.26.128:7001
    192.168.26.128:7002
    this is the primary server prompts 7000,7001,7002

  8. 7003 172.16.179.131 connector on the machine, add parameters connected to a cluster represented -c
    redis-cli -h 172.16.179.131 -c -p 700

  9. Writing data
    set name itheima
    will automatically adjust to the primary server, and data is written successfully

Guess you like

Origin blog.csdn.net/weixin_44074810/article/details/90722628