Docker MySQL 5.7 via docker-compose and using on Ubuntu 14.04

Installing Docker and docker-compose was covered here and here .

We will pull all services for the container from docker hub .

1. Create a working directory :

First of all, let's create a working directory that will contain docker-compose.yml with the rules for installing a container with mysql 5.7.

mkdir /var/www/docker/mysql5.7

2. Create docker-compose.yml :

In /var/www/docker/mysql5.7 we create a file docker-compose.yml , which will contain the rule for installing the required version of mysql.

version: '3.1'

services:

db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: new_base
ports:
- 3308:3306

version - specifies the schema version. In previous versions of docker and docker-compose, there were different structures for describing services in a container. Therefore, the version is specified to recognize the structure of the yml file.

services - describes the services that will be in our container. There may be several of them, now we consider only one.

db is the name of the service in the container.

image - specify what is in this service. In our case, mysql with version 5.7. Read more at hub , tags, parameters.

restart - as far as I understand, this is the action of the service after docker-compose down. That is, it restarts after the container is closed.

environment - I think it's clear here, the password and the base created by the machine.

ports - here carefully! 3308 is the port that will be available for connecting to the service in the container, 3306 is the standard mysql port on which it will work in the container. If you put 3306:3306 and if mysql is already running locally (not in the docker), then there will be an error, since port 3306 is already busy.

3. We raise the Docker container :

Let me remind you that we are working in the /var/www/docker/mysql5.7 directory. You can raise the container in the background and without the background. We use one of the methods .

The command to run in the background (To continue working in the window of the same console):

sudo docker-compose up -d

docker-compose up -d

Just launching the container not in the background (Will hang in the console displaying logs):

sudo docker-compose up

docker-compose up

4. Check and connect :

If everything went without errors and MySQL 5.7 has risen, then it will not be difficult to connect to it.

mysql -uroot -proot -h127.0.0.1 -P3308

The scheme described in paragraph (2) uses:

-P3308 - port 3308

-uroot - default login root

-proot - root password

-h127.0.0.1 - ip of the local mysql container.

the new_base database has already been created automatically.

We see version 5.7 and the database we created.

docker-compose mysql5.7

5. Connecting to Php site :

<?php

$mysqli = mysqli_init();
if (!$mysqli) {
die('mysqli_init завершилась провалом');
}

if ( !$mysqli->real_connect('127.0.0.1', 'root', 'root', 'new_base', '3308') ) {
die('Ошибка подключения (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}

$sql = "CREATE TABLE IF NOT EXISTS `logs` ( ".
"`id` int(11) NOT NULL AUTO_INCREMENT, ".
"`user_id` int(11) NOT NULL, ".
"PRIMARY KEY (`id`)".
") ENGINE=Innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";

$mysqli->query($sql);

$result = $mysqli->query("SHOW Tables;");
while ($row = $result->fetch_object()){
var_dump($row);
}


$mysqli->close();

real_connect('127.0.0.1', 'root', 'root', 'new_base', '3308') - the connection itself.

The confirmation of a successful connection is that var_dump() will return the table we created.

object(stdClass)#3 (1) { ["Tables_in_new_base"]=> string(4) "logs" }

6. Completion of work :

Being in the directory where docker-compose.yml is

sudo docker-compose down

If you do not close the container and start it again, there will be an error.

16052 0

Comments

Be the first to review this item. Share your rating and review so that other customers can decide if this is the right item for them.
You have to log in to leave a comment. Sign in