redis默认启动方法都是以root启动的,不安全,现在创建一个新的用户来启动。
一、新增用户
新增用户组(lanren312group)
groupadd lanren312group
新建用户(lanren312)并加入(lanren312group)组中,并禁止登录
useradd -M lanren312 -g lanren312group -s /sbin/nologin
二、修改开机启动文件
方法一:修改 /etc/rc.local 文件
vim /etc/rc.local
# 最下面增加
sudo -u lanren312 /usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
# 退出赋权限
chmod +x /etc/rc.local
方法二:添加开机启动服务(推荐)
vi /etc/systemd/system/redis.service
######## 写入内容
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
User=lanren312 # 添加用户
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
修改redis.service文件后可能有提示:
Run 'systemctl daemon-reload' to reload units.
执行 systemctl daemon-reload 就可以了
三、重新启动redis
systemctl start redis.service ## 启动redis服务
systemctl stop redis.service ## 停止redis服务
systemctl restart redis.service ## 重启redis服务
systemctl status redis.service ## 查看redis服务当前状态
四、检查是否修改成功
ps -ef|grep redis
就会看到不再是root启动了,而是刚修改的lanren312用户。
五、其它
后面又碰到redis报错
MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
修改 redis.conf 文件配置
快速定位 /stop-writes-on-bgsave-error
stop-writes-on-bgsave-error 将yes改成no