Antony
Antony

Reputation: 4364

Can't connect to Neo4j with PHP Client

I am running neo4j 4.1.12 and can connect via bolt in my browser using my login credentials. I am also using https://github.com/neo4j-php/neo4j-php-client the PHP Client for Neo4j and have setup the following code:

public function __construct($table = null){
    $config = config('Database')->neo4j;

    if (!empty($config['Username'])){
        $auth = Authenticate::basic($config['Username'],$config['Password']);
    } else {
        $auth = null;
    }

    $this->transact = $config['Transact']; // allows config setting of whether to use the Transaction feature

    $this->client = ClientBuilder::create()->withDriver('bolt','bolt://neo4j:PW_THAT_WORKS@localhost:7687')->build();

    $this->table = $table;
}

public function query($query,$params = []){
    $client = $this->client;
    $statement = new Statement($query,$params);

    if ($this->transact){
        $result = $client->writeTransaction(static function (TransactionInterface $tsx) use ($statement) {
            return $tsx->runStatement($statement);
        });
    } else {
        $result = $client->runStatement($statement);
    }

return $result;
}

public function insert($data = null, bool $returnID = true){
    if (empty($this->table)){
        return false;
    }

    $result = $this->query('CREATE ('.$this->table. ')');

    print'<pre>';print_r($result);print'</pre>';
}

However when I call:

    $neo4j = new \App\Models\Neo4jModel('n:Test');

    $neo4j->insert(array('Hello'=>'World','Foobar'=>'Baz'));

I get a connection error saying Cannot connect to any server on alias: bolt with Uris: ('bolt://neo4j:PW_THAT_WORKS@localhost:7687') and I have absolutely no idea why !? And the following Cypher query neo4j$ CREATE (n:Test) works absolutely fine: Added 1 label, created 1 node, completed after 408 ms.

Also if I try the http driver I get Http\Discovery\Exception\DiscoveryFailedException - which I know looks obvious but as I say, my browser can access localhost fine! I am accessing from subdomain.localhost but that shouldn't be an issue surely?

And running curl produces the following: curl localhost:7474 { "bolt_routing" : "neo4j://localhost:7687", "transaction" : "http://localhost:7474/db/{databaseName}/tx", "bolt_direct" : "bolt://localhost:7687", "neo4j_version" : "4.1.12", "neo4j_edition" : "community" }

Please can someone explain why this wouldn't be connecting and what I need to do to fix it?

Upvotes: 3

Views: 351

Answers (3)

sNICkerssss
sNICkerssss

Reputation: 6430

I was getting the same error until I specified the database name in the connection code. I've customised the db name in my docker-compose.yml (NEO4J_dbms_default__database=my-neo4j-db) and didn't set it in the connection config.

Here is my working connection:

use Laudis\Neo4j\Authentication\Authenticate;
use Laudis\Neo4j\ClientBuilder;
use Laudis\Neo4j\Databags\SessionConfiguration;

$auth = Authenticate::basic('neo4j', 'my_neo4j_password');
$client = ClientBuilder::create()
    ->withDriver('bolt', 'bolt://my_neo4j_container_name:7687', $auth)
    // fixed error for me
    ->withDefaultSessionConfiguration(SessionConfiguration::default()->withDatabase('my-neo4j-db'))
    ->build();

Upvotes: 0

Alexey Yurov
Alexey Yurov

Reputation: 1

I had the same issue with neo4j 4.1.13. The PHP refused to request Neo4j from the site throught the bolt and returned

Error: Cannot connect to any server on alias: default with Uris: ('bolt://neo4j:password@localhost:7687/database=neo4j')

and PHP worked perfectly from the CLI and I had normal acces from remote Neo4j Desktop. So the solution was to upgrade to Neo4j 4.4.28 (Thanks mhepton for the advice) from here:

wget -O - https://debian.neo4j.com/neotechnology.gpg.key | sudo apt-key add -
echo 'deb https://debian.neo4j.com stable 4.4' | sudo tee -a /etc/apt/sources.list.d/neo4j.list
sudo apt-get update
sudo apt-get install neo4j=4.4.28

The most problem was that in fact I had to totaly reamove Neo4j and then install the newer version from scratch. And this is the shortest way because in upgrade i've met the conflicts between Neo4j and cypher-shell version.

Upvotes: 0

mhepton
mhepton

Reputation: 128

I've run into the same issue while working to upgrade from neo4j v3.51 to a new version (converting to the newer php client before making the n4j version switch).

It's not in the docs from what I can tell, but within the code you'll find that the neo4j-php-client only supports bolt protocol versions 4.4.* and ^5.0. Given that, and based on neo4j's bolt protocol compatibility table, this would mean that you must use a neo4j version of v4.4 or greater to make use of Bolt with this client. Note that the client's underlying Bolt driver (by stefanak-michal) does support lesser protocol versions, so if you really need Bolt and can't upgrade your DB version, using that directly might be an option.

For completeness, it's within the ProtocolFactory::createProtocol() where you'll find your Bolt connection is failing:

public function createProtocol(IConnection $connection, AuthenticateInterface $auth, string $userAgent): array
{
    $bolt = new Bolt($connection);
    $bolt->setProtocolVersions(5, 4.4);
    enter code here
    $protocol = $bolt->build();

    if ( !($protocol instanceof V4_4) && !($protocol instanceof V5) ) {
        throw new RuntimeException('Client only supports bolt version 4.4.* and ^5.0');
    }

    $response = $auth->authenticateBolt($protocol, $userAgent);

    return [$protocol, $response];
}

Upvotes: 1

Related Questions