singlestoredb-laravel-driver icon indicating copy to clipboard operation
singlestoredb-laravel-driver copied to clipboard

Vapor Serverless - Laravel can't find singlestore driver

Open LucasNCastro opened this issue 3 years ago • 20 comments

Hi,

The singlestore driver seems incompatible with Vapor.

From a middleware where we do this : auth()->check() === false

Here is the error received :

PDOException
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002]  (trying to connect via (null)) (SQL: select * from `user` where `id` = 1024732 limit 1)

Even trying to register singlestore connection in the AppServiceProvider following this issue : https://github.com/singlestore-labs/singlestore-laravel-driver/issues/2 it does not work

Maybe an extension is missing on Vapor but which one? Is there an incompatibility with Alpine?

On EC2 it works but not on serverless :(.

Any idea?

LucasNCastro avatar Jun 30 '22 12:06 LucasNCastro

Hey @LucasNCastro, interesting question! I'm wondering if your env isn't set up correctly, because it says it's trying to connect via null and not via singlestore.

I believe Vapor injects DB_* credentials into the environment, so it may be overwriting whatever you're trying to do.

image

aarondfrancis avatar Jun 30 '22 14:06 aarondfrancis

Hey @aarondfrancis, thanks for your response.

Here is the database configuration and environment :

.env

DB_CONNECTION=singlestore
DB_HOST=*******
DB_PORT=3306
DB_DATABASE=*******
DB_USERNAME=*******

config/database.php

'connections' => [
    'singlestore' => [
        'driver' => 'singlestore',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST'),
        'port' => env('DB_PORT'),
        'database' => env('DB_DATABASE'),
        'username' => env('DB_USERNAME'),
        'password' => env('DB_PASSWORD'),
        'unix_socket' => env('DB_SOCKET'),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => false,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            PDO::ATTR_EMULATE_PREPARES => true,
        ]) : [],
    ],
]

Maybe this can help you in analyzing the problem.

LucasNCastro avatar Jun 30 '22 16:06 LucasNCastro

Can you tinker in your environment and dump out the config for me? I need to see what the actual evaluated config is inside of vapor. dump(config('database')) (Of course redact your credentials)

aarondfrancis avatar Jun 30 '22 16:06 aarondfrancis

Hey @aarondfrancis,

Exact same issue here, just keep getting this: SQLSTATE[HY000] [2002] (trying to connect via (null))

Been spinning my wheels on this for a few hours but narrowed it down to the singlestore driver, the moment you add it, it breaks the database in any vapor deployment

Hannes-Kruger avatar Jul 01 '22 07:07 Hannes-Kruger

dump(config('database'))

