skip to Main Content

In localhost, symfony console doctrine:database:create runs perfectly.
But when I try to install the project for production on Ubunthu server, that does not work.

I have a .env file with vars like :

.env

###> symfony/framework-bundle ###
APP_ENV=%APP_ENV%
APP_SECRET=%APP_SECRET%
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
DATABASE_URL="mysql://%DB_USER%:%DB_PASS%@%DB_SERVER%/%DB_DBNAME%?serverVersion=%DB_VERSION%&charset=utf8mb4"
###< doctrine/doctrine-bundle ###

###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN='%CORS_ALLOW_ORIGIN%'
###< nelmio/cors-bundle ###

and

.env.prod

APP_ENV=prod
APP_SECRET=xxxxxxx
CORS_ALLOW_ORIGIN=^https?://(admin.|)(example.com)(:[0-9]+)?$
DB_USER=xxxxx
DB_PASS=xxxx
DB_SERVER=xxxxx:3306
DB_DBNAME=mydbname
DB_VERSION=10.11.2-MariaDB

I´m trying to run symfony console doctrine:database:create

but by ssh that does not work and I got this error :

[critical] Error thrown while running command "doctrine:database:create". Message: "Malformed parameter "url"."
In ConnectionFactory.php line 262:
Malformed parameter "url".
In MalformedDsnException.php line 12:
Malformed database connection URL
doctrine:database:create [-c|–connection [CONNECTION]] [–if-not-exists]

even if I try to set each var before like…

export DB_USER=foo

So what is the best way to deploy on production
… run easily

symfony console doctrine:database:create
symfony console doctrine:schema:create

.. and Symfony/doctrine can interprates .env with vars without I need to gitignore doctrine.yaml and .env ?

2

Answers


  1. Chosen as BEST ANSWER

    Actually there were different matters to create Symfony database during production deployment we need by SSH :

    • As @Andrey Hmel said, you must use .env and .env.local (env.local contains production vars values and it´s never commited). In my case env.prod is unuseful.
    • Avoid using chars like ? or other special character swhich can be encoded in the database user password. I could tested this with composer dump-env prod and discover this kind of error.
    • You need to make sure your database user has got privileges to create databases in your server, to fix it :
    1. Log in as root to see privileges by user : sudo mysql -u root
    2. Check privileges : SHOW GRANTS FOR 'username'@'%';
    3. All all privileges for databases : GRANT ALL PRIVILEGES ON *.* TO 'username'@'%';
    4. Save information : FLUSH PRIVILEGES;

  2. I think symfony loads .env.prod only if APP_ENV already set to prod in .env or .env.local.

    Create .env.local on production machine and put APP_ENV=prod there.

    Or just put APP_ENV=prod into .env, but in this case you will need to set APP_ENV=dev into .env.local on development machine.

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