Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php artisan migrate on Azure (in BitBucket pipeline)

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=******
like image 636
Richard Hedges Avatar asked Feb 07 '19 19:02

Richard Hedges


People also ask

How does php artisan migrate work?

php artisan migrate:reset reverses all migration, unlike :rollback . php artisan migrate:fresh is used when we want a fresh or new installation of our database. It deletes all the existing tables of the database and runs the migrate command.

What is php artisan migrate rollback?

By default, php artisan migrate:rollback will rollback all of your database migrations. By specifying --step=1 , you're saying that you only want to rollback the latest database migration. Plus, if you change the number, e.g. into --step=2 , you're telling Laravel to only rollback the last two migrations.

How do I migrate in Laravel?

To create a new migration, you can run the make:migration Artisan command and that will bootstrap a new class on your Laravel application, in the database/migrations folder. This class will contain a default boilerplate code.

How do I create a PHP pipeline in azure?

Select PHP in the Configure tab. Examine your new pipeline. When you're ready, select Save and run. You're prompted to commit a new azure-pipelines.yml file to your repository. Select Save and run again. If you want to watch your pipeline in action, select the build job.

How do I use Bitbucket Pipelines with PHP?

Bitbucket Pipelines runs all your builds in Docker containers using an image that you specify at the beginning of your configuration file. You can easily use PHP with Bitbucket Pipelines by using one of the official PHP Docker images on Docker Hub.

How to deploy an Azure web app using Azure CLI?

The release will provision an Azure Web app using the Azure CLI and deploy the zip file to the Web App generated by the associated build. Under the Releases under Pipelines tab, select release definition PHP and click on Edit. Go to Tasks and select Dev environment. Select the Azure CLI task, choose the Azure subscription.

What are Bitbucket-Pipelines?

Bitbucket Pipelines allows you to launch extra services during the execution of your pipeline by defining the service, and instantiating it on the appropriate step. We've compiled a list of of bitbucket-pipeline.yml examples to help get started with your favourite database.


1 Answers

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)

like image 183
Udhav Sarvaiya Avatar answered Oct 12 '22 17:10

Udhav Sarvaiya