Redis Cache Database Server
Redis is an open source technology and memory can also be persisted in the log-type, Key-value database
The Redis storage is divided into memory storage, disk storage, and log files, which are configured with three parameters in the configuration file.
Advantage:
It is more secure and supports storing more value types than mecached.
Redis periodically writes updated data to disk or writes modifications to appended records
Master-slave (Master-Slave) synchronization is implemented on this basis.
/var/log/redis_6379.log log files
/var/lib/redis/6379 Data Catalog
/usr/local/bin/redis-server redis Server Software storage path
vim/etc/redis/6379.conf configuration file
.....
MaxMemory <bytes> Max Memory
Maxmemory-policy VOLATILE-LRU when memory is full, use the LRU algorithm to clean up old data
Daemonize Yes daemon
PIDFILE/VAR/RUN/REDIS_6379.PID Process PID
Port 6379 Port number
Timeout 300 connection time-out
LogLevel Notice Log level
Logfile/var/log/redis_6379.log log files
Databases 16 Number of databases
Save 900 1 Database Mirroring frequency
Dbfilename Dump.rdb Image backup file name
/var/lib/redis/6379 backup File path
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 client to connect to the server, you need to enter the password before doing other operations
MaxClients 10000 maximum number of client concurrent connections
MaxMemory <bytes> Maximum Memory usage
key匹配>keys * 显示所有key>keys h?llo ?匹配任意一个字符>keys h*llo *匹配任意多个字符>keys h[ae]llo 匹配hello和hallo
Flushall
Clear All data
select id 选择数据库,id用数字指定,默认数据库为0 >select 0 >select 1 move key db_id将当前数据库的key移动到db_id数据库中>move key 1 将key移动到1数据库中
Rename Key Newkey
When you rename key to Newkey,newkey already exists, its value is overwritten
sort key 对key进行排序>lpush cost 1 8 7 2 5>sort cost 默认对数字排序,升序>sort cost desc 降序
First, the basic management of Redis
1. Installation
Tar xzf redis-3.0.6.tar.gz
CD redis-3.0.6/
Make && make install
2. Initialization
./utils/install_server.sh
Prompt questions can all enter, but to observe the various file storage path
3. Service Management
ls/etc/init.d/redis_6379//View startup script
Service redis_6379 Status View state
Service redis_6379 Stop
Service redis_6379 Start
NETSTAT-NAUTLP |grep Redis
4. Server Basic test
Redis-cli
Ping test Server
PONG
127.0.0.1:6379> set username Songtao setting variables
127.0.0.1:6379> get Username View Test value
"Songtao"
127.0.0.1:6379> Set Counter 10
Ok
127.0.0.1:6379> INCR counter
(integer) 11
127.0.0.1:6379> GET counter
"11"
127.0.0.1:6379> INCR mycounter Set counter MyCounter
(integer) 1
127.0.0.1:6379> INCR mycounter Self-increment operation of counter MyCounter
(integer) 2
5. Manipulating strings
Format: Set key value [ex seconds] [px milliseconds] [nx|xx]
Set the key and value, and the expiration time can be set to seconds or milliseconds.
NX operates on key only if key does not exist
XX only key already exists, only to operate the key
SetRange Key offset value starts with the offset of a specific bit of the key that is set first "Hello" SetRange 5 "Xixi" to hellxixisrtlen ket statistic string length Strl En first Append key value character is appended, does not exist then creates key and value returns the length of key >append myname jacobsetbit key iffset value to key stored string, set or clear A bit on a specific offset (bit) value can be 1 or 0,odffset to 0~2^32 a key does not exist, then a new key>setbit ABC 0 1>setbit ABC 1 0ABC is created: No. 0 bit 1, first bit 0bitcount The number of bits in the key statistic string that is set to 1 >setbit ABCD 0 1//0001>setbit ABCD 3 1//1001>bitcount ABCD//result 2 record user online frequency, set key to user Name, offset is set to the on-line day and set to 1 on the offset. >setbit ABCD 100 1/website online 100 days User login 1 times >setbit ABCD 225 1//website online 250 days User login 1 times >bitcount ABCD//result for 2DECR key will be in key Value minus 1,key is first initialized to 0, then minus 1>set test 10>DECR Testdecrby key decrement the value in key, minus Decrement>set Count 100>decrby Co UNT 20get Key returns the string value stored by key if key does not exist returns a special value nil if the value of key is not a string, then an error is returned, get can only handle string GetRange key start end returns the substring in the string value, The Intercept range is the start and end negative offset representations of the never-ending count, 1 for the last character, 2 for the penultimate character >set first "Hello,the World" >getrange first-5 -1>getrange First 0 4
INCR key
Add the value of key 1, if key does not exist, then initialize to 0 and then add 1
For counters:
Set Page 20
INCR page
incrby key increment将key的值增加incrementincrbyfloat key increment为key中多存储的值加上浮点数增量 increment>set num 16.1>incrbyfloat num 1.1mget key [key...]一次获取一个或多个key的值,空格分隔mset key value [key value ...]一次设置多个key及值,空格分隔
Cases
Redis-cli
127.0.0.1:6379> set first ' Hello World ' setting string variable
127.0.0.1:6379> get first
"Hello World"
127.0.0.1:6379> SETRANGE First 6 ' hehehe ' will be modified from the 6th character to be rewritten as Hello hehehe
127.0.0.1:6379> get first
127.0.0.1:6379> APPEND username "Sudan" is appended to the variable
127.0.0.1:6379> setbit mytest 0 1
(integer) 0
127.0.0.1:6379> setbit mytest 1 0
(integer) 0
127.0.0.1:6379> Get MyTest
127.0.0.1:6379> setbit Peter 100 1 set 2 binary 100th bit 1
(integer) 0
127.0.0.1:6379> Setbit Peter 105 1 set 2 binary 105th bit 1
(integer) 0
127.0.0.1:6379> Bitcount Peter Statistics Peter set to 1 the number of digits
(integer) 2
127.0.0.1:6379> set NUM1 100 is equivalent to num1=100
Ok
127.0.0.1:6379> Get NUM1
"100"
127.0.0.1:6379> INCR NUM1 is equivalent to num1++
(integer) 101
127.0.0.1:6379> DECR NUM1 is equivalent to num1--
(integer) 100
127.0.0.1:6379> Decrby NUM1 # num1=num1-10
127.0.0.1:6379> Incrby NUM1 # num1=num1+10
127.0.0.1:6379> set HI "Hello World"
127.0.0.1:6379> STRLEN Hi
127.0.0.1:6379> GETRANGE Hi 6-1
127.0.0.1:6379> GETRANGE Hi 6 1
127.0.0.1:6379> Set num2 10.5
127.0.0.1:6379> Incrbyfloat num2 0.3
"10.8"
127.0.0.1:6379> incrbyfloat num2-0.8
"10"
127.0.0.1:6379> MGET NUM1 num2
127.0.0.1:6379> MSET a ten B "ABC"
Ok
127.0.0.1:6379> get a
"10,"
127.0.0.1:6379> Get B
"ABC"
Hash table Data
Redis Hash is a string-type field and value mapping table
A key can correspond to more than one field, and a field corresponds to a value
Storing an object as a hash type can save memory more than storing each field as a srting type
hset key field value将hash表中field值设置为value>hset site google ‘www.goog.cn‘>hset site baidu ‘www.baidu.com‘hget key filed获取hash表中field的值>hget site googlehmset key field value [field value..]同时给hash表中的多个field赋值>hmset site google www.goog.cn baidu www.baidu.comhmget key field [field...]返回hash表中多个field的值>hmget site google baiduhkeys key返回hash表中所有field名称>hmset site google www.goog.cn baidu www.baidu.com>hkeys sitehgetall key 返回hash表中所有field的值hvals key 返回hash表中所有field的值>hvals keyhdel key field [field...]删除hash表中多个field的值,不存在则忽略>hdel site google baidu
Cases:
Redis-cli
127.0.0.1:6379> hset site Google "www.google.com"
(integer) 1
127.0.0.1:6379> hset site Baidu "www.baidu.com"
(integer) 1
127.0.0.1:6379> Hget Site Google
"Www.google.com"
127.0.0.1:6379> Hget site Baidu
"Www.baidu.com"
127.0.0.1:6379> hmset site2 Tudu "www.tedu.cn" tar "www.tarler.com"
127.0.0.1:6379> hmget site2 Tudu Tar
127.0.0.1:6379> Hkeys site to view the value that exists in key
127.0.0.1:6379> Hkeys Site2
127.0.0.1:6379> hgetall site returns field and value
127.0.0.1:6379> hvals Site Returns all the value in the site
127.0.0.1:6379> hdel site Google removes Google from the site
List lists
The Redis list is a character queue
A key can have multiple values
lpush key value [value...]将一个或多个值value插入到列表key的表头key不存在,则创建key>lpush list a b c //list1值依次为c b a同等与lpush list a;lpush list b;lpush list clrange key start stop从开始位置读取key的值到stop结束>lrange xixi 0 2 从0位置开始,读到2位结束>lrange xixi 0 -1 从开始读到结束为止>lrange xixi 0 -2 从开始读到倒数第2位为止lpop key 移除并返回列表头元素数据,key不存在则返回nil>lpop xiix 删除表头元素,可以多次执行llen key返回列表key的长度lindex key index返回列表中第index个值>lindex key 0>lindex key -2lset key index value将key中index位置的值修改为value>lset abc 3 test 将list中第3个值修改为testrpush key value [value..]将value插入到key的末尾>rpush haha a b c //haha值为a b c>rpush haha d //末尾插入d
Cases:
Redis-cli
127.0.0.1:6379> Lpush mylist Chenshun chenglixin Yegutian
127.0.0.1:6379> Lrange mylist 1-1 View the value of 1th to last 1 digits in the list
127.0.0.1:6379> Lpush MyList Mayang
127.0.0.1:6379> Lrange mylist 0-1 View all values
127.0.0.1:6379> help @<tab> #<tab> means pressing the TAB key
127.0.0.1:6379> Help @list
127.0.0.1:6379> Rpush mylist Wanghong
127.0.0.1:6379> Lrange mylist 0-1
127.0.0.1:6379> LSET mylist 2 Chenyuan
127.0.0.1:6379> Llen MyList
127.0.0.1:6379> Lpop MyList
127.0.0.1:6379> Rpop MyList
127.0.0.1:6379> LINDEX mylist 1
127.0.0.1:6379> Get username
127.0.0.1:6379> TTL username # view life cycle
127.0.0.1:6379> PERSIST Username # setting never expires
127.0.0.1:6379> EXPIRE Username 30 # Survival time is 30 seconds
127.0.0.1:6379> del mylist # Delete MyList
127.0.0.1:6379> keys# View all the keys
127.0.0.1:6379> Set Hello 10
Ok
127.0.0.1:6379> Set Hllo 20
Ok
127.0.0.1:6379> Set Habllo 30
Ok
127.0.0.1:6379> Set Hfllo 40
Ok
127.0.0.1:6379> KEYS H?llo #? match any one character
127.0.0.1:6379> KEYS HLlo #match 0 to more than one character
127.0.0.1:6379> KEYS H[a-z]llo # [] matches 1 characters
127.0.0.1:6379> Set H1llo 5
127.0.0.1:6379> KEYS H[a-z0-9]llo
127.0.0.1:6379> KEYS H[ade0-9]llo
127.0.0.1:6379> KEYS
127.0.0.1:6379> Flushall Erase All data
127.0.0.1:6379> KEYS
127.0.0.1:6379> Set username Zhangsan
127.0.0.1:6379> Get username
"Zhangsan"
127.0.0.1:6379> Select 1
127.0.0.1:6379[1]> Get username
(nil)
127.0.0.1:6379[1]> Select 0
127.0.0.1:6379> Get username
"Zhangsan"
127.0.0.1:6379> Select 0
127.0.0.1:6379> MOVE username 1
(integer) 1
127.0.0.1:6379> Get username
(nil)
127.0.0.1:6379> Select 1
Ok
127.0.0.1:6379[1]> Get username
"Zhangsan"
127.0.0.1:6379[1]>
127.0.0.1:6379[1]> Keys
1) "username"
127.0.0.1:6379[1]> RENAME username Name # Renaming
Ok
127.0.0.1:6379[1]> keys *
1) "Name"
127.0.0.1:6379[1]> Get Name
"Zhangsan"
127.0.0.1:6379[1]> Lpush mylist 10 2 38 69 42
127.0.0.1:6379[1]> SORT MyList # does not change mylist
127.0.0.1:6379[1]> Lrange mylist 0-1
127.0.0.1:6379[1]> Sort mylist desc # Descending
127.0.0.1:6379[1]> lpush names Zhangsan Lisi Bob Alice
127.0.0.1:6379[1]> Sort names Alpha #按字母顺序排序
Sort alphabetically, starting with the word subscript 1 and removing 2 items
127.0.0.1:6379[1]> SORT names Alpha Limit 1 2
127.0.0.1:6379[1]> SORT names Alpha Limit 1 2 desc
After sorting mylist, save as Mylist2
127.0.0.1:6379[1]> Sort MyList Store Mylist2
127.0.0.1:6379[1]> Lrange Mylist2 0-1
配置redis主从
1. The master server uses the existing Redis
2. From the server
Yum install-y gcc gcc-c++
Tar xzf redis-3.0.6.tar.gz
CD redis-3.0.6/
Make && make install
./utils/install_server.sh
3, the master server set the synchronization password
vim/etc/redis/6379.conf configuration file
Requirepass redis123 Setting the server password
Restart Service:
Service redis_6379 Restart or
/etc/init.d/redis_6379 restart
4, set the authentication password to turn off the service
vim/etc/init.d/redis_6379
$CLIEXEC-a redis123-p $REDISPORT shutdown
Restart Services: Service redis_6379 restart
5. Configuring the Subordinate server configuration file
Vim/etc/redis/6379.conf
Slaveof 192.168.4.1 6379 Primary server IP
Masterauth redis123
Restart Services: Service redis_6379 restart
6, verify the master and slave data is the same
Login main:
Redis-cli-h 192.168.4.1-a redis123
192.168.4.1:6379> Set AAA 100
From:
Redis-cli
127.0.0.1:6379> keys *
127.0.0.1:6379> Get AAA
Redis Cache Database Server