下载Redis并安装
- 启动CentOS7系统,并使用Shell客户端工具进入/usr/local/hnguigu目录,然后下载、解压
# 将redis的压缩包下载到/usr/local/hnguigu目录
cd /usr/local/hnguigu
wget http://download.redis.io/releases/redis-5.0.8.tar.gz
# 解压tar包
tar -xvf redis-5.0.8.tar.gz
- 进入解压后redis-5.0.8目录,然后编译程序,因为redis是由C语言编写的,需要先编译。
cd redis-5.0.8
#编译
make
编译会出现以下错误:
/bin/sh: cc: command not found
make[1]: *** [adlist.o] 错误 127
make[1]: Leaving directory `/usr/local/hnguigu/redis-5.0.8/src'
make: *** [all] 错误 2
解决:因为没有安装gcc , 导致无法make成功
yum -y install gcc
继续make时报如下错误:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/data0/src/redis-2.6.2/src'
make: *** [all] Error 2
原因是jemalloc重载了Linux下的ANSI C的malloc和free函数。解决办法:make时添加参数
make MALLOC=libc
编译成功后,会出现一句提示:
Hint: It's a good idea to run 'make test' ;)
此时,若执行make test就又会提示需要安装:tcl8.5 。但是我们不需要安装tcl,所以不要执行make test。
You need tcl 8.5 or newer in order to run the Redis test
make: ***[test] Error_1
- 安装redis,安装到/usr/local/redis/5.0.8目录
make PREFIX=/usr/local/redis/5.0.8 install
- 从解压后的安装文件中拷贝redis.conf文件到安装目录/usr/local/redis/5.0.8下
cd /usr/local/hnguigu/redis-5.0.8
cp redis.conf /usr/local/redis/5.0.8/
Redis的启动
- 启动Redis分为前端启动和后端启动
- 不推荐使用前端启动,不适用集群;后端启动会读redis.conf文件,不同虚拟机上可以改redis.conf的ip及端口。
前端启动
# 进入安装目录,再启动服务端
cd /usr/local/redis/5.0.8
./bin/redis-server
客户端启动
在shell窗口中再打开一个会话,进入/usr/local/redis/5.0.8/目录,启动本机客户端
cd /usr/local/redis/5.0.8/
./bin/redis-cli
启动本机客户端后,会自动连上本机的redis服务器,接下来就可以往redis服务器存储键值对数据了
上面这种方式是连本机的redis服务器,如果启动这个客户端时想要连接其他虚拟机上的redis服务器
./bin/redis-cli -h ip -p 6379
-
后端启动
-
修改/usr/local/redis/5.0.8目录下的redis.conf文件
-
将daemonize no设置为yes
-
daemonize yes 代表开启守护进程模式,在该模式下,redis会在后台运行,并将进程pid号写入至redis.conf选项pidfile设置的文件中,此时redis将一直运行,除非手动kill该进程 ,或者使用shutdown命令关闭。
-
当daemonize选项设置成no时,那就还是前端启动,ctrl + c强制退出或者关闭连接工具(xshell等)都会导致redis服务端进程退出。
-
-
-
启动服务端
# 如果没有加./redis.conf,默认还是前端启动
./bin/redis-server ./redis.conf
问题:如果外网ip连接不了,则需要修改redis.conf文件
在bind 127.0.0.1 192.168.43.131(外网ip),即可连接外网ip
Redis的关闭
- 杀死进程:kill -9 进程ID 非正常关闭,容易丢失数据,不推荐
- 客户端关闭命令:
- ./bin/redis-cli shutdown 正常关闭,推荐
- ./bin/redis-cli -h 192.168.43.160 -p 6379 shutdown
- 客户端连接后,不用退出,可以直接输入shutdown命令关闭redis服务器
ps -ef | grep -i redis
如果进程占用,就需要kill -9 来杀死进程