霖轩 發表於 2020-11-5 10:51:00

在 Debian 10 上安装和配置 Redis 服务

<p>Redis版本5.0.x包含在默认的Debian 10存储库中。要安装它,请以root或具有sudo特权的用户身份运行以下命令:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">sudo apt update
sudo apt install redis</span>-server</pre>
</div>
<p>安装完成后,Redis服务将会自动启动,你可以通过以下命令校验Redis服务是否正常运行:</p>
<div class="cnblogs_code">
<pre>sudo systemctl status redis-server<br>输出类似如下所示:</pre>
</div>
<div class="cnblogs_code">
<pre>redis-server.service - Advanced key-<span style="color: rgba(0, 0, 0, 1)">value store
   Loaded: loaded (</span>/lib/systemd/system/redis-<span style="color: rgba(0, 0, 0, 1)">server.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu </span><span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">12</span>-<span style="color: rgba(128, 0, 128, 1)">05</span> <span style="color: rgba(128, 0, 128, 1)">14</span>:<span style="color: rgba(128, 0, 128, 1)">15</span>:<span style="color: rgba(128, 0, 128, 1)">23</span><span style="color: rgba(0, 0, 0, 1)"> PST; 27s ago
   Docs: http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">redis.io/documentation,</span>
         man:redis-server(<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">)
Main PID: </span><span style="color: rgba(128, 0, 128, 1)">2024</span> (redis-<span style="color: rgba(0, 0, 0, 1)">server)
    Tasks: </span><span style="color: rgba(128, 0, 128, 1)">4</span> (limit: <span style="color: rgba(128, 0, 128, 1)">2359</span><span style="color: rgba(0, 0, 0, 1)">)
   Memory: </span><span style="color: rgba(128, 0, 128, 1)">6.9M</span><span style="color: rgba(0, 0, 0, 1)">
   CGroup: </span>/system.slice/redis-<span style="color: rgba(0, 0, 0, 1)">server.service
         └─</span><span style="color: rgba(128, 0, 128, 1)">2024</span> /usr/bin/redis-server <span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">6379<br><br>如果您的服务器上禁用了IPv6,则Redis服务将无法启动。<br></span></pre>
</div>
<p>至此,Redis已在Debian 10服务器上安装并运行,您可以开始使用它了。</p>
<h3 id="list1">配置Redis远程访问</h3>
<p>默认情况下,Redis配置为仅在本地主机上侦听。您只能从运行Redis服务的计算机连接到Redis服务器。</p>
<p>如果使用单个服务器设置,并且连接到数据库的客户端也在同一主机上运行,则不应启用远程访问。</p>
<p>要将Redis配置为接受远程连接,请使用文本编辑器打开Redis配置文件:</p>
<div class="cnblogs_code">
<pre>sudo nano /etc/redis/redis.conf</pre>
</div>
<p>搜索以开头的行<code>bind 127.0.0.1 ::1</code>并对其进行注释,如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# </span>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<span style="color: rgba(0, 0, 0, 1)">
# bind </span><span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span> ::<span style="color: rgba(128, 0, 128, 1)">1<br><br><strong>修改保护模式为no</strong><br>protected-mode no</span></pre>
</div>
<p>保存文件并关闭编辑器。</p>
<p>重新启动Redis服务以使更改生效:</p>
<div class="cnblogs_code">
<pre>sudo systemctl restart redis-server</pre>
</div>
<p>使用<code>ss</code>或<code>netstat</code>验证Redis正在侦听端口上的所有接口<code>6379</code>:</p>
<div class="cnblogs_code">
<pre>ss -an | grep <span style="color: rgba(128, 0, 128, 1)">6379</span><span style="color: rgba(0, 0, 0, 1)">
or
netstat </span>-lntp|grep <span style="color: rgba(128, 0, 128, 1)">6379</span></pre>
</div>
<p>您应该看到类似以下的内容:</p>
<div class="cnblogs_code">
<pre>tcpLISTEN<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(128, 0, 128, 1)">128</span><span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:<span style="color: rgba(128, 0, 128, 1)">6379</span><span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:*<span style="color: rgba(0, 0, 0, 1)">                     
tcpLISTEN</span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(128, 0, 128, 1)">128</span>[::]:<span style="color: rgba(128, 0, 128, 1)">6379</span>   [::]:* </pre>
</div>
<p>您还需要添加防火墙规则,以启用来自远程计算机上TCP端口的流量<code>6379</code>。</p>
<p>假设您正在使用<code>UFW</code>防火墙来管理防火墙,并且希望允许从<code>192.168.121.0/24</code>子网进行访问,则可以运行以下命令:</p>
<div class="cnblogs_code">
<pre>sudo ufw allow proto tcp <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">121.0</span>/<span style="color: rgba(128, 0, 128, 1)">24</span> to any port <span style="color: rgba(128, 0, 128, 1)">6379</span></pre>
</div>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    记忆力下降,日常日志<br><br>
来源:https://www.cnblogs.com/yushuo/p/13930487.html
頁: [1]
查看完整版本: 在 Debian 10 上安装和配置 Redis 服务