Centos Redis 安装配置和启动脚本
Redis管理脚本在Centos上使用之前,需要先配置下面的内核参数,否则Redis脚本在重启或停止redis时,将会报错,并且不能自动在停止服务前同步数据到磁盘上。
redis 源码: https://github.com/antirez/redis
redis 官方: https://redis.io
redis 最新:Github —> Redis —> Tags
redis-stable.tar.gz (最新稳定版本,不是RC候选版本)
redis-7.2.5.tar.gz (2024-05-19)
redis-4.0.10.tar.gz (2018-06-13)
redis 安装配置:
wget -O redis-7.2.5.tar.gz https://github.com/redis/redis/archive/refs/tags/7.2.5.tar.gz
tar zxvf redis-7.2.5.tar.gz
cd redis-7.2.5/
make
make install # 未指定安装目录 (不推荐)
make install PREFIX=/usr/local/redis # 指定安装目录 (推荐)
1. 配置内核参数
# vim /etc/sysctl.conf
vm.overcommit_memory = 1
然后应用生效:
# sysctl -p
若不配置内核参数,启动 redis-server 命令时会报错,并给出了如上修复方案:
# redis-server
2459:C 30 Jun 2024 22:24:54.852 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. 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.
2. 添加Redis启动脚本
redis 可分为默认和指定安装目录,其中指定安装目录: make install prefix=/use/local/redis
vim /etc/init.d/redis
#!/bin/bash # # Init file for redis # # chkconfig: - 80 12 # description: redis daemon # author: www.mimvp.com # create: 2017-04-30 # update: 2024-06.28 # # vim /etc/init.d/redis # processname: redis source /etc/init.d/functions ## redis脚本启动配置 # 1. 手动拷贝源目录下的 redis.conf 到 /etc/ 或 /usr/local/redis/etc/ # 2. 修改 redis.conf 文件里的后台进程daemon, 端口号port, logfile, rdbfile, aoffile(开启aof) # ## redis安装目录的两种配置 ## 1. redis默认安装目录 BIN="/usr/local/bin" CONFIG=/etc/redis.conf PIDFILE=/var/run/redis.pid #LOGFILE=/var/log/redis.log #RDBFILE=/var/lib/redis/dump.rdb #AOFFILE=/var/lib/redis/appendonlydir/appendonly.aof[.1.base.rdb|.1.incr.rdb|.manifest] #PORT=6379 #TSL-PORT=6379 # 不存在目录, 则创建目录, 防止启动报错 if [ ! -d "/var/lib/redis" ]; then /usr/bin/mkdir -p /var/lib/redis fi ## 2. redis指定安装目录 REDIS_SERVER="/usr/local/redis/bin/redis-server" if [ -e ${REDIS_SERVER} ]; then BIN="/usr/local/redis/bin" CONFIG=/usr/local/redis/etc/redis.conf PIDFILE=/usr/local/redis/logs/redis.pid #LOGFILE=/usr/local/redis/logs/redis.log #RDBFILE=/usr/local/redis/data/dump.rdb #AOFFILE=/usr/local/redis/data/appendonlydir/appendonly.aof[.1.base.rdb|.1.incr.rdb|.manifest] #PORT=6379 #TSL-PORT=6379 ## 不存在目录, 则创建目录, 防止启动报错 if [ ! -d "/usr/local/redis/logs" ]; then /usr/bin/mkdir -p /usr/local/redis/logs fi if [ ! -d "/usr/local/redis/data" ]; then /usr/bin/mkdir -p /usr/local/redis/data fi fi ### Read configuration [ -r "$SYSCONFIG" ] && source "$SYSCONFIG" RETVAL=0 prog="redis-server" desc="Redis Server" start() { if [ -e $PIDFILE ]; then echo "$desc already running...." exit 1 fi echo -n $"Starting $desc: " daemon $BIN/$prog $CONFIG RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog return $RETVAL } stop() { echo -n $"Stop $desc: " killproc $prog RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE return $RETVAL } restart() { stop start } case "$1" in start) start ;; stop) stop ;; restart) restart ;; condrestart) [ -e /var/lock/subsys/$prog ] && restart RETVAL=$? ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|condrestart|status}" RETVAL=1 esac exit $RETVAL
脚本说明:
1)默认安装目录
make && make install
需手动拷贝并配置其内容: vim /etc/redis.conf
2)指定安装目录 (推荐)
make && make install prefix=/use/local/redis
需手动拷贝并配置其内容: vim /usr/local/redis/etc/redis.conf
为了方便也可使用软链接:ln -s /usr/local/redis/etc/redis.conf /etc/redis.conf
bind 127.0.0.1 -::1 # 若注释掉此行, 则表示从任何机器可访问redis-server因此需加访问密码requirepass protected-mode yes # 开启保护模式, 当没设置密码requirepass时则只能从本机(127.0.1或::1)登录 requirepass 'mimvp@24' # 设置访问密码, 增强安全性, 默认密码为"foobared", 推荐同时开启此三项安全配置 port 16379 # 修改自定义端口号, 默认端口号 16379 daemonize yes # 后台服务器启动, 默认为"no" ## pidfile 仅redis-server通过后台daemon启动才会被创建(不配置也会默认创建pidfile), 若不是daemon启动则不会创建pidfile文件 pidfile /var/run/redis.pid # 1. redis默认安装目录, 默认值为"pidfile /var/run/redis_6379.pid" pidfile /usr/local/redis/logs/redis.pid # 2. redis指定安装目录, 使用 "/usr/local/redis/etc/" 方便统一管理 logfile "/var/log/redis.log" # 1. redis默认安装目录, 默认值为空""表示日志会标准输出或丢弃到/dev/null logfile "/usr/local/redis/logs/redis.log" # 2. redis指定安装目录, 使用 "/usr/local/redis/logs/" 方便统一管理 loglevel notice # 日志级别, 保持默认级别"notice", 所有级别包含: debug, verbose, notice, warning, nothing ## redis 7.x.x dump.rdb bgsave持久化保存频率, 格式: save <seconds> <changes> [<seconds> <changes> ...] 单位:秒 save 3600 1 300 100 60 10000 ## redis 4.x.x dump.rdb bgsave持久化保存频率, 格式: save <seconds> <changes> 单位:秒, 表示15分钟,5分钟,1分钟 keys save 900 1 save 300 10 save 60 100000 dbfilename dump.rdb # 持久化日志文件名 dir /var/lib/redis/ # 1. redis默认安装目录, 持久化目录为 "/var/lib/redis/dump.rdb" dir /usr/local/redis/data/ # 2. redis指定安装目录, 持久化目录为 "/usr/local/redis/data/dump.rdb" appendonly yes # 开启AOF文件(Append Only File)持久化, 与RDB持久化可同时开启, 默认值为"no" appendfilename "appendonly.aof" # AOF文件名,其根目录与dir相同, 即全路径为 "/var/lib/redis/appendonlydir/appendonly.aof" appenddirname "appendonlydir" # AOF文件的目录,其根目录与dir相同, 即全路径为 "/var/lib/redis/appendonlydir/" appendfsync everysec # 毎一秒保存一次, 默认值为 "everysec", 其值还包含: always, everysec, no
查看 redis-7.x.x 指定安装目录结构,方便统一管理,非常清晰易记忆:
# tree /usr/local/redis/ /usr/local/redis/ ├── bin │ ├── redis-benchmark │ ├── redis-check-aof -> redis-server │ ├── redis-check-rdb -> redis-server │ ├── redis-cli │ ├── redis-sentinel -> redis-server │ └── redis-server ├── data │ ├── appendonlydir │ │ ├── appendonly.aof.1.base.rdb │ │ ├── appendonly.aof.1.incr.aof │ │ └── appendonly.aof.manifest │ └── dump.rdb ├── etc │ └── redis.conf └── logs ├── redis.log └── redis.pid 5 directories, 13 files
3、添加环境变量
## 配置环境变量(指定安装目录需配置; 若默认安装不需配置, 其路径为 /usr/local/bin/redis-server) vim /etc/profile ## 在/etc/profile文件末尾添加如下内容 export REDIS_ROOT=/usr/local/redis export PATH=${REDIS_ROOT}/bin:$PATH ## 使环境变量生效 source /etc/profile
4、添加可执行权限
chmod +x /etc/init.d/redis
/etc/init.d/redis start /etc/init.d/redis stop /etc/init.d/redis restart /etc/init.d/redis status
5、增加开机自启动:
# chmod 755 /etc/init.d/redis
# chkconfig --add redis
# chkconfig --level 345 redis on
# chkconfig --list redis
[root@mimvp-hz ~]# chkconfig --list redis
redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
6、redis-server通过配置systemctl启动
1)配置 redis.service文件
vim /usr/lib/systemd/system/redis.service
[Unit] Description=redis daemon Documentation=redis-server --help Documentation=https://blog.mimvp.com/article/12428.html After=network.target [Service] ## 启动命令, --daemonize必须为no, 若为yes启动失败, 详见米扑博客: https://blog.mimvp.com/article/12428.html #ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf --daemonize no ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf --daemonize no --supervised systemd ExecStop=/usr/bin/killall redis-server [Install] WantedBy=multi-user.target
注意:
ExecStart 启动命令行,必须使用 --daemonize no,若设置为 yes 会启动失败。
在redis.conf配置文件里使用了 --daemonize yes,是在后台服务启动redis
2)配置开机启动
systemctl enable redis.service
systemctl daemon-reload
systemctl start redis.service
systemctl stop redis.service
systemctl restart redis.service
systemctl status redis.service
3)查看开机启动的服务
systemctl list-unit-files | grep -iE "mysql|mariadb|redis|memcached|nginx|httpd|apache|php"
# systemctl list-unit-files | grep -iE "mysql|mariadb|redis|memcached|nginx|httpd|apache|php" mariadb.service enabled mariadb@.service disabled memcached.service enabled mysql.service enabled mysqld.service enabled nginx.service enabled redis.service enabled mariadb-extra.socket static mariadb-extra@.socket static mariadb.socket static mariadb@.socket static
7、测试验证
1)redis-server 启动进程验证
# ps -ef | grep redis root 10038 1 0 20:31 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:16379
2)redis-cli 客户端验证
## redis-cli 客户端登录, 指定机器IP、端口号、密码、默认数据库序号 redis-cli -h 127.0.0.1 -p 16379 -a 'mimvp@24' -n 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:16480[2]> # ## 选择数据库2,默认有16个库,序号 0-15 127.0.0.1:16480> select 2 OK 127.0.0.1:16480[2]> keys * (empty array) 127.0.0.1:16480[2]> set foo bar # 设置字符串赋值 OK 127.0.0.1:16480[2]> setex key 100 second-100 # 设置超时时间,单位秒 OK 127.0.0.1:16480[2]> psetex key-ms 10000 millis-10000 # 设置超时时间,单位毫秒 OK 127.0.0.1:16480[2]> get foo # 获取字符串值 "bar" 127.0.0.1:16480[2]> mget foo key key-ms # 批量获取字符串值 1) "bar" 2) "second-100" 3) (nil) 127.0.0.1:16480[2]> lpush list l1 l2 l3 l4 l5 l6 # list列表赋值 (integer) 6 127.0.0.1:16480[2]> lrange list 0 3 # list列表获取值 1) "l6" 2) "l5" 3) "l4" 4) "l3" # ## redis-cli客户端登录, 扫描查看指定数据库里的keys redis-cli -h 127.0.0.1 -p 16379 -a 'mimvp@24' -n 2 --scan Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. "list" "foo"
参考推荐:
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2024-07-01 20:34:43
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!