Ubuntu18.04安装Redis
Redis简介
Redis是一种开放源代码(BSD许可)的内存中数据结构存储,用作数据库,缓存和消息代理。Redis提供数据结构,例如字符串,哈希,列表,集合,带范围查询的排序集合,位图,超日志,地理空间索引和流。Redis具有内置的复制,Lua脚本,LRU驱逐,事务和不同级别的磁盘持久性,并通过Redis Sentinel和Redis Cluster自动分区提供了高可用性。
Redis官网
https://redis.io/
官方安装教程
编译环境安装
Redis由C/C++编写,因此由源码编译成可执行程序需要C/C++编译器以及Make工具链
#安装gcc
root@DESKTOP:/usr/local# apt install gcc
Reading package lists... Done
Building dependency tree
Reading state information... Done
gcc is already the newest version (4:7.4.0-1ubuntu2.3).
The following packages were automatically installed and are no longer required:
git-man golang-1.10-src liberror-perl
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 90 not upgraded.
#安装g++
root@DESKTOP:/usr/local# apt install g++
Reading package lists... Done
Building dependency tree
Reading state information... Done
g++ is already the newest version (4:7.4.0-1ubuntu2.3).#g++已经是最新版本
The following packages were automatically installed and are no longer required:
git-man golang-1.10-src liberror-perl
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 90 not upgraded.
#安装make
root@DESKTOP:/usr/local# apt install make
Reading package lists... Done
Building dependency tree
Reading state information... Done
make is already the newest version (4.1-9.1ubuntu1). #make已经是最新版本
The following packages were automatically installed and are no longer required:
git-man golang-1.10-src liberror-perl
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 90 not upgraded.
编译安装
root@DESKTOP:/usr/local/src/REDIS# ls
redis-6.2.3.tar.gz
root@DESKTOP:/usr/local/src/REDIS# tar -xvf redis-6.2.3.tar.gz #解压缩
root@DESKTOP:/usr/local/src/REDIS# cd redis-6.2.3/
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# make #编译Redis
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# make install redis-server redis-cli redis-benchmark #安装至指定目录
自定义配置文件
#创建配置目录
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# mkdir MYCONF
#复制/redis-6.2.3/redis.conf Redis服务端配置文件 至MYCONF目录下
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# cp redis.conf MYCONF/
修改配置文件
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# cd MYCONF/
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3/MYCONF# vim redis.conf
Redis默认非后台启动,而通常情况下希望它能后台启动,则修改如下:
daemonize no
#修改为
daemonize yes
Redis默认是没有登录密码的,官方将密码配置部分注释起来了,而考虑到安全问题添加相应的密码:
#requirepass foobared
#修改为
requirepass wsharkcoder
#其中密码:wsharkcoder
Redis服务器启动
#启动方式:redis-server <配置文件>
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3/MYCONF# redis-server /usr/local/src/REDIS/redis-6.2.3/MYCONF/redis.conf
#检查进程是否启动
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3/MYCONF# ps -ef |grep -e redis
root 16388 11797 0 18:33 ? 00:00:00 redis-server 127.0.0.1:6379
Redis内置客户端连接Redis服务器
#连接方式:redis-cli -h <主机> -p <端口> -a <密码>
redis-cli -h localhost -p 6379 -a wsharkcoder
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3/MYCONF# redis-cli -h localhost -p 6379 -a wsharkcoder
#不安全性
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
localhost:6379>
关闭Redis
#Redis-cli连接后可使用shutdown进行关闭
localhost:6379> shutdown
not connected>
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3/MYCONF# ps -ef |grep -e redis
root 17824 11798 0 18:41 pts/0 00:00:00 grep --color=auto -e redis
启动脚本
#简单脚本启动
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# touch Start_Redis.sh #创建启动脚本
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# vim Start_Redis.sh #编辑启动脚本
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# cat Start_Redis.sh #查看启动脚本内容
#!/bin/bash
redis-server ./MYCONF/redis.conf
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# chmod u+x Start_Redis.sh #赋予启动脚本执行权限
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# ./Start_Redis.sh #脚本启动
root@DESKTOP:/usr/local/src/REDIS/redis-6.2.3# ps -ef |grep -e redis #检索进程
root 19201 11797 0 18:48 ? 00:00:00 redis-server 127.0.0.1:6379
root 19228 11798 0 18:48 pts/0 00:00:00 grep --color=auto -e redis
Shell脚本和命令不能说毫不相干,只能说一摸一样,哈哈!更加方便的脚本方式,日后学会了用熟了再补充,嘿嘿~