Skip to content

Commit 9572d8d

Browse files
committed
Fix MySQL default connectivity
Dockerfile - install 'default-mysql-client' PHP extension so we can log into MySQL from the PHP container if we want to interact with the database container from there. - install and enable the pdo_mysql PHP extension with 'docker-php-ext-install pdo_mysql' and 'docker-php-ext-enable pdo_mysql'. This is required for Laravel docker-compose.yml - MYSQL_USER and MYSQL_PASSWORD don't seem to do much as the user 'myuser' does not seem to be created. For now, we'll use root + rootpassword /src/.env.example - set all the default MySQL connectivity and login information so it works out of the box - add a default MONGODB_URI for people to replace with their ATLAS connection string config\database.php - change DB_URI to the standard MONGODB_URI we use in our drivers and everything else \routes\api.php - added a \api\test_mysql\ route to check if the MySQL connection works. It's a VERY basic test and does not check for the table, but we can improve this later. The table is created by our Docker config
1 parent 1d3c2d0 commit 9572d8d

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

.devcontainer/.docker/php/Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ RUN touch /var/log/php-errors.log && chown www-data:www-data /var/log/php-errors
1212

1313
# Update the packages
1414
# Install system packages required for MongoDB extension
15+
# 'mysql-client' so we can log into mysql from the PHP container with the command 'mysql -h mysql -u root -p' where mysql is the service name
1516
RUN apt-get update \
16-
&& apt-get install -y libssl-dev wget git unzip
17+
&& apt-get install -y libssl-dev wget git unzip default-mysql-client
1718

1819
RUN pecl apt update \
1920
&& apt install libzip-dev -y \
2021
&& docker-php-ext-install zip \
2122
&& rm -rf /var/lib/apt/lists/*
2223

2324
# Required for MySQL to work in PHP
24-
RUN docker-php-ext-install mysqli
25+
RUN docker-php-ext-install mysqli && \
26+
docker-php-ext-install pdo_mysql
2527

2628
# Test if already installed and
2729
# install the <latest> mongodb PHP extension
@@ -38,7 +40,8 @@ RUN bash -c '[[ -n "$(pecl list | grep xdebug)" ]]\
3840
# install Redis PHP driver
3941
RUN pecl install -o -f redis \
4042
&& rm -rf /tmp/pear \
41-
&& docker-php-ext-enable redis
43+
&& docker-php-ext-enable redis \
44+
&& docker-php-ext-enable pdo_mysql
4245

4346
# Task: copy rep's PHP .ini files to be automatically parsed
4447
#

.devcontainer/docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ services:
4343
# we don't need to expose the port 9003 here for Xdebug because the
4444
# connection comes from inside the PHP container to the IDE via port 9003
4545

46-
4746
# MySQL Service
4847
mysql:
4948
image: ${MYSQL_IMAGE}
5049
environment:
5150
MYSQL_ROOT_PASSWORD: rootpassword
5251
MYSQL_DATABASE: mydatabase
53-
MYSQL_USER: rootuser
52+
MYSQL_USER: myuser
5453
MYSQL_PASSWORD: mypassword
5554
volumes:
5655
# map local /data/ folder to container /var/lib/mysql for MySQL data persistence

src/.env.example

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
MONGODB_URI=mongodb://127.0.0.1:27017/ # replace with your live MongoDB Atlas cluster!
2+
13
APP_NAME=Laravel
24
APP_ENV=local
35
APP_KEY=
@@ -8,12 +10,12 @@ LOG_CHANNEL=stack
810
LOG_DEPRECATIONS_CHANNEL=null
911
LOG_LEVEL=debug
1012

11-
DB_CONNECTION=mysql
12-
DB_HOST=127.0.0.1
13+
DB_CONNECTION=mysql # this 'mysql' is the Laravel connection name
14+
DB_HOST=mysql # this 'mysql' refers to the Docker environment's 'mysql' service in lieu of the IP. It is defined in the docker-compose.yml file
1315
DB_PORT=3306
14-
DB_DATABASE=laravel
15-
DB_USERNAME=root
16-
DB_PASSWORD=
16+
DB_DATABASE=mydatabase # arbitrary database name for this project
17+
DB_USERNAME=root # our docker MySQL root username and password, defined in docker-compose.yml
18+
DB_PASSWORD=rootpassword
1719

1820
BROADCAST_DRIVER=log
1921
CACHE_DRIVER=file

src/config/database.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737

3838
'mongodb' => [
3939
'driver' => 'mongodb',
40-
'dsn' => env('DB_URI'),
40+
'dsn' => env('MONGODB_URI'),
4141
'database' => 'bigsearch', // replace 'bigsearch' with your database name
4242
],
4343

4444
'mongodb_mflix' => [
4545
'driver' => 'mongodb',
46-
'dsn' => env('DB_URI'),
46+
'dsn' => env('MONGODB_URI'),
4747
'database' => 'sample_mflix',
4848
],
4949

@@ -157,4 +157,4 @@
157157

158158
],
159159

160-
];
160+
];

src/routes/api.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,17 @@
263263
$result = DB::connection('mongodb')->getCollection('laracoll')->createIndex($indexKeys, $indexOptions);
264264

265265
return ['status' => 'executed', 'data' => $result ];
266+
});
267+
268+
269+
/*
270+
Laravel check on the MySQL connection
271+
*/
272+
Route::get('/test_mysql/', function (Request $request) {
273+
try {
274+
DB::connection()->getPdo();
275+
return ['status' => 'executed', 'data' => 'Successfully connected to the DB.' ];
276+
} catch (\Exception $e) {
277+
return ['status' => 'FAIL. exception', 'data' => $e ];
278+
}
266279
});

0 commit comments

Comments
 (0)