singlestore-labs / singlestoredb-laravel-driver

The official SingleStore Laravel driver.
https://github.com/singlestore-labs/singlestore-laravel-driver
Apache License 2.0
223 stars 22 forks source link

Vapor Serverless - Laravel can't find singlestore driver #13

Closed LucasNCastro closed 2 years ago

LucasNCastro commented 2 years ago

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?

aarondfrancis commented 2 years ago

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
LucasNCastro commented 2 years ago

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.

aarondfrancis commented 2 years ago

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)

Hannes-Kruger commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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).

campope commented 2 years ago

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 commented 2 years ago

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.

fgilio commented 2 years ago

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?

aarondfrancis commented 2 years ago

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

LucasNCastro commented 2 years ago

@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.

Hannes-Kruger commented 2 years ago

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 commented 2 years ago

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

Hannes-Kruger commented 2 years ago

Switched session to Redis problem went away

fgilio commented 2 years ago

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: ```php '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`
Hannes-Kruger commented 2 years ago

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

Hannes-Kruger commented 2 years ago

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

fgilio commented 2 years ago

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

campope commented 2 years ago

Yep worked for me too! Thank you!

carlsverre commented 2 years ago

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