
接下来对Redis进行build。
下载并提取源代码
由于我们不需要长期保留源代码,因此可以直接在/tmp目录内进行build:
- cd /tmp
12
现在下载Redis最新版本,大家可以使用稳定下载URL:
- curl -O h//download.redis.io/redis-stable.tar.gz
12
解压tar:
- tar xzvf redis-stable.tar.gz
12
前往Redis源目录:
- cd redis-stable
12
Build并安装Redis
现在对Redis二进制代码进行编译:
- make
12
编译完成后,运行测试套件以确保built正确:
- make test
12
这一过程通常需要几分钟。完成后,大家可以使用以下命令进行安装:
- sudo make install
12
配置Redis
Redis已经安装完成,接下来进行配置。首先创建一个配置目录,这里我们使用/etc/redis目录:
- sudo mkdir /etc/redis
12
将Redis源归档文件内的示例Redis配置文件复制进来:
- sudo cp /tmp/redis-stable/redis.conf /etc/redis
12
而后打开文件并进行调整:
- sudo nano /etc/redis/redis.conf
12
在文件中找到supervised命令。现在其被设置为no。由于我们运行的 *** 作系统使用systemd init系统,因此需要将其变更为systemd:
/etc/redis/redis.conf
. . .
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto- detect upstart or systemd method based on
#UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd
. . .
1234567891011121314151617
下面找到dir目录。此选项指定Redis用于放置持久数据的目录。我们需要挑选合适的位置,并确保Redis有权限写入但普通用户无权查看。
这里我们使用/var/lib/redis目录,稍后进行创建:
/etc/redis/redis.conf
. . .
# 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 /var/lib/redis
. . .
12345678910111213141516
完成后保存并退出。
创建Redis systemd Unit文件
接下来,我们可以创建一个systemd unit文件,从而利用该init系统管理Redis进程。
首先创建并打开/etc/systemd/system/redis.service文件:
- sudo nano /etc/systemd/system/redis.service
12
在这里,我们在[Unit]部分处添加一条描述,定义要求网络在服务启动前必须处于可用状态:
/etc/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
123456
在[Service]部分,我们需要指定该服务的运作方式。出于安全考虑,我们不应以root方式运行服务。我们应当使用专用用户及群组,并以此调用redis。我们稍后再创建这部分内容。
要启动服务,我们只需要在配置中调用redis-server二进制文件。要将其关闭,则可使用Reids的shutdown命令,其可利用redis-cli加以执行。另外,由于我们希望Redis能够在故障情况下得到恢复,因此需要将Restart指令设定为“always”:
/etc/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]User=redisGroup=redisExecStart=/usr/local/bin/redis-server /etc/redis/redis.confExecStop=/usr/local/bin/redis-cli shutdownRestart=always
12345678
最后在[Install]部分,我们将systemd定义为在该服务可用时始终关联(即在引导过程中即行启动):
/etc/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install]WantedBy=multi-user.target
123456789101112131415
完成后保存并退出。
方法/步骤1、打开已经安装成功的Xshell,点击左上角【新建】按钮,打开新建会话属性对话框
2、连接信息验证,输入redis服务器的主机IP,端口号,名称(自定义,如test)
3、用户身份验证,方法:Password,输入连接redis服务器的用户名和密码;点击【确定】按钮
4、点击会话对话框中的【连接】按钮
5、连接成功状态
6、通过命令
【redis-cli
-a
test123】;连接redis数据库,其中【test123】数据库是名称。到这里为止,您已经通过Xshell能完整的 *** 作redis了,您会了吗?
如果是无序的话,用set存储用户的唯一id,如SADD users {id};然后用hash map存储用户的各种属性,如HSET user_{id} name "Tom"。这样你可以在set中查看用户数量,然后在set中增减的同时,同步 *** 作hash中的对应user就行了!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)