Under CentOS7 yum install Redis

(1) .Redis Overview

  Redis is an open source use written in ANSI C, support network, based on the persistence of memory can log type, Key-Value (key type) database (non-relational database), and available in multiple languages ​​API.

  Redis is a high performance database Key-Value. It appears to a large extent compensate for lack of this type of Key-Value Memcached type of storage can play a very good complement to relational databases in the lower part of the occasion. It provides for Java, C / C ++, PHP, JavaScript, Perl, Object-C, Python, Ruby, Erlang and other clients, easy to use.

  Redis supports master-slave synchronization, by means of Redis Sentinel (Sentinel, Redis comes) tools to monitor the master-slave node when the primary node fails, another will elevate himself to become the new master node from the node.

 1) Supported data types

  And Memcached similar, but it supports relatively more storage Value type, including String (String), List (list), Sets (collections), Sorted Sets (ordered set) and Hash (hash type, associative array), bitmaps (bitmaps) and HyperLoglog .

 2) Performance

  One million small string of key storage, consumes about 100M memory;

  Since Redis is single-threaded, if there is more than one CPU on the host server, only one can be used, but it does not mean that CPU will become a bottleneck, because Redis is a relatively simple KV data storage, CPU usually do not become a bottleneck;

  On the common linux server, 500K (50 million) of the concurrent need to deal with only one second, if the host hardware under better circumstances, every second can reach millions of concurrency.

 3) Redis comparison with Memcache

  Memcache can only use memory to cache objects. Redis addition to using the memory to cache on the image, may also periodically save data to the disk, the data is permanently stored. When the server crash or a sudden power failure, redis disk-based data recovery;

  Redis is single-threaded server, only one thread to respond to all requests. Memcache is multi-threaded;

  Redis supports more data types.

(2) Install

  When yum install redis, it recommended Remi repository source. Because Remi source provides the latest version of Redis, you can pass the source using Redis YUM to install the latest version. It also provides the latest PHP and MySQL yum source, as well as related services program.

 1) Remi repository epel source depends on the source, it is necessary to install a source epel

[root@youxi1 ~]# yum -y install epel-release

 2) Installation Remi repository source

[Root Atto Youxi1 ~] # Yum -Y Install Http://Rpms.Remirepo.Net/enterprise/remi-release-7.Rpm 
[Root Atto Youxi1 ~] # Ls /Etc/yum.Repos.D/ // under载completed after meeting out现许multi-remi specific yum source,这里Yoyo arrival manner Shi remi.repo这个source 
CentOS-Base.Repo CentOS-Sources.Repo remi-Glpi92.Repo remi-Php70.Repo remi-Safe.Repo 
CentOS-CR. CentOS-Vault.Repo Repo Remi-Glpi93.Repo Remi-Php71.Repo 
CentOS-Debuginfo.Repo Epel.Repo Remi-Glpi94.Repo Remi-Php72.Repo 
CentOS-Fasttrack.Repo Epel-Testing.Repo Remi-Modular.Repo Remi -Php73.Repo 
CentOS-Media.Repo Remi-Glpi91.Repo Remi-Php54.Repo Remi.Repo

 3) using the specified source installation Redis yum

[root @ youxi1 ~] # yum --enablerepo = remi install -y redis // - enablerepo specify the source yum 
[root @ youxi1 ~] # redis-cli --version // After installation is complete check using the command version 
redis- cli 5.0.5

  Note: After installation is complete remi source, default is not activated, when the need to use remi repository source setup, demand --enablerepo = remi remi repository option to specify the source can be used, and then install it.

 4) Start Redis and set the boot from Kai

