Can't connect to Neo4j with PHP Client

179 views Asked by At

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?

2

There are 2 answers

0
mhepton On

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];
}
0
Alexey Yurov On

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.