Linux下安装Redis
1.下载redis压缩包
官网下载
https://redis.io/download
远程下载
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
2.解压redis压缩包
tar -zxvf redis-4.0.9.tar.gz
3.使用make编译
make
安装C语言编译环境
yum install -y gcc g++ gcc-c++ make
注:由于redis是通过C语言编写,Linux下未安装C语言编译环境,所以需要使用该命令安装C语言的编译环境
使用以上命令安装C语言编译环境后又报如下错误
报错原因分析
在README 有这个一段话。
Allocator
---------
Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.
To force compiling against libc malloc, use:
% make MALLOC=libc
To compile against jemalloc on Mac OS X systems, use:
% make MALLOC=jemalloc
说关于分配器allocator, 如果有MALLOC 这个 环境变量, 会有用这个环境变量的 去建立Redis。
而且libc 并不是默认的 分配器, 默认的是 jemalloc, 因为 jemalloc 被证明 有更少的 fragmentation problems 比libc。
但是如果你又没有jemalloc 而只有 libc 当然 make 出错。 所以加这么一个参数。
解决方式一:指定参数libc
make MALLOC=libc
解决方式二:
使用命令-- make distclean 先清除残留文件,之后再make.
4.使用make install进行安装
make install
5.修改redis.conf文件
daemonize(守护进程,默认为no)属性改为yes(表明需要在后台运行)
6.启动服务
./redis-server ../redis.conf
7.查看后台运行的端口的服务
netstat -tunpl|grep 6379
查看redis进程
ps -ef|grep redis
8.设置开机自启动
./install_server.sh
注:在utils路径下
This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
出现以上错误,解决方案
vi install_server.sh 查看该执行文件
注释以下代码
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
然后重新运行 ./install_server.sh即可
Mmmmm... the default config is missing. Did you switch to the utils directory?
出现以上错误原因(由于将redis.conf文件默认的地址路径进行了修改),解决方案
vi install_server.sh 查看执行文件
找到以下出错信息,可以看到,文件必须在该执行文件的上级目录
#render the templates
TMP_FILE="/tmp/${REDIS_PORT}.conf"
DEFAULT_CONFIG="${SCRIPTPATH}/../redis.conf" -- 文件必须在该执行文件的上级目录,或者修改该文件路径
INIT_TPL_FILE="${SCRIPTPATH}/redis_init_script.tpl"
INIT_SCRIPT_DEST="/etc/init.d/redis_${REDIS_PORT}"
PIDFILE="/var/run/redis_${REDIS_PORT}.pid"
if [ ! -f "$DEFAULT_CONFIG" ]; then
echo "Mmmmm... the default config is missing. Did you switch to the utils directory?"
exit 1
fi
将redis.conf文件放到该执行文件的上级目录即可
9.查看开机自启动的服务
chkconfig --list
10.进入redis客户端
./redis-cli
11. 杀死进程,关闭服务
kill pid
12.初始化密码
在redis.conf配置文件中有个参数: requirepass 这个就是配置redis访问密码的参数;
requirepass test123
(Ps:需重启Redis才能生效)
redis的查询速度是非常快的,外部用户一秒内可以尝试多大150K个密码;所以密码要尽量长(对于DBA 没有必要必须记住密码);
13、不重启Redis设置密码:
在配置文件中配置requirepass的密码(当redis重启时密码依然有效)。
redis 127.0.0.1:6379> config set requirepass test123
查询密码:
redis 127.0.0.1:6379> config get requirepass
(error) ERR operation not permitted
密码验证:
redis 127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test123"
PS:命令修改的是临时密码,是不会修改配置文件中的密码的,所以如果配置文件中没添加密码 那么redis重启后,密码失效;
14、登陆有密码的Redis:
在登录的时候的时候输入密码:
redis-cli -p 6379 -a test123
先登陆后验证:
redis-cli -p 6379
redis 127.0.0.1:6379> auth test123
OK
AUTH命令跟其他redis命令一样,是没有加密的;阻止不了攻击者在网络上窃取你的密码;
认证层的目标是提供多一层的保护。如果防火墙或者用来保护redis的系统防御外部攻击失败的话,外部用户如果没有通过密码认证还是无法访问redis的。