37

When trying to run the following in Redis using booksleeve.

using (var conn = new RedisConnection(server, port, -1, password))
{
    var result = conn.Server.FlushDb(0);
    result.Wait();
}

I get an error saying:

This command is not available unless the connection is created with admin-commands enabled"

I am not sure how do i execute commands as admin? Do I need to create an a/c in db with admin access and login with that?

Richard
  • 4,740
  • 4
  • 32
  • 39
Justin Homes
  • 3,739
  • 9
  • 49
  • 78

4 Answers4

80

Updated answer for StackExchange.Redis:

var conn = ConnectionMultiplexer.Connect("localhost,allowAdmin=true");

Note also that the object created here should be created once per application and shared as a global singleton, per Marc:

Because the ConnectionMultiplexer does a lot, it is designed to be shared and reused between callers. You should not create a ConnectionMultiplexer per operation. It is fully thread-safe and ready for this usage.

Todd Menier
  • 37,557
  • 17
  • 150
  • 173
37

Basically, the dangerous commands that you don't need in routine operations, but which can cause lots of problems if used inappropriately (i.e. the equivalent of drop database in tsql, since your example is FlushDb) are protected by a "yes, I meant to do that..." flag:

using (var conn = new RedisConnection(server, port, -1, password,
          allowAdmin: true)) <==== here

I will improve the error message to make this very clear and explicit.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Is there a list of which commands require `allowAdmin`? From searching the source code, it appears to just be flushing and changing which servers are masters. – JamesFaix Jul 23 '19 at 17:06
7

You can also set this in C# when you're creating your multiplexer - set AllowAdmin = true

    private ConnectionMultiplexer GetConnectionMultiplexer()
    {
        var options = ConfigurationOptions.Parse("localhost:6379");
        options.ConnectRetry = 5;
        options.AllowAdmin = true;
        return ConnectionMultiplexer.Connect(options);
    }
David McEleney
  • 3,397
  • 1
  • 26
  • 32
4

For those who like me faced the error:

StackExchange.Redis.RedisCommandException: This operation is not available unless admin mode is enabled: ROLE

after upgrading StackExchange.Redis to version 2.2.4 with Sentinel connection: it's a known bug, the workaround was either to downgrade the client back or to add allowAdmin=true to the connection string and wait for the fix.

Starting from 2.2.50 public release the issue is fixed.

d_f
  • 4,599
  • 2
  • 23
  • 34