7

I have setup a pipeline in BitBucket to automatically deploy my master branch of my project to an Azure Web App instance.

The app deploys the files and runs composer update as expected (although it does warn that it's running as root), but php artisan migrate --force returns:

Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user 'forge'@'127.0.0.1' (using password: NO) (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations)

I have already created the .env file, and when I run php artisan migrate from within a shell it runs successfully and the tables are created.

Being that 'forge' is the default user in database.php I figure .env isn't being loaded when the command is fired from the deploy script.

Is there something obvious I've missed to cause this issue, or should I somehow set it up to not run as root? I could replace the database details in database.php but I feel that's the wrong thing to do.

edit

.env contents (with certain data replaced with ********):

APP_NAME=Laravel
APP_ENV=local
APP_KEY=********
APP_DEBUG=true
APP_URL=********

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=********
DB_DATABASE=********
DB_USERNAME=********
DB_PASSWORD=********

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

edit 2

I realise I'm yet to publish my bitbucket-pipelines.yml file:

image: php:7.2-fpm

pipelines:
  branches:
    master:
      - step:
          script:
            - apt-get update && apt-get install -qy git curl libmcrypt-dev mysql-client && apt-get install -qy unzip git
            - yes | pecl install mcrypt-1.0.1
            - docker-php-ext-install pdo_mysql
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - composer update
            - php artisan migrate --force
            - php artisan serve --port=80 &
            - sleep 5
            - curl -vk http://localhost:80
          deployment: staging
          services:
            - mysql

definitions:
  services:
    mysql:
      image: mysql:5.7
      environment:
        MYSQL_DATABASE: '******'
        MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
        MYSQL_USER: '******'
        MYSQL_PASSWORD: '******'
        MYSQL_PORT: '******'

I also have a .env.pipelines file:

APP_ENV=local
APP_KEY=******
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=******
DB_USERNAME=******
DB_PASSWORD=******
Richard Hedges
  • 1,180
  • 5
  • 14
  • 39

3 Answers3

3

This error basically comes from the after changes in the .env file:

Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user 'forge'@'127.0.0.1' (using password: NO) (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations)

Whenever we change the DB_DATABASE, DB_USERNAME and DB_PASSWORD in .env file, we need to clear the cache.

After completion of .env edit, must be clear cache: php artisan config:cache

NOTE: If no password is set on the database, clear it DB_PASSWORD, empty space must also be removed(In the past I've also faceout this problem, It's consider blank space as a password)

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
  • I have run `php artisan config:cache` after changing the `.env` file and still the same error - I've also restarted the Web App instance with the same result. – Richard Hedges Mar 04 '19 at 16:50
  • Hello @RichardHedges Maybe you have a problem with some other codes, But with this solution your problem can be solved. I am giving you a solution from my previous experience, I had a problem similar to yours. – Udhav Sarvaiya Mar 05 '19 at 05:22
  • I'm not sure what other 'codes' could be causing this problem, as nothing else has been changed. I've tried running this with a fresh Laravel installation using the correct database details, and when I run `php artisan migrate` from the command line it works fine - but when running from BitBucket pipelines it doesn't use the correct variables from `.env` – Richard Hedges Mar 05 '19 at 09:39
0

Without seeing your deploy script and how you are connecting with your Azure server you would need to put

php artisan config:clear // This will reload the .env file to cache

after you have connected to your server but before you run

php artisan migrate

Josh
  • 1,316
  • 10
  • 26
  • I've added the deploy script to the original question now, and I've run `php artisan config:clear` but still getting the same problem. – Richard Hedges Mar 04 '19 at 16:51
0

Please checkout the link:

https://laravel.com/docs/5.7/configuration#configuration-caching

php artisan config:cache

The above command will just regenerate the cache for you. (if added as a part of deployment script)

Else you can use php artisan config:clear just to clear the existing config and fetch values from .env/config files (add as a part of your deployment script)

Arvind Kala
  • 542
  • 4
  • 7
  • I have tried clearing the cache but still the same problem. – Richard Hedges Mar 04 '19 at 16:51
  • Ok, Do like this, on your server create a .env file in a separate directory let's say /home//config/.env and in your deployment script just add a command at last, which just symlink the .env file to that location, similar like `ln -s .env`. As generally we don't commit `.env` , seems like your .env is not even present in your build folder. – Arvind Kala Mar 07 '19 at 11:24
  • I have already created a `.env` file in the same directory as `artisan` though - the file definitely exists. I'll give your suggestion a run through later. – Richard Hedges Mar 07 '19 at 11:37
  • Refer https://confluence.atlassian.com/bitbucket/laravel-with-bitbucket-pipelines-913473967.html – Arvind Kala Mar 07 '19 at 11:47