Skip to content

Added doctrine #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# default environment variables https://docs.docker.com/compose/env-file/
# default environment variables https://docs.docker.com/compose/env-file/

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=f4d72215cca250696794418379c8bc98
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
#TRUSTED_HOSTS='^localhost|example\.com$'
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_PRIMARY_URL=mysql://root:111@172.27.0.2:3306/mydb
DATABASE_REPLICA_URL=mysql://root:111@172.27.0.4:3306/mydb
###< doctrine/doctrine-bundle ###
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
.DS_Store
.idea
data

###> symfony/framework-bundle ###
/.env.local
/.env.local.php
/.env.*.local
/public/bundles/
/var/
/vendor/
###< symfony/framework-bundle ###

###> symfony/web-server-bundle ###
/.web-server-pid
###< symfony/web-server-bundle ###
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM php:7.2-alpine

WORKDIR /app
#RUN php ./composer.phar update
RUN docker-php-ext-install -j$(nproc) pdo_mysql
RUN docker-php-ext-install -j$(nproc) pcntl pdo_mysql
EXPOSE 8000
CMD ./bin/console server:run 0.0.0.0:8000
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
Docker MySQL master-slave replication
Symfony Doctrine Docker MySQL master-slave replication
========================

A simple Symfony app that uses a primary database for writes and a replica database for reads.
MySQL 8.0 master-slave replication with using Docker.

See the code in Doctrine DBAL [here](https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php).

Forked from [vbabak](https://github.com/vbabak/docker-mysql-master-slave)
Previous version based on MySQL 5.7 is available in [mysql5.7](https://github.com/vbabak/docker-mysql-master-slave/tree/mysql5.7) branch.
Mixed with code from [backbeat-tech](https://github.com/backbeat-tech/doctrine-mysql-replica-demo.git)

## Run

Expand Down Expand Up @@ -76,3 +81,25 @@ docker exec -it mysql_master bash
```bash
docker exec -it mysql_slave bash
```

## URLs

* http://localhost:8001/ - select query, uses replica only
* http://localhost:8001/insert - insert query, uses primary only
* http://localhost:8001/update/{id} - select and update queries, uses replica to fetch the data then switches to primary
* http://localhost:8001/ping - troubleshooting, no database used

## Observing logs

The app logs are available in the docker-compose output (run `docker-compose logs -f app`), showing which database is being used:

```
app_1 | 2019-03-27T17:17:39+00:00 [info] Matched route "app_app_update".
app_1 | 2019-03-27T17:17:40+00:00 [debug] SELECT t0.id AS id_1, t0.level AS level_2, t0.hash AS hash_3 FROM item t0 WHERE t0.id = ?
app_1 | 2019-03-27T17:17:40+00:00 [debug] Used REPLICA for the previous query.
app_1 | 2019-03-27T17:17:40+00:00 [debug] "START TRANSACTION"
app_1 | 2019-03-27T17:17:40+00:00 [debug] UPDATE item SET level = ?, hash = ? WHERE id = ?
app_1 | 2019-03-27T17:17:40+00:00 [debug] "COMMIT"
app_1 | 2019-03-27T17:17:40+00:00 [debug] Used PRIMARY for the previous query.
app_1 | [Wed Mar 27 17:17:40 2019] 172.19.0.1:60352 [200]: /update/1
```
38 changes: 38 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env php
<?php

use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;

set_time_limit(0);

require dirname(__DIR__).'/vendor/autoload.php';

if (!class_exists(Application::class)) {
throw new RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
}

$input = new ArgvInput();
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env);
}

if ($input->hasParameterOption('--no-debug', true)) {
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
}

require dirname(__DIR__).'/config/bootstrap.php';

if ($_SERVER['APP_DEBUG']) {
umask(0000);

if (class_exists(Debug::class)) {
Debug::enable();
}
}

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG'], dirname(__DIR__));
$application = new Application($kernel);
$application->run($input);
5 changes: 5 additions & 0 deletions bin/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

./bin/console doctrine:schema:update --force
cd app
./bin/console server:run 0.0.0.0:8000
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

wget https://getcomposer.org/download/2.5.5/composer.phar
chmod 777 composer.phar

docker-compose down -v
rm -rf ./master/data/*
rm -rf ./slave/data/*
Expand Down Expand Up @@ -32,3 +35,5 @@ start_slave_cmd+='"'
docker exec mysql_slave sh -c "$start_slave_cmd"

docker exec mysql_slave sh -c "export MYSQL_PWD=111; mysql -u root -e 'SHOW SLAVE STATUS \G'"

rm composer.phar
41 changes: 41 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"require": {
"doctrine/doctrine-bundle": "^1.10",
"doctrine/orm": "^2.6",
"symfony/dotenv": "^4.2",
"symfony/flex": "^1.2",
"symfony/framework-bundle": "^4.2",
"symfony/maker-bundle": "^1.11",
"symfony/monolog-bundle": "^3.8",
"symfony/web-server-bundle": "^4.2",
"symfony/yaml": "^4.2"
},
"autoload": {
"psr-4": {
"App\\": "src"
}
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true,
"platform": {
"php": "7.1.18"
},
"allow-plugins": {
"symfony/flex": false
}
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
}
}
Loading