PS; my config is under the mysql (I didn't declare a new connection called singlestore, but its essentially the same thing)

array:4 [
  "default" => "mysql"
  "connections" => array:4 [
......
    "mysql" => array:15 [
      "driver" => "singlestore"
      "url" => null
      "host" => "svc-top-secret-uuid-dml.aws-virginia-3.svc.singlestore.com"
      "port" => "3306"
      "database" => "my_singlestore_db"
      "username" => "username_as_specified_in_.env"
      "password" => "password_as_specified_in_.env"
      "unix_socket" => ""
      "charset" => "utf8mb4"
      "collation" => "utf8mb4_unicode_ci"
      "prefix" => ""
      "prefix_indexes" => true
      "strict" => false
      "engine" => null
      "options" => array:3 [
        1009 => "/var/task/rds-combined-ca-bundle.pem"
        20 => true
        12 => true
      ]
    ]
.....

Hannes-Kruger avatar Jul 01 '22 08:07 Hannes-Kruger

Just another note, changing back to mysql driver doesn't fix it. Seems to break soon as you add the package in composer

Hannes-Kruger avatar Jul 01 '22 08:07 Hannes-Kruger

It's weird, it looks like it makes vapor shit the bed, I had to delete and recreate the vapor project to get it to work again (without the laravel / singlestore driver).

Hannes-Kruger avatar Jul 01 '22 08:07 Hannes-Kruger

Not much help but I also experienced this when trying to use this driver on vapor.

The config when DD'd from tinker all looked correct through vapor to me as well.

campope avatar Jul 02 '22 19:07 campope

Also just like above switching my config back to mysql isn't solving it but it definitely all worked until I pushed a commit with this driver.

campope avatar Jul 02 '22 19:07 campope

This is so strange. We use this package in Vapor without any issue, with PHP 8.1+layers, PHP 8.1+Docker and even PHP 8.0+layers.

Are you guys using managed or self hosted SingleStore?

fgilio avatar Jul 03 '22 12:07 fgilio

Super bizarre. I'll spin up a new vapor app and try it on Tuesday, let me know if y'all figure anything else out before then! Sorry for the weirdness

aarondfrancis avatar Jul 03 '22 19:07 aarondfrancis

@aarondfrancis There is output dump(config('database')) :

  "default" => "singlestore"
  "connections" => array:6 [
    "singlestore" => array:15 [
      "driver" => "singlestore"
      "url" => null
      "host" => ****
      "port" => "3306"
      "database" => ****
      "username" => ****
      "password" => *****
      "unix_socket" => null
      "charset" => "utf8mb4"
      "collation" => "utf8mb4_unicode_ci"
      "prefix" => ""
      "prefix_indexes" => true
      "strict" => false
      "engine" => null
      "options" => array:2 [
        1009 => "/var/task/rds-combined-ca-bundle.pem"
        20 => true
      ]
    ]

I also updated singlestoredb/singlestoredb-laravel package to use version 1.0.0.

No success, I got the same error on Vapor Serverless.

@fgilio I use managed SingleStore.

LucasNCastro avatar Jul 04 '22 09:07 LucasNCastro

This is so strange. We use this package in Vapor without any issue, with PHP 8.1+layers, PHP 8.1+Docker and even PHP 8.0+layers.

Are you guys using managed or self hosted SingleStore?

managed

Hannes-Kruger avatar Jul 04 '22 12:07 Hannes-Kruger

Update: I've now been able to emulate this without the Laravel SingleStore driver (when trying to store sessions to DB)

Hannes-Kruger avatar Jul 04 '22 15:07 Hannes-Kruger

Switched session to Redis problem went away

Hannes-Kruger avatar Jul 04 '22 15:07 Hannes-Kruger

Interesting!


I was going to ask if you guys are adding the singlestore_bundle.pem certificate in your codebases, since I had similar issues when I first started using the package and all I was missing was the certificate. I just remembered that and I don't remember talking about it in any other issue here in GitHub 🤔 , @aarondfrancis I'll see if I can draft a PR about it this week.

This is what we do:

In our database.php config file:

'options' => extension_loaded('pdo_mysql') ? array_filter([
    /**
     * For staging and production environments we use a managed SingleStore
     * cluster that expects us to trust this certificate.
     */
    PDO::MYSQL_ATTR_SSL_CA => env('APP_ENV') !== 'testing' && env('APP_ENV') !== 'local' ? database_path('singlestore_bundle.pem') : false,
    PDO::ATTR_EMULATE_PREPARES => true,
]) : [],

And then the certificate in database/singlestore_bundle.pem

fgilio avatar Jul 04 '22 18:07 fgilio

Will try in the morning and let you know, thanks for the suggestion

Hannes-Kruger avatar Jul 04 '22 18:07 Hannes-Kruger

I couldn't resist and had to test. It's the certificate, it fixed it. Thank you!

Hannes-Kruger avatar Jul 04 '22 19:07 Hannes-Kruger

Wohoo! Happy to know it worked, the error is very misleading

fgilio avatar Jul 04 '22 19:07 fgilio

Yep worked for me too! Thank you!

campope avatar Jul 05 '22 18:07 campope

@aarondfrancis and the rest of the people on this thread - are there particular things we should add to the readme to improve this?

carlsverre avatar Sep 02 '22 20:09 carlsverre