how can i create a database on mysql docker service - mysql

I'm trying to run moodle phpunit on my gitlab ci server. Using gitlab-ci.yml file i'm creating a container with php 5.6 and mysql service.
# Services
services:
- mysql:latest
before_script:
- mysql -e 'CREATE DATABASE gitlab_ci_test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_bin;' ;
I'm getting ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) and not sure how to proceed.

Tomasz this is my gitlab-ci.yml file you will need something like this:
# Select image from https://hub.docker.com/r/_/php/
image: php:7.0.0
services:
- mysql:5.7
variables:
# Configure mysql environment variables (https://hub.docker.com/r/_/mysql/)
MYSQL_DATABASE: symfony
MYSQL_ROOT_PASSWORD: qwerty
# Composer stores all downloaded packages in the vendor/ directory.
# Do not use the following if the vendor/ directory is commited to
# your git repository.
cache:
paths:
- vendor/
before_script:
# Install dependencies
- bash ci/docker_install.sh > /dev/null
- cp ci/parameters.yml app/config/parameters.yml
- composer install
test:app:
script:
- phpunit
And this is my docker_install.sh inside ci folder
#!/bin/bash
# We need to install dependencies only for Docker
[[ ! -e /.dockerenv ]] && [[ ! -e /.dockerinit ]] && exit 0
set -xe
# Install git (the php image doesn't have it) which is required by composer
apt-get update -yqq
apt-get install git -yqq
apt-get install wget -yqq
apt-get install zip unzip -yqq
# Install composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
# Install phpunit, the tool that we will use for testing
curl -o /usr/local/bin/phpunit https://phar.phpunit.de/phpunit.phar
chmod +x /usr/local/bin/phpunit
# Install mysql driver
# Here you can install any other extension that you need
docker-php-ext-install pdo pdo_mysql mbstring

Related

An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory

i just have push my Symfony 5.4 application in my company server with Gitlab-ci and docker.
I have a Dokcerfile in the root avec my projet:
FROM php:7.4-apache
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
COPY . /var/www/html/
RUN apt-get update \
&& apt-get install -y --no-install-recommends locales apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev;
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
# INSTALL COMPOSER
RUN curl -sSk https://getcomposer.org/installer | php -- --disable-tls && \
mv composer.phar /usr/local/bin/composer
# INSTALL EXTENSION PHP
RUN docker-php-ext-configure intl
RUN docker-php-ext-install pdo pdo_mysql gd opcache intl zip calendar dom mbstring zip gd xsl
RUN pecl install apcu && docker-php-ext-enable apcu
# INSTALL YARN
RUN apt-get update && apt-get install -y gnupg2
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn
RUN apt update
RUN chown -R www-data:www-data /var/www/html
RUN a2enmod rewrite
RUN service apache2 restart
And i have this job on my gitlab pipeline:
deploy_image:
stage: deploy
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- cd /srv/docker/stacks/r-panda
- docker network disconnect --force r-panda_default traefik_app
- /usr/local/bin/docker-compose down
- /usr/local/bin/docker-compose pull
- /usr/local/bin/docker-compose up -d
- docker network connect r-panda_default traefik_app
after_script:
- docker exec r-panda_app yarn install
- docker exec r-panda_app yarn run build
- ls -al
environment:
name: dev
url: $SERVER_URL
variables:
DATABASE_URL: $DATABASE_URL
tags:
- deploy
And there is my docker-compose.yaml on the server who run the contener with the application:
version: '3.9'
services:
r-panda_app:
container_name: r-panda_app
image: panda
environment:
- DATABASE_URL="mysql://db_user:db_pass#bbd_server:3306/panda_bdd"
restart: always
labels:
- traefik.enable=true
- traefik.http.routers.router-r-panda.rule=Host(`r-panda.fr`)
- traefik.http.routers.router-r-panda.tls.certresolver=myresolver
- traefik.http.routers.router-r-panda.tls=true
With this config i have the following error: An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory
But if i change my config/packages/doctrine.yaml
This
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
url: '%env(resolve:DATABASE_URL)%'
To this:
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
url: 'the real path for the DATABASE_URL'
Everything works
I don't know what to do, can someone help me

Bitbucket pipeline import mysql schema

