Linux under the Redis installation configuration tutorial
- 2020-05-17 06:52:58
- OfStack
As an application of NoSQL database, redis is efficient in response speed and hit ratio. Projects need to be horizontal scalability caching framework for centralized, made a point of research, even redis, memcached efficiency differences (specific comparative reference http: / / timyang net data/mcdb - tt - redis /), but it can meet the current needs of the project; However, redis is still quite popular. It supports linked list and set operations and regular expression searching for key. Currently, the cache results of the project are mostly linked lists.
1. Download redis
Download address http: / / code. google. com p/redis/downloads/list
It is recommended to download redis-1.2.6.tar.gz. My colleagues have already installed and run redis-2.0.4.tar.gz successfully. I cannot operate the cache data after installing redis-2.4.4.tar.gz
2. Install redis
After downloading, unzip tar zxvf redis-1.2.6.tar.gz to any directory, such as /usr/local/ redis-1.2.6
After unzip, enter the redis directory
cd /usr/local/redis-1.2.6
make
Copy files
cp redis.conf /etc/ etc file when redis launches the configuration file
cp redis-benchmark redis-cli redis-server /usr/bin/ # this is useful, so you don't have to add./, and you can do it anywhere
Set the memory allocation policy (optional, depending on the actual situation of the server)
/proc/sys/vm/overcommit_memory
Optional values: 0, 1, 2.
0, means the kernel will check whether there is enough available memory for the process to use; If there is enough memory available, the memory request is allowed; Otherwise, the memory application fails and the error is returned to the application process.
1, means that the kernel allows all physical memory to be allocated, regardless of the current state of memory.
2, which means that the kernel allows more memory to be allocated than the sum of all physical memory and swap Spaces
Notable 1 point is, redis when dump data will fork 1 child process, theory of memory and parent child process occupies is 1 kind, such as parent memory is 8 G, this time also want to the same distribution of 8 G memory to child, if memory can't burden, often can cause redis server down machine or IO load is too high, the efficiency decline. So the optimal memory allocation policy here should be set to 1 (meaning that the kernel allows all physical memory to be allocated, regardless of the current memory state)
Open redis port and modify firewall configuration file
vi /etc/sysconfig/iptables
Add port configuration
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
Reload rule
service iptables restart
3. Start redis service
[root@Architect redis-1.2.6]# pwd
/usr/local/redis-1.2.6
[root@Architect redis-1.2.6]# redis-server /etc/redis.conf
Check the process to make sure redis is started
[root@Architect redis-1.2.6]# ps -ef | grep redis
root 401 29222 0 18:06 pts/3 00:00:00 grep redis
root 29258 1 0 16:23 ? 00:00:00 redis-server /etc/redis.conf
If the launch of redis service fails here, it is generally due to the problem of redis.conf file. It is recommended to check or find an available configuration file for overwriting, so as to avoid detour. Here, it is recommended to modify redis.conf and set redis process as the background daemon
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
4. Test redis
[root@Architect redis-1.2.6]# redis-cli
redis> set name songbin
OK
redis> get name
"songbin"
5. Shut down redis service
redis-cli shutdown
When the redis service is turned off, the cached data is automatically dump to the hard disk with the hard disk address set by the configuration item dbfilename dump.rdb in redis.conf
To force the backup data to disk, use the following command
redis-cli save or redis-cli-p 6380 save (specified port)