So you’ve set up containers for Nginx and for PHP – now you’d like to have a database. After all the work of the last two labs you’ll be glad to hear than adding MariaDB to the mix is very simple – we can get it up and running in minutes.
Why MariaDB?
I used to use MySQL – but, I’ve changed to MariaDB for the following reasons:
- Since Oracle bought MySQL, the open development of MySQL has been significantly curtailed. Because of this, MariaDB now enjoys a significantly more vibrant community, and is more open about sharing what changes have been made and why.
- MariaDB is quicker than MySQL. Some, rather excitable, metrics show that MariaDB is massively faster than MySQL – it isn’t. It’s a smidgen faster – reckon on about 5%. But I’ll take every little boost I can.
- MariaDB is significantly compatible with MySQL – as you might expect since MariaDB was forked from MySQL. This doesn’t mean that you can just take your MySQL database and load it into MariaDB without any issues – you’ll still need to test thoroughly – but you’ll experience significantly fewer problems than if you migrated to another DB.
It’s worth noting that whilst you can administer MariaDB using MySQL Workbench I’d advise against it. There are lots of alternative tools available (including a web based admin package that you’ll install at the end of this tutorial), but I like Navicat Premium Essentials – which is not free, but which is worth every penny.
As before, These instructions assume that you have followed the steps in the previous instalment (Docker Lab – Containerising your website, Part 2)
Configuring your Containers.
Yet again, it will come as no surprise that, in order to install PHP, we need to edit the docker-compose.yml file. It will be necessary to stop Docker (just in case you left it running from the last session). You may remember that you can edit the docker-compose.yml file while Docker is still running – but the changes won’t take effect until Docker is restarted. I shan’t tell you how to stop the containers. You can look that up for yourself in Part 2.
Edit docker-compose.yml to add the maria-db container. I added it just below services and before nginx. Replace <Your Root Password Goes Here> with a password of your choice. Make it complicated – and remember that you probably aren’t going to have to type it all that often, so it doesn’t need to be convenient for your fingers. I generally use a UUID – something like this ‘1e1fdb9c-6ba5-48ea-9640-7801eed2022e’.
maria-db: image: 'bitnami/mariadb:latest' networks: - app-tier environment: - MARIADB_ROOT_PASSWORD=<Your Root Password Goes Here> volumes: - ./db-data:/bitnami
Generating a UUID.
If you do want to use a UUID for your password then you can generate one using the following command:
cat /proc/sys/kernel/random/uuid
Starting Docker
Now start Docker, giving it a little while to start up and configure MariaDB before moving on to the next step:
cd ~/lemp-compose docker-compose up --remove-orphans -d
Testing your MariaDB install
A useful online tool for administering your database and also for testing that MariaDB has been installed correctly is phpminiadmin.php – in fact this will exercise every part of your set up.
Place the phpminiadmin.php into your site directory as follows:
cd ~/lemp-compose/public/<your website name>/htdocs wget https://raw.github.com/osalabs/phpminiadmin/master/phpminiadmin.php
Browse to your website URL – in this example, http://<your domain name>/phpminiadmin.php, and you’ll see the phpminiadmin page. Click “advanced settings” and log in as follows:
Username: root
Hostname: maria-db
Password: whatever you specified in the Docker Compose file.
You will now have access to the phpMiniAdmin database management interface. You can use this interface to create databases and tables and execute SQL queries on your MariaDB database server.
If that all worked then next time we’ll add certificates. If it didn’t work, go back and check your working.
Note!
If you want to connect to this MariaDB instance from your desktop computer using a tool like Navicat Essentials, you need the following setup.
Click the SSH tab, check the ‘Use SSH tunnel’ checkbox and fill in the details that you use to SSH into the server.
Next click on the General tab and fill in the User Name as ‘root’ and the password as whatever you specified in the Docker Compose file. The Host Name / IP Address needs to be retrieved from Docker with the following command:
docker inspect <containerID> | grep \"IPAddress\"
Get the Container ID using docker ps, as previously. Note that the IP Address may change periodically – so if you can’t connect for whatever reason, check this first.