I'm trying to import database schema to mysql service through following statment
mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE < DB_Schema.sql
and it return mysql: not found. I have even tried the following command
docker exec -i mysql mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE < DB_Schema.sql
Even though received error + docker exec -i mysql mysql --user=$DB_USERNAME --password=$DB_PASSWORD 5i < DB_Schema.sql
Error: No such container: mysql
What would be the best way to use mysql so that I can import a stance of DB into it for testing purpose and how?
Please find the .yml file below.
# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# Specify a docker image from Docker Hub as your build environment.
# All of your pipeline scripts will be executed within this docker image.
image: php:8.0-fpm-alpine
# All of your Pipelines will be defined in the `pipelines` section.
# You can have any number of Pipelines, but they must all have unique
# names. The default Pipeline is simply named `default`.
pipelines:
default:
# Each Pipeline consists of one or more steps which each execute
# sequentially in separate docker containers.
# name: optional name for this step
# script: the commands you wish to execute in this step, in order
- parallel:
- step:
name: Installing Dependancies and Composer
caches:
- composer
script:
# Your Pipeline automatically contains a copy of your code in its working
# directory; however, the docker image may not be preconfigured with all
# of the PHP/Laravel extensions your project requires. You may need to install
# them yourself, as shown below.
- apt-get update && apt-get install -qy git curl libmcrypt-dev unzip libzip-dev libpng-dev zip git gnupg gnupg2 php-mysql
- docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg --with-webp && \
- docker-php-ext-install gd && \
- docker-php-ext-install exif && \
- docker-php-ext-install zip && \
- docker-php-ext-install pdo pdo_mysql
- rm -rf ./vendor
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install --ignore-platform-reqs
- composer dump-autoload
# Here we create link between the .env.pipelines file and the .env file
# so that our database can retreieve all the variables inside .env.pipelines
- ln -f -s .env.pipelines .env
artifacts:
- vendor/**
- step:
name: Installing and Running npm
image: node:16
caches:
- node
script:
- npm install -g grunt-cli
- npm install
- npm run dev
artifacts:
- node_modules/**
- step:
name: Running Test
deployment: local
script:
# Start up the php server so that we can test against it
- php artisan serve &
# # Give the server some time to start
- sleep 5
# - php artisan migrate
- docker ps
- docker container ls
- mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE < DB_Schema.sql
# - docker exec -i mysql mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD -e "SHOW DATABASES"
- php artisan optimize
- php artisan test
services:
- mysql
- docker
# You might want to create and access a service (like a database) as part
# of your Pipeline workflow. You can do so by defining it as a service here.
definitions:
services:
mysql:
image: mysql:latest
environment:
MYSQL_DATABASE: $DB_DATABASE
MYSQL_USER: $DB_USERNAME
MYSQL_PASSWORD: $DB_PASSWORD
MYSQL_ROOT_PASSWORD: $DB_PASSWORD
SERVICE_TAGS: mysql
SERVICE_NAME: mysql
You cannot install/update/change you main image in the first step for them to be there in the last step. Make your custom Docker image with all those installations, which will make it faster to run the pipeline and will let you use other tools you need in your pipeline.
I prefer to use the "mysql" client outside Docker and have it reach into the Docker container based on the port mapping set up. Then, conceptually, it is like reading to a "mysqld" server on a separate "server".
LOAD DATA INFILE and INSERT, including use of mysql ... < dump.sql works fine.

How to install mysql-server in a Dockerfile?

I have a complex Dockerfile which install much more than just mysql-server so I cannot start from an existing mysql container.
When removing all the extra-stuff I get this Dockerfile
FROM ubuntu:latest
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_ROOT_USER=root
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y mysql-server
RUN usermod -d /var/lib/mysql/ mysql
RUN service mysql start
Unfortunately, mysql does not want to start:
---> 57a66bd64c2c
Step 8/9 : RUN usermod -d /var/lib/mysql/ mysql
---> Running in 596df248c2e4
---> ee78442bcc56
Step 9/9 : RUN service mysql start
---> Running in 0d9e5803cf33
* Starting MySQL database server mysqld
...fail!
The command '/bin/sh -c service mysql start' returned a non-zero code: 1
What is my mistake?
Looks like you've removed the most important parts of your docker file. Here is the Official MySQL repo Docker file.
FROM oraclelinux:7-slim
ENV PACKAGE_URL https://repo.mysql.com/yum/mysql-8.0-community/docker/x86_64/mysql-community-server-minimal-8.0.2-0.1.dmr.el7.x86_64.rpm
# Install server
RUN rpmkeys --import http://repo.mysql.com/RPM-GPG-KEY-mysql \
&& yum install -y $PACKAGE_URL \
&& yum install -y libpwquality \
&& rm -rf /var/cache/yum/*
RUN mkdir /docker-entrypoint-initdb.d
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 3306 33060
CMD ["mysqld"]
You need to include a proper source with correct version to pull the image from.
and expose right ports, separate out volumes for MySQL to run. your container maybe failing due to any of this. I'd say remove the MySQL part out of your dockerfile and run the rest of the container.
Use the official mySQL image and install it in separate container. and then you can connect the Database with other apps.

Can't connect a go app to mysql (both inside a gitlab runner)

The go app simply inserts a harcoded value in a mysql table and spits it back out. This is done using this database driver. It works fine on linux servers, but during gitlab's CI this returns:
dial tcp 127.0.0.1:3306: connect: connection refused
This is the .gitlab-ci.yml
image: mysql
services:
- mysql:latest
variables:
MYSQL_DATABASE: storage
MYSQL_ROOT_PASSWORD: root
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_TRANSAPORT: tcp
MYSQL_ADDRESS: "127.0.0.1:3306"
job:
script:
- apt-get update -qq && apt-get install -qq curl && apt-get install -qq git
- echo "SHOW GLOBAL VARIABLES LIKE 'PORT';" | mysql --user="$MYSQL_USER" --password="$MYSQL_ROOT_PASSWORD" --host="$MYSQL_HOST" "$MYSQL_DATABASE"
- curl -O https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz
- tar -C /usr/local -xzf go1.10.1.linux-amd64.tar.gz
- rm go1.10.1.linux-amd64.tar.gz
- echo "export PATH=\$PATH:/usr/local/go/bin" >> ~/.bashrc
- echo "export GOPATH=\$HOME/go" >> ~/.bashrc
- echo "export PATH=\$PATH:\$GOPATH/bin" >> ~/.bashrc
- source ~/.bashrc
- go get github.com/go-sql-driver/mysql
- go build main.go
- ./main
Is there a standard way to use mysql from golang during CI?

Gitlab CI Runner | Custom mysql conf

I'm trying to set my own mysql conf for Gitlab CI Runner.
I found in the documentation how to set my own php.ini :
before_script:
- cp ci/php.ini /usr/local/etc/php/conf.d/test.ini
I didn't find informations about how setting my.cnf, I tried :
before_script:
- cp ci/my.cnf /usr/local/etc/mysql/conf.d/my.cnf
But /usr/local/etc/mysql/ doesn't exist in the generated environment.
This is all my gitlab.ci :
services:
- mysql:latest
variables:
# Configure mysql environment variables
WITH_XDEBUG: "1"
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: symfony
cache:
paths:
- vendor/
before_script:
# Install dependencies
- bash ci/docker_install.sh > /dev/null
- cp ci/php.ini /usr/local/etc/php/conf.d/test.ini
- cp ci/my.cnf /usr/local/etc/mysql/conf.d/my.cnf
- mv app/config/parameters.gitlab.yml app/config/parameters.yml
- mv app/config/config_test.gitlab.yml app/config/config_test.yml
- composer clear-cache
- composer install
- php bin/console doctrine:schema:update --force
- php bin/console doctrine:fixtures:load
test:
image: php:7.0
stage: test
script:
- echo "Running PHPUnit Tests"
- vendor/bin/phpunit --configuration phpunit.xml.dist --colors --debug --coverage-text
And my docker_install.sh :
#!/bin/bash
# We need to install dependencies only for Docker
[[ ! -e /.dockerenv ]] && [[ ! -e /.dockerinit ]] && exit 0
set -xe
# Install git (the php image doesn't have it) which is required by composer
apt-get update -yqq
apt-get install git -yqq
apt-get install wget -yqq
apt-get install zip unzip -yqq
# Install composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
# Install mysql driver
# Here you can install any other extension that you need
docker-php-ext-install pdo_mysql mbstring
Thanks.
I don't think it is possible to easily mount the config inside of the service container using the Gitlab CI config. It is probably easier to just create a Dockerfile that extends the image that you use for testing and set the the configs you need there.
You can find an example of such a small Dockerfile here, where the author copies a config file to set the encoding to utf8:
https://hub.docker.com/r/dnhsoft/mysql-utf8/dockerfile
Or you can simply append your configs for example like this if you don't want to write a Dockerfile that copies files:
FROM mysql:8.0
RUN echo '[mysqld]' >> /etc/mysql/conf.d/mysql.cnf
RUN echo 'default-authentication-plugin = mysql_native_password' >> /etc/mysql/conf.d/mysql.cnf
RUN echo 'collation-server = utf8mb4_general_ci' >> /etc/mysql/conf.d/mysql.cnf
RUN echo 'character-set-server = utf8mb4' >> /etc/mysql/conf.d/mysql.cnf
Once your Dockerfile has the configs you need you can publish it to Dockerhub under your username. Then in your gitlab-ci.yml you can use it like this:
services:
- {name: 'mydockerusername/mysql:1', alias: 'mysql'}
In your scripts in gitlab-ci.yml you can then connect to your custom database container like this:
script:
- 'mysql -u gitlabci -h mysql --password="gitlabci" -e "SHOW DATABASES"'