Accessing mysql from build commands in bitbucket pipelines - mysql

I'm getting ERROR 2002 (HY000): Can't connect to local MySQL when trying to execute a mysql command during my CI process.
Here is my bitbucket-pipelines.yml file
image: theotherperson/php-ci:5.6
pipelines:
default:
- step:
caches:
- composer
script:
- apt-get update && apt-get install -y unzip mysql-client
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install --no-scripts --no-plugins
- cp test-assets/vhosts/000-default.conf /etc/apache2/sites-enabled/000-default.conf
- cp test-assets/hosts/hosts /etc/hosts
- rm /var/www/html/index.html
- cp -R $BITBUCKET_CLONE_DIR /var/www/html
- service apache2 restart
- mysql -u root -p$MYSQL_ROOT_PASSWORD -e "test < $BITBUCKET_CLONE_DIR/data/test/test.sql"
- phantomjs --webdriver=4444 &
- vendor/bin/behat -p test_behat
services:
- mysql
definitions:
services:
mysql:
image: mysql
environment:
MYSQL_DATABASE: 'test'
MYSQL_ROOT_PASSWORD: 'mypassword'
And here is the error:
+ mysql -u root -p$MYSQL_ROOT_PASSWORD -e "test < $BITBUCKET_CLONE_DIR/data/test/test.sql"
Enter password: ERROR 2002 (HY000): Can't connect to local MySQL
What do I need to do to be able to access mysql from this command line?

look at their documentation
Host name: 127.0.0.1 (avoid using localhost, as some clients will attempt to connect via a local "Unix socket", which will not work in Pipelines)

Related

Connect from a docker container to a mysql database in another container

