1. Download and install Redis
In the remote server where you want to download execute the following command to download the Redis file to the server
$ wget http://download.redis.io/releases/redis-4.0.9.tar.gz
说明:$是指你的当前目录,不是命令的一部分,wget命令用来下载网上资源,后面的地址是网上资源路径,你可以去redis官网上查找你想下载的redis版本的下载路径
解压文件
$ tar xzf redis-4.0.9.tar.gz
编译文件
$ cd redis-4.0.9
$ make
注意:如果在make时报错有如下原因
1.没有安装gcc(因为redis是用c写得),并且是4以上版本需要执行 yum install gcc
2.有可能也要安装gcc++,需要执行 yum install gcc++
3.如果要用make test命令测试redis,还需要安装tcl,命令为 yum install tcl
2.启动redis服务和客户端
在redis-4.0.9(解压编译目录)下执行
$ src/redis-server
这样就启动了服务,界面会出项相应的启动信息
现在再来启动客户端
$ src/redis-cli
3.使用java客户端访问远程redis服务
因为redis本身默认是不支持远程地址访问的,所以我们需要先配置一下redis
1.编辑安装目录下的redis.conf文件,注释掉本地ip绑定,
#bind 127.0.0.1
2.在本地客户端中设置登录密码
$ config set requirepass mypwd
这样基本就可以啦
具体的这块配置参考:
https://www.cnblogs.com/y-l-h/p/7930085.html
https://www.cnblogs.com/machanghai/p/5497043.html
接下来在开发工具中创建maven项目,添加jedis依赖
<Dependencies> <Dependency> <groupId>Redis.clients</groupId> <Artifactid>Jedis</Artifactid> <version>2.9.0</version> </Dependency> </Dependencies>
Writing clients
ImportRedis.clients.jedis.Jedis; Public classRedistest { Public Static voidMain (string[] args) {
Create your Redis IP address
String Redisip = ""; //connecting to Redis server on cloud serverJedis Jedis =NewJedis (Redisip); //Authentication PasswordJedis.auth ("12358"); System.out.println ("Connection to server successfully"); //Check whether server is running or notSYSTEM.OUT.PRINTLN ("Server is running:" +jedis.ping ()); }}
The results are as follows, indicating a successful connection
Connection to server Successfullyserver is Running:pong
其他问题:之前用阿里的云服务器进行redis远程连接时会报错
Xception in thread "main"Redis.clients.jedis.exceptions.JedisConnectionException:java.net.SocketTimeoutException:connect timed out at Redis.clients.jedis.Connection.connect (Connection.java:207) at Redis.clients.jedis.BinaryClient.connect (Binaryclient.java:93) at Redis.clients.jedis.Connection.sendCommand (Connection.java:126) at Redis.clients.jedis.Connection.sendCommand (Connection.java:117) at Redis.clients.jedis.BinaryClient.auth (Binaryclient.java:564) at Redis.clients.jedis.BinaryJedis.auth (Binaryjedis.java:2138) at Top.pancras.redis.RedisTest.main (Redistest.java:11) caused by:java.net.SocketTimeoutException:connect timed out at Java.net.DualStackPlainSocketImpl.waitForConnect (Native Method) at Java.net.DualStackPlainSocketImpl.socketConnect (Dualstackplainsocketimpl.java:85) at Java.net.AbstractPlainSocketImpl.doConnect (Abstractplainsocketimpl.java:350) at Java.net.AbstractPlainSocketImpl.connectToAddress (Abstractplainsocketimpl.java:206) at Java.net.AbstractPlainSocketImpl.connect (Abstractplainsocketimpl.java:188) at Java.net.PlainSocketImpl.connect (Plainsocketimpl.java:172) at Java.net.SocksSocketImpl.connect (Sockssocketimpl.java:392) at Java.net.Socket.connect (Socket.java:589) at Redis.clients.jedis.Connection.connect (Connection.java:184) ... 6 more
Firewall is not open, not a firewall problem, mainly in Alibaba cloud service want to open port also need to be configured in its official website, refer to http://www.cnblogs.com/coffee9527/p/8711906.html, Change the port to the port you want to open it should be OK
CENTOS7 installation of Redis in remote server and Java connection