赞
踩
目录
本文介绍在 CentOS 7.2.5 发行版中安装 Redis 数据库。
- [root@localhost ~]# lsb_release
- LSB Version: :core-4.1-amd64:core-4.1-noarch
- [root@localhost ~]# lsb_release -a
- LSB Version: :core-4.1-amd64:core-4.1-noarch
- Distributor ID: CentOS
- Description: CentOS Linux release 7.2.1511 (Core)
- Release: 7.2.1511
- Codename: Core
- [root@localhost ~]#
1、无论从下面哪个官网都可以进行下载:
Redis 英文官网:Redis Redis 中文官网:Redis中文网 Redis GitHub 开源地址:https://github.com/antirez/redis |
2、如以 Redis Download 5.0.4 为例下载即可,才 1.88M,下载好后,可以用工具先传到 Linux服务器。
3、也可以从 Linux 中下载:wget http://download.redis.io/releases/redis-5.0.4.tar.gz
1、Redis 是用 C 语言写的,下载的压缩文件只是它的源码,需要依靠 C 环境编译后才能正式安装使用。如果 c 环境没有准备好,那么 redis 源码编译的时候会失败的。
2、CentOS 系统自然首选 yum 安装了。yum list 查询可安装的包,yum install 安装,约经过几分钟之后,如下所示安装成功。
- [root@localhost home]# yum list gcc-c++
- 已加载插件:fastestmirror
- Loading mirror speeds from cached hostfile
- * base: mirrors.aliyun.com
- * extras: mirrors.cn99.com
- * updates: mirrors.cqu.edu.cn
- 可安装的软件包
- gcc-c++.x86_64 4.8.5-36.el7_6.2 updates
- [root@localhost home]# yum install gcc-c++
- ...省略 N 多行...
- 已安装:
- gcc-c++.x86_64 0:4.8.5-36.el7_6.2
-
- 作为依赖被安装:
- cpp.x86_64 0:4.8.5-36.el7_6.2 gcc.x86_64 0:4.8.5-36.el7_6.2
- glibc-devel.x86_64 0:2.17-260.el7_6.5 glibc-headers.x86_64 0:2.17-260.el7_6.5
- kernel-headers.x86_64 0:3.10.0-957.12.2.el7 libmpc.x86_64 0:1.0.1-3.el7
- libstdc++-devel.x86_64 0:4.8.5-36.el7_6.2 mpfr.x86_64 0:3.1.1-4.el7
-
- 作为依赖被升级:
- glibc.x86_64 0:2.17-260.el7_6.5 glibc-common.x86_64 0:2.17-260.el7_6.5
- libgcc.x86_64 0:4.8.5-36.el7_6.2 libgomp.x86_64 0:4.8.5-36.el7_6.2
- libstdc++.x86_64 0:4.8.5-36.el7_6.2
-
- 完毕!
- [root@localhost home]#
-

1、将上传的 redis 压缩文件先进行解压:
- [root@localhost home]# ls
- baoAn court momon redis-5.0.4.tar.gz
- [root@localhost home]# tar -xvf redis-5.0.4.tar.gz
- redis-5.0.4/
- redis-5.0.4/.gitignore
- redis-5.0.4/00-RELEASENOTES
- redis-5.0.4/BUGS
- redis-5.0.4/CONTRIBUTING
- redis-5.0.4/COPYING
- redis-5.0.4/INSTALL
- ...省略 N 行...
- [root@localhost home]# ls
- baoAn court momon redis-5.0.4 redis-5.0.4.tar.gz
- [root@localhost home]# cd redis-5.0.4
- [root@localhost redis-5.0.4]# ls
- 00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
- BUGS deps MANIFESTO runtest sentinel.conf utils
- CONTRIBUTING INSTALL README.md runtest-cluster src
- [root@localhost redis-5.0.4]#

2、Redis 源码需要 C++ 编译后才能使用。进入 redis-5.0.4 目录编译 redis,指令:make
- [root@localhost redis-5.0.4]# make
- cd src && make all
- make[1]: 进入目录“/home/redis-5.0.4/src”
- CC Makefile.dep
- make[1]: 离开目录“/home/redis-5.0.4/src”
- make[1]: 进入目录“/home/redis-5.0.4/src”
- rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
- (cd ../deps && make distclean)
- ...省略 N 行...
- LINK redis-server
- INSTALL redis-sentinel
- CC redis-cli.o
- LINK redis-cli
- CC redis-benchmark.o
- LINK redis-benchmark
- INSTALL redis-check-rdb
- INSTALL redis-check-aof
-
- Hint: It's a good idea to run 'make test' ;)
- make[1]: 离开目录“/home/redis-5.0.4/src”
- [root@localhost redis-5.0.4]# cd ..
- [root@localhost home]# du -sh redis-5.0.4
- 130M redis-5.0.4
- [root@localhost home]#

