在本章中,您將了解和學習Redis的環境安裝設置。
一、RedHat 6.0 自定義安裝
1、創建redis用戶
Redis 默認的安裝路徑是/usr/local/redis,並且安裝是root用戶。
為安裝在自定義的目錄下,首先創建一個redis用戶,並且修改密碼。
[root@localhost home]# useradd -d /home/redis -g redis -m redis [root@localhost /]# passwd redis Changing password for user redis. New password:
2、下載redis安裝包
目前,redis官方的最新版本的是Redis 3.2.8版本。
下載完畢后使用tar命令解壓到當前目錄。
[redis@localhost ~]$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz [redis@localhost ~]$ tar zxvf redis-3.2.8.tar.gz
3、編譯redis
進入安裝目錄下,執行make命令,編譯成功后會顯示下面內容。
編譯后在目錄下生成一個src目錄,里面會有編譯好的執行文件
[redis@localhost ~]$ cd redis-3.2.8 [redis@localhost redis-3.2.8]$ make cd src && make all make[1]: Entering directory `/home/redis/redis-3.2.8/src' Hint: It's a good idea to run 'make test' ;) make[1]: Leaving directory `/home/redis/redis-3.2.8/src'
4、安裝redis(安裝到redis用戶下)
進入src目錄下,使用vi編輯Makefile文件,在文件中找到“PRIFIX?=/usr/local”,修改為你需要安裝的目錄,這里使用局對路徑。
然后執行make install命令進行安裝,安裝完畢后會在安裝目錄下生成一些執行文件。
[redis@localhost src]$ make install Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install
[redis@localhost bin]$ pwd /home/redis/redis_home/bin [redis@localhost bin]$ ls dump.rdb redis-check-aof redis-cli redis-server redis-benchmark redis-check-rdb redis-sentinel
5、編輯環境變量
在 .bash_profile文件中,注冊下面的環境變量,注冊后可以在任何位置直接執行redis-server命令。
在REDIS_HOME目錄下創建一個conf目錄,把啟動Redis的conf文件放在里面。
redis.conf文件在redis-3.2.8的源碼目錄里面。
export REDIS_HOME=/home/redis/redis_home
export PATH=${REDIS_HOME}/bin:${PATH};
[redis@localhost conf]$ pwd /home/redis/redis_home/conf [redis@localhost conf]$ ls redis.conf
二、如何啟動Redis
Redis 啟動很簡單,直接在任意位置執行redis-server命令即可,但是由於沒有運行在后台,關閉終端后Redis的進程會終止。
因此,需要對Redis的啟動進行設置,使用vi編輯redis.conf文件,找到daemonize屬性,把后面的no修改為yes后保存。
在redis-server的后面指定redis.conf的路徑就可以了。
[redis@localhost conf]$ redis-server 21209:C 29 Apr 02:32:54.052 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 21209:M 29 Apr 02:32:54.052 # You requested maxclients of 10000 requiring at least 10032 max file descriptors. 21209:M 29 Apr 02:32:54.052 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted. 21209:M 29 Apr 02:32:54.052 # Current maximum open files is 1024. maxclients has been reduced to 992 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'. _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 21209 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 21209:M 29 Apr 02:32:54.053 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 21209:M 29 Apr 02:32:54.053 # Server started, Redis version 3.2.8 21209:M 29 Apr 02:32:54.053 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 21209:M 29 Apr 02:32:54.054 * DB loaded from disk: 0.000 seconds 21209:M 29 Apr 02:32:54.054 * The server is now ready to accept connections on port 6379
下面是在$REDIS_HOME/conf目錄下,以deamon的方式執行redis-server。
可以看到TCP=127.0.0.1,PORT=6379,這兩個參數都可以在redis.conf中進行修改。
[redis@localhost conf]$ redis-server redis.conf [redis@localhost conf]$ ps -ef | grep redis root 20887 20647 0 02:20 pts/1 00:00:00 su - redis redis 20888 20887 0 02:20 pts/1 00:00:00 -bash redis 21322 1 0 02:42 ? 00:00:00 redis-server 127.0.0.1:6379 redis 21330 20888 0 02:42 pts/1 00:00:00 ps -ef redis 21331 20888 0 02:42 pts/1 00:00:00 grep redis
三、使用客戶端連接Redis
在$REDIS_HOME/bin目錄下,有名為redis-cli的客戶端程序。
到此為止,Redis3.2.8的安裝就完成了,下面就可以學習如何使用了。
[redis@bogon ~]$ redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379>