[root@youxi1 ~]# systemctl start redis
[root@youxi1 ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.

  Note: Redis port number is 6379

(3). Profile information

  Under Linux, Redis configuration file stored redis.conf in the / etc / directory. Out with the following parameters may be used:

// line 69, Redis listening address 
the bind 127.0.0.1 
// line 88 in safe mode, whether to prohibit external network access Redis, yes representation is enabled only through local access 
protected-the MODE yes 
// line 92, listens the port number 
port 6379 
/ * on line 101, designated tcp-backlog length. tcp-backlog is a waiting queue. 
* When a large number of requests needed Redis processing requests needs to wait queues will be cached by backlog. 
* Number of queues to determine the number of backlog can be cached 
* / 
TCP-511 backlog 
// line 109, sock sock file location and the file specified communication use. If the server and client on the same host, the proposed open. may be exchanged directly communicate sock in memory without passing through TCP / IP protocol stack encapsulation, decapsulation 
# unixsocket /tmp/redis.sock 
// first line 110, the file permissions defined sock 
# unixsocketperm 700 
, line 113 //, after that the client connection is successful, how long idle time-out (inactive, no data exchange). 0 means not turn this feature 
timeout 0 
Line 130 // long duration linked in seconds 
tcp-Keepalive 300 
// 136 line, the daemon is running in the background. If you use a script to start redis service, even if to no, it will run a daemon. General Settings yes 
daemonize NO
// 147 line, redis process is taken over by the upstart or systemd. Default unsupervised interaction without modification. 
NO Supervised 
// line 158, pid file address 
the PidFile /var/run/redis_6379.pid 
// line 166, the log level 
the LogLevel Notice 
// line 174, the log file location 
logfile /var/log/redis/redis.log 
// line 186, how many default database. However, in a distributed, only one 
Databases 16 

/ 218-220 * first row, the snapshot storage policy, the storage policy disk persistence 
* first number is the unit of time, in seconds; the second number is a bond number of times changes 
* a first example, the primary key values vary at least 900 seconds, it takes a snapshot of (persistent) 
* the second is, change the key at least 10 times within 300 seconds, doing a snapshot (persistent) 
* All the conditions are juxtaposed relationship, choose to use the rules change depending on the key 
* / 
the Save 900 1 
the Save 300 10 
60 10000 the Save 
// 235 line, during a snapshot backup, once the error has stopped. default yes you can 
STOP-Writes-error-oN-bgsave yes  
// line 241, specify RDB file compression. yes it means that compression will consume CPU resources
rdbcompression yes 
/ * 250 lines, do check whether the code detection of RDB file. 
* This defines loaded at startup whether redis RDB file to file check check code generated RDB file will generate parity information in redis, when redis start or load RDB file again and checks whether parity information. 
* If you will consume time when it is detected, it will lead to a slow start when redis, but can determine whether the RDB file error. 
* / 
Rdbchecksum Yes 
// line 253, RDB file name 
 dbfilename dump.rdb 
// line 263, RDB files stored path 
the dir / var / lib / Redis 

// line 286, IP and port definition Master server, the main replicated from the configuration information 
# replicaof <masterip> <masterport> 
password @ line 293, defined master server, the configuration information from the master copy 
# masterauth <-master password> 
// line 308, when viewed from the side of the main replication disconnected from the main terminal connection, indicates Yes continue to provide services, even though the data may not be current; no means to request returns an error message 
Replica-serve-STALE-data Yes 
// line 324, from the end of the read-only 
replica- yes-only the Read 
// line 355, the default does not use diskless (diskless) synchronously 
repl-diskless-sync no
// there before line 367, diskless (no disk) data transfer mode of a time delay, to enable the destination queue to be transmitted from the terminal, the default five seconds  
repl-diskless-sync-delay 5
@ line 373, from end to end of the main sending ping interval, 10 seconds by default 
# ping-Replica the repl-period-10 
// line 385, set the timeout 
# 60 the repl-timeout 
/ * on line 400, if enabled TCP_NODELAY. 
* If enabled a small number of TCP packets will be used to transmit data and bandwidth from the terminal to the slower; 
* not enabled if more bandwidth is used for data transmission, faster. 
* / 
The repl-disable-NoDelay ™ NO-TCP 
/ * line 413, provided the size of the backlog. backlog is a buffer to store the data to be synchronized from the terminal when the terminal from the associated losses. 
* Thus, when reconnecting from the end, generally need not be completely synchronized. backlog greater, from the end of the longer time may be lost to (relatively) 
* / 
# 1MB the repl-backlog-size 
// line 426, after a period of time from the end not yet connected to the master, then the backlog (buffer District) memory will be released. 0 indicates no release, 3600 seconds by default 
# 3600 the repl-backlog-TTL 
// line 441, from the smaller end of the higher priority setting, the priority number. The master failure can be restored in accordance with a higher priority from the end. If the setting is 0, then the end will never be selected from the 
replica-priority 100
// row of 457-458, when the master of the available delay islands 10 seconds from the end of the web or less than 3, the primary end user refuses to accept the write request. 
Replicas-to-min # the Write-3  
# min-max-LAG-10 Replicas

// line 507, authentication password is specified, the default does not start 
# requirepass foobared 

// line 539, while the maximum number of connections redis 
# maxclients 10000

  Particular attention to: bind the second row 69, row 88 protected-mode, daemonize line 136, line 539 MaxClients, and all the configuration parameters from the master copy.

(4) the simple use .redis

  Most simple to use: redis-cli -h [IP Address] -p [port number]. If the connection is a local, -h, and -p can be omitted.

 1) Reids string manipulation

  Naming key: You can use ASCII characters; the length of the bond is not too long, the longer the key the more space is consumed; in the same library (namespace), the key name must be unique, if the copy key name actually modify the values ​​of the key; the different libraries (namespace), name of the key can be repeated more; automatic key expiration.

