Unable to connect to MySQL database using Laravel Vapor in production

rich :

I have a small simple API, basically for testing purposes built in Laravel 6 using a simple JWT Auth to allow login and restricted access to a couple of routes. All simple stuff really and it works just fine using Homestead.

However, on Vapor I can't get it to actually talk to the database. Here's what my vapor.yml looks like, it's not changed much from the default other than adding the db name in

id: 7529
name: mything
environments:
    production:
        database: mydb
        database-user: vapor
        memory: 1024
        cli-memory: 512
        runtime: php-7.4
        build:
            - 'composer install'
            - 'php artisan jwt:secret'
            - 'php artisan event:cache'
            - 'npm ci && npm run prod && rm -rf node_modules'
    mything-environment:
        build:
            - 'composer install'
    staging:
        memory: 1024
        cli-memory: 512
        runtime: php-7.4
        build:
            - 'composer install'
            - 'php artisan jwt:secret'
            - 'php artisan event:cache'
            - 'npm ci && npm run dev && rm -rf node_modules'

I have set up the db using the vapor CLI as so php vendor/bin/vapor mydb

And have edited the .env.production file as so php vendor/bin/vapor env:pull production

And added the relevant conn details

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb

I have re-deployed numerous times, and the application works. However when performing any kind of database work I always end up with an error, for example:

{"message":"SQLSTATE[HY000] [2002] Connection refused (SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (testtest, [email protected], $2y$10$SYYdAXC1aYrFUBQ/wVHcDuUgzBfTK76cvqxaiS2d8Kbg4Vbp/VGXK, 2020-02-29 22:37:36, 2020-02-29 22:37:36))","context":{"exception":{"class":"Illuminate\\Database\\QueryException","message":"SQLSTATE[HY000] [2002] Connection refused (SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (testtest, [email protected], $2y$10$SYYdAXC1aYrFUBQ/wVHcDuUgzBfTK76cvqxaiS2d8Kbg4Vbp/VGXK, 2020-02-29 22:37:36, 2020-02-29 22:37:36))","code":2002,"file":"/var/task/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669","previous":{"class":"PDOException","message":"SQLSTATE[HY000] [2002] Connection refused","code":2002,"file":"/var/task/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70"}},"aws_request_id":"9bba7341-a0f3-481c-961a-a0b59fcb64b9"},"level":400,"level_name":"ERROR","channel":"

Can anyone point me in the right direction? I don't belive I need to add in the .env vars for the user and password, I have tried it anyway, but still the same issue.

Robert Kujawa :

Your configuration should look like this:

vapor.yml

id: 7529
name: mything
environments:
    production:
        database: mydb
        memory: 1024
        cli-memory: 512
        runtime: php-7.4
        build:
            - 'composer install'
            - 'php artisan jwt:secret'
            - 'php artisan event:cache'
            - 'npm ci && npm run prod && rm -rf node_modules'
    mything-environment:
        build:
            - 'composer install'
    staging:
        memory: 1024
        cli-memory: 512
        runtime: php-7.4
        build:
            - 'composer install'
            - 'php artisan jwt:secret'
            - 'php artisan event:cache'
            - 'npm ci && npm run dev && rm -rf node_modules'

There is no need to specify the database user if you are using the default "vapor" user.

.env

DB_DATABASE=mydb

You should not override any other database environment variables other then the database name, because vapor will automatically inject them for you, and in your case you are specifying a connection to localhost, which is not where your database is located.

Also note that when your create a database environment in vapor, the only existing database schema will be "vapor", in order to connect to a custom database named "mydb" you must first create a database schema in that database environment.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=17900&siteId=1