Step 3: Connect to an instance

更新时间:
复制 MD 格式

Connect to a Tair (Redis OSS-compatible) instance by using redis-cli, code, or Data Management (DMS).

Prerequisites

Procedure

redis-cli

This example uses redis-cli on a Linux Elastic Compute Service (ECS) instance to connect to a Tair (Redis OSS-compatible) instance in the same virtual private cloud (VPC).

Note

To connect over the Internet, apply for a public endpoint.

  1. Log on to the ECS instance and run the following commands to download, compile, and install redis-cli:

    sudo yum -y install gcc            # Install GNU Compiler Collection (GCC).
    wget https://download.redis.io/releases/redis-7.2.0.tar.gz
    tar xzf redis-7.2.0.tar.gz
    cd redis-7.2.0&&make

    This example uses redis-cli 7.2.0. You can install a different version. Compilation takes 2 to 3 minutes.

  2. Run the following command to connect to the instance:

    src/redis-cli -h hostname -a password -p port

    Parameters:

    • hostname: the instance endpoint. In the Connection Information section on the instance details page, find the VPC endpoint, for example, r-8vbwds91ie1rdl****.redis.zhangbei.rds.aliyuncs.com. For more information, see View endpoints.

    • password: the instance password.

    • port: the port number. Default: 6379.

    Sample command:

    src/redis-cli -h r-8vbwds91ie1rdl****.redis.zhangbei.rds.aliyuncs.com -a TestPassword123 -p 6379
  3. Write and read data.

    1. Run the SET bar foo command.

      Expected output:OK.

    2. Run the GET bar command.

      Expected output:"foo".

Code

Note

To connect over the Internet, apply for a public endpoint.

This example uses the Jedis client. For the complete code example, see redistest. For other client types, see Connection code examples with common types of clients.

  1. Add the required dependencies to pom.xml.

    <!-- Import spring-data-redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <!-- Since Spring Boot 2.0, Lettuce is the default client. Exclude lettuce when using Jedis. -->
        <exclusions>
            <exclusion>
                <groupId>io.lettuce</groupId>
                <artifactId>lettuce-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Import jedis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </dependency>
  2. Modify the connection parameters based on the comments.

    @Configuration
    public class RedisConfig {
        
        @Bean
        JedisConnectionFactory redisConnectionFactory() {
            // This example is used only to test connectivity. In production environments, we recommend that you place connection information in a configuration file and retrieve it using the @Value annotation.
            // The endpoint (hostName) and port (port) can be obtained from the Connection Information section of the instance details page. Select a VPC endpoint or public endpoint based on your client network environment.
            RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("r-8vbwds91ie1rdl****.redis.zhangbei.rds.aliyuncs.com", 6379);
            // Specify the password in the username:password format. For example, if the username is testaccount and the password is Rp829dlwa, enter testaccount:Rp829dlwa as the password.
            // If you forget your username or password, you can reset the password or create a new account by clicking Account Management in the left-side navigation pane of the instance details page.
            config.setPassword(RedisPassword.of("Username:Password"));
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            // Specify the maximum number of connections based on your business requirements. This value cannot exceed the maximum number of connections supported by the instance.
            jedisPoolConfig.setMaxTotal(30);
            // Specify the maximum number of idle connections based on your business requirements. This value cannot exceed the maximum number of connections supported by the instance.
            jedisPoolConfig.setMaxIdle(20);
            // Disable testOn[Borrow|Return] to prevent additional PING commands from being generated.
            jedisPoolConfig.setTestOnBorrow(false);
            jedisPoolConfig.setTestOnReturn(false);
    
            JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling().poolConfig(
                    jedisPoolConfig).build();
    
            return new JedisConnectionFactory(config, jedisClientConfiguration);
        }
        @Bean
        public RedisTemplate<String, Object> redisTemplate() {
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(redisConnectionFactory());
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            return template;
        }
    }
  3. Test connectivity.

    @SpringBootTest
    public class RedisTest {
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
        @Test
        void test() {
            try {
                redisTemplate.opsForValue().set("test_key", "hello world!");
                System.out.println("Connection successful:"+redisTemplate.opsForValue().get("test_key"));
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("An exception occurred during the connection attempt. Refer to the documentation " +
                        "https://www.alibabacloud.com/help/en/redis/support/how-do-i-troubleshoot-connection-issues-in-apsaradb-for-redis?spm=a2c63.p38356.help-menu-26340.d_5_1_1_4.47ca2024nvxRlS" +
                        "to troubleshoot network, whitelist, and account/password issues." +
                        "You can also refer to the documentation based on the error message: https://www.alibabacloud.com/help/en/redis/support/common-errors-and-troubleshooting?spm=a2c63.p38356.help-menu-26340.d_5_0.77cc79fejApYJN");
            }
    
        }
    }

    If the connection succeeds, the output is:

    Connection successful: hello world!

DMS

  1. Log on to the console and go to the Instances page. In the top navigation bar, select the region in which the instance that you want to manage resides. Then, find the instance and click the instance ID.

  2. In the upper-right corner of the instance details page, click Log into Database.

  3. In the Log on to Database Instance dialog box, set Access mode to password login and enter a password.

    This mode uses the default account. View account details on the Account Management page in the console.

  4. Click Login.

  5. Write and read data.

    1. On the SQLConsole page, enter the SET foo hello command and click Execute(F8).

      Expected output:OK.

    2. Enter the GET foo command and click Execute(F8).

      Expected output:hello.

References

For more information, see the following topics:

Special connection methods

  • Enable TLS (SSL) encrypted connections to an instance: TLS encryption improves transport link security and ensures data integrity.

  • Use direct connection mode to connect to a cluster instance: If your instance is a cluster instance in direct connection mode, you can use the private endpoint to bypass proxy nodes and directly access backend data shards. This connection to cluster instances is similar to the connection to open source Redis clusters. Compared with the proxy mode, the direct connection mode reduces the response time because requests do not need to pass through proxy nodes.

  • Connect to an instance using Sentinel-compatible mode: Tair (Redis OSS-compatible) provides the Sentinel-compatible mode. After you enable this mode, instances can be connected from the client in the same manner as the native Redis Sentinel.

Common errors and troubleshooting

Error message

Cause and solution

(error) ERR illegal address

The whitelist is incorrectly configured.

  1. Check whether the IP address of your client is added to a whitelist of the instance. For more information, see Step 2: Configure whitelists.

  2. Verify the instance endpoint. Use the public endpoint for Internet connections.

  3. Verify that your ECS instance is in the same VPC as the Tair (Redis OSS-compatible) instance. If not, connect over the Internet.

Run ping <Instance endpoint> to verify connectivity. Example: ping r-bp1zxszhcgatnx****.redis.rds.aliyuncs.com.

(error) ERR client ip is not in whitelist

Could not connect to Redis

  • (error) ERR invalid password

  • (error) WRONGPASS invalid username-password pair

Invalid password. The required format varies by account type.

  • If you use the default account, enter the password. For example, if the username of the default account is r-bp1zxszhcgatnx**** and the password is Password21, the command used to verify the password is AUTH Password21.

  • If you use a custom account, enter the password in the user:password format. For example, if the username of the custom account is testaccount and the password is Rp829dlwa, the command used to verify the password is AUTH testaccount:Rp829dlwa.

Note
  • When using a third-party tool such as RDM, enter the password in user:password format and leave the username field empty. Otherwise, connection to the instance fails.

  • If you forget your password, you can reset it. For more information, see Change or reset the password.