3、编译的作用是将源码编译成安装包,仍然不能直接使用,需要进行下一步的安装后才能正式使用。如果系统是 64 位的,则自动编译成 64 位的安装包,如果系统是 32 位,则自动编译成 32 位的安装包。
1、进入 redis-5.0.4 安装包目录,使用 make PREFIX=/home/redis5 install 命令安装 redis 到 /home/redis5 中
2、PREFIX:用于指定安装到哪个目录下,否则默认安装时很容易找不到地方,名字建议区别原来的安装包。
- [root@localhost redis-5.0.4]# make PREFIX=/home/redis5 install
- cd src && make install
- make[1]: 进入目录“/home/redis-5.0.4/src”
- Hint: It's a good idea to run 'make test' ;)
- INSTALL install
- INSTALL install
- INSTALL install
- INSTALL install
- INSTALL install
- make[1]: 离开目录“/home/redis-5.0.4/src”
- [root@localhost redis-5.0.4]# cd ..
- [root@localhost home]# ls
- baoAn court momon redis5 redis-5.0.4 redis-5.0.4.tar.gz
- [root@localhost home]# cd redis5
- [root@localhost redis5]# ls
- bin
- [root@localhost redis5]# cd bin/
- [root@localhost bin]# ls
- redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
- [root@localhost bin]#

此时的 redis5 目录才是真正安装后的应用。
3、可以看到 bin 目录下的文件是对应着 windons 版本的 Redis中 的 exe 文件的
1、进入安装后 redis 目录中的 bin 目录下,如果 windows 系统一样,可以直接运行 redis-server 就会使用默认的配置进行启动 redis 数据库,此时默认端口为 6379.
2、前端启动的 redis-server 窗口会输出自己的一些信息,是不允许用户操作的,用户可以使用自带的客户端来进行操作,新开一个连接窗口进行操作:
./redis-cli :默认连接本地服务器,默认连接断开为 6379 ./redis-cli -h ip地址 -p 端口:指定连接远程 redis 服务器 |
1、对于前端启动的 redis 服务器,直接使用 “ctrl+c” 就会对其关闭
2、也可以直接杀进程的方式(不建议):
- [root@localhost ~]# ps -A|grep redis
- 2674 ? 00:00:02 redis-server
- 2726 pts/1 00:00:00 redis-cli
- [root@localhost ~]# kill -9 2674
- [root@localhost ~]# ps -A|grep redis
- 2726 pts/1 00:00:00 redis-cli
- [root@localhost ~]#
3、使用客户端发送关闭服务器命令 shutdown:
1、前端启动 Redis 会弹窗,当前窗口无法做其它操作了,所以建议使用后端启动。
2、解压后的 redis 目录下有一个 redis.conf 核心配置文件,可以将它复制粘贴到安装目录下 bin 目录下(位置没有强制要求)。
3、修改 redis.conf 中的配置参数 “daemonize no” 改为“daemonize yes,表示使用守护进程启动。(同理可以修改监听端口)
4、现在可以再次启动 redis-server redis.conf,表示加载 redis.conf 配置文件,没有指定配置文件时,将使用默认的参数值。
1、Mysql 可以远程连接,Redis 数据库同样也可以,同样用 ”-h” 指定目标 ip,”-p” 指定端口
第一步:开放防火墙
1)远程连接第一步应该先将 Linux 防火墙对 redis 服务器端口进行开放,否则 windows 上的客户端将无法访问。
- [root@localhost bin]# firewall-cmd --zone=public --list-ports
- [root@localhost bin]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
- success
- [root@localhost bin]# firewall-cmd --reload
- success
- [root@localhost bin]# firewall-cmd --zone=public --list-ports
- 6379/tcp
第二步:关闭保护模式访问
1)redis 服务器本身默认只允许本地访问,想要远程可以使用如下方式:
方式一,启动时指定关闭保护模式: [root@localhost bin]# ./redis-server --protected-mode no 方式二,修改 redis.conf 配置文件: #bind 127.0.0.1 #注释掉或配置成0.0.0.0表示任意IP均可访问。(如果是 windows 系统,则必须显示设置 bind 0.0.0.0,而不能注释) protected-mode no #设置为 no,关闭保护模式 |
第三步:启动 redis 服务:./redis-server redis.conf,必须带上配置文件,否则默认并不会读取配置文件的内容,导致客户端连接访问时报错:Error:磁盘正在使用中,或被另一个进程锁定。
如果只是平时的开发或者测试,一、二、三步之后就可以远程访问了,而且不需要密码。
第四步:设置登录密码访问
1)实际生产中,为了安全,应该开启保护模式,同时设置登录密码,redis.conf 配置文件中取消对 requirepass 的注释,然后设置密码即可:
#bind 127.0.0.1 #注释掉或配置成0.0.0.0表示任意IP均可访问。 protected-mode yes #开启保护模式了 requirepass 123456 #设置登陆密码 ./redis-server redis.conf #启动服务时,必须指定配置文件 |
2、设置了密码之后,使用 redis.cli.exe -h ip -p port 连接时,可以连接成功,但因为没有授权,所以无法操作命令,必须使用 “auth 密码” 进行授权验证。
3、也可以在登录的同时使用 -a 指定密码。redis.cli.exe -h ip -p port -a 密码
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。