Standard Simple Laravel Site to test things out... Cannot connect Database
jimnayzium
HOBBYOP

17 days ago

I have to get the database values into the .env values for the laravel deployment and am completely baffled by which variables go where. I am used to the .env file just handling it all for me. I found all these:

MYSQL_DATABASE="railway"
MYSQL_PUBLIC_URL="mysql://${{MYSQLUSER}}:${{MYSQL_ROOT_PASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}/${{MYSQL_DATABASE}}"
MYSQL_ROOT_PASSWORD="*************"
MYSQL_URL="mysql://${{MYSQLUSER}}:${{MYSQL_ROOT_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:3306/${{MYSQL_DATABASE}}"
MYSQLDATABASE="${{MYSQL_DATABASE}}"
MYSQLHOST="${{RAILWAY_PRIVATE_DOMAIN}}"
MYSQLPASSWORD="${{MYSQL_ROOT_PASSWORD}}"
MYSQLPORT="3306"
MYSQLUSER="root"

I have one service that is my mysql database with the data populated. And I have another service thqt is the laravel deployment via my github repository.

In that laravel github deployment I tried adding some of the values on the right side of the ones above into the standard laravle env values like:

DB_HOST="${{RAILWAY_PRIVATE_DOMAIN}}"

But I wasn't sure if I was supposed to do that or maybe:

DB_HOST="${{MYSQLHOST}}"

I am very confused.

$10 Bounty

3 Replies

cyszx
HOBBY

17 days ago

you can just connect them by putting the Railway values into Laravel’s names like

DB_HOST=${{MYSQLHOST}}

DB_PORT=${{MYSQLPORT}}

DB_DATABASE=${{MYSQLDATABASE}}

DB_USERNAME=${{MYSQLUSER}}

DB_PASSWORD=${{MYSQLPASSWORD}}


There's a variable called MYSQL_URL you can directly use in your application for private networking connections.

Also, to use reference variables, you must add their service name in front of the variable.

eg: If you're using MYSQL_URL, you would use ${{MySQL.MYSQL_URL}}.

https://docs.railway.com/variables#referencing-another-services-variable


jimnayzium
HOBBYOP

16 days ago

So I tried the variables in my site to connect to the Mysql service both with MySQL prefix and without and get this from the deployment:

```
DB_HOST=${{MYSQLHOST}}

DB_PORT=${{MYSQLPORT}}

DB_DATABASE=${{MYSQLDATABASE}}

DB_USERNAME=${{MYSQLUSER}}

DB_PASSWORD=${{MYSQLPASSWORD}}
```

as well as

```

DB_HOST=${{MySQL.MYSQLHOST}}

DB_PORT=${{MySQL.MYSQLPORT}}

DB_DATABASE=${{MySQL.MYSQLDATABASE}}

DB_USERNAME=${{MySQL.MYSQLUSER}}

DB_PASSWORD=${{MySQL.MYSQLPASSWORD}}

```

produces this error
```
Starting Container

Running migrations and seeding database ...

Illuminate\Database\QueryException

SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, Host: 127.0.0.1, Port: 3306, Database: laravel, SQL: select exists (select 1 from information_schema.tables where table_schema = schema() and table_name = 'migrations' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED')) as exists)

at vendor/laravel/framework/src/Illuminate/Database/Connection.php:838

834▕ $exceptionType = $this->isUniqueConstraintError($e)

835▕ ? UniqueConstraintViolationException::class

836▕ : QueryException::class;

837▕

➜ 838▕ throw new $exceptionType(

839▕ $this->getNameWithReadWriteType(),

840▕ $query,

841▕ $this->prepareBindings($bindings),

842▕ $e,

+42 vendor frames

43 artisan:16

Illuminate\Foundation\Application::handleCommand()

Running migrations and seeding database ...

Illuminate\Database\QueryException

SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, Host: 127.0.0.1, Port: 3306, Database: laravel, SQL: select exists (select 1 from information_schema.tables where table_schema = schema() and table_name = 'migrations' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED')) as exists)

at vendor/laravel/framework/src/Illuminate/Database/Connection.php:838

834▕ $exceptionType = $this->isUniqueConstraintError($e)

835▕ ? UniqueConstraintViolationException::class

836▕ : QueryException::class;

837▕

➜ 838▕ throw new $exceptionType(

839▕ $this->getNameWithReadWriteType(),

840▕ $query,

841▕ $this->prepareBindings($bindings),

842▕ $e,

+42 vendor frames

43 artisan:16

Illuminate\Foundation\Application::handleCommand()

Running migrations and seeding database ...

Illuminate\Database\QueryException

SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, Host: 127.0.0.1, Port: 3306, Database: laravel, SQL: select exists (select 1 from information_schema.tables where table_schema = schema() and table_name = 'migrations' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED')) as exists)

at vendor/laravel/framework/src/Illuminate/Database/Connection.php:838
```


Loading...