skip to Main Content

I am having some issues with connecting and using an Azure SQL Database when coming from my Laravel app on an ubuntu server.

I have gone through the Microsoft docs for connecting to a SQL database from a PHP app and run the connection wizard from Azure to see if my servers could connect, which they can.

I believe I have an issue with the PHP.ini configuration but followed Microsofts docs on that as well. Does anyone have experience with this?

The Digitial ocean server is configured by Laravel Forge.

The Azure database we need to connect to is on Microsoft Azure and set up through their admin area.

I have whitelisted the Azure database server to accept incoming connections from the Digital Ocean Server.

2

Answers


  1. Chosen as BEST ANSWER

    I ended up finding that I was able to connect. I ran the php artisan db:show command to find out that I indeed had connected to the Azure database. The issue was that the seed command I was running was a very long-running command which caused me to think that it was not working.

    So, the laravel documentation on connecting to a SQL server database is correct.

    You can follow the Laravel docs with this link: https://laravel.com/docs/9.x/database#mssql-configuration

    You can follow the Microsoft docs for connecting from a PHP app here: https://learn.microsoft.com/en-us/azure/azure-sql/database/connect-query-php?view=azuresql


  2. • I would suggest you to please check the connection code of your Laravel application running on the Digital Ocean ubuntu VM and ensure that it is as below with regards to connecting with Azure SQL Database: –

     DB_CONNECTION=<connection name of the sql db>
     DB_HOST=<ip address of the SQL Server>
     DB_PORT=<port number for the SQL DB>
     DB_DATABASE=mydb
     DB_USERNAME=user
     DB_PASSWORD=secret
    
     'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'mydb'),
        'username' => env('DB_USERNAME', 'user'),
        'password' => env('DB_PASSWORD', 'secret'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    

    Change the ‘mysql’ connection parameters with that of the Azure SQL database connection and try to connect once again. Would suggest you to please check the below community thread for more information on configuring the Laravel server for that purpose: –

    How to connect db of a Laravel 7.12 project on a remote ubuntu server without use artisan

    • In the ‘.env’ file, do ensure to configure the following lines in it with proper values: –

    APP_URL=http://laravel.example.com DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laraveldb DB_USERNAME=laravel DB_PASSWORD=password ’
    

    And the ‘.conf’ file as below: –

    <VirtualHost *:80> ServerAdmin [email protected] ServerName laravel.example.com DocumentRoot /var/www/html/laravel/public <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/html/laravel> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
    

    Once the above are configured successfully, ensure that the outbound network connections from the ubuntu server on which the Laravel app is configured is allowed for the SQL Database port and the respective incoming network connection is allowed on the SQL Server as well. You will need to make these changes on the NSGs relating to the VM and in the ‘Networking’ section on Azure SQL Server wherein you can configure the VM’s IP address from a virtual network to be allowed in it.

    For more information, kindly refer to the below link: –

    https://snapshooter.com/learn/guides/how-to-install-laravel-ubuntu

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search