run mysql:5.5 docker image from ansible - mysql

How to run mysql: 5.5 from ansible? If I run it directly:
docker run -e MYSQL_ROOT_PASSWORD=pass mysql:5.5
it's work ok. But if I run from ansible:
- name: run database
local_action:
module: docker
image: mysql:5.5
state: running
it's start and immediately stop. Also post in case 1 is 3306/tcp, but in case 2 there is no port.

You need to specify mysql root password as environment variable for container. For example:
- hosts: ansible_host
gather_facts: False
sudo: yes
pre_tasks:
- name: install pip pkg.
yum:
name: python-pip
state: present
- name: install boto pkg.
pip:
name: docker-py
state: present
- name: docker
docker:
image: "mysql:5.5"
state: running
env: "MYSQL_ROOT_PASSWORD=my-secret-pw"

Related

Github action yml file and mysql

I'm having issues using mysql as the database for my tests that are running in a Github action. I'm using this as a guide.
I'm getting the following error:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'172.18.0.2' (using password: NO) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
here is my yaml file:
name: LaravelTest
on:
push:
branches: [ test ]
jobs:
laravel_tests:
runs-on: ubuntu-latest
container:
image: kirschbaumdevelopment/laravel-test-runner:8.1
services:
testdb:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test
MYSQL_ALLOW_EMPTY_PASSWORD: 1
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout#main
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install dependencies
run: npm install
- name: Compile assets
run: npm run dev
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
forge_deploy:
runs-on: ubuntu-latest
needs: laravel_tests
steps:
- name: Make Get Request
uses: satak/webrequest-action#master
with:
url: ${{ secrets.MOMENTUM_TEST_DEPLOY_URL }}
method: GET
UPDATE
I removed this line from my phpunit.xml file:
<env name="DB_HOST" value="testdb"/>
and now I'm getting a different error:
SQLSTATE[HY000] [2002] Connection refused (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
After doing some more googling, I found this post which made me go back and pull out the docker container and try to do it using the mysql service that's already available on ubuntu.
Once I was able to successfully start the service and create a database, I then realized that the steps in my action were copying the .env file, and if it wasn't there, it was copying the .env.example file, which I overlooked, and was getting odd results. Once I realized that it was looking in that file for database connection values, I was able to override them with the env option in the yaml file. So, I finally got it working, and this is my working yaml file for anyone that may run into this at some point:
name: LaravelTest
on:
push:
branches: [ test ]
jobs:
laravel_tests:
runs-on: ubuntu-20.04
env:
DB_CONNECTION: mysql
DB_HOST: localhost
DB_PORT: 3306
DB_DATABASE: testdb
DB_USERNAME: root
DB_PASSWORD: root
steps:
- name: Set up MySQL
run: |
sudo systemctl start mysql
mysql -e 'CREATE DATABASE testdb;' -uroot -proot
mysql -e 'SHOW DATABASES;' -uroot -proot
- uses: actions/checkout#main
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install dependencies
run: npm install
- name: Compile assets
run: npm run dev
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
forge_deploy:
runs-on: ubuntu-20.04
needs: laravel_tests
steps:
- name: Make Get Request
uses: satak/webrequest-action#master
with:
url: ${{ secrets.MOMENTUM_TEST_DEPLOY_URL }}
method: GET

Connect openshift pod to external mysql database

I am trying to set up a generic pod on OpenShift 4 that can connect to a mysql server running on a separate VM outside the OpenShift cluster (testing using local openshift crc). However when creating the deployment, I'm unable to connect to the mysql server from inside the pod (for testing purposes).
The following is the deployment that I use:
kind: "Service"
apiVersion: "v1"
metadata:
name: "mysql"
spec:
ports:
- name: "mysql"
protocol: "TCP"
port: 3306
targetPort: 3306
nodePort: 0
selector: {}
---
kind: "Endpoints"
apiVersion: "v1"
metadata:
name: "mysql"
subsets:
- addresses:
- ip: "***ip of host with mysql database on it***"
ports:
- port: 3306
name: "mysql"
---
apiVersion: v1
kind: DeploymentConfig
metadata:
name: "deployment"
spec:
template:
metadata:
labels:
name: "mysql"
spec:
containers:
- name: "test-mysql"
image: "***image repo with docker image that has mysql package installed***"
ports:
- containerPort: 3306
protocol: "TCP"
env:
- name: "MYSQL_USER"
value: "user"
- name: "MYSQL_PASSWORD"
value: "******"
- name: "MYSQL_DATABASE"
value: "mysql_db"
- name: "MYSQL_HOST"
value: "***ip of host with mysql database on it***"
- name: "MYSQL_PORT"
value: "3306"
I'm just using a generic image for testing purposes that has standard packages installed (net-tools, openjdk, etc.)
I'm testing by going into the deployed pod via the command:
$ oc rsh {{ deployed pod name }}
however when I try to run the following command, I cannot connect to the server running mysql-server
$ mysql --host **hostname** --port 3306 -u user -p
I get this error:
ERROR 2003 (HY000): Can't connect to MySQL server on '**hostname**:3306' (111)
I've also tried to create a route from the service and point to that as a "fqdn" alternative but still no luck.
If I try to ping the host (when inside the pod), I cannot reach it either. But I can reach the host from outside the pod, and from inside the pod, I can ping sites like google.com or github.com
For reference, the image being used is essentially the following dockerfile
FROM ubi:8.0
RUN dnf install -y python3 \
wget \
java-1.8.0-openjdk \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm \
postgresql-devel
WORKDIR /tmp
RUN wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm && \
rpm -ivh mysql-community-release-el7-5.noarch.rpm && \
dnf update -y && \
dnf install mysql -y && \
wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.48.tar.gz && \
tar zxvf mysql-connector-java-5.1.48.tar.gz && \
mkdir -p /usr/share/java/ && \
cp mysql-connector-java-5.1.48/mysql-connector-java-5.1.48-bin.jar /usr/share/java/mysql-connector-java.jar
RUN dnf install -y tcping \
iputils \
net-tools
I imagine there is something I am fundamentally misunderstanding about connecting to an external database from inside OpenShift, and/or my deployment configs need some adjustment somewhere. Any help would be greatly appreciated.
As mentioned in the conversation for the post, it looks to be a firewall issue. I've tested again with the same config, but instead of an external mysql db, I've tested via deploying mysql in openshift as well and the pods can connect. Since I don't have control of the firewall in the organisation, and the config didn't change between the two deployments, I'll mark this as solved as there isn't much more I can do to test it

GitHub Actions migration and seeding issue with Laravel

I am trying to implement a CI/CD structure with GitHub Actions to automatically run tests on my Laravel folder. To successfully execute my tests, there has to be a functional MySQL database that has to be seeded.
So far, the seeding isn't successful. I get the following error:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations and table_type = 'BASE TABLE')
Here is my laravel.yml:
name: Laravel CI
on:
push:
branches: [ test ]
jobs:
laravel-tests:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./laravel
services:
mysql:
image: mysql:latest
ports:
- 3306
env:
MYSQL_USER: laravel_user
MYSQL_PASSWORD: laravel_pass
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: testingdatabase
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: shivammathur/setup-php#15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.0'
extensions: mysql
- uses: actions/checkout#v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install dependencies
run: composer update --no-interaction --no-progress --prefer-dist --optimize-autoloader --ignore-platform-reqs
- name: Generate key
run: php artisan key:generate
- name: Configuration caching for speed
run: php artisan config:cache
- name: Route caching for speed
run: php artisan route:cache
- name: Installing npm dependencies
run: npm install
- name: Running npm prod
run: npm run prod
- name: Migrate & seed database
run: php artisan migrate --seed
- name: Setting up file storage
run: php artisan storage:link
- name: Serve application
run: php artisan serve &
- name: Run Feature tests
run: vender/bin/phpunit
How can I successfully run my migrations and seeding?
The issue was that I only declared environment variables at the mysql service, whereas I also should have declared them when attempting to migrate and seed my database:
on:
push:
paths:
- './laravel'
name: Laravel CI
jobs:
phpunit:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./laravel
services:
mysql:
image: mysql:5.7
env:
MYSQL_USER: user
MYSQL_PASSWORD: secret
MYSQL_DATABASE: testdatabase
MYSQL_ROOT_PASSWORD: root
DB_PORT: ${{ job.services.mysql.ports[3306] }}
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout#v1
with:
fetch-depth: 1
- name: Verify MySQL connection
run: |
mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -uuser -psecret -e "SHOW DATABASES"
- name: Install dependencies
run: |
php --version
composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
chmod -R 777 storage bootstrap/cache
- name: Boot Laravel application
run: |
php -r "file_exists('.env') || copy('.env.example', '.env');"
php artisan key:generate
php artisan --version
- name: Execute PHPUnit tests
env:
DB_CONNECTION: mysql
DB_DATABASE: testdatabase
DB_PORT: 33306
DB_USER: root
DB_PASSWORD: secret
run: |
php artisan migrate:fresh --seed
./vendor/bin/phpunit

docker-compose MySQL container for automated testing with GithubActions

I have GitHub repo and I want to use GithubActions to automatically execute unit Tests with every pull request.
I already set up a workflow file:
name: CI
on:
pull_request:
branches: [ master ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
- name: Install
run: npm ci
- name: Linter
run: npm run lint
- name: Build
run: npm run build
- name: Docker
run: docker-compose up -d
- name: Wait / Sleep
uses: jakejarvis/wait-action#v0.1.0
with:
time: '10s'
- run: |
docker ps
cat ./dumps/backup.sql | docker exec -i mysql-development_1 /usr/bin/mysql -u root --password=password
- name: Test
run: npm test
As I need to insert the tables first, I want to insert a dump which does work on my machine with the exact same command used here.
However, the Action fails with this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
read unix #->/var/run/docker.sock: read: connection reset by peer
cat: write error: Broken pipe
##[error]Process completed with exit code 1.
How can I access the database within GithubActions?
docker-compose.yml:
version: '3'
services:
mysql-development:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test_db
ports:
- "3308:3306"
To anyone who might run into the same problem:
https://github.blog/changelog/2020-02-21-github-actions-breaking-change-ubuntu-virtual-environments-will-no-longer-start-the-mysql-service-automatically/
You just need to start the mysql-service manually and perhaps wait for a couple of seconds

Ansible Yum Module is not working correctly

I am creating an ansible script to automate an LDAP configuration. However, when I do a test run on the script, I always get:
ERROR: yum is not a legal parameter of an Ansible Play
I am a bit rusty with using ansible, but I am pretty sure I got this correct (syntactically):
---
#Kicks off the installation of Tomcat and MySQL
- name: Connecting to Anssible_centos
hosts: ansible_centos
remote_user: root
- name: Retreiving MySQL RPM and Installing
yum: name=http://dev.sql.com/get/mysql157-community-release-e16-7.noarch.rpm state=present
- debug: var=outputmySql
- name: Disabling MySql57-Community
yum: disablerepo=mysql57-community
- debug: var=outputDisable
- name: Enabling Mysql56-Community
yum: enablerepo=mysql56-community
- debug: var=outputEnable
- name: Installing mySql 5.6
yum: name="mysql-community-server" state=present
- debug: var=install56
- name: Starting MySql 5.6
service: name=mysqld state=started
- debug: var=serviceStart
- name: Update MySql root password
mysql_user: name=root host=127.0.0.1 password=codiscope
- debug: var=rootmysql
Any ideas?
Your syntax is actually a bit off. Your playbook should look more like this:
- name: Connecting to Anssible_centos
hosts: ansible_centos
remote_user: root
tasks:
- name: Retreiving MySQL RPM and Installing
yum: name=http://dev.sql.com/get/mysql157-community-release-e16-7.noarch.rpm state=present
And if you want to view the results of each task then you want do do something more akin to this:
- name: Retreiving MySQL RPM and Installing
yum: name=http://dev.sql.com/get/mysql157-community-release-e16-7.noarch.rpm state=present
register: outputmySql
- debug: var=outputmySql