测试客户端连接

Redis 企业软件

在各种场景中,例如创建新集群或升级后 集群中,您应该验证客户端是否可以连接到 数据库。

要测试客户端连接:

  1. 创建 Redis 数据库后,复制包含集群名称 (FQDN) 的数据库终端节点。

    要查看和复制集群中数据库的终端节点,请参阅集群管理器 UI 中数据库的 Configuration > General 部分:

    从数据库的 Configuration (配置) 屏幕的 General (常规) 部分查看公有和私有终端节点。
  2. 尝试从您选择的客户端连接到数据库终端节点, 并运行数据库命令。

  3. 如果数据库没有响应,请尝试连接到数据库 端点使用 IP 地址而不是 FQDN。如果你 成功,则 DNS 配置不正确。为 其他详细信息,请参阅配置集群 DNS

如果在测试数据库连接时出现任何问题,请联系 支持

测试数据库连接

创建 Redis 数据库后,您可以连接到 数据库并使用以下方法之一存储数据:

  • redis-cli、内置命令行工具

  • Redis Insight,适用于 macOS、Windows 和 Linux 的免费 Redis GUI

  • 使用 Redis 客户端库的应用程序,例如redis-py用于 Python。查看客户端列表以按语言查看所有 Redis 客户端。

与 redis-cli 连接

连接到您的数据库redis-cli(位于/opt/redislabs/bin目录中),然后存储和检索一个 key:

$ redis-cli -h <endpoint> -p <port>
127.0.0.1:16653> set key1 123
OK
127.0.0.1:16653> get key1
"123"

For more redis-cli connection examples, see the redis-cli reference.

Connect with Redis Insight

Redis Insight is a free Redis GUI that is available for macOS, Windows, and Linux.

  1. Install Redis Insight.

  2. Open Redis Insight and select Add Redis Database.

  3. Enter the host and port in the Host and Port fields.

  4. Select Use TLS if TLS is set up.

  5. Select Add Redis Database to connect to the database.

See the Redis Insight documentation for more information.

Connect with Python

Python applications can connect to the database using the redis-py client library. For installation instructions, see the redis-py README on GitHub.

  1. From the command line, create a new file called redis_test.py:

    vi redis_test.py
    
  2. Paste the following code in redis_test.py, and replace <host> and <port> with your database's endpoint details:

    import redis
    
    # Connect to the database
    r = redis.Redis(host='<host>', port=<port>)
    
    # Store a key
    print("set key1 123")
    print(r.set('key1', '123'))
    
    # Retrieve the key
    print("get key1")
    print(r.get('key1'))
    
  3. Run the application:

    python redis_test.py
    
  4. If the application successfully connects to your database, it outputs:

    set key1 123
    True
    get key1
    123
    

Connect with discovery service

You can also connect a Python application to the database using the discovery service, which complies with the Redis Sentinel API.

In the IP-based connection method, you only need the database name, not the port number. The following example uses the discovery service that listens on port 8001 on all nodes of the cluster to discover the endpoint for the database named "db1".

from redis.sentinel import Sentinel

# with IP based connections, a list of known node IP addresses is constructed
# to allow connection even if any one of the nodes in the list is unavailable.
sentinel_list = [
('10.0.0.44', 8001),
('10.0.0.45', 8001),
('10.0.0.46', 8001)
]

# change this to the db name you want to connect
db_name = 'db1'

sentinel = Sentinel(sentinel_list, socket_timeout=0.1)
r = sentinel.master_for(db_name, socket_timeout=0.1)

# set key "foo" to value "bar"
print(r.set('foo', 'bar'))
# set value for key "foo"
print(r.get('foo'))

For more redis-py connection examples, see the redis-py developer documentation.

RATE THIS PAGE
Back to top ↑