redis 安装
Posted 逃跑的沙丁鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis 安装相关的知识,希望对你有一定的参考价值。
目录
1 下载
wget http://download.redis.io/releases/redis-6.0.9.tar.gz
2 解压
tar -xzvf redis-6.0.9.tar.gz -C ../soft
3 安装
3.1 进入解压目录
[liucf@node1 soft]$ cd redis-6.0.9/
3.2 安装gcc编译环境
如果已经安装过了,那么跳过本步骤
[liucf@node1 redis-6.0.9]$ yum install gcc-c++
更新gcc:
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
yum install tcl
scl enable devtoolset-9 bash
3.3 编译并安装
sudo make
sudo make install
3.4 测试
[liucf@node1 redis-6.0.9]$ redis-server ./redis.conf
77320:C 19 Sep 2021 12:50:23.812 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
77320:C 19 Sep 2021 12:50:23.812 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=77320, just started
77320:C 19 Sep 2021 12:50:23.812 # Configuration loaded
77320:M 19 Sep 2021 12:50:23.813 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
77320:M 19 Sep 2021 12:50:23.813 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
77320:M 19 Sep 2021 12:50:23.813 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.9 (00000000/0) 64 bit
.-`` .-```. ```\\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 77320
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
77320:M 19 Sep 2021 12:50:23.813 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
77320:M 19 Sep 2021 12:50:23.813 # Server initialized
77320:M 19 Sep 2021 12:50:23.813 # 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.
77320:M 19 Sep 2021 12:50:23.813 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
77320:M 19 Sep 2021 12:50:23.813 * Ready to accept connections
[liucf@node1 ~]$ redis-cli
127.0.0.1:6379> CONFIG GET *
1) "rdbchecksum"
2) "yes"
3) "daemonize"
4) "no"
5) "io-threads-do-reads"
6) "no"
7) "lua-replicate-commands"
8) "yes"
9) "always-show-logo"
10) "yes"
11) "protected-mode"
...
4 配置redis
4.1 进入redis utils目录
cd utils
[liucf@node1 utils]$ pwd
/home/liucf/soft/redis-6.0.9/utils
[liucf@node1 utils]$ ll
total 68
-rw-rw-r-- 1 liucf liucf 593 Oct 27 2020 build-static-symbols.tcl
-rw-rw-r-- 1 liucf liucf 1303 Oct 27 2020 cluster_fail_time.tcl
-rw-rw-r-- 1 liucf liucf 1098 Oct 27 2020 corrupt_rdb.c
drwxrwxr-x 2 liucf liucf 60 Oct 27 2020 create-cluster
-rwxrwxr-x 1 liucf liucf 2147 Oct 27 2020 generate-command-help.rb
-rwxrwxr-x 1 liucf liucf 660 Oct 27 2020 gen-test-certs.sh
drwxrwxr-x 3 liucf liucf 31 Oct 27 2020 graphs
drwxrwxr-x 2 liucf liucf 39 Oct 27 2020 hashtable
drwxrwxr-x 2 liucf liucf 70 Oct 27 2020 hyperloglog
-rwxrwxr-x 1 liucf liucf 9981 Oct 27 2020 install_server.sh
drwxrwxr-x 2 liucf liucf 63 Oct 27 2020 lru
-rw-rw-r-- 1 liucf liucf 1277 Oct 27 2020 redis-copy.rb
-rwxrwxr-x 1 liucf liucf 1352 Oct 27 2020 redis_init_script
-rwxrwxr-x 1 liucf liucf 1047 Oct 27 2020 redis_init_script.tpl
-rw-rw-r-- 1 liucf liucf 1762 Oct 27 2020 redis-sha1.rb
drwxrwxr-x 2 liucf liucf 135 Oct 27 2020 releasetools
-rwxrwxr-x 1 liucf liucf 3787 Oct 27 2020 speed-regression.tcl
drwxrwxr-x 2 liucf liucf 61 Oct 27 2020 srandmember
-rw-rw-r-- 1 liucf liucf 1310 Oct 27 2020 systemd-redis_multiple_servers@.service
-rw-rw-r-- 1 liucf liucf 1550 Oct 27 2020 systemd-redis_server.service
-rw-rw-r-- 1 liucf liucf 2271 Oct 27 2020 tracking_collisions.c
-rwxrwxr-x 1 liucf liucf 693 Oct 27 2020 whatisdoing.sh
4.2 拷贝文件至/etc/init.d
[liucf@node1 utils]$ sudo cp redis_init_script /etc/init.d/
进入目录查看
[liucf@node1 init.d]$ cd /etc/init.d/
4.3 创建目录拷贝redis.conf
这个目录方便后面存放redis.conf
[liucf@node1 local]$ sudo mkdir /usr/local/redis
拷贝redis.conf到 /usr/local/redis
[liucf@node1 redis-6.0.9]$ sudo cp redis.conf /usr/local/redis
4.4 修改配置文件
4.4.1修改 daemonize
修改 daemonize no -> daemonize yes,目的是为了让redis启动在linux后台运行
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
##daemonize no
##
daemonize yes
4.4.2 修改工作空间目录
创建目录
[liucf@node1 redis]$ sudo mkdir /usr/local/redis/working
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /usr/local/redis/working
4.4.3 修改bind使其外部可以访问
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 loopback interface address (this means Redis will only be able to
# accept client connections from the same host that it is running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
##protected-mode yes
protected-mode no
4.4.4 设置redis密码
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# requirepass foobared
requirepass liucf123456
配置修改完毕
5 修改启动脚本文件
① 进入目录
[liucf@node1 redis]$ cd /etc/init.d/
[liucf@node1 init.d]$ ll
total 44
-rw-r--r--. 1 root root 18281 May 22 2020 functions
-rwxr-xr-x. 1 root root 4569 May 22 2020 netconsole
-rwxr-xr-x. 1 root root 7928 May 22 2020 network
-rw-r--r--. 1 root root 1160 Oct 2 2020 README
-rwxr-xr-x 1 root root 1352 Sep 19 13:53 redis_init_script
[liucf@node1 init.d]$
②修改
[liucf@node1 init.d]$ sudo vim redis_init_script
...
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_$REDISPORT.pid
##CONF="/etc/redis/$REDISPORT.conf"
##指定到自己配置的配置文件目录
CONF="/usr/local/redis/redis.conf"
...
6 启动redis
[liucf@node1 init.d]$ sudo /etc/init.d/redis_init_script start
Starting Redis server...
[liucf@node1 init.d]$
7 远程连接测试
[liucf@node1 init.d]$ redis-cli -h 192.168.109.151 -p 6379
192.168.109.151:6379> CONFIG GET *
(error) NOAUTH Authentication required. ## 提示认证出现问题
192.168.109.151:6379> get key
(error) NOAUTH Authentication required. ## 提示认证出现问题
192.168.109.151:6379> auth liucf123456 ## 输入密码
OK ##认证成功
192.168.109.151:6379> CONFIG GET * ##使用命令查看
1) "rdbchecksum"
2) "yes"
3) "daemonize"
4) "yes"
5) "io-threads-do-reads"
6) "no"
7) "lua-replicate-commands"
8) "yes"
9) "always-show-logo"
10) "yes"
11) "protected-mode"
12) "no"
13) "rdbcompression"
14) "yes"
.....
完成
补充命令:
192.168.109.151:6379> set liucfkey liucfvalue ## 设置缓存
OK
192.168.109.151:6379> get liucfkey ## 查看缓存
"liucfvalue"
192.168.109.151:6379> del liucfkey ## 删除缓存
(integer) 1
192.168.109.151:6379> get liucfkey ## 验证是否删除
(nil)
192.168.109.151:6379>
以上是关于redis 安装的主要内容,如果未能解决你的问题,请参考以下文章