Redis Basic Applications
Redis is an open source, memory-based, persistent, Key-value database
Redis storage is divided into memory storage, disk storage, and log files in part Three
Configuration file has three parameters to configure it
Advantage:
Compared to memcached, it supports storage of the value type relatively more,
Includes strings,lists,zsets (sorted set) and hashes
Redis periodically writes updated data to disk or writes modifications to appended log files
The Master-slave (master-Slave) synchronization is implemented on this basis.
Redis Server
Server program: Redis-server
Client program: REDIS-CLI
Master configuration file:/etc/redis/redis_portnumber
Startup script:/root/redis-3.0.6/utils install_server.sh
Deploying REDSI
[[email protected] ~]# yum-y install GCC
[Email protected] ~]# TAR-XF redis-3.0.6.tar.gz
[Email protected]]# CD redis-3.0.6
[[email protected] redis-3.0.6]# make
[[email protected] redis-3.0.6]# make install
[[email protected] utils]#./install_server.sh (Start)
port:6379
Config File:/etc/redis/6379.conf
Log File:/var/log/redis_6379.log
Data dir:/var/lib/redis/6379
Executable:/usr/local/bin/redis-server
Cli executable:/USR/LOCAL/BIN/REDIS-CLI
[[email protected] ~]# redis-cli (test server)
127.0.0.1:6379> Ping
PONG
[Email protected] ~]# REDIS-CLI
127.0.0.1:6379> set Hydra xxx (write data)
Ok
127.0.0.1:6379> Get Hydra (check data)
"XXX"
——————————————————————————————————————————————
Database Operations directives
String string manipulation
Set name value [ex seconds] [px milliseconds] [nx|xx]: Set name and value, expiration time can be set to seconds or milliseconds
NX operates on name only if name does not exist
XX only name exists to operate on name
。。。。。。。。
STRLEN Name: statistic string length
Append Name Value: The character exists is appended, does not exist and is created
Setbit Name Offset value: Sets or clears a bit (bit) on a specific offset to the string stored by name
Bitcount Name: Number of bits in the statistics string that are set to 1
DECR Name: The value in name minus 1,name does not exist then it is initialized to 0, minus 1
INCR Name: Add the value of name to 1, if name does not exist, then initialize it to 0 and then add 1, the main application is the counter
GetRange name start end: Returns the substring in the string value, with a range of start and end
Getbit name offset: The value in name, gets the bit on the offset, the offset is larger than the string length, or the name does not exist, returns 0
Incrbyfloat name Increment: Adds a floating-point increment to the value stored in Naem increment
Mget name name2: Multiple values can be obtained
Mset Naem name2: Multiple values can be set
Hash table
Redis Hash is a string-type field and value mapping table
A name can correspond to multiple filed, one field (column) corresponds to one value (value)
Stores an object as a hash type, saving memory more than storing each field as a string type
Hmset Name column 1 value 1 column 2 value 2; Assigning values to multiple columns in the hash table
Hget Name column; Gets the value of the column in the hash table
Hsetnx Name column value, assigning a value to the hash table column when the column does not exist
Keys *; View all data
List (advanced post-out)
The Redis list is a character queue, and a key can have multiple values
List operations (refer to Manual for specific operation)
Lpush Name Value [value ... ]: Insert a box or more values value into the table header of the table name, name does not exist, then create the name
127.0.0.1:6379> Lpush xx a b C (List1 value followed by C B a)
Lrange name start stop: read from the start location to the value of name to stop end
127.0.0.1:6379> lrange xx 0-1
1) "C"
2) "B"
3) "A"
Lpushx Name Value: Inserts a value into name only if name exists, otherwise returns an empty
Lpop Name: Removes and returns a list header element data that returns nil if name does not exist
Llen Name: Returns the length of the list name
Introduction to Set Sets
The set type is a character set that has no sort, and, like the list type, can be added on a data value of that type
Delete and other actions
Unlike the list type, duplicate elements are not allowed in the set collection.
Refer to the Redis Manual for specific operations
————————————————————————————————————————————————————————————
Redis Advanced Applications
Server settings
Configuration file Resolution
[Email protected] ~]# vim/etc/redis/6379.conf
MaxMemory <bytes>: Max memory
Maxmemory-policy VOLATILE-LRU: Use Lur algorithm to clean up old data when memory is full
Daemonize Yes: Daemon
Pidfile/va/run/redis_6379.pid: Process PID
Port 6379: Port number
Timeout 300: Link time-out
LogLevel Notice: Log level
Logfile/var/log/redis_6379.log: Log file
Databases 16: Number of databases
Save 900 1: Database Mirroring frequency
Dbfilename Dump.rdb: Mirrored backup file name
/va/lib/redis/6379: Backup file path
Save 900 1
Save 300 10
Save 60 10000
Note: If there are 10,000 keys, the mirrored backup
Otherwise, if there are 10 keys in 300 seconds, the mirrored backup
Otherwise, if there are 1 key changes in 900 seconds, the mirrored backup
Slaveof <masterip> <masterport>: Set the primary server IP and port, and actively synchronize data with the primary server
Masterauth <master-password>: master-slave authentication password
Requirepass foobared: After the client connects to the server, it needs to enter the password before doing other operations
MaxClients 10000: Maximum number of client concurrent connections
MaxMemory <bytes>: Maximum memory usage
Master server operations:
Install deployment software
[[email protected] lnmp_soft]# tar-xf redis-3.0.6.tar.gz
[[email Protected] lnmp_soft]# CD redis-3.0.6
[[email protected] redis-3.0.6]# make
[[email protected] redis-3.0.6]# make install
[[email protected] redis-3.0.6]#./utils/install_server.sh (Initialize)
Modify the configuration file
[[ Email protected] redis-3.0.6]# vim/etc/redis/6379.conf
Requirpass 123456 (create master server password)
[[email Protected] redis]# vim/etc/init.d/redis_6379 (Modify startup script)
$CLIEXEC-a 123456-p $REDISPORT shutdown (-a password)
[[email& Nbsp;protected] redis-3.0.6]#/etc/init.d/redis_6379 restart
Operation from server
Write data is not supported by default from the server, read-only
[Email protected] lnmp_soft]# TAR-XF redis-3.0.6.tar.gz
[Email protected] lnmp_soft]# CD redis-3.0.6
[[email protected] redis-3.0.6]# make
[[email protected] redis-3.0.6]# make install
[[email protected] redis-3.0.6]#./utils/install_server.sh (Initialize)
Modifying a configuration file
[Email protected] redis-3.0.6]# vim/etc/redis/6379.conf
Slaveof 192.168.2.100 6379 (primary server IP port)
Masterauth 123456 (master server password)
[Email protected] redis-3.0.6]#/etc/init.d/redis_6379 restart
——————————————————————————————————————————————————
Linux Redis Base Application master server configuration