Nosql Redis Database

1. Introduction to the basics of redis
2. Advanced redis applications

1. Concepts
Redis is an open source key-value, which is both a cache and a storage. It supports Persistence, with the help of sentinel to achieve a certain meaning of high availability, data structure server: string, list, hash, set, sorted set, bitmap, hyperloglog
nosql Four genres:
key-value key-value type: Memcached redis
documemtation document type: Mongodb
columu family column type: Hbase
graph image type: Neo4j

Second, install redis
download link www.redis.io redis-3.0 .2-1.el6.remi.x86_64.rpm
rpm -ivh redis-3.0.2-1.el6.remi.x86_64.rpm
rpm -ql View installation files
cp /etc/redis .conf{,.bak}
vim /etc/redis.conf configuration file introduction
daemonize no daemon process
port 6379
tcp-backlog 511 #tcp request waiting queue
bind 127.0 .0.1 The default listening address, you need to specify your own ip address
such as bind 127.0.0.1 192.168.100.6
timeout 0 The client link timeout time, 0 means that this function is disabled and will not time out
loglevel notice log Level
logfile /var/log/redis/redis.log log file
databases 16 redis support (0-15 libraries), select 0 select 0 library, distributed does not support multi-database form
— —————Snapshot———————————- ———
save seconds changes
save 900 1 There is one record sending change in 900 seconds, record a snapshot
save 300 10
save 60 10000
save ” ”Means that the persistence function is forbidden, only the cache is used, and the above three lines need to be commented.
—————-Master-Slave———— ——————–
slaveof masterip masterport
salveof master ip master port, if this is started, it is the slave server, otherwise the master
only this When the item is enabled, the following master-slave parameters will take effect.
—————–SECYRUTTY Security—————————- ————-
requirepass foobared
——————-LIMITS limit——– ——————————————
maxclients 10000 Maximum concurrent clients Number of terminals
maxmemory Maximum memory space capacity used
——————-APPEND ONLY MODE———– ——————–
appendonly no is disabled by default, which is equivalent to binary log

3. Common commands
#redis -cli -h View help
#redis-cli -h 192.168.100.6 Remote link
redis>help @STRING View which commands are available and which version they come from
redis>help set
redis>set key Value such as set name lisan
redis>get key
“lisan”

Four. Advanced Application
How does redis realize the authentication function, in the configuration file vi etc/redis.conf< br>requirepass 123456 #123456 is the password, it will take effect after restarting
#redis-cli -h 192.168.100.6

select 0, prompt authentication information
AUTH 123456
OK, authentication Through

Clear the database:
FLUSHDB: Clear the current database FLUSHALL: Clear all libraries

Redis persistence: RDB and AOF, which depends on Our purpose
RDB: snapshot, binary format, according to a pre-customized strategy, periodically save data to disk, the data file defaults to dump.rdb, the save command is to customize the save cycle. The client can also display the use of the save or bgsave command to start the snapshot saving mechanism.
save, synchronous, save the snapshot in the main thread, at this time all client requests will be blocked,
bgsave, asynchronous, the main process copies a child process, and the child process saves the data without affecting business use
AOF: Append Only File, record every write operation to the end of the specified file to achieve persistence. When redis restarts, you can re-execute the command in the file to rebuild the database in memory, so this file will become more and more Large, BGREWRITEAOF: AOF file rewrite, it will not read the AOF file in use, but save the data in the memory to a temporary file by command, and replace the original AOF file after completion.

AOF rewriting process:
1. The redis main process creates a child process through fork
2. The child process creates a database reconstruction command sequence based on the data in redis memory in a temporary file
3. The parent process inherits the client's request, and will continue to append these requested write operations to the original AOF file. In addition, these new write requests will also be placed in a buffer queue.
4. When the child process is re-completed, the parent process will be notified; the parent process writes the commands in the buffer to a temporary file
5. The parent process replaces the old aof with a temporary file

Parameters:
appendonly no
appendfilename appendonly.aof

appendfsync always writes the AOF file to the disk every time it receives a write command, which affects performance.
appendfsync everysec is executed every second Write to disk aof, recommended operation
appendfsync no

Note that persistence itself cannot replace backup. You should also develop a backup strategy to regularly back up the redis database.
RDB and AOF are enabled at the same time:
1. BGSAVE and BGREWRITEAOF will not be executed at the same time.
2. AOF will be used first when the Redis server is started to restore data.

Replication features:
1. A master can have multiple slaves
2. Chain replication is supported
3. The master synchronizes data to the salve in a non-blocking manner
The replication process:< br /> 1. After starting Salve, send a synchronization request from the library to synchronize the data of the main library, and the main library starts a background sub-process to save the data snapshot in the local data file and send the data file to the Salve
2. Salve After receiving the data file, save it locally and load it into the memory to complete the data synchronization

Leave a Comment

Your email address will not be published.