The Laravel database connection using Laradock is very straightforward and intuitive with MySQL or Mariadb database servers in a Laravel PHP project when using Laradock Project Docker containers.
But some tips and reminders are important to prevent you from wasting time when preparing your environment of developing a Laradock Laravel application that uses DBMs.
In this article I will exemplify in addition to configuration and connection, tips for importing mysql or mariadb SQL dumps from your host for the desired Container Docker.
I also present how to connect a database customer, DBeaver with the MYSQL or MARIADB server running in the Container Docker Laradock.
In addition to solving some connection problems with database containers using Docker Laradock in Mac OS (M1 and PRO).
Creating and running the database container with Laradock
Laradock comes with an interesting diversity of database server containers ready to be used. See the list in the article Laradock - Containers docker for your PHP and Laravel Development - Introduction.
Among the available database servers are MySQL and Mariadb.
For a container with one of these servers to go up and be available, you must execute the docker-compose up
command.
To up - and build for the first time MySQL, execute:
docker-compose up -d mysql
To up - and build for the first time MariaDB, execute:
docker-compose up -d mysql
You can up several containers at the same time, including having both servers running at the same time, along with other services such as Apache2, Workspace and Redis:
docker-compose up -d apache2 mysql mariadb workspace redis
# resultado
Starting laradock_redis_1 ... done
Starting laradock_docker-in-docker_1 ... done
Creating laradock_mariadb_1 ... done
Starting laradock_workspace_1 ... done
Starting laradock_php-fpm_1 ... done
Starting laradock_apache2_1 ... done
This way you will be able to access the different database servers according to the connection configuration of your PHP Laravel project, or pure PHP.
Connecting the PHP Laravel project with the database server in Docker Laradock
Remember that containers go up with specific names, see the example of the previous section again.
In our example the MySQL server is called laradock_mysql_1
, while the Mariadb server is called laradock_mariadb_1
.
PHP Laravel PHP with MySQL in Docker Laradock
To connect your Laravel PHP project to the MySQL server that is running in the Laradock Configuration Container,
Configure the .env
file with the following variables:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=name_of_db
DB_USERNAME=root
DB_PASSWORD=root
Note that the root user password is defined in the .env
of Laradock file, not your Laravel project.
Connecting PHP Laravel with Mariadb to Docker Laradock
To connect your PHP Laravel project to the Mariadb server that is running in the Laradock Configuration Container Docker, configure the .env
file with the following variables:
DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=nome_do_meu_db
DB_USERNAME=root
DB_PASSWORD=root
Note that the root user password is defined in the .env
of Laradock file, not your Laravel project.
Connecting Laravel PHP with PostgreSQL in Docker Laradock
To connect your PHP Laravel project to the PostgreSQL server that is running in the Laradock configuration container,
set the .env
file with the following variables:
DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=nome_do_meu_db
DB_USERNAME=default
DB_PASSWORD=secret
How to connect DBeaver with mysql or mariadb in the Laradock
DBEAVER database customer connection to MySQL or MariaDB database servers, in a Docker Laradock container differ only in identifying the host.
For the connection to DBEAVER, you must use Localhost’s IP path: 127.0.0.1, as shown in the following image:
How to connect DBeaver with PostgreSQL in Laradock
DBEAVER database customer connection to PostgreSQL (PGSQL) database servers, in a Docker Laradock container, are performed similarly to the MySQL configuration, they differ from the identification of the host.
For DBEAVER connection, you can use the local host name: localhost, as shown in the following image:
The url: jdbc:postgresql://localhost:5432/database
is created by DBeaver after you save the connection, in the image above I am in the editing window of an existing connection.
How to connect database in Laradock with Mac M1 and PRO
A common mistake when using Mac computers is the impossibility of connecting using the container name directly, as shown above to Linux Ubuntu, Debian and Mint.
To solve simply add the IP 127.0.0.1 pointing to the desired host, for example MySQL, in the MAC /host
file.
An example of file with the insertion for MySQL and Mariadb:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
## Adicione as linhas abaixo
127.0.0.1 mysql
127.0.0.1 mariadb
# End of section
Comments