Ubuntu18.04,安装Redis配置远程连接访问和简单使用Redis

来源:微信公众号「编程学习基地」

//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 
### 回答1: 以下是在Ubuntu 18.04上安装Redis的步骤: 1. 打开终端并更新软件包列表: sudo apt update 2. 安装Redis: sudo apt install redis-server 3. 启动Redis服务: sudo systemctl start redis-server 4. 确认Redis服务已经启动: sudo systemctl status redis-server 5. 如果Redis服务未启动,则可以使用以下命令启动它: sudo systemctl enable redis-server 6. 现在,您可以使用以下命令测试Redis是否正在运行: redis-cli ping 如果Redis正在运行,则会返回“PONG”消息。 希望这可以帮助您在Ubuntu 18.04上安装Redis。 ### 回答2: Redis是一款快速、可靠的开源内存数据存储系统,广泛应用于许多Web应用程序中的数据缓存、会话管理、消息队列等场景中。本文将介绍如何在Ubuntu 18.04上安装Redis。 1. 更新Ubuntu软件包列表 在安装Redis之前,我们需要更新Ubuntu的软件包列表,以确保我们安装的是最新版的Redis。通过运行以下命令更新软件包列表: sudo apt update 2. 安装Redis 运行以下命令在Ubuntu 18.04上安装Redis: sudo apt install redis-server 该命令会安装Redis服务器及其依赖项。在安装过程中,会询问是否继续安装,确认后等待几秒钟即可安装完成。 3. 配置Redis Redis安装完成后,我们需要确认一些默认配置是否正确。首先,我们需要检查Redis的监听IP地址和端口是否正确。运行以下命令打开Redis配置文件: sudo nano /etc/redis/redis.conf 在该文件中,找到以下行(大约位于第68行): bind 127.0.0.1 将该行中的IP地址修改为0.0.0.0,即: bind 0.0.0.0 这将使Redis监听所有可用的网络接口。保存文件并关闭编辑器。 4. 启动Redis服务 Redis安装配置完成后,我们需要启动Redis服务。运行以下命令启动Redis: sudo systemctl start redis 此时,Redis已启动并正在运行。我们可以运行以下命令检查Redis的运行状态: sudo systemctl status redis 如果Redis正在运行,则输出将显示active (running)。 5. 测试Redis 安装配置Redis完成后,我们可以测试Redis是否能够正常工作。运行以下命令连接到本地Redis服务器: redis-cli 此时会打开Redis命令行界面。在该界面中,我们可以输入一些Redis命令,例如set和get,来测试Redis是否正常工作。例如,我们可以运行以下命令设置一个键值对: set mykey "Hello, world!" 然后,我们可以运行以下命令获取该键的值: get mykey 如果Redis正在运行并且配置正确,则输出应为“Hello, world!”。 6. 其他操作 除了上述操作,我们还可以通过以下命令来停止Redis服务: sudo systemctl stop redis 如果需要在服务器启动时自动启动Redis,则可以运行以下命令启用Redis服务自启动: sudo systemctl enable redis Ubuntu 18.04上安装Redis就是这么简单安装之后,您可以将Redis用于您的Web应用程序中的缓存、会话管理、消息队列等方面,从而提升应用程序的性能和可靠性。 ### 回答3: 首先,需要在Ubuntu 18.04上安装Redis。这可以通过在终端中输入以下命令来完成: sudo apt-get update sudo apt-get install redis-server 安装完成后,Redis服务器将在后台运行,并在默认端口6379上监听连接。 接下来,需要检查Redis配置文件是否正常,以确保服务器可以正确地运行。可以通过运行以下命令来打开Redis配置文件: sudo nano /etc/redis/redis.conf 通过检查以下设置,可以确认该配置文件是否已正确配置: 1.监听端口是否设置为6379。 2.确保内存限制已设置为适当的水平,确保Redis不会使用太多系统资源。 3.确认Redis配置文件中的其他设置已正确配置。 然后,需要配置防火墙以允许Redis流量通过。Ubuntu 18.04默认使用UFW防火墙,可以通过运行以下命令启用防火墙: sudo ufw enable 然后,需要允许Redis的流量通过开放端口6379,可以通过运行以下命令实现: sudo ufw allow 6379/tcp 最后,需要启动Redis服务器。可以通过运行以下命令来启动Redis服务: sudo systemctl start redis 通过运行以下命令可以确认Redis服务器是否已在运行: sudo systemctl status redis 这些步骤可以确保在Ubuntu 18.04系统上正确安装配置Redis服务器。接下来,可以通过安装适当的客户端来使用Redis,例如通过使用Node.js的Node Redis库。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DeRoy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值