How to connect to the server correctly?

Hey.

I installed a new Couchbase server 5.
On another server, I installed the PHP 7 and cli v2.3.4.
Now when I try to write data, I get an error.
If I do this:

$bucketName = "logs";
$cluster = new CouchbaseCluster("couchbase://192.168.0.51");
$authenticator5 = new \Couchbase\ClassicAuthenticator();
$authenticator5->bucket('Administrator', 'Pass');
$cluster->authenticate($authenticator5);
$bucket = $cluster->openBucket($bucketName);
// Store a document
echo "Storing 0:king_arthur\n";
$result = $bucket->upsert('0:king_arthur', array(
    "email" => "kingarthur@couchbase.com",
    "interests" => array("African Swallows")
));
var_dump($result);

Error:

Fatal error: Uncaught Couchbase\Exception: LCB_AUTH_ERROR: Authentication failed. You may have provided an invalid username/password combination in /var/www/html/system/test/cb5test1.php:20 Stack trace: #0 /var/www/html/system/test/cb5test1.php(20): Couchbase\Cluster->openBucket('logs') #1 {main} thrown in /var/www/html/system/test/cb5test1.php on line 20

How to connect to the server correctly?

I read https://developer.couchbase.com/documentation/server/current/sdk/php/managing-connections.html

Now i do:

$bucketName = "logs";
$clusterLogs5 = "couchbase://192.168.0.51";
$authenticator = new \Couchbase\PasswordAuthenticator();
$authenticator->username('Administrator')->password('Pass');
$cluster = new CouchbaseCluster($clusterLogs5);

$cluster->authenticate($authenticator);
$bucket = $cluster->openBucket($bucketName);

Thank you.

That’s correct. PasswordAuthenticator is what you should use. Also there is a shortcut, which will create PasswordAuthenticator instance internally:

$cluster = new CouchbaseCluster($clusterLogs5);
$cluster->authenticate('Administrator', 'Pass');

Hello Sergey @avsej .

When using this code:

        $cluster = new CouchbaseCluster($clusterLogs5);
        $cluster->authenticate('Administrator', 'Pass');
        return $cluster->openBucket("bucketName");

I get an error:

Warning: Couchbase\Cluster::authenticate() expects exactly 1 parameter, 2 given in /var/www/...  on line ..
Fatal error: Uncaught Couchbase\Exception: LCB_AUTH_ERROR: Authentication failed. You may have provided an invalid username/password combination

What am I doing wrong?

Thank you.

That’s strange. Are you sure, you loading correct extension? The feature has been added in 2.3.3, and I’ve just verified it with fresh deployment.

phpinfo()

couchbase
couchbase support 	enabled
extension version 	2.3.4
libcouchbase runtime version 	2.7.7 (git: 269fae2e8f40d01650b1700af375bd111b54a358)
libcouchbase headers version 	2.7.7 (git: 269fae2e8f40d01650b1700af375bd111b54a358)
igbinary transcoder 	disabled (install pecl/igbinary and rebuild pecl/couchbase)
zlib compressor 	enabled 

Couchbase 5

Sorry, it should be authenticateAs()

$cluster->authenticateAs('Administrator', 'Pass');

Thanks Sergey, this really works.
But which method is preferable?
Which method is more modern?

authenticateAs() is a shortcut. and there no really difference if you use PasswordAuthenticator. Maybe in some cases you want to create authenticator before cluster object, and feed it to multiple instances.

Thanks @avsej again!