Source Address: http://hanqunfeng.javaeye.com/blog/684108
Install redis version 1.2.6
I. Download redis
: Http://code.google.com/p/redis/downloads/list
Select redis-1.2.6.tar.gz to copy the downloaded package to/usr/local/redis)
II. Install
CD/usr/local/redis
Tar zxvf redis-redis-1.2.6.tar.gz
CD redis-redis-1.2.6
Make
Because make install is not available, you must manually copy the key files in the source code directory to the appropriate location:
CP redis. CONF/etc/# see some materials for this. I don't know why I must put them there.
CP redis-benchmark redis-cli redis-server/usr/bin/# is very useful, so you do not need to add./during execution, and you can execute it anywhere.
If the memory is insufficient, you need to set the kernel parameters:
Echo 1>/proc/sys/Vm/overcommit_memory
Here we will talk about the meaning of this Configuration:
/Proc/sys/Vm/overcommit_memory
This file specifies the kernel memory allocation policy. The value can be 0, 1, or 2.
0 indicates that the kernel will check whether there is enough available memory for use by the process. If there is enough available memory, the memory application will be allowed; otherwise, the memory application will fail, and return the error to the application process.
1 indicates that the kernel allows all physical memory allocation regardless of the current memory status.
2. indicates that the kernel is allowed to allocate more memory than the total physical memory and swap space.
Edit the redis. conf configuration file (/etc/redis. conf) and make appropriate adjustments as needed, for example:
Daemonize yes # convert to daemon; otherwise, a monitoring information line is output every 5 seconds at startup.
Save 60 1000 # reduce the number of changes, which can be specified as needed
Maxmemory 256000000 # allocate MB of memory
The redis port must be opened. Otherwise, the client cannot connect. The method is as follows:
VI/etc/sysconfig/iptables # You must have the modification permission.
Add a row:
# Redis
-A RH-Firewall-1-INPUT-M state -- state new-m tcp-p tcp -- dport 6379-J accept
Restart iptables after saving:
Service iptables restart (/sbin/included in path) or/etc/init. d/iptables restart
3. Verify
1. Start the service:
Redis-server/etc/redis. conf
- [WAP @ hanqunfeng redis-1.2.6] $ redis-server/etc/redis. conf
- New PID: 12776
Check whether redis has been started:
- [WAP @ hanqunfeng redis-1.2.6] $ PS-Ef | grep redis
- WAP 12776 1 0? 00:00:00 redis-server/etc/redis. conf
- WAP 12782 12751 0 00:00:00 pts/2 grep redis
2. Access value
Java code
- [WAP @ hanqunfeng redis-1.2.6] $ redis-cli set name value
- OK
- [WAP @ hanqunfeng redis-1.2.6] $ redis-cli get name
- Value
3. Close the service
- [WAP @ hanqunfeng redis-1.2.6] $ redis-cli Shutdown
If the port changes, you can specify the port: redis-cli-P 6380 shutdown.
Data in the memory is automatically written to the hard disk. The file address is configured in redis. conf:
Dbfilename dump. RDB
Note: Data Backup can be achieved through regular backup of this file.
In addition, I can see from the Internet that redis cannot transfer memory data to the hard disk. I also encountered this problem. Later I found it was a problem with user permissions. I started redis as a root user, later, the WAP user discovered that the data cannot be written to the hard disk because of dump. RDB is already the root permission. I don't know if the person who says redis cannot store it on the hard disk is making the same mistake as me.
In addition, I found that the memory data is also written to the hard disk when executing the redis-benchmark command.
Redis-benchmark: checks the processing performance of the current machine.
4. Restart the service and set the value again. The value is successful.
- [WAP @ hanqunfeng redis-1.2.6] $
- [WAP @ hanqunfeng redis-1.2.6] $ redis-server/etc/redis. conf
- New PID: 12797
- [WAP @ hanqunfeng redis-1.2.6] $ redis-cli get name
- Value
5. Forcibly save memory data to the hard disk
Because redis writes data to the disk asynchronously, if you want to write data in the memory to the hard disk immediately, run the following command:
Redis-cli save or redis-cli-P 6380 save (specified port)
Note that the preceding deployment operations require certain permissions, such as copying and setting kernel parameters.
OK. redis configuration is complete.