I would like to access mariadb in the db container from the app container with the following folder configuration, but it does not work with an error.
directory structure
.
├── app
│ └── Dockerfile
├── db
│ └── Dockerfile
└── docker-compose.yml
Code
app/Dockerfile
FROM debian:buster
RUN set -x \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
mariadb-client \
vim
CMD [ "tail", "-f" ]
db/Dockerfile
FROM debian:buster
RUN set -x \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
mariadb-server \
mariadb-client \
vim
CMD service mysql start \
&& tail -f /dev/null
docker-compose.yml
version: "3.9"
services:
app:
build: ./app
networks:
- frontend
db:
build: ./db
volumes:
- db_data:/var/lib/mysql
expose:
- 3306
networks:
- frontend
networks:
frontend:
driver: bridge
volumes:
db_data: {}
Execution Commands
I entered the app container and executed the following commands, but I get an error.
root#0e0ad0889639:/# mysql -h db -uroot
ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)
root#0e0ad0889639:/# ping db
PING db (192.168.128.3) 56(84) bytes of data.
64 bytes from test_db_1.test_frontend (192.168.128.3): icmp_seq=1 ttl=64 time=0.104 ms
64 bytes from test_db_1.test_frontend (192.168.128.3): icmp_seq=2 ttl=64 time=0.142 ms
How can I connect?
Environment
❯ docker -v
Docker version 20.10.12, build e91ed57
❯ docker-compose -v
docker-compose version 1.29.2, build 5becea4c
❯ sw_vers
ProductName: macOS
ProductVersion: 12.3
BuildVersion: 21E230
The following method in db container solved the problem.
database setting
USE mysql;
GRANT ALL ON *.* TO 'root'#'%' identified by 'pass' WITH GRANT OPTION ;
GRANT ALL ON *.* TO 'root'#'localhost' identified by 'pass' WITH GRANT OPTION ;
FLUSH PRIVILEGES ;
mysql config file setting
echo "bind-address = app" >> etc/mysql/mariadb.conf.d/50-server.cnf
As pointed out by comments, the default Debian MariaDB configures a bind-address to localhost. This is why the Docker Library mariadb image removes those configuration items.
If all you want is a vim installed in your container maybe the following to gain all the benefits of a maintained and tested container image:
db/Dockerfile
FROM mariadb:10.6
ENV MARIADB_ROOT_PASSWORD=pass
RUN set -x \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
vim; \
rm -rf /var/lib/apt/lists/*
note small editors exist if you are interested.

Gitlab-CI : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock'

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
Hi guys !
I have a little (big) issue with my gitlab-ci.yml :(
I'm trying to run my tests PHPUnit on the pipeline but when I want to create a mysql database and to connect it, it fails.
This is my .gitlab-ci.yml :
image: jakzal/phpqa:php8.0
before_script:
- composer install
stages:
- SecurityChecker
- UnitTests
cache:
paths:
- vendor/
- ~/.composer/cache/files
#On teste qu'il n'y a pas de faille de sécurité dans les différentes librairies
security-checker:
stage: SecurityChecker
script:
- local-php-security-checker composer.lock
allow_failure: false
#On lance les tests unitaires PHP UNIT
phpunit:
stage: UnitTests
services:
- name: mysql:latest
before_script:
- apt-get update && apt-get dist-upgrade && apt-get install -y git libzip-dev autoconf build-essential pkg-config libmariadb-dev-compat libmariadb-dev default-mysql-client mariadb-client mariadb-server
- curl -sSk https://getcomposer.org/installer | php -- --disable-tls && mv composer.phar /usr/local/bin/composer
- docker-php-ext-install mysqli pdo pdo_mysql zip
# Connect MySQL
- echo "SET PASSWORD FOR 'root'#'localhost' = PASSWORD('password');" | mysql -u root
- echo "SELECT 'OK';" | mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -h mysql "$MYSQL_DATABASE"
# Import BDD tests
- mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -h localhost < "database.sql"
script:
- vendor/bin/phpunit --testdox
allow_failure: false
This is the result :
Pipeline error
Do you have any ideas fo my issue ?
I didn't found anything on the different forums for now... :sleepy:
Thanks a lot !
Take a look here: https://docs.gitlab.com/ee/ci/services/
I was able to get it work like this:
stages:
- test
services:
- mariadb
test:
stage: test
image: mariadb
variables:
MARIADB_ROOT_PASSWORD: password
MARIADB_DATABASE: dbname
script:
- mysql --user=root --password="$MARIADB_ROOT_PASSWORD" --host=mariadb
Might you have to change -h localhost to -h mysql.
The answer to my issue is this code !
It works for me :)
#On lance les tests unitaires PHP UNIT
phpunit:
stage: UnitTests
services:
- name: mysql:8.0.11
variables:
MYSQL_DATABASE: 'database-name'
MYSQL_ROOT_PASSWORD: ''
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
DB_USERNAME: 'runner'
DB_PASSWORD: 'password'
DB_HOST: 'mysql'
before_script:
# Install packages and docker
- apt-get update -y && apt-get dist-upgrade -y && apt-get install -y git libzip-dev autoconf curl libmcrypt-dev build-essential pkg-config libmariadb-dev-compat libmariadb-dev default-mysql-client mariadb-client mariadb-server
- docker-php-ext-install mysqli pdo pdo_mysql zip
# Config MySQL
- rm -f /tmp/mysql.sock.lock
- sleep 10
# Import BDD for tests units
- mysql -u root -h "$DB_HOST" < "web/files/database-test.sql"
# Check BDD
- echo "SHOW tables;" | mysql -u root -h "$DB_HOST" -D "$MYSQL_DATABASE"
script:
- vendor/bin/phpunit --testdox
allow_failure: false

How to setup connection to MySql database with gitlab CI/CD

I'm trying to set up automatic testing of django project using CI/CD gitlab. The problem is, I can't connect to the Mysql database in any way.
gitlab-ci.yml
services:
- mysql:5.7
variables:
MYSQL_DATABASE: "db_name"
MYSQL_ROOT_PASSWORD: "dbpass"
MYSQL_USER: "username"
MYSQL_PASSWORD: "dbpass"
stages:
- test
test:
stage: test
before_script:
- apt update -qy && apt-get install -qqy --no-install-recommends default-mysql-client
- mysql --user=$MYSQL_USER --password=$MYSQL_PASSWORD --database=$MYSQL_DATABASE --host=$MYSQL_HOST --execute="SHOW DATABASES; ALTER USER '$MYSQL_USER'#'%' IDENTIFIED WITH mysql_native_password BY '$MYSQL_PASSWORD'"
script:
- apt update -qy
- apt install python3 python3-pip virtualenvwrapper -qy
- virtualenv --python=python3 venv/
- source venv/bin/activate
- pwd
- pip install -r requirement.txt
- python manage.py test apps
With this file configuration, I get error
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
What have I tried to do
add to mysql script tcp connection unstead socket
mysql --protocol=TCP --user=$MYSQL_USER --password=$MYSQL_PASSWORD --database=$MYSQL_DATABASE --host=$MYSQL_HOST --execute="SHOW DATABASES; ALTER USER '$MYSQL_USER'#'%' IDENTIFIED WITH mysql_native_password BY '$MYSQL_PASSWORD'"
And in this case I got
ERROR 2002 (HY000): Can't connect to MySQL server on 'localhost' (99)
How do I set up properly?
There can be multiple reasons for your issue:
Incorrect MySQL version.
Solution: Use mysql:5.7 instead of mysql:latest
MySQL host is missing.
Solution: add MYSQL_HOST in the variables with the hostname of the MySQL server. (Should be mysql when using mysql:5.7 in services key)
Django uses different credentials of DB.
Solution: check that the credentials in the variables section of your .gitlab-ci.yml and compare against Django's settings.py. They should be the same.
MySQL client not installed.
Solution: install the mysql-client in the script section and check if it is able to connect.
Here is a sample script that installs MySQL client and connects to the database in a debian based image (or a python:latest image):
script:
- apt-get update && apt-get install -y git curl libmcrypt-dev default-mysql-
- mysql --version
- sleep 20
- echo "SHOW tables;"| mysql -u root -p"$MYSQL_ROOT_PASSWORD" -h "${MYSQL_HOST}" "${MYSQL_DATABASE}"
Here is a complete and valid example of using MySQL 5.7 as a service and a python image with mysql-client installed successfully connecting to the MySQL database:
stages:
- test
variables:
MYSQL_DATABASE: "db_name"
MYSQL_ROOT_PASSWORD: "dbpass"
MYSQL_USER: "username"
MYSQL_PASSWORD: "dbpass"
MYSQL_HOST: mysql
test:
image: python:latest
stage: test
services:
- mysql:5.7
script:
- apt-get update && apt-get install -y git curl libmcrypt-dev default-mysql-client
- mysql --version
- sleep 20
- echo "SHOW tables;" | mysql -u root -p"$MYSQL_ROOT_PASSWORD" -h "${MYSQL_HOST}" "${MYSQL_DATABASE}"
- echo "Database host is '${MYSQL_HOST}'"
You need to use service name as database hostname. In this case MYSQL_HOST should be mysql.
You can see example on Gitlab page and read about how services are linked to the job
I see there is an accepted answer but with mysql 8.0 and python3:buster some things broke. The Python Debian images ship with mariadb and it is not easy to set up the standard mysql-client packages, resulting in the error:
"django.db.utils.OperationalError: 2059, “Authentication plugin..."
I got a working YAML below, using Ubuntu as the base image and mysql 8.0 as a service. You could either use the root user in both the .gitlab-ci and the test_settings or give the MYSQL user the privileges to create new databases and alter existing ones.
The initial MYSQL_DB _USER and _PASS variables can be set in Gitlab under Settings -> CI/CD -> Variables.
.gitlab-ci.yml:
variables:
# "When using a service (e.g. mysql) in the GitLab CI that needs environtment variables
# to run, only variables defined in .gitlab-ci.yml are passed to the service and
# variables defined in GitLab GUI are unavailable."
# https://gitlab.com/gitlab-org/gitlab/-/issues/30178
# DJANGO_CONFIG: "test"
MYSQL_DATABASE: $MYSQL_DB
MYSQL_ROOT_PASSWORD: $MYSQL_PASS
MYSQL_USER: $MYSQL_USER
MYSQL_PASSWORD: $MYSQL_PASS
# -- In your django settings file for the test environment you could put:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': os.environ.get('MYSQL_DATABASE'),
# 'USER': os.environ.get('MYSQL_USER'),
# 'PASSWORD': os.environ.get('MYSQL_PASSWORD'),
# 'HOST': 'mysql',
# 'PORT': '3306',
# 'CONN_MAX_AGE':60,
# },
# }
# -- You could us '--settings' to specify a custom settings file on the command line
# -- below or use an environment variable to trigger an include in your settings:
# if os.environ.get('DJANGO_CONFIG')=='test':
# from .settings_test import * # or specific overrides
#
default:
image: ubuntu:20.04
# -- Pick zero or more services to be used on all builds.
# -- Only needed when using a docker container to run your tests in.
# -- Check out: http://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-a-service
services:
- mysql:8.0
# This folder is cached between builds
# http://docs.gitlab.com/ee/ci/yaml/README.html#cache
# cache:
# paths:
# - ~/.cache/pip/
before_script:
- echo -e "Using Database $MYSQL_DB with $MYSQL_USER"
- apt --assume-yes update
- apt --assume-yes install apt-utils
- apt --assume-yes install net-tools python3.8 python3-pip mysql-client libmysqlclient-dev
# - apt --assume-yes upgrade
- pip3 install -r requirements.txt
djangotests:
script:
# -- The MYSQL user gets only permissions for MYSQL_DB and therefor cant create a test_db.
- echo "GRANT ALL on *.* to '${MYSQL_USER}';"| mysql -u root --password="${MYSQL_ROOT_PASSWORD}" -h mysql
# -- use python3 explicitly. see https://wiki.ubuntu.com/Python/3
- python3 manage.py test
migrations:
script:
- python3 manage.py makemigrations
- python3 manage.py makemigrations myapp
- python3 manage.py migrate
- python3 manage.py check

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?

TravisCI - Is it possible to start docker service and expect mysql to work?

I am new to using Travis CI and i am trying to use the MySQL database along with a docker container. I am able to start docker service and run my container or I am able to connect to mysql. But I am not able to do both.
MySQL - Works
language: bash
sudo: required
before_script:
- "mysql -e 'create database atg_core;'"
- "mysql -e 'create database atg_ca;'"
script:
- "mysql -e 'show databases;'"
Docker - Works
sudo: required
language: bash
services:
- docker
install:
- docker pull asnagaraj/oracle-atg-11.1:v4
script:
- docker run -v $TRAVIS_BUILD_DIR:/workspace/test-atg-module asnagaraj/oracle-atg-11.1:v4 /bin/bash -c ". ~/.bash_profile; cd /workspace/test-atg-module; gradle --stacktrace gATGM; "
Combined - Doesnt Work
sudo: required
language: bash
services:
- docker
install:
- docker pull asnagaraj/oracle-atg-11.1:v4
before_script:
- "mysql -e 'create database atg_core;'"
- "mysql -e 'create database atg_ca;'"
script:
- docker run --net="host" -v $TRAVIS_BUILD_DIR:/workspace/test-atg-module asnagaraj/oracle-atg-11.1:v4 /bin/bash -c ". ~/.bash_profile; cd /workspace/test-atg-module; gradle --stacktrace gATGM; telnet localhost 3306; telnet 127.0.0.1 3306"
I am not even expecting the telnet to work, even the mysql -e create commands fail..
Please help.
-Naga
Under the services section of you travis.yml file you will have to let travis know that you are using mysql as such:
services:
- mysql
as taken from https://docs.travis-ci.com/user/database-setup/#MySQL
So your services section will become:
services:
-docker
-mysql