2.1. Windows installation
Jingyan.baidu.com/article/0f5…
2.2. The Linux installation
2.2.1. Install GCC
Installing GCC is a simple matter of making sure root is logged in and Linux can connect to the Internet
Yum install GCC -c++ GCC -vCopy the code
or
yum -y install gcc automake autoconf libtool make
Copy the code
Error: /var/run/yum.pid was locked when running yum. Pid is XXXX and another program is running
rm -f /var/run/yum.pid
Copy the code
2.2.2. Install Redis
- For example, in /usr/local/, the redis version is redis-5.0.5. You can also download the latest redis6.0 version from the official website, redis. IO /download
Wget command 1: http://download.redis.io/releases/redis-5.0.5.tar.gz command 2: tar ZXVF redis – 5.0.5. Tar. Gz command 3: CD redis-5.0.5: make or make MALLOC=libc
Make install // checkCopy the code
Command 5:
make PREFIX=/usr/local/redis install
Copy the code
(Install compiled files) Install to indicated directory:
Note: PREFIX must be capitalized and a redis directory will be automatically created for us and the result installed
Command 6: CD /usr/local/redis
/usr/local/ will have a redis folder containing the bin directory
Command 7: View the bin directory, as shown in the figure:
3. Redis startup
3.1. Start Redis service
Go to the installation directory /usr/local/redis
Run the./bin/redis-server command
3.2. Start the Redis client
Clone Session (Clone Session) :
Go to the installation directory CD /usr/local/redis
Run the./bin/redis-cli command
To start the Redis client command:
Redis -cli -h IP address -p portCopy the code
Command to exit the client: Ctrl+C
Check whether the server is started Start the Redis client, open the terminal, and enter the redis-cli command. This command connects to the local Redis service.
$redis-cli
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> PING
PONG
Copy the code
In the above example, we connect to the local Redis service and run the PING command, which is used to check whether the Redis service is started.
4. Configure Redis on Linux
The Redis configuration file is located in the Redis installation directory. The file name is Redis. Conf (for Windows, the file name is Redis.
4.1 configure Redis
- Redis port number or startup has default configuration. However, we usually do this manually by going back to the root directory and finding reids.conf in the unzipped file
- Copy the redis.conf configuration file to the redis directory
- Run the cp redis.conf /usr/local/redis command to copy the configuration file to the installation directory
cp redis.conf .. /redis/Copy the code
The redis configuration is now complete.
4.2. Redis. Conf Configuration file description
Conf configuration items are described as follows:
- Redis does not run as a daemon by default. You can modify this configuration item by using yes to enable daemonize no
- Pid is written to the /var/run/redis.pid file by default when Redis is running as a daemon. You can specify pidfile /var/run/redis.pid with pidfile
- The default port is 6379. Why choose 6379 as the default port, because 6379 is the number corresponding to MERZ on the phone button, and MERZ is derived from the Italian singer Alessia MERZ’s name port 6379
- Host ADDRESS bind 127.0.0.1 5. Close the connection after the client is idle. If the value is 0, timeout 300 is disabled
- Redis supports four log levels: debug, verbose, notice, and warning. The default value is verbose loglevel verbose
- The log mode is standard output by default. If Redis is configured to run in daemon mode and the log mode is configured as standard output, the log will be sent to /dev/null logfile stdout
- Set the number of databases. The default database is 0. You can use the SELECT command to specify the database ID on the connection
- The default Redis configuration file provides three conditions: Save 900 1 Save 300 10 Save 60 10000 indicates 1 change in 900 seconds (15 minutes), 10 changes in 300 seconds (5 minutes), and 10000 changes in 60 seconds respectively.
- Redis uses LZF compression. If you want to save CPU time, you can turn this option off, but it will cause database files to become large Rdbcompression yes
- Specifies the name of the local database file. The default value is dump. RDB dbfilename dump. RDB
- Specify the local database directory dir./
- If the Slav service is used, set the IP address and port number of the master service. When Redis starts, it will automatically synchronize data from the master to slaveof
- If password protection is enabled for the master service, the Slav service connects to the Master using the masterauth password
- Set the Redis connection password. If the password is configured, the client needs to provide the password by using the AUTH command when connecting to Redis. Requirepass foobared is disabled by default
- The maximum number of client connections that Redis can open at a time is the maximum number of file descriptors that the Redis process can open at the same time. If maxClients 0 is set, there is no limit. When the number of clients reached the limit, Redis will close new connections and return Max Number of clients reached error message maxClients 128
- Specifies the maximum memory limit for Redis. When Redis starts up, it loads data into memory. When the maximum memory is reached, Redis tries to clear expired or expiring keys first. The new VM mechanism of Redis will store the Key in memory and the Value in swap maxMemory
- Specifies whether to log after each update operation. Redis writes data to disk asynchronously by default. If not enabled, data may be lost for a period of time in the event of a power outage. Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time. The default is no appendonly No
- Appendone.aof appendfilename appendone.aof specifies the update log filename
- Three values are available: no: data is cached by the operating system and synchronized to disk (fast) always: data is written to disk (slow and safe) manually by calling fsync() after each update operation everysec: Represents synchronization once per second (compromise, default) appendfSync everysec
- Specifies whether to enable the virtual memory mechanism. The default value is no. For a brief introduction, the VM mechanism stores data in paging mode, and Redis swaps cold data on less visited pages to disk. Frequently visited pages are automatically swapped out from disk to memory (I will analyze Redis VM mechanism in detail in a later article) VM-enabled No
- The default value is/TMP /redis.swap. Multiple Redis instances cannot share vm-swap-file/TMP /redis.swap
- All data larger than VM-max-memory is stored in virtual memory. No matter how small the VM-max-memory setting is, all index data is stored in memory (Redis index data) Keys), which means that when vM-max-memory is set to 0, all values actually exist on disk. The default value is 0 vM-max-memory 0
- The Redis swap file is divided into many pages. An object can be stored on multiple pages, but a page cannot be shared by multiple objects. Vm-page-size is set according to the size of the stored data. The page size is best set to 32 or 64bytes; If you store large objects, you can use a larger page, or if in doubt, use the default vM-page-size 32
- Set the number of pages in the swap file. Since the page table (a bitmap indicating that a page is free or used) is placed in memory, every 8 pages on disk consumes 1byte of memory. vm-pages 134217728
- Set the number of threads that can access the swap file. It is best not to exceed the number of cores on the machine. If set to 0, all operations on the swap file are serial and may cause a long delay. The default value is 4 VM-max-threads 4
- Glueoutputbuf Yes Specifies whether to combine smaller packets into one packet when replying to clients. By default, glueOutputBuf Yes is enabled
- Hash-max-zipmap-entries 64 hash-max-zipmap-value 512 Specifies that a special hash algorithm is used when the number of elements exceeds a certain threshold or the maximum number exceeds a certain threshold
- Activerehashing Yes specifies whether rehash hash is enabled. The default is enable
- Include additional configuration files. You can use the same configuration file among multiple instances of Redis on the same host, and each instance has its own specific configuration file include /path/to/local.conf
4.3. Customize Redis configuration
1. Go to the /usr/local/redis installation directory and modify the redis.conf configuration file vim redis.conf.
2. Redis configuration must be modified by default:
- Daemonize no Changed to daemonize yes
- Bind 127.0.01 comment out
- Requirepass Sets the password
After the configuration is complete, run the startup command to make the configuration take effect and start it on the server
./bin/redis-server redis.conf
Copy the code
- Redis uses the mode of single process and multiple threads. If daemonize in redis.conf is set to yes, the daemon mode is enabled. In this mode, Redis runs in the background and writes the process PID number to the file set by the redis. Conf option pidfile. Redis will run until the process is killed manually. If daemonize is set to No, the redis command line interface is displayed. Exit Forcibly exits the redis process or closes the connection tool (such as putty and Xshell).
- Most applications developed by the server are run in the background mode
Memory maintenance strategies in Redis
As an excellent intermediate cache, Redis often stores a large amount of data. Even if cluster deployment is adopted for dynamic expansion, the memory should be cleaned immediately to maintain system performance. There are two solutions in Redis,
- One is to set a timeout for the data,
- LRU algorithm is used to dynamically delete unused data. A page replacement algorithm for memory management. For blocks of data that are in memory but not in use (memory blocks), called LRU, the operating system moves them out of memory based on which data belongs to the LRU to make room for loading additional data.
Volatile – lRU: Delete the least frequently used data of the timeout period.
2. Allkeys-lru: query allkeys and delete the most recently used data. This is the most widely used strategy.
3. Volatile -random: Randomly delete data that has been timed out.
4. Allkeys-random: queries allkeys and deletes them randomly.
5. Volatile – TTL: queries all the data with the specified timeout period, sorts the data, and deletes the data that is about to expire.
6. Noeviction: If set to this property, no deletion will be performed, an error will be returned if memory is out of order.
• volatile-lfu: expels the least frequently used key from allkeys configured with expiration time • allkeys-lfu: expels the least frequently used key from allkeys