在CentOS7中安装和配置Redis,centos7配置redis
文章目录
- 在CentOS7中安装和配置Redis
- 下载Redis安装包
- 编译和安装Redis
- 启动Redis Server
- 配置Redis Server为开机自启动
- 修改Redis Server为后台运行
- 设置Redis Server的密码
- 设置允许远程连接
- 参考文档
在CentOS7中安装和配置Redis
本文介绍了在CentOS7上安装Redis 5.0.3,并配置Redis Server。
下载Redis安装包
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
如果要安装其它版本的Redis,请到http://download.redis.io/releases 中选择。
编译和安装Redis
解压Redis安装包:
tar -zxvf redis-5.0.3.tar.gz -C /usr/local/
安装gcc来编译Redis源代码:
# 检查gcc是否已经安装
gcc -v
# 安装gcc
sudo yum install -y gcc
gcc -v
编译Redis源代码:
cd /usr/local/redis-5.0.3
make
安装tcl包依赖来运行make test:
sudo yum install -y tcl
检查编译后的Redis程序是否正常:
cd /usr/local/redis-5.0.3
make test
添加Redis程序到PATH环境变量中:
cd /usr/local/redis-5.0.3
make install
启动Redis Server
启动Redis Server:
redis-server
这时Redis Server将以默认配置在前台运行。
后面会讲到如何设置为后台运行和设置开机自动运行,并加载指定配置。
在另一个会话窗口,打开Redis Client来验证Redis是否工作正常:
redis-cli
ping
help
quit
在Redis Server会话窗口按下Ctrl + C键关闭Redis Server。
配置Redis Server为开机自启动
使用Redis内置的install_server.sh来配置Redis Server为开机自启动:
cd /usr/local/redis-5.0.3
cd utils
./install_server.sh
一般地,按下Enter键使用默认设置即可完成Redis Server配置。
默认设置包括:
# Redis configuration file
/etc/redis/6379.conf
# Redis log file
/var/log/redis_6379.log
# Redis working directory
/var/lib/redis/6379
# Redis Pid file
/var/run/redis_6379.pid
关闭和启动Redis Server的命令:
sudo /etc/init.d/redis_6379 stop
sudo /etc/init.d/redis_6379 start
修改Redis Server为后台运行
编辑Redis configuration file (/etc/redis/6379.conf),将daemonize no改为daemonize yes。
重启Redis Server。
设置Redis Server的密码
编辑Redis configuration file (/etc/redis/6379.conf),取消# requirepass foobared的注释,并在requirepass后输入密码。
可以使用
echo “your_password” | sha1sum来生成较为复杂的代码。
重启Redis Server。
在设置Redis Server密码后,需要在Redis Client中运行auth <your_password>后才可以shutdownRedis server和执行Redis操作命令。
设置允许远程连接
为安全起见,Redis Server缺省只允许本机连接。
如果要允许任意机器访问Redis Server,需要编辑Redis configuration file (/etc/redis/6379.conf),将bind 127.0.0.1改为bind 0.0.0.0。
在安全的网络内,可以设置为允许任意机器访问;在不安全的网络中,需要设置为指定IP才能访问,并配置防火墙。
这部分内容请参考:
- https://www.digitalocean.com/community/tutorials/how-to-install-secure-redis-centos-7
参考文档
- https://redis.io/topics/quickstart
- https://blog.csdn.net/u010623954/article/details/80037078
- https://www.digitalocean.com/community/tutorials/how-to-install-secure-redis-centos-7