Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ronit5rg's avatar

how to connect to the database of a cloud server for a laravel app?

I am trying to deploy my larval web app with cloudways (cloud server), please tell me how to connect my table and model with the database they provide. I used to connect to my database using the terminal for localhost with

php artisan migrate

I got some info that I have to use ssh or something. I am using a mac.

Please help me anyone.

0 likes
12 replies
seanwu's avatar

Have you tried updating the DB_* variables in your .env file?

...
DB_HOST=<REMOTE DATABASE SERVER IP ADDRESS>
DB_DATABASE=<YOUR DATABASE NAME>
DB_USERNAME=<YOUR DB USERNAME>
DB_PASSWORD=<YOUR DB PASSWORD>
...

All these information should be available from the cloud server you are hosting with.

ronit5rg's avatar

@seanwu thanks for stopping by... Yes, I have changed those info in the .env file, I am just confused can I use the same artisan commands I use in my terminal in ssh or i have to do something else to connect to the server's database?

martinbean's avatar

@ronit5rg Contact Cloudways support if you are having an issue with their platform. They should provide you with the credentials to connect to the database on their platform.

YanTao's avatar

you must update your config/database.php file , .env file is just your local environment

Thijmen's avatar

You should run those commands on the server which you connect to with SSH.

ronit5rg's avatar

@YanTao @Thijmen yes, i changed the database.php file according to specifications, but when I am typing php artisan migrate, it's giving me this error:

[PDOException] SQLSTATE[42000] [1044] Access denied for user 'myusername '@'localhost' to database 'my db name'

Thijmen's avatar

Can you access, for instance, PHPMyAdmin with those credentials?

ronit5rg's avatar

@Thijmen Actually, cloudways don't use phpmyadmin, but yes i m able to get into my mysql database with those credentials

Thijmen's avatar

You are 100% sure you are running that command on the server?

YanTao's avatar

can you show your config about your database.php?

YanTao's avatar

this is my config

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database'  => env('DB_DATABASE', 'mydbname'),
            'username'  => env('DB_USERNAME', 'username'),
            'password'  => env('DB_PASSWORD', 'password'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
            'engine'    => null,
        ],
seanwu's avatar

the hosting company most likely have some sort of IP restrictions on the database server, so if you run php artisan migrate on your local machine, it's not going to authenticate you. You either run this on the server via ssh, or add remote access like this:

GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'1.2.3.4' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

Please or to participate in this conversation.