在Linux下部署Redis以及使用说明

Linux 安装redis(需要gcc)

wget http://download.redis.io/releases/redis-4.0.6.tar.gz
tar xzf redis-4.0.6.tar.gz

(也可以在浏览器打开http://download.redis.io/releases选择自己想要的版本)
之后进入解压后的文件夹,使用make指令
编译结束后进入解压后目录的src文件夹,将其中三个文件使用cp拷贝到你自定义的目录,使用redis-server即可启动redis,注意redis.conf不在src文件夹,而是在解压后的目录下

-rwxr-xr-x 1 root root 1952104 322 18:38 redis-benchmark
-rwxr-xr-x 1 root root 2031272 322 18:38 redis-cli
-rwxr-xr-x 1 root root 3737528 322 18:38 redis-server

-rw-r--r-- 1 root root   29590 322 20:33 redis.conf

启动服务


[root@VM_16_11_centos redis]# ./redis-server redis.conf 

常见问题


无法远程登陆

redis现在的版本开启redis-server后,redis-cli只能访问到127.0.0.1,因为在配置文件中固定了ip,因此需要修改redis.conf(有的版本不是这个文件名,只要找到相对应的conf后缀的文件即可)文件以下几个地方。
1.bind 127.0.0.1改为 #bind 127.0.0.1

2.protected-mode yes 改为 protected-mode no,这样就可以使用公网ip远程登陆

3.加入 daemonize no(这个是是否在后台启动不占用一个主程窗口,在配置文件设置后启动将默认挂在后台)


创建集群


安装 ruby

由于redis3.x版本的集群创建是使用redis-trib.rb,该文件运行需要Ruby环境
1.使用RVM修改Ruby到最新版本
RVM是一个命令行工具,可以提供一个便捷的多版本Ruby环境的管理和切换。
《 ruby 中国官网 》也介绍了RVM的安装,安装方法如下:
这里所有的命令都是在用户权限下操作的,任何命令最好都不要用 sudo,

[root@localhost ~]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
[root@localhost ~]# \curl -sSL https://get.rvm.io | bash -s stable
[root@localhost ~]# source ~/.bashrc
[root@localhost ~]# source ~/.bash_profile

修改 RVM 的 Ruby 安装源到 Ruby China 的 Ruby 镜像服务器,这样能提高安装速度

[root@localhost ~]# echo "ruby_url=https://cache.ruby-china.org/pub/ruby" > ~/.rvm/user/db

2..输入命令 “source /usr/local/rvm/scripts/rvm ,查看rvm中管理的所有ruby版本,
3..输入命令 rvm list known 进行查询,会出现下面的内容

[root@VM_16_11_centos ~]# rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]
ruby-head

.....省略

4.选择一个适合的版本进行安装,注意redfis要求的版本是2.0.0以上,输入rvm install 2.3.4
5.使用刚才安装完成的Ruby版本,输入rvm use 2.3.4


启动集群

1.这里以创建六个节点为样例,首先创建6个配置文件700x.conf
文件内容如下,根据端口号进行修改:

[root@VM_16_11_centos cluster]# cat 7000.conf 
daemonize yes
port 7000 
cluster-enabled yes 
#集群启动后的节点的配置文件设置,下面默认在同一目录下
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000
appendonly yes
protected-mode no         

2.启动6个节点,这里写了一个脚本简化运行

#打开节点所在目录
cd /usr/local/redis/cluster

rm -f dump.rdb

#使用redis-server启动,注意要用自己的路径
../redis-server 7000.conf
../redis-server 7001.conf
../redis-server 7002.conf
../redis-server 7003.conf
../redis-server 7004.conf
../redis-server 7005.conf

。。启动集群
../redis-trib.rb create --replicas 1 xxx.xxx.xxx.xxx:7000 xxx.xxx.xxx.xxx:7001 xxx.xxx.xxx.xxx:7002 xxx.xxx.xxx.xxx:7003 xxx.xxx.xxx.xxx:7004 xxx.xxx.xxx.xxx:7005

xxx.xxx.xxx.xxx为节点的ip地址,其中的redis-trib.rb在编译好的redis文件夹下的src里面


客户端cli常用指令

shutdown#关闭客户端连接
ttl [key]#查看key的存活时间:
keys * #查看所有key 
info #查看server版本内存使用连接等信息 
client list #获取客户连接列表 
client kill 127.0.0.1:33441 #终止某个客户端连接 
dbsize #当前保存key的数量 
save #立即保存数据到硬盘 
bgsave #异步保存数据到硬盘 
flushdb #当前库中移除所有key 
flushall #移除所有key从所有库中 
lastsave #获取上次成功保存到硬盘的时间戳 
monitor #实时监测服务器接收到的请求 
slowlog len #查询慢查询日志条数 
slowlog get #返回所有的慢查询日志,最大值取决于slowlog-max-len配置 
slowlog get 2 #打印两条慢查询日志 
slowlog reset #清空慢查询日志信息

猜你喜欢

转载自blog.csdn.net/madonghyu/article/details/79659643