I am trying to install MySQL version 8.0.21 using ansible. I have downloaded the required tar file from the MySQL community and tried to install using dpkg command but found issues while passing passwords.
- name: Set MySQL root password before installing
debconf: name='mysql-server' question='mysql-server/root_password' value='{{MySQL_root_pass | quote}}' vtype='password'
become: yes
- name: Confirm MySQL root password before installing
debconf: name='mysql-server' question='mysql-server/root_password_again' value='{{MySQL_root_pass | quote}}' vtype='password'
become: yes
- name: Install my_package
apt: deb="required_package.deb"
become: true
become_method: sudo
This is my YAML.The mysql version is installed with an empty password
What about the role from geerlingguy
ansible-galaxy install geerlingguy.mysql
After installing you yml like this:
- hosts: YOUR_SERVER_IP_HERE
become: yes
vars_files:
- vars/main.yml
roles:
- { role: geerlingguy.mysql }
pre_tasks:
- name: Install the MySQL repo.
yum:
name: http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
state: present
when: ansible_os_family == "RedHat"
- name: Override variables for MySQL (RedHat).
set_fact:
mysql_daemon: mysqld
mysql_packages: ['mysql-server']
mysql_log_error: /var/lib/mysql/error.log
mysql_syslog_tag: mysqld
mysql_pid_file: /var/run/mysqld/mysqld.pid
mysql_socket: /var/lib/mysql/mysql.sock
when: ansible_os_family == "RedHat"
Make sure to updat the vars/main.yml file including the version you wish!
Related
I am trying to get a coverage report in GitHub Actions
but when I run the pipeline I gives me this error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock
Then I search around add added the sudo mysql start service and now I get this error, but I don't know were or have to write mit -root -password and the -host?
Access denied for user 'root'#'localhost' (using password: YES)")
how do I do that?
name: Django CI
on:
push:
branches: [ unittests ]
paths-ignore: '**/SkoleProtocol/attendanceCode/tests/test_selenium.py'
pull_request:
branches: [ unittests ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.7, 3.8, 3.9]
steps:
- uses: actions/checkout#v2
- name: Set up Python 3.7
uses: actions/setup-python#v2
with:
python-version: 3.7
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Create test database
run: |
sudo service mysql start
- name: Coverage report
run: |
pip install coverage
coverage run manage.py test
coverage report
- name: Lint with flake8
run: |
pip install flake8
flake8 ./attendanceCode --exit-zero # Exit with status code "0" even if there are errors.
- name: Django Tests
run: |
python3 manage.py test
Two things that stand out.
Do you happen to have the credentials in an env file else where?
- name: Create test database
run: |
sudo service mysql start
You just happen to start the service rather than creating the database.
Try:
sudo /etc/init.d/mysql start // To start the service
mysql -uroot -proot -e "CREATE DATABASE __dbname__;"
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
It's me again...
I'm learning ansible. I want to deploy a Wordpress server, so I've install mysql correctly. But durring the creation of the Database I've received the following error :
[vm1devops]: FAILED! => {"changed": false, "msg": "unable to find
/home/ERTYerty1234/.my.cnf. Exception message: (1045, \"Access denied
for user 'ERTYerty1234'#'localhost' (using password: NO)\")"}
So I've add the following line :
sudo: True
The database create corretcly, but now when I want to acces on my VM. the Wordpress website can't access the Database. Any idea why ?
Here is how is configure MySQL on my files :
mysql/defaults/main.yml
wp_mysql_db: wordpress
wp_mysql_user: ERTYerty1234
wp_mysql_password: arandompassword
mysql/tasks/main.yml
- name: Create mysql database
mysql_db: name={{ wp_mysql_db }} state=present
#EDIT
- name: Create mysql user
mysql_user:
login_user:{{ wp_mysql_user }}
login_password:{{ wp_mysql_password }}
host:localhost
append_privs:true
priv:'*.*:REQUIRESSL'
host:%
name={{ wp_mysql_user }}
password={{ wp_mysql_password }}
And here are the roles installed on the server :
server/tasks/main.yml
- name: Update apt cache
apt: update_cache=yes cache_valid_time=3600
sudo: yes
- name: Install required software
apt: name={{ item }} state=present
sudo: yes
with_items:
- apache2
- mysql-server
- php7.0-mysql
- php7.0
- libapache2-mod-php7.0
- php7.0-mcrypt
- python-mysqldb
If someone have an idea here. It could be great. THanks a lot.
I'm trying to install Mysql-5.7 on ubuntu 14.04 which by default has no has no 5.7 package in its repositories.
So I try to add it manually with this little role:
---
- name: download the mysql deb package
get_url: url=http://dev.mysql.com/get/{{ deb_filename }} dest=/tmp/{{ deb_filename }} mode=0644
tags: apt
- name: install the package
sudo: true
apt: deb=/tmp/{{ deb_filename }}
tags: apt
- name: update apt cache
sudo: true
apt: update_cache=yes
tags: apt
Where deb_filename is the latest one “mysql-apt-config_0.8.7-1_all.deb”.
When I run a playbook with this role everything seems to run smoothly. No errors.
But then when it comes to a real installation of a package it says that:
msg: No package matching 'mysql-client-5.7' is available
And when I login to the server and run “apt-cache search mysql-client” I see no mysql-5.7.
How can I add this package using ansible to my server?
Thanks everyone!
My Ansible script is installing Mysql version 5.4, but I need it to install 5.7.
This is the code:
- name: mysql | Install MySQL Packages
sudo: yes
apt: pkg={{ item }} state=latest
with_items:
- mysql-server
- mysql-client
- python-mysqldb
I have tried this, but it doesn't work:
- name: mysql | Install MySQL Packages
sudo: yes
apt: pkg={{ item }} state=latest
with_items:
- mysql-server-5.7
- mysql-client-5.7
- python-mysqldb
Anyone know how to get Ansible to install Mysql 5.7?
I would expect 5.4 is the latest version available per apt. Unless you're saying you can install MySQL 5.7 with apt manually on the command line, this is not an Ansible issue.
You can try to activate the update_cache option.
- name: mysql | Install MySQL Packages
sudo: yes
apt:
pkg: "{{ item }}"
state: latest
update_cache: yes
with_items:
- mysql-server
- mysql-client
- python-mysqldb
Also you could try to add the MySQL apt repository with the apt_repository module before installing MySQL. Something like this:
- apt_repository:
repo: 'deb http://repo.mysql.com/apt/debian/ precise mysql-5.7'
state: present