doctrine:database:create & server_version

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

We have a Docker database container running and our app is instantly configured to talk to it thanks to the Symfony web server. But... we can't really do anything yet... because that MySQL instance is empty! In var:export, you can see that the database name is apparently "main". But that does not exist yet.

No problem! When we installed Doctrine, it added a bunch of new bin/console commands to our app. Run:

php bin/console

and scroll up to find a huge list that start with doctrine:. The vast majority of these are not very important - and we'll talk about the ones that are.

The "symfony console" Command

One of the handy ones is doctrine:database:create, which reads the database config and creates the database. So, in our case, it should create a database called main.

Ok! Copy the command name and run:

php bin/console doctrine:database:create

And... yikes!

Access denied for db_user at localhost.

Huh. For some reason, it's using this DATABASE_URL from .env instead of the one that's set by the Symfony binary.

30 lines .env
... lines 1 - 27
... lines 29 - 30

The problem is that, when you load your site in the browser, this is processed through the Symfony web server. That allows the Symfony binary to inject all of the environment variables.

But when you just run a random bin/console command, that does not use the symfony binary. And so, it does not have an opportunity to add the environment variables.

No worries! There is, of course, a solution. Instead of running:

php bin/console

We'll run:

symfony console

symfony console literally means bin/console... but because we're running it through the Symfony executable, it will inject the environment variables that are coming from Docker.


The latest version of MakerBundle generates a MYSQL_DATABASE: main config into your docker-compose.yaml file. If you have this, then the database will already be created! Feel free to run the command just in case ;).


symfony console doctrine:database:create

And... boom! We have a database!

Docker Image Versions

Before we jump into fun stuff like generating entities and database tables, I want to tighten up one more thing. Open up docker-compose.yaml. The :latest next to the image means that we want to use the latest version of MySQL. Where does that image come from?

... line 1
image: 'mysql:latest'
... lines 5 - 13

Google for Docker hub to find When you say that you want a mysql image at version latest, Docker communicates back to Docker Hub to get the details. Search for MySQL for all the info about that image including the tags that are currently available. Right now, the latest tag is equal to 8.0.

Head back over to docker-compose.yaml. You don't have to do this, but I'm going to change latest to 8.0 so that I'm locked at a specific version that won't suddenly change.

... line 1
image: 'mysql:8.0'
... lines 5 - 13

Over at the terminal, even though latest and 8.0 are technically the same image, let's restart docker-compose anyways to update the image. Run:

docker-compose down

And then:

docker-compose up -d

It quickly downloaded the new image... which was probably just a "pointer" to the same image we used before.

Setting server_version

Now that we've set the MySQL version in Docker, we should also do the same thing with Doctrine. Open up config/packages/doctrine.yaml. See that server_version key?

... lines 3 - 4
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '5.7'
... lines 8 - 19

Set this to 8.0. If you're using mariadb, you can use a format like mariadb-10.5.4.

... lines 3 - 6
server_version: '8.0'
... lines 8 - 19

This is... kind of an annoying thing to set, but it is important. It tells Doctrine what version of MySQL we're running so that it knows what features are supported. It uses that to adjust the exact SQL it generates. Make sure that your production database uses this version or higher.

Next, let's create our first database table by generating an entity.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
    "require": {
        "php": "^7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.8", // 1.8.2
        "doctrine/doctrine-bundle": "^2.1", // 2.1.0
        "doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.1
        "doctrine/orm": "^2.7", // v2.7.3
        "knplabs/knp-markdown-bundle": "^1.8", // 1.8.1
        "knplabs/knp-time-bundle": "^1.11", // v1.12.0
        "sensio/framework-extra-bundle": "^5.5", // v5.6.1
        "sentry/sentry-symfony": "^3.4", // 3.5.2
        "stof/doctrine-extensions-bundle": "^1.4", // v1.4.0
        "symfony/asset": "5.1.*", // v5.1.2
        "symfony/console": "5.1.*", // v5.1.2
        "symfony/dotenv": "5.1.*", // v5.1.2
        "symfony/flex": "^1.3.1", // v1.9.0
        "symfony/framework-bundle": "5.1.*", // v5.1.2
        "symfony/monolog-bundle": "^3.0", // v3.5.0
        "symfony/stopwatch": "5.1.*", // v5.1.2
        "symfony/twig-bundle": "5.1.*", // v5.1.2
        "symfony/webpack-encore-bundle": "^1.7", // v1.7.3
        "symfony/yaml": "5.1.*", // v5.1.2
        "twig/extra-bundle": "^2.12|^3.0", // v3.0.4
        "twig/twig": "^2.12|^3.0" // v3.0.4
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.3", // 3.3.1
        "symfony/debug-bundle": "5.1.*", // v5.1.2
        "symfony/maker-bundle": "^1.15", // v1.20.0
        "symfony/var-dumper": "5.1.*", // v5.1.2
        "symfony/web-profiler-bundle": "5.1.*", // v5.1.2
        "zenstruck/foundry": "^1.1" // v1.1.0