Redis在Linux环境下的搭建
1.下载安装
在官网下载,这里默认下载最新版本
地址:http://redis.io/download
在linux中新建一个文件夹,用来存放安装后的redis
使用FileZilla等远程传输工具把tar压缩包复制到Linux中
解压
tar zxvf 压缩包名
编译
进入解压后的文件夹内
make
执行命令进行编译,编译成功后 redis-6.2 中会有对应的src、conf等文件夹
编译完成后,进入src文件夹,执行安装命令
make install
执行安装命令时可能会遇到
install: cannot create regular file ‘/usr/local/bin/redis-server’: Permission denied
提示权限不够,网上有授权方法
chmod 777 redis-server
授权后依然不能安装,我尝试在命令后面加上了路径,放在之前创建的redis的位置
PREFIX=/usr/local/workspace/redis-server
成功了
目测失败原因是因为我的make install命令没有创建文件夹的权限,或者是默认安装的路径没有访问权限
安装成功后建议把redis.config文件放到redis-server中,方便统一管理(非必须,但大佬们都这么做)
这个时候,可以尝试通过配置文件的方式启动redis
./redis-server /路径/redis.conf
出现熟悉的界面
30473:C 18 Feb 2021 15:27:57.225 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30473:C 18 Feb 2021 15:27:57.225 # Redis version=6.1.242, bits=64, commit=00000000, modified=0, pid=30473, just started
30473:C 18 Feb 2021 15:27:57.225 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
30473:M 18 Feb 2021 15:27:57.225 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.1.242 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 30473
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
30473:M 18 Feb 2021 15:27:57.226 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30473:M 18 Feb 2021 15:27:57.226 # Server initialized
30473:M 18 Feb 2021 15:27:57.226 # 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.
30473:M 18 Feb 2021 15:27:57.226 * Ready to accept connections
^C30473:signal-handler (1613633307) Received SIGINT scheduling shutdown...
30473:M 18 Feb 2021 15:28:27.361 # User requested shutdown...
30473:M 18 Feb 2021 15:28:27.361 * Saving the final RDB snapshot before exiting.
30473:M 18 Feb 2021 15:28:27.362 * DB saved on disk
安装成功!
可以尝试一下,运行redis自带的客户端工具
./redis.cli
默认的端口是6379,如果修改了端口,需要执行
./redis.cli -p 端口号
出现以下
127.0.0.1:63079>
2.设置开机自启动
redis在shell中启动后,如果关闭shell终端,默认redis服务也会跟着关闭,我们可以通过设定守护进程的方式让redis服务持续运行
修改redis.conf文件, 将daemonize属性改为yes
再次启动redis,则默认在守护进程下运行
但是,我们后来尝试通过systemctl指令的方式来开启redis服务,但是使用systemctl指令的方式启动时,如果daemonize属性设置为yes(默认开启守护进程),会导致redis开启立即关闭,无法正常运行,所以,回去把daemonize再改回no吧
言归正传
我们选择用systemctl命令的方式来设置开机自启动,首先需要在systemctl的路径下写一个脚本
路径
systemd的Unit放在目录/usr/lib/systemd/system(Centos)或/etc/systemd/system(Ubuntu)
创建一个脚本
mkdir redis.service
编辑内容
[Unit]
Description=Redis on port
[Service]
Type=forking
ExecStart=/etc/init.d/redis start
ExecStop=/etc/init.d/redis stop
[Install]
WantedBy=multi-user.target
然后,只需要通过命令
systemctl start redis.service
就可以启动redis服务了
关闭服务
systemctl stop redis.service
查看服务状态
systemctl status redis.service