来源:微信公众号「编程学习基地」
//ex 设置键的过期时间为 second 秒。NX :只在键不存在时,才对键进行设置操作。
set %s %s ex %d nx
expire 设置过期时间
expire %s %d
Redis数据库安装使用过程
ubuntu@VM-16-5-ubuntu:~$ sudo apt update
Hit:1 http://mirrors.tencentyun.com/ubuntu bionic InRelease
Get:2 http://mirrors.tencentyun.com/ubuntu bionic-security InRelease [88.7 kB]
Get:3 http://mirrors.tencentyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://mirrors.tencentyun.com/ubuntu bionic-updates/main Sources [509 kB]
Get:5 http://mirrors.tencentyun.com/ubuntu bionic-updates/main amd64 Packages [2,043 kB]
Get:6 http://mirrors.tencentyun.com/ubuntu bionic-updates/main i386 Packages [1,278 kB]
Get:7 http://mirrors.tencentyun.com/ubuntu bionic-updates/main Translation-en [410 kB]
Get:8 http://mirrors.tencentyun.com/ubuntu bionic-updates/universe i386 Packages [1,566 kB]
Get:9 http://mirrors.tencentyun.com/ubuntu bionic-updates/universe amd64 Packages [1,733 kB]
Fetched 7,717 kB in 2s (3,715 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
55 packages can be upgraded. Run 'apt list --upgradable' to see them.
ubuntu@VM-16-5-ubuntu:~$ sudo apt install redis
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
amd64-microcode gyp intel-microcode iucode-tool libhttp-parser2.7.1 libjs-async libjs-inherits libjs-node-uuid
libjs-underscore libuv1-dev node-abbrev node-ansi node-ansi-color-table node-archy node-async node-balanced-match
node-block-stream node-brace-expansion node-builtin-modules node-combined-stream node-concat-map node-cookie-jar
node-delayed-stream node-forever-agent node-form-data node-fs.realpath node-fstream node-fstream-ignore
node-github-url-from-git node-glob node-graceful-fs node-hosted-git-info node-inflight node-inherits node-ini
node-is-builtin-module node-isexe node-json-stringify-safe node-lockfile node-lru-cache node-mime node-minimatch
node-mkdirp node-mute-stream node-node-uuid node-nopt node-normalize-package-data node-npmlog node-once node-osenv
node-path-is-absolute node-pseudomap node-qs node-read node-read-package-json node-request node-retry node-rimraf
node-semver node-sha node-slide node-spdx-correct node-spdx-expression-parse node-spdx-license-ids node-tar
node-tunnel-agent node-underscore node-validate-npm-package-license node-which node-wrappy node-yallist nodejs
nodejs-doc
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libjemalloc1 redis-server redis-tools
Suggested packages:
ruby-redis
The following NEW packages will be installed:
libjemalloc1 redis redis-server redis-tools
0 upgraded, 4 newly installed, 0 to remove and 55 not upgraded.
Need to get 637 kB of archives.
After this operation, 3,083 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.tencentyun.com/ubuntu bionic/universe amd64 libjemalloc1 amd64 3.6.0-11 [82.4 kB]
Get:2 http://mirrors.tencentyun.com/ubuntu bionic-security/universe amd64 redis-tools amd64 5:4.0.9-1ubuntu0.2 [516 kB]
Get:3 http://mirrors.tencentyun.com/ubuntu bionic-security/universe amd64 redis-server amd64 5:4.0.9-1ubuntu0.2 [35.4 kB]
Get:4 http://mirrors.tencentyun.com/ubuntu bionic-security/universe amd64 redis all 5:4.0.9-1ubuntu0.2 [3,084 B]
Fetched 637 kB in 0s (1,352 kB/s)
Selecting previously unselected package libjemalloc1.
(Reading database ... 105955 files and directories currently installed.)
Preparing to unpack .../libjemalloc1_3.6.0-11_amd64.deb ...
Unpacking libjemalloc1 (3.6.0-11) ...
Selecting previously unselected package redis-tools.
Preparing to unpack .../redis-tools_5%3a4.0.9-1ubuntu0.2_amd64.deb ...
Unpacking redis-tools (5:4.0.9-1ubuntu0.2) ...
Selecting previously unselected package redis-server.
Preparing to unpack .../redis-server_5%3a4.0.9-1ubuntu0.2_amd64.deb ...
Unpacking redis-server (5:4.0.9-1ubuntu0.2) ...
Selecting previously unselected package redis.
Preparing to unpack .../redis_5%3a4.0.9-1ubuntu0.2_all.deb ...
Unpacking redis (5:4.0.9-1ubuntu0.2) ...
Setting up libjemalloc1 (3.6.0-11) ...
Setting up redis-tools (5:4.0.9-1ubuntu0.2) ...
Setting up redis-server (5:4.0.9-1ubuntu0.2) ...
Created symlink /etc/systemd/system/redis.service → /lib/systemd/system/redis-server.service.
Created symlink /etc/systemd/system/multi-user.target.wants/redis-server.service → /lib/systemd/system/redis-server.service.
Setting up redis (5:4.0.9-1ubuntu0.2) ...
Processing triggers for libc-bin (2.27-3ubuntu1.4) ...
Processing triggers for systemd (237-3ubuntu10.45) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ureadahead (0.100.0-21) ...
ubuntu@VM-16-5-ubuntu:~$ redis-cli --version
redis-cli 4.0.9
ubuntu@VM-16-5-ubuntu:~$ sudo systemctl status redis
ubuntu@VM-16-5-ubuntu:~$ sudo systemctl status redis
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-05-08 15:19:14 CST; 32s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 22161 (redis-server)
Tasks: 4 (limit: 2122)
CGroup: /system.slice/redis-server.service
└─22161 /usr/bin/redis-server 127.0.0.1:6379
May 08 15:19:13 VM-16-5-ubuntu systemd[1]: Starting Advanced key-value store...
May 08 15:19:14 VM-16-5-ubuntu systemd[1]: redis-server.service: Can't open PID file /var/run/redis/redis-server.pid (y
May 08 15:19:14 VM-16-5-ubuntu systemd[1]: Started Advanced key-value store.
ubuntu@VM-16-5-ubuntu:~$ sudo vim /etc/redis/redis.conf
ubuntu@VM-16-5-ubuntu:~$ Redis-cli
Command 'Redis-cli' not found, did you mean:
command 'redis-cli' from deb redis-tools
Try: sudo apt install <deb name>
ubuntu@VM-16-5-ubuntu:~$ ps -agx|grep redis
22161 ? Ssl 0:00 /usr/bin/redis-server 127.0.0.1:6379
23736 pts/0 S+ 0:00 grep redis
ubuntu@VM-16-5-ubuntu:~$ redis-cli
127.0.0.1:6379> help
redis-cli 4.0.9
To get help about Redis commands type:
"help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
To set redis-cli preferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
127.0.0.1:6379> set key1 "hello"
OK
127.0.0.1:6379> get key1
"hello"
127.0.0.1:6379> set key1 "hello world"
OK
127.0.0.1:6379> get key1
"hello world"
127.0.0.1:6379> keys *
1) "key1"
127.0.0.1:6379> HGETALL key1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> keys *
1) "key1"
127.0.0.1:6379>
127.0.0.1:6379> ls
(error) ERR unknown command 'ls'
127.0.0.1:6379> set key2 1
OK
127.0.0.1:6379> INCR key2
(integer) 2
127.0.0.1:6379> INCR key2
(integer) 3
127.0.0.1:6379> keys *
1) "key1"
2) "key2"
127.0.0.1:6379> get key2
"3"
127.0.0.1:6379> LPUSH key3 a
(integer) 1
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"
127.0.0.1:6379> get key3
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> get key2
"3"
127.0.0.1:6379> LPUSH key3 b
(integer) 2
127.0.0.1:6379> LPUSH key3 c
(integer) 3
127.0.0.1:6379> LPUSH key3 0 3
(integer) 5
127.0.0.1:6379> LRANGE key3 0 3
1) "3"
2) "0"
3) "c"
4) "b"
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"
127.0.0.1:6379> HSET key4 name "John Smith"
(integer) 1
127.0.0.1:6379> HSET key4 email "abc@gmail.com"
(integer) 1
127.0.0.1:6379> get key4
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> keys *
1) "key4"
2) "key1"
3) "key3"
4) "key2"
127.0.0.1:6379> HGET key4 name
"John Smith"
127.0.0.1:6379> HGETALL key4
1) "name"
2) "John Smith"
3) "email"
4) "abc@gmail.com"
127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3
OK
127.0.0.1:6379> HMGET key5 username age
1) "antirez"
2) "3"
127.0.0.1:6379> HGETALL key5
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "3"
127.0.0.1:6379> keys *
1) "key1"
2) "key2"
3) "key3"
4) "key4"
5) "key5"
127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> exit
配置远程连接访问
提权限
首先把权限升到root
su root
编辑redis配置文件
sudo vi /etc/redis/redis.conf
重启Redis服务器
sudo /etc/init.d/redis-server restart
root@VM-16-5-ubuntu:/home/ubuntu/redis# vi /etc/redis/redis.conf
root@VM-16-5-ubuntu:/home/ubuntu/redis# /etc/init.d/redis-server restart
[ ok ] Restarting redis-server (via systemctl): redis-server.service.
root@VM-16-5-ubuntu:/home/ubuntu/redis# sudo /etc/init.d/redis-server status
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-05-11 18:37:44 CST; 7s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Process: 27417 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 27423 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 27436 (redis-server)
Tasks: 4 (limit: 2122)
CGroup: /system.slice/redis-server.service
└─27436 /usr/bin/redis-server *:6379
May 11 18:37:44 VM-16-5-ubuntu systemd[1]: Stopped Advanced key-value store.
May 11 18:37:44 VM-16-5-ubuntu systemd[1]: Starting Advanced key-value store...
May 11 18:37:44 VM-16-5-ubuntu systemd[1]: redis-server.service: Can't open PID file /var/run/redis/redis-server.pid (yet?) after start: No such fil…r directory
May 11 18:37:44 VM-16-5-ubuntu systemd[1]: Started Advanced key-value store.
Hint: Some lines were ellipsized, use -l to show in full.
root@VM-16-5-ubuntu:/home/ubuntu/redis# redis-cli