Database for tests

To bootstrap the database, we also will use a Docker image. You can install MySQL locally, but a container is a more flexible approach that doesn't clog the system, and you can easily start an empty database for testing purposes in seconds.

There is an official image, mysql, of the MySQL database that you can find here: https://hub.docker.com/_/mysql. You can load and run the container using these images with the following command:

docker run -it --rm --name test-mysql -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -p 3306:3306 mysql

There are two necessary parameters that you can set with environment variables. First, the MYSQL_ROOT_PASSWORD environment variable sets a password for the root user. Second, the MYSQL_DATABASE environment variable sets the name of a default database that will be created on the first start of a container. We named our container test-mysql and forwarded the local port 3306 to port 3306 inside our container.

To make sure that our container has started, you can use the mysql client, if it's installed locally:

mysql -h 127.0.0.1 -P 3306 -u root -p test

The preceding command connects to 127.0.0.1 (to avoid using sockets) on port 3306, with user as root. The -p argument asks for a password for the connection. We set a password for our testing container because the database images require it.

Our database is ready to use. You can also stop it with the following command:

docker stop test-mysql
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.169