skip to Main Content

I have redis Server with a few db, i want to connect to db1 in the server.

i manage to connect to the server but i couldn’t connect to one of the db.

this is my code:

package main

import (
    "fmt"
    "redigo-master"
)

    func main() {

        conn, err := redis.Dial("tcp", "qacd:6410")
        defer conn.Close()
        if err != nil {
            fmt.Println(err)
        }
        keys, err := conn.Do("SELECT","db1")
        fmt.Println(keys)
    }

the result is:

ERR invalid DB index

any way to get to the 1st db?

2

Answers


  1. As you can see in the SELECT command documentation:

    Select the Redis logical database having the specified zero-based
    numeric index. New connections always use the database 0.

    which means that you’re supposed to pass an integer (in your case that would be 1), so it should look like this: keys, err := conn.Do("SELECT","1")

    In general Redis databases are assigned numbers starting from 0, and you have to configure in the redis.conf how many of them you want to have (by default you have 16 at indexes from 0 to 15):

    # Set the number of databases. The default database is DB 0, you can select
    # a different one on a per-connection basis using SELECT <dbid> where
    # dbid is a number between 0 and 'databases'-1
    databases 16
    
    Login or Signup to reply.
  2. Redis support 16 database. You can switch a DB using an integer starting from 0 to 15

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search