安装环境:
操作系统:Centos7 x86_64
redis版本:redis-4.0.2.tar.gz
上传redis安装文件redis4.0
[root@localhost soft]# mkdir redis4.0
[root@localhost soft]# cd redis4.0/
[root@localhost redis4.0]# pwd
/root/soft/redis4.0
[root@localhost redis4.0]# ll
total 1676
-rw-r--r--. 1 root root 1713990 Jul 19 22:09 redis-4.0.2.tar.gz
解压安装文件:
[root@localhost redis4.0]# tar -zxvf redis-4.0.2.tar.gz
redis-4.0.2/
redis-4.0.2/.gitignore
redis-4.0.2/00-RELEASENOTES
redis-4.0.2/BUGS
redis-4.0.2/CONTRIBUTING
........
redis-4.0.2/utils/releasetools/04_release_hash.sh
redis-4.0.2/utils/releasetools/changelog.tcl
redis-4.0.2/utils/speed-regression.tcl
redis-4.0.2/utils/whatisdoing.sh
[root@localhost redis4.0]# ll
total 1680
drwxrwxr-x. 6 root root 4096 Sep 21 2017 redis-4.0.2
-rw-r--r--. 1 root root 1713990 Jul 19 22:09 redis-4.0.2.tar.gz
[root@localhost redis4.0]#
将解压后的安装文件夹 移动并重命名到 /usr/local/redis
[root@localhost redis4.0]#
[root@localhost redis4.0]# mv redis-4.0.2 /usr/local/redis
[root@localhost redis4.0]# cd /usr/local/redis
[root@localhost redis]# pwd
/usr/local/redis
[root@localhost redis]# ll
total 272
-rw-rw-r--. 1 root root 131381 Sep 21 2017 00-RELEASENOTES
-rw-rw-r--. 1 root root 53 Sep 21 2017 BUGS
-rw-rw-r--. 1 root root 1815 Sep 21 2017 CONTRIBUTING
-rw-rw-r--. 1 root root 1487 Sep 21 2017 COPYING
drwxrwxr-x. 6 root root 124 Sep 21 2017 deps
-rw-rw-r--. 1 root root 11 Sep 21 2017 INSTALL
-rw-rw-r--. 1 root root 151 Sep 21 2017 Makefile
-rw-rw-r--. 1 root root 4223 Sep 21 2017 MANIFESTO
-rw-rw-r--. 1 root root 20530 Sep 21 2017 README.md
-rw-rw-r--. 1 root root 57764 Sep 21 2017 redis.conf
-rwxrwxr-x. 1 root root 271 Sep 21 2017 runtest
-rwxrwxr-x. 1 root root 280 Sep 21 2017 runtest-cluster
-rwxrwxr-x. 1 root root 281 Sep 21 2017 runtest-sentinel
-rw-rw-r--. 1 root root 7606 Sep 21 2017 sentinel.conf
drwxrwxr-x. 3 root root 4096 Sep 21 2017 src
drwxrwxr-x. 10 root root 167 Sep 21 2017 tests
drwxrwxr-x. 8 root root 4096 Sep 21 2017 utils
[root@localhost redis]#
[root@localhost redis]#
使用4个CPU进行编译
[root@localhost redis]# make -j 4
cd src && make all
make[1]: Entering directory `/usr/local/redis/src'
CC Makefile.dep
make[1]: Leaving directory `/usr/local/redis/src'
make[1]: Entering directory `/usr/local/redis/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
。。。。。。
INSTALL redis-sentinel
INSTALL redis-check-rdb
INSTALL redis-check-aof
LINK redis-cli
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory `/usr/local/redis/src'
[root@localhost redis]#
编译完成后 ,再需要make install 安装下 , 把编译完成的一些 文件添加到系统目录里:
[root@localhost redis]#
[root@localhost redis]# make install
cd src && make install
make[1]: Entering directory `/usr/local/redis/src'
CC Makefile.dep
make[1]: Leaving directory `/usr/local/redis/src'
make[1]: Entering directory `/usr/local/redis/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/usr/local/redis/src'
[root@localhost redis]#
[root@localhost redis]# ll
total 280
-rw-rw-r--. 1 root root 131381 Sep 21 2017 00-RELEASENOTES
-rw-rw-r--. 1 root root 53 Sep 21 2017 BUGS
-rw-rw-r--. 1 root root 1815 Sep 21 2017 CONTRIBUTING
-rw-rw-r--. 1 root root 1487 Sep 21 2017 COPYING
drwxrwxr-x. 6 root root 192 Jul 19 22:18 deps
-rw-rw-r--. 1 root root 11 Sep 21 2017 INSTALL
-rw-rw-r--. 1 root root 151 Sep 21 2017 Makefile
-rw-rw-r--. 1 root root 4223 Sep 21 2017 MANIFESTO
-rw-rw-r--. 1 root root 20530 Sep 21 2017 README.md
-rw-rw-r--. 1 root root 57764 Sep 21 2017 redis.conf
-rwxrwxr-x. 1 root root 271 Sep 21 2017 runtest
-rwxrwxr-x. 1 root root 280 Sep 21 2017 runtest-cluster
-rwxrwxr-x. 1 root root 281 Sep 21 2017 runtest-sentinel
-rw-rw-r--. 1 root root 7606 Sep 21 2017 sentinel.conf
drwxrwxr-x. 3 root root 8192 Jul 19 22:22 src
drwxrwxr-x. 10 root root 167 Sep 21 2017 tests
drwxrwxr-x. 8 root root 4096 Sep 21 2017 utils
[root@localhost redis]#
修改redis.conf 文件 :
[root@localhost redis]# vim redis.conf
修改如下内容:
(1)参数bind
修改前:bind 127.0.0.1 ,即只有本机才能访问
修改后:bind 0.0.0.0 , 即不限制其他服务器访问(当前为了测试和学习,生产环境请勿这样设置)
(2)参数daemonize no
修改前:daemonize no , 不能后台运行
修改后:daemonize yes ,允许后台允许
启动redis : redis-server ./redis.conf
[root@localhost redis]# pwd
/usr/local/redis
[root@localhost redis]# ll
total 280
-rw-rw-r--. 1 root root 131381 Sep 21 2017 00-RELEASENOTES
-rw-rw-r--. 1 root root 53 Sep 21 2017 BUGS
-rw-rw-r--. 1 root root 1815 Sep 21 2017 CONTRIBUTING
-rw-rw-r--. 1 root root 1487 Sep 21 2017 COPYING
drwxrwxr-x. 6 root root 192 Jul 19 22:18 deps
-rw-rw-r--. 1 root root 11 Sep 21 2017 INSTALL
-rw-rw-r--. 1 root root 151 Sep 21 2017 Makefile
-rw-rw-r--. 1 root root 4223 Sep 21 2017 MANIFESTO
-rw-rw-r--. 1 root root 20530 Sep 21 2017 README.md
-rw-rw-r--. 1 root root 57763 Jul 19 22:29 redis.conf
-rwxrwxr-x. 1 root root 271 Sep 21 2017 runtest
-rwxrwxr-x. 1 root root 280 Sep 21 2017 runtest-cluster
-rwxrwxr-x. 1 root root 281 Sep 21 2017 runtest-sentinel
-rw-rw-r--. 1 root root 7606 Sep 21 2017 sentinel.conf
drwxrwxr-x. 3 root root 8192 Jul 19 22:22 src
drwxrwxr-x. 10 root root 167 Sep 21 2017 tests
drwxrwxr-x. 8 root root 4096 Sep 21 2017 utils
[root@localhost redis]# redis-server ./redis.conf
51137:C 19 Jul 22:30:26.605 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
51137:C 19 Jul 22:30:26.605 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=51137, just started
51137:C 19 Jul 22:30:26.605 # Configuration loaded
[root@localhost redis]# ps -ef | grep redis
root 51138 1 0 22:30 ? 00:00:00 redis-server 0.0.0.0:6379
root 51144 47694 0 22:30 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis]#
使用redis-cli 登录 redis
[root@localhost redis]# redis-cli
127.0.0.1:6379> set key1 123456
OK
127.0.0.1:6379> get key1
"123456"
127.0.0.1:6379>
127.0.0.1:6379> set key2 234567
OK
127.0.0.1:6379> get key2
"234567"
127.0.0.1:6379>
127.0.0.1:6379> exit
[root@localhost redis]#
为了安全期间,设置为redis设置登录密码:
修改 redis.conf 文件: vim redis.conf
修改前: # requirepass foobared , #为注释行
修改后: requirepass 123456 ,去掉#,设置密码为123456
修改redis.conf 文件后, 需要关闭,再重新启动redis
[root@localhost redis]# redis-cli
127.0.0.1:6379> shutdown save
not connected> exit
[root@localhost redis]# ps -ef | grep redis
root 51167 47694 0 22:38 pts/0 00:00:00 grep --color=auto redis
重新启动redis
[root@localhost redis]# redis-server ./redis.conf
51168:C 19 Jul 22:38:30.385 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
51168:C 19 Jul 22:38:30.385 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=51168, just started
51168:C 19 Jul 22:38:30.385 # Configuration loaded
[root@localhost redis]# ps -ef | grep redis
root 51169 1 0 22:38 ? 00:00:00 redis-server 0.0.0.0:6379
root 51175 47694 0 22:38 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis]# redis-cli
127.0.0.1:6379> get key1
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get key1
"123456"
127.0.0.1:6379> get key2
"234567"
127.0.0.1:6379>
通常redis安装为系统服务,
[root@localhost redis]# ll
total 284
-rw-rw-r--. 1 root root 131381 Sep 21 2017 00-RELEASENOTES
-rw-rw-r--. 1 root root 53 Sep 21 2017 BUGS
-rw-rw-r--. 1 root root 1815 Sep 21 2017 CONTRIBUTING
-rw-rw-r--. 1 root root 1487 Sep 21 2017 COPYING
drwxrwxr-x. 6 root root 192 Jul 19 22:18 deps
-rw-r--r--. 1 root root 202 Jul 19 22:38 dump.rdb
-rw-rw-r--. 1 root root 11 Sep 21 2017 INSTALL
-rw-rw-r--. 1 root root 151 Sep 21 2017 Makefile
-rw-rw-r--. 1 root root 4223 Sep 21 2017 MANIFESTO
-rw-rw-r--. 1 root root 20530 Sep 21 2017 README.md
-rw-rw-r--. 1 root root 57760 Jul 19 22:36 redis.conf
-rwxrwxr-x. 1 root root 271 Sep 21 2017 runtest
-rwxrwxr-x. 1 root root 280 Sep 21 2017 runtest-cluster
-rwxrwxr-x. 1 root root 281 Sep 21 2017 runtest-sentinel
-rw-rw-r--. 1 root root 7606 Sep 21 2017 sentinel.conf
drwxrwxr-x. 3 root root 8192 Jul 19 22:22 src
drwxrwxr-x. 10 root root 167 Sep 21 2017 tests
drwxrwxr-x. 8 root root 4096 Sep 21 2017 utils
[root@localhost redis]# cd utils/
[root@localhost utils]# pwd
/usr/local/redis/utils
[root@localhost utils]# ll
total 52
-rw-rw-r--. 1 root root 593 Sep 21 2017 build-static-symbols.tcl
-rw-rw-r--. 1 root root 1303 Sep 21 2017 cluster_fail_time.tcl
-rw-rw-r--. 1 root root 1070 Sep 21 2017 corrupt_rdb.c
drwxrwxr-x. 2 root root 60 Sep 21 2017 create-cluster
-rwxrwxr-x. 1 root root 2137 Sep 21 2017 generate-command-help.rb
drwxrwxr-x. 3 root root 31 Sep 21 2017 graphs
drwxrwxr-x. 2 root root 39 Sep 21 2017 hashtable
drwxrwxr-x. 2 root root 70 Sep 21 2017 hyperloglog
-rwxrwxr-x. 1 root root 9567 Sep 21 2017 install_server.sh
drwxrwxr-x. 2 root root 63 Sep 21 2017 lru
-rw-rw-r--. 1 root root 1277 Sep 21 2017 redis-copy.rb
-rwxrwxr-x. 1 root root 1098 Sep 21 2017 redis_init_script
-rwxrwxr-x. 1 root root 1047 Sep 21 2017 redis_init_script.tpl
-rw-rw-r--. 1 root root 1762 Sep 21 2017 redis-sha1.rb
drwxrwxr-x. 2 root root 135 Sep 21 2017 releasetools
-rwxrwxr-x. 1 root root 3787 Sep 21 2017 speed-regression.tcl
-rwxrwxr-x. 1 root root 693 Sep 21 2017 whatisdoing.sh
[root@localhost utils]#
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /usr/local/redis/redis.conf
Please select the redis log file name [/var/log/redis_6379.log] /usr/local/redis/redis.log
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis/data
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /usr/local/redis/redis.conf
Log file : /usr/local/redis/redis.log
Data dir : /usr/local/redis/data
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort. ==>此处回车即可
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
[root@localhost utils]#
检查下系统服务是否安装成功:
[root@localhost utils]# chkconfig --list | grep redis
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
redis_6379 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost utils]#
查看redis 服务的状态:
[root@localhost utils]# systemctl status redis_6379
● redis_6379.service - LSB: start and stop redis_6379
Loaded: loaded (/etc/rc.d/init.d/redis_6379; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
[root@localhost utils]#
使用系统服务 启动和关闭redis
[root@localhost utils]#
[root@localhost utils]# systemctl stop redis_6379
[root@localhost utils]# systemctl start redis_6379
[root@localhost utils]# ps -ef | grep redis
root 51169 1 0 22:38 ? 00:00:01 redis-server 0.0.0.0:6379
root 51294 47694 0 22:50 pts/0 00:00:00 grep --color=auto redis
[root@localhost utils]#
生成系统服务对应的文件:
[root@localhost utils]# cat /etc/init.d/redis_6379
至此 redis 安装完毕
[root@localhost utils]#
[root@localhost utils]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get key1
"123456"
127.0.0.1:6379> get key2
"234567"
127.0.0.1:6379> set key3 456789
OK
127.0.0.1:6379> get key3
"456789"
127.0.0.1:6379>
# 列出全部KEY
127.0.0.1:6379> keys *
1) "UserKey:id1"
2) "key3"
3) "key1"
4) "key2"
127.0.0.1:6379>
127.0.0.1:6379>
# 测试增加值和减少值 incr 和 decr
127.0.0.1:6379> set key1 123
OK
127.0.0.1:6379> incr key1
(integer) 124
127.0.0.1:6379> incr key1
(integer) 125
127.0.0.1:6379> decr key1
(integer) 124
127.0.0.1:6379>
Linux 防火墙 开通 端口
[root@localhost ~]# redis cli
-bash: redis: command not found
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get key1
"123456"
127.0.0.1:6379> exit
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-05-17 21:40:04 CST; 1 years 2 months ago
Docs: man:firewalld(1)
Main PID: 1037 (firewalld)
CGroup: /system.slice/firewalld.service
└─1037 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
May 17 21:40:04 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
May 17 21:40:04 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
[root@localhost ~]# sudo firewall-cmd --permanent --add-port=6379/tcp
success
[root@localhost ~]# sudo firewall-cmd --reload
success
[root@localhost ~]#