mirror of
https://github.com/cytopia/devilbox.git
synced 2025-01-18 10:46:23 +00:00
#506 documentation: how to connect to MySQL and other services
This commit is contained in:
parent
5064effe89
commit
acf3293692
19
docs/_includes/snippets/core-container.rst
Normal file
19
docs/_includes/snippets/core-container.rst
Normal file
@ -0,0 +1,19 @@
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| Container | Name | Hostname | IP Address |
|
||||
+===========================+=======+==========+================+
|
||||
| DNS | bind | bind | 172.16.238.100 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| PHP | php | php | 172.16.238.10 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| Apache, Nginx | httpd | httpd | 172.16.238.11 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| MySQL, MariaDB, PerconaDB | mysql | mysql | 172.16.238.12 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| PostgreSQL | pgsql | pgsql | 172.16.238.13 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| Redis | redis | redis | 172.16.238.14 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| Memcached | memcd | memcd | 172.16.238.15 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| MongoDB | mongo | mongo | 172.16.238.16 |
|
||||
+---------------------------+-------+----------+----------------+
|
@ -74,19 +74,11 @@ PHP project hostname settings
|
||||
=============================
|
||||
|
||||
When configuring your PHP projects to use MySQL, PostgreSQL, Redis, Mongo and other services,
|
||||
make sure to set the hostname of each of those services to ``127.0.0.1``.
|
||||
make sure to set the hostname/address of each of those services to how they are defined within the
|
||||
Devilbox network:
|
||||
|
||||
**Why is that?**
|
||||
.. include:: /_includes/snippets/core-container.rst
|
||||
|
||||
The PHP container port-forwards each service port to its own listen address on ``127.0.0.1``.
|
||||
The Devilbox also exposes each of those service ports to the host operating system on ``127.0.0.1``.
|
||||
|
||||
This allows you to keep your project configuration unchanged and have the same behaviour inside the
|
||||
PHP container and on your host operating system.
|
||||
|
||||
.. important::
|
||||
Do not mix up ``localhost`` with ``127.0.0.1``. They behave differently!
|
||||
Use ``127.0.0.1`` and do not use ``localhost``.
|
||||
|
||||
As an example, if you want to access the MySQL database from within the PHP container, you do the
|
||||
following:
|
||||
@ -101,10 +93,11 @@ following:
|
||||
host> ./shell.sh
|
||||
|
||||
# Enter the MySQL console
|
||||
php> mysql -u root -h 127.0.0.1 -p
|
||||
php> mysql -u root -h mysql -p
|
||||
mysql>
|
||||
|
||||
The very same command applies to access the MySQL database from your host operating system:
|
||||
To access the MySQL database from your host operating system you would need the address to what it
|
||||
exposes to on your host (usually ``127.0.0.1``):
|
||||
|
||||
.. code-block:: bash
|
||||
:emphasize-lines: 2
|
||||
@ -113,17 +106,14 @@ The very same command applies to access the MySQL database from your host operat
|
||||
host> mysql -u root -h 127.0.0.1 -p
|
||||
mysql>
|
||||
|
||||
So no matter if you use the Devilbox or have another LAMP stack installed locally on your host
|
||||
operating system, you do not have to change your configuration files if you stick to this tip.
|
||||
|
||||
So any of your projects php files that configure MySQL as an example should point the hostname
|
||||
or IP address of the MySQL server to ``127.0.0.1``:
|
||||
Any of your projects php files that configure MySQL as an example should point the hostname
|
||||
or IP address of the MySQL server to ``mysql``:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
// MySQL server connection in your project configuration
|
||||
mysql_host = '127.0.0.1';
|
||||
mysql_host = 'mysql';
|
||||
mysql_port = '3306';
|
||||
mysql_user = 'someusername';
|
||||
mysql_pass = 'somepassword';
|
||||
@ -135,11 +125,11 @@ or IP address of the MySQL server to ``127.0.0.1``:
|
||||
Timezone
|
||||
========
|
||||
|
||||
The :ref:`env_timezone` value will affect PHP, web server and MySQL container equally. It does
|
||||
The :ref:`env_timezone` value will affect PHP and web serverequally. It does
|
||||
however not affect any other official Docker container that are used within the Devilbox. This is
|
||||
an issue that is currently still being worked on.
|
||||
|
||||
Feel free to change this to any timezone you require for PHP and MySQL, but keep in mind that
|
||||
Feel free to change this to any timezone you require for PHP, but keep in mind that
|
||||
timezone values for databases can be painful, once you want to switch to a different timezone.
|
||||
|
||||
A good practice is to always use ``UTC`` on databases and have your front-end application calculate
|
||||
|
@ -171,71 +171,36 @@ See the following table for a few examples:
|
||||
IP address mappings
|
||||
-------------------
|
||||
|
||||
The following table shows a mapping of IP addresses of available service from the perspective
|
||||
of your host operating system and from within the PHP container.
|
||||
The following table shows a mapping of IP addresses and hostnames. In other words, when you are inside
|
||||
the PHP container, you can reach the services via the below defined IP addresses or hostnames:
|
||||
|
||||
+--------------+-----------------+------------------------------+
|
||||
| Service | IP from host os | IP from within PHP container |
|
||||
+==============+=================+==============================+
|
||||
| PHP | ``127.0.0.1`` | ``127.0.0.1`` |
|
||||
+--------------+-----------------+------------------------------+
|
||||
| Apache/Nginx | ``127.0.0.1`` | ``127.0.0.1`` |
|
||||
+--------------+-----------------+------------------------------+
|
||||
| MySQL | ``127.0.0.1`` | ``127.0.0.1`` |
|
||||
+--------------+-----------------+------------------------------+
|
||||
| PostgreSQL | ``127.0.0.1`` | ``127.0.0.1`` |
|
||||
+--------------+-----------------+------------------------------+
|
||||
| Redis | ``127.0.0.1`` | ``127.0.0.1`` |
|
||||
+--------------+-----------------+------------------------------+
|
||||
| Memcached | ``127.0.0.1`` | ``127.0.0.1`` |
|
||||
+--------------+-----------------+------------------------------+
|
||||
| MongoDB | ``127.0.0.1`` | ``127.0.0.1`` |
|
||||
+--------------+-----------------+------------------------------+
|
||||
.. include:: /_includes/snippets/core-container.rst
|
||||
|
||||
As you can see, everyhing is available under ``127.0.0.1``.
|
||||
.. note:: It is recommended to use hostnames as they can be remembered much easiert.
|
||||
|
||||
The PHP container is using ``socat`` to forward the services from all other available containers
|
||||
to its own ``127.0.0.1`` address.
|
||||
|
||||
An example to access the MySQL database from either host or within the PHP container is the same:
|
||||
An example to access the MySQL database from within the PHP container:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# Access MySQL from your host operating system
|
||||
host> mysql -h 127.0.0.1
|
||||
host> mysql -h 127.0.0.1 -u root -p
|
||||
|
||||
# Access MySQL from within the PHP container
|
||||
devilbox@php-7.0.19 in /shared/httpd $ mysql -h 127.0.0.1
|
||||
|
||||
.. important::
|
||||
Do not use ``localhost`` to access the services, it does not map to ``127.0.0.1`` on
|
||||
all cases.
|
||||
devilbox@php-7.0.19 in /shared/httpd $ mysql -h mysql -u root -p
|
||||
|
||||
So when setting up a configuration file from your PHP project you would for example use
|
||||
``127.0.0.`` as the host for your MySQL database connection:
|
||||
``mysql`` as the host for your MySQL database connection:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
// MySQL server connection
|
||||
mysql_host = '127.0.0.1';
|
||||
mysql_host = 'mysql';
|
||||
mysql_port = '3306';
|
||||
mysql_user = 'someusername';
|
||||
mysql_pass = 'somepassword';
|
||||
?>
|
||||
|
||||
Imagine your PHP framework ships a command line tool to run database migration. You could run
|
||||
it from your host operating system or from within the PHP container. It would work from both
|
||||
sides as the connection to the database is exactly the same locally or within the container.
|
||||
|
||||
You could also even switch between the Devilbox and a locally installed LAMP stack
|
||||
and still use the same configuration.
|
||||
|
||||
.. important::
|
||||
The mapping of ``127.0.0.1`` to your host operating system does not work with
|
||||
Docker Toolbox out of the box. In order to achieve the same behaviour read up on:
|
||||
:ref:`howto_docker_toolbox_and_the_devilbox`.
|
||||
|
||||
|
||||
Port mappings
|
||||
-------------
|
||||
@ -283,7 +248,5 @@ Checklist
|
||||
2. You know how to become root inside the PHP container
|
||||
3. You know how to leave the container
|
||||
4. You know that file and directory permissions are synronized
|
||||
5. You know that ``127.0.0.1`` is available on your host and inside the PHP container
|
||||
6. You know that ports are the same inside the container and on your host os
|
||||
7. You know that project urls are available inside the container and on your host
|
||||
8. You know about the limitations of :ref:`howto_docker_toolbox_and_the_devilbox`
|
||||
5. You know by what hostnames you can reach a specific service
|
||||
6. You know that project urls are available inside the container and on your host
|
||||
|
@ -18,25 +18,7 @@ Core container
|
||||
|
||||
These container are well integrated into the Devilbox intranet and are considered core container:
|
||||
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| Container | Name | Hostname | IP Address |
|
||||
+===========================+=======+==========+================+
|
||||
| DNS | bind | bind | 172.16.238.100 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| PHP | php | php | 172.16.238.10 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| Apache, Nginx | httpd | httpd | 172.16.238.11 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| MySQL, MariaDB, PerconaDB | mysql | mysql | 172.16.238.12 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| PostgreSQL | pgsql | pgsql | 172.16.238.13 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| Redis | redis | redis | 172.16.238.14 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| Memcached | memcd | memcd | 172.16.238.15 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
| MongoDB | mongo | mongo | 172.16.238.16 |
|
||||
+---------------------------+-------+----------+----------------+
|
||||
.. include:: /_includes/snippets/core-container.rst
|
||||
|
||||
|
||||
Additional container
|
||||
|
Loading…
Reference in New Issue
Block a user