[root @ youxi1 ~] # Redis-cli 
127.0.0.1:6379> System CentOS // create the SET key 
the OK 
127.0.0.1:6379> GET System // get key 
"CentOS" 
127.0.0.1:6379> Exit 
[root @ youxi1 ~] # systemctl restart redis // restart 
[root @ youxi1 ~] # Redis-cli 
127.0.0.1:6379> GET // can see the system key is there 
"CentOS" 
127.0.0.1:6379> name the SET "youxi" EX 10 // so as to set, it indicates that the key value exists 10 seconds 
the OK 
127.0.0.1:6379> GET name // 10 seconds later to view again, this is a temporary key 
(nil)

 2) If you turn on authentication

  First, modify the configuration file, open authentication

[root @ youxi1 ~] # vim /etc/redis.conf 
requirepass 123456 // In line 507, the removal of the comment, and set the authentication password 
[root @ youxi1 ~] # systemctl restart redis

  At this time, again using redis

[root @ youxi1 ~] # Redis-cli 
127.0.0.1:6379> // get system when there is no certification, will get an error 
(error) NOAUTH Authentication required. 
127.0.0.1:6379> auth 123456 // certified 
the OK 
127.0.0.1:6379 > after get system // authentication, access keys, success again 
"centos"

(5). Arrangement endurance of

  All work is Redis data set stored in memory. If Redis crash or power failure can result in loss of all data, so Redis provided to ensure the reliability of data persistence features. Redis persistence There are two methods: RDB and AOF of .

  RDB: stored as binary data file format is the default persistence mechanism activated ; according to pre-customized policies, periodically save data to disk.

  AOF: Append Only File is similar to the MySQL binary log, every record redis write operation command to order additional IO mode at the end of the specified file, using an additional way to achieve, which is also called an additional persistent log type mechanism. Since each operation are recorded, along with a long time it will increase the capacity of the file, and the recording command is somewhat redundant. But redis process can automatically scan to the corresponding AOF document, some redundant operations to a merger, in order to achieve a one-time to restore data in the future.

  RDB configuration in fact already been described above, and then copy what I have here, as follows. A total of just six points configuration.

/ * 
* Of 218 to 220 rows, the snapshot storage policy, persistence strategy saved to disk 
* first number is the unit of time, in seconds; the second number is the number of key changes 
* a first example, 1 key changes at least within 900 seconds, then do a snapshot (persistent) 
* the second is that the key changes at least 10 times within 300 seconds, do a snapshot (persistent) 
* All the conditions are juxtaposed relationship , choose to use different keys change rules based on 
* / 
the Save 900 1 
the Save 300 10 
60 10000 the Save 
// 235 line during snapshot backup, in the event an error is stopped, the default yes you can 
stop-writes-on-bgsave yes -error 
// line 241, specify whether RDB file compression. yes means that compression will consume CPU resources 
rdbcompression yes 
/ * 250 lines, whether to do RDB file checksum to detect. 
* This defines loaded at startup redis RDB file to check whether the file checksum, file is generated RDB will generate parity information in redis, when redis start or load RDB file again and checks whether parity information. 
* If you will consume time when it is detected, it will lead to a slow start when redis, but can determine whether the RDB file error. 
* / 
Rdbchecksum yes 
// line 253, RDB file name 
 dbfilename dump.rdb 
// line 263, RDB file storage path
dir / var / lib / redis

  AOF configuration starts on line 679, if necessary, their own look.

Guess you like

Origin www.cnblogs.com/diantong/p/11255019.html