Connettersi a un'istanza Memorystore for Redis Cluster
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa pagina spiega come connettersi a un'istanza Memorystore for Redis Cluster.
Assistenza clienti
Memorystore for Redis Cluster non fornisce librerie client proprie, ma supporta librerie client di terze parti esistenti che supportano il protocollo Redis.
Un elenco di esempio di client è disponibile nella pagina Client di Redis.
Best practice per il client Redis-py
Per connetterti all'istanza Memorystore for Redis Cluster utilizzando il client Python redis-py, devi aggiungere skip_full_coverage_check=True quando dichiari un cluster Redis:
Inserire porte nella lista consentita del firewall
Se non autorizzi le porte corrette sul firewall, la tua istanza può riscontrare errori di connessione. Per saperne di più sull'inserimento di porte nella lista consentita, vedi
Risolvere i problemi.
Accesso tra regioni
Puoi accedere a un'istanza Memorystore for Redis Cluster da un client che si trova in un'area geografica diversa da quella in cui si trova l'istanza Memorystore.
Visualizzare l'endpoint di rilevamento del cluster
Per visualizzare l'endpoint di rilevamento del cluster Memorystore, segui queste istruzioni:
Console
Vai alla pagina Memorystore for Redis Cluster nella Google Cloud console.
Nella sezione Connetti a questa istanza, prendi nota dell'indirizzo IP e del numero di porta accanto a Endpoint di rilevamento. Questi due valori utilizzati
insieme costituiscono l'endpoint di rilevamento del cluster.
gcloud
Per visualizzare l'endpoint di rilevamento del cluster, esegui describe
gcloud CLI come spiegato nella sezione Visualizzare i dettagli dell'istanza.
Prendi nota dell'indirizzo dell'endpoint e del numero di porta elencati nella sezione
discovery_endpoints:. Questi due valori utilizzati insieme costituiscono l'endpoint di rilevamento del cluster.
Connettiti da una VM Compute Engine utilizzando redis-cli
Puoi connetterti all'istanza Memorystore for Redis Cluster da qualsiasi
VM di Compute Engine che utilizza la rete autorizzata
dell'istanza Memorystore.
Per connetterti all'istanza:
Se non hai già una VM Linux Compute Engine che utilizza la rete autorizzata per l'istanza Memorystore for Redis Cluster, creane una e connettiti seguendo la Guida rapida per creare una VM Linux.
Installa redis-cli versione 6.0 o successive sulla VM Compute Engine seguendo le istruzioni riportate in Installare Redis su Linux.
DISCOVERY_ENDPOINT_ADDRESS e PORT_NUMBER
sono i valori che hai annotato nel passaggio precedente.
Esegui il comando CLUSTER SHARDS per visualizzare la topologia del cluster. Prendi nota
di uno degli indirizzi IP e dei numeri di porta del nodo.
Connettiti al nodo che preferisci eseguendo questo comando:
redis-cli -h NODE_IP_ADDRESS -p NODE_PORT -c
Sostituisci quanto segue:
NODE_IP_ADDRESS è l'indirizzo IP del nodo che hai trovato nel passaggio precedente.
NODE_PORT è il numero di porta del nodo che hai trovato nel passaggio precedente.
Una volta connesso al nodo, inserisci alcuni comandi Redis:
Digita:
PING
Risultato:
PONG
Invio
SET HELLO WORLD
Risultato:
OK
Digita:
GET HELLO
Risultato:
"WORLD"
Una volta terminato il test della connessione al nodo Redis, ti consigliamo di eliminare la VM di Compute Engine che hai utilizzato per connetterti all'istanza Redis. In questo modo, eviterai di addebitare costi al tuo
account di fatturazione Cloudg.
Connettiti a un'istanza con crittografia in transito abilitata
Questa sezione fornisce un esempio di come connettersi a un'istanza Memorystore for Redis Cluster
con la crittografia in transito abilitata.
Se non hai già una VM di Compute Engine che utilizza la stessa rete autorizzata dell'istanza Redis del passaggio precedente, creane una e connettiti seguendo la Guida rapida all'utilizzo di una VM Linux.
Installa redis-cli versione 6.0 o successive sulla VM Compute Engine
seguendo le istruzioni riportate in Installare Redis su Linux.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[],[],null,["# Connect to a Memorystore for Redis Cluster instance\n\nThis page explains how to connect to a Memorystore for Redis Cluster instance.\n\nClient support\n--------------\n\nMemorystore for Redis Cluster does not provide its own client libraries, but rather\nsupports existing third-party client libraries that support the Redis protocol.\nAn example list of clients can be found on the Redis [Clients](https://redis.io/docs/clients/)\npage.\n\n### Redis-py client best practice\n\nTo connect to your Memorystore for Redis Cluster instance using the [redis-py](https://github.com/redis/) Python client, you must add the `skip_full_coverage_check=True` when declaring a Redis\nCluster: \n\n from rediscluster import RedisCluster\n endpoints = [{\"host\": \"IPADDRESS\", \"port\": \"6379\"}]\n\n rdb = RedisCluster(\n startup_nodes=endpoints,\n skip_full_coverage_check=True, # Required for Memorystore\n decode_responses = True)\n\n print(rdb.set('PYTHON', 'CLUSTER'))\n print(rdb.get('PYTHON'))\n print(rdb.unlink('PYTHON'))\n\nAllowlist ports on firewall\n---------------------------\n\nIf you don't allowlist the correct ports on your firewall, your instance can\nencounter connection errors. For more information about allowlisting ports, see\n[Troubleshoot issues](/memorystore/docs/cluster/troubleshoot-issues#connection_error_caused_by_firewall_rules).\n\nCross region access\n-------------------\n\nYou can access a Memorystore for Redis Cluster instance from a client located in a\ndifferent region than where the Memorystore instance is located.\n\nView your cluster's discovery endpoint\n--------------------------------------\n\nTo view your Memorystore cluster's [discovery endpoint](/memorystore/docs/cluster/overview#discovery_endpoint), use the following instructions: \n\n### Console\n\n1. Go to the **Memorystore for Redis Cluster** page in the Google Cloud console.\n\n [Memorystore for Redis Cluster](https://console.cloud.google.com/memorystore/redis/clusters?)\n2. Click your Cluster ID.\n\n3. In the **Connect to this instance** section, take a note of the IP address\n and port number next to **Discovery Endpoint**. These two values used\n together are your cluster's discovery endpoint.\n\n### gcloud\n\nTo view your cluster's discovery endpoint, run the `describe`\ngcloud CLI as explained in the [view instance details](/memorystore/docs/cluster/create-manage-instances#view_instance_details) section.\n\nThe output should be similar to the following: \n\n authorizationMode: AUTH_MODE_DISABLED\n createTime: '2023-08-04T17:49:07.988918793Z'\n discoveryEndpoints:\n - address: 10.142.0.13\n port: 6379\n pscConfig:\n - network: projects/my-project-364018/global/networks/default\n name: projects/my-project-364018/locations/us-east1/clusters/my-cluster-1\n pscConnections:\n - address: 10.142.0.13\n forwardingRule: https://www.googleapis.com/compute/v1/projects/my-project-364018/regions/us-east1/forwardingRules/sca-auto-fr-5bc46f69-ec80-4c5a-8971-ce267a4d8ddd\n network: projects/my-project-364018/global/networks/default\n projectId: my-project-364018\n pscConnectionId: '25734306048376845'\n - address: 10.142.0.10\n forwardingRule: https://www.googleapis.com/compute/v1/projects/my-project-364018/regions/us-east1/.\n forwardingRules/sca-auto-fr-817b64c5-85ac-48d0-82e3-c6fa99b4e67d\n network: projects/my-project-364018/global/networks/default\n projectId: my-project-364018\n pscConnectionId: '25734306048376842'\n replicaCount: 0\n shardCount: 3\n sizeGb: 39\n state: ACTIVE\n tier: PERFORMANCE\n transitEncryptionMode: TRANSIT_ENCRYPTION_MODE_DISABLED\n uid: cddda5de-d91f-449b-8755-3a665430271a\n\nTake a note of the endpoint address and port number listed in the\n`discovery_endpoints:` section. These two values used together are your\ncluster's discovery endpoint.\n\nConnect from a Compute Engine VM using redis-cli\n------------------------------------------------\n\nYou can connect to the Memorystore for Redis Cluster instance from any\nCompute Engine VM that uses the Memorystore instance's\nauthorized network.\n\nTo connect to the instance:\n\n1. If you don't already have a Linux Compute Engine VM that uses the\n authorized network for your Memorystore for Redis Cluster instance, create one and\n connect to it by following [Quickstart to create a Linux VM](/compute/docs/create-linux-vm-instance).\n\n2. Install `redis-cli` version 6.0 or newer on the Compute Engine VM by\n following the instructions at [Install Redis on Linux](https://redis.io/docs/getting-started/installation/install-redis-on-linux/).\n\n3. [View your cluster's discovery endpoint](/memorystore/docs/cluster/connect-cluster-instance#view_your_clusters_discovery_endpoint)\n and make a note of it.\n\n4. Connect to your instance's discovery endpoint by running the following\n command:\n\n ```\n redis-cli -h DISCOVERY_ENDPOINT_ADDRESS -p PORT_NUMBER -c\n ```\n\n Where:\n - \u003cvar translate=\"no\"\u003eDISCOVERY_ENDPOINT_ADDRESS\u003c/var\u003e and \u003cvar translate=\"no\"\u003ePORT_NUMBER\u003c/var\u003e are the values you noted in the previous step.\n\n | **Note:** The `-c` switch is used to enable the `redis-cli` cluster support mode. If the `-c` switch is not provided, when you try to access keys from a different shard than the one you are connected to, it results in a `MOVED` error. In the cluster support mode the client is automatically reconnected to the address returned in the error response. For more details, see [Using Redis-CLI with a Redis Cluster](https://developer.redis.com/operate/redis-at-scale/scalability/redis-cli-with-redis-cluster/).\n5. Run the `CLUSTER SHARDS` command to view your cluster topology. Take a note\n of one of the node's IP addresses and port numbers.\n\n6. Connect to your desired node by running the following command:\n\n ```\n redis-cli -h NODE_IP_ADDRESS -p NODE_PORT -c\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eNODE_IP_ADDRESS\u003c/var\u003e is the node's IP address you found in the previous step.\n - \u003cvar translate=\"no\"\u003eNODE_PORT\u003c/var\u003e is the node's port number you found in the previous step.\n7. Once connected to the node, enter some Redis commands:\n\n Enter: \n\n ```\n PING\n ```\n\n Result: \n\n ```\n PONG\n ```\n\n Enter \n\n ```\n SET HELLO WORLD\n ```\n\n Result: \n\n ```\n OK\n ```\n\n Enter: \n\n ```\n GET HELLO\n ```\n\n Result: \n\n ```\n \"WORLD\"\n ```\n8. Once you are done testing your connection to the Redis node, you should\n consider deleting the Compute Engine VM you used to connect to the\n Redis instance. Doing so helps you avoid incurring charges to your\n Cloud Billing account.\n\nConnect to an in-transit encryption enabled instance\n----------------------------------------------------\n\nThis section provides an example of how to connect to a Memorystore for Redis Cluster\ninstance that has in-transit encryption enabled.\n\n1. If you don't already have a Compute Engine VM that uses the same\n authorized network as the Redis instance from the previous step, create one\n and connect to it by following [Quickstart using a Linux VM](/compute/docs/create-linux-vm-instance).\n\n 1. Install `redis-cli` version 6.0 or newer on the Compute Engine VM by following the instructions at [Install Redis on Linux](https://redis.io/docs/getting-started/installation/install-redis-on-linux/).\n2. Install your instance's certificate authority on the Linux VM by following\n the instructions at [Install certificate authorities on your client](https://cloud.google.com/memorystore/docs/cluster/manage-in-transit-encryption#install_certificate_authorities_on_your_client).\n\n3. [View your cluster's discovery endpoint](/memorystore/docs/cluster/connect-cluster-instance#view_your_clusters_discovery_endpoint)\n and make a note of it.\n\n4. Connect to your instance's discovery endpoint by running the following\n command:\n\n ```\n redis-cli -h DISCOVERY_ENDPOINT_ADDRESS -p PORT_NUMBER -c --tls --cacert /tmp/server_ca.pem\n ```\n\n Where:\n - \u003cvar translate=\"no\"\u003eDISCOVERY_ENDPOINT_ADDRESS\u003c/var\u003e and \u003cvar translate=\"no\"\u003ePORT_NUMBER\u003c/var\u003e are the values you noted in the previous step.\n\n | **Note:** The `-c` switch is used to enable the `redis-cli` cluster support mode. If the `-c` switch is not provided, when you try to access keys from a different shard than the one you are connected to, it results in a `MOVED` error. In the cluster support mode the client is automatically reconnected to the address returned in the error response. For more details, see [Using Redis-CLI with a Redis Cluster](https://developer.redis.com/operate/redis-at-scale/scalability/redis-cli-with-redis-cluster/).\n5. Run the `CLUSTER SHARDS` command to view your cluster topology. Take a note\n of one of the node's IP addresses and port numbers.\n\n6. Connect to your desired node by running the following command:\n\n ```\n redis-cli -h NODE_IP_ADDRESS -p NODE_PORT -c --tls --cacert /tmp/server_ca.pem\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eNODE_IP_ADDRESS\u003c/var\u003e is the node's IP address you found in the previous step.\n - \u003cvar translate=\"no\"\u003eNODE_PORT\u003c/var\u003e is the node's port number you found in the previous step.\n7. Once connected to the node, enter some Redis commands:\n\n Enter: \n\n ```\n PING\n ```\n\n Result: \n\n ```\n PONG\n ```"]]