sync_gateway with couchbase-server

#Connecting sync_gateway to couchbase-server

I have a couchbase-server running successfully at http://172.20.21.11:8091, and I am trying to connect my sync_gatway to the server. Here is my config.json

{
   "interface":":4984",
   "adminInterface":"0.0.0.0:4985",
   "log":["REST"],
   "databases":{
      "sync_gateway":{
         "server":"http://172.20.21.11:8091",
         "username":"admin",
         "password":"password",
         "sync":`function(doc) {channel(doc.channels);}`
      }
   }
}

and I am running sync_gateway config.json. It seems like it’s somehow finding the server, but it’s failing because of SASL_AUTH. Is there something I am missing here?

17:01:44.500445 Enabling logging: [REST]
17:01:44.501112 ==== Couchbase Sync Gateway/master(0db4913+CHANGES) ====
17:01:44.502480 Opening db /sync_gateway as bucket "sync_gateway", pool "default", server <http://172.20.21.11:8091>
17:01:44.502924 Opening Couchbase database sync_gateway on <http://172.20.21.11:8091> as user "admin"
17:01:44.655575 WARNING: Error from Incr in _reserveSequences(0): MCResponse status=0x20, opcode=SASL_AUTH, opaque=0, msg: Auth failure -- db.(*sequenceAllocator)._reserveSequences() at sequence_allocator.go:59
17:01:44.657072 FATAL: Error opening database: MCResponse status=0x20, opcode=SASL_AUTH, opaque=0, msg: Auth failure -- rest.RunServer() at config.go:415

The username must be set to the name of the bucket, in this case “sync_gateway”

The password is the password you set when you configured the Couchbase Server bucket, in the Access Control section:

Standard port (TCP port 11211. Needs SASL auth.)
Enter password:

You can set the password after creation by editing the bucket in the Admin Console.

If you set the bucket password to “passw0rd” then your config would look like this

{
   "interface":":4984",
   "adminInterface":"0.0.0.0:4985",
   "log":["REST"],
   "databases":{
      "sync_gateway":{
         "server":"http://172.20.21.11:8091",
         "username":"sync_gateway",
         "password":"passw0rd",
         "sync":`function(doc) {channel(doc.channels);}`
      }
   }
}

Nice! Thank you. I was under the impression the username & password fields are for accessing the couchbase server, but that’s really for the bucket. It sounds like username is always the same as the name, in this case sync_gateway. Is that true, or can I change the username?
Also if there is no password, I can just leave off the two fields, and it seems like the connection will be successful.

The credential name must always match the name of the CBS bucket, there is no option to use a different name.

If you are developing an application and don’t need security in that environment, you can leave the password empty on the CBS bucket and then you do not require credentials in your sync gateway configuration file.

Andy

This thread was helpful and got me past my first barrier, but I’m still having a related problem. Here is the configuration:

{
  "log": ["HTTP+"],
  "databases": {
    "smm_sync": {
      "server": "http://10.10.10.13:8091",
      "bucket": "smm_sync",
      "username": "smm_sync",
      "password": "*********",
      "users": {
        "GUEST": {"disabled": true},
        "configuration.user@sci-mobile.com": {
          "admin_channels": ["smm_configuration"],
          "password": "**********"
        },
        "configuration.admin@sci-mobile.com": {
          "admin_channels": ["smm_configuration", "smm_data"],
          "password": "**********"
        }
      },
      "sync": `function(doc, oldDoc) {
        channel(doc.channels);
      }`
    }
  }
}

The bucket “smm_sync” exists on the couchbase 3.0.1 server, and has the password assigned. When I start sync_gateway, I get:

22:31:04.316411 Enabling logging: [HTTP+]
22:31:04.316646 ==== Couchbase Sync Gateway/1.0.4(34;04138fd) ====
22:31:04.316843 Opening db /smm_sync as bucket “smm_sync”, pool “default”, server http://10.10.10.13:8091
22:31:04.317126 Opening Couchbase database smm_sync on http://10.10.10.13:8091 as user "smm_sync"
22:31:04.354594 FATAL: Error opening database: 502 Unable to connect to server: HTTP error 401 Unauthorized getting “http://10.10.10.13:8091/pools”: – rest.RunServer() at config.go:415

Even stranger, when I browse to that URI, I get a JSON response without any request for authentication.

Any advice?

Thanks,
Mark

Your config looks correct to me. The line

22:31:04.354594 FATAL: Error opening database: 502 Unable to connect to server: HTTP error 401 Unauthorized getting “http://10.10.10.13:8091/pools”: – rest.RunServer() at config.go:415

is the standard error when trying to connect using invalid credentials.

You might try the following to validate that you’ve got the right credentials for the bucket:

curl -v ‘http://smm_sync:******@10.10.10.13:8091/pools

If this comes back with a 401, then you don’t have the right credentials for your bucket.

Thank you. I had apparently mistyped my chosen password when creating the bucket. Working now.
Mark

I had the same problem, using Docker and Docker Compose.

Using Docker CLI (this scenario works):

  1. Start Couchbase Server:
    docker run -d -v ~/couchbaseserver-data:/opt/couchbase/var -p 8091:8091 --name couchbaseserver couchbase:community-4.0.0

  2. Setup the server and sync_gateway bucket.

  3. Start Couchbase Sync:
    docker run -d -p 4984:4984 -p 4985:4985 --link couchbaseserver:couchbaseserver couchbase/sync-gateway:1.1.0 http://www.example.com/config.json

Using Docker Compose (this scenario works, because I have added delay for 10 seconds “sleep 10” between Couchbase Server start and Couchbase Sync start):

docker-compose.yml:

syncgateway:
  image: couchbase/sync-gateway:1.1.0
  entrypoint: /bin/bash
  command: -c "sleep 10 && /usr/local/bin/sync_gateway /data/config/config.json"
  ports:
    - "4984:4984"
    - "4985:4985"
  volumes:
    - ~/syncgateway-data:/data
  links:
    - couchbaseserver:couchbaseserver
couchbaseserver:
  image: couchbase:community-4.0.0
  ports:
    - "8091:8091"
  volumes:
    - ~/couchbaseserver-data:/opt/couchbase/var

~/syncgateway-data/config/config.json:

{
  "interface": ":4984",
  "adminInterface": "0.0.0.0:4985",
  "log": [
    "*"
  ],
  "databases": {
    "sync_gateway": {
      "server": "http://couchbaseserver:8091",
      "bucket": "sync_gateway",
      "username": "sync_gateway",
      "password": "p4ssw0rd"
    }
  }
}
  1. First setup Couchbase Server and the bucket:
    docker-compose up and kill it later Ctrl + C
    or
    docker-compose start couchbaseserver
    when it’s ready stop it:
    docker-compose stop couchbaseserver

  2. Now start the server and sync again:
    docker-compose up

Everything will work, because there is a delay between the services, otherwise the same error:
Error opening database: 502 Unable to connect to server: HTTP error 401 Unauthorized is occurred.

Probably the docker image should be changed like adding delay options or maybe something else?
I can see there are a lot of examples here: https://github.com/couchbase/sync_gateway/tree/master/examples , but none of them are using docker compose + real server (not walrus)

Hello, did you solve this? I’ve tried everything and still the same error. If change the entrypoint for a sleep 5000000 and then get inside the container with docker exec -it [containerid] bash, and run the syncgateway command it works! but thats not the idea. Please Help!!!

Hi @AresDev, you can find a working docker-compose example here https://github.com/vladoatanasov/docker-cbsg

Regards,
Vladimir

1 Like

Thank you! It worked!