From b648c25dd1973fd7d51bff6ded51d7280fd7f6aa Mon Sep 17 00:00:00 2001 From: mkocka Date: Jun 08 2017 09:29:44 +0000 Subject: merging late master with f26 --- diff --git a/mariadb.yaml b/mariadb.yaml index 4633e53..2182320 100644 --- a/mariadb.yaml +++ b/mariadb.yaml @@ -28,9 +28,11 @@ data: default: rpms: - mariadb + - mariadb-server api: rpms: - mariadb + - mariadb-server components: rpms: mariadb: @@ -62,7 +64,7 @@ data: ref: f26 buildorder: 3 systemtap: - rationale: MariaDB package. + rationale: A dependency of mariadb. ref: f26 buildorder: 5 compat-openssl10: @@ -89,9 +91,3 @@ data: rationale: A dependency of mariadb. ref: f26 buildorder: 9 - coreutils: -# Probably not needed - rationale: A dependency of mariadb. - ref: f26 - buildorder: 9 - diff --git a/root/etc/my.cnf b/root/etc/my.cnf new file mode 100644 index 0000000..0844075 --- /dev/null +++ b/root/etc/my.cnf @@ -0,0 +1,12 @@ +[mysqld] + +# Disabling symbolic-links is recommended to prevent assorted security risks +symbolic-links = 0 + +# http://www.percona.com/blog/2008/05/31/dns-achilles-heel-mysql-installation/ +skip_name_resolve + +# http://www.chriscalender.com/ignoring-the-lostfound-directory-in-your-datadir/ +ignore-db-dir=lost+found + +!includedir /etc/my.cnf.d diff --git a/root/help.1 b/root/help.1 new file mode 100644 index 0000000..59c37b8 --- /dev/null +++ b/root/help.1 @@ -0,0 +1,332 @@ +.\"t +.\" WARNING: Do not edit this file manually, it is generated from README.md automatically. +.\" +.\"t +.\" Automatically generated by Pandoc 1.16.0.2 +.\" +.TH "MARIADB\-101\-RHEL7" "1" "February 22, 2017" "Container Image Pages" "" +.hy +.SH MariaDB Docker image +.PP +This container image includes MariaDB server 10.1 for OpenShift and +general usage. +Users can choose between RHEL and CentOS based images. +.PP +Dockerfile for CentOS is called Dockerfile, Dockerfile for RHEL is +called Dockerfile.rhel7. +.SS Environment variables and volumes +.PP +The image recognizes the following environment variables that you can +set during initialization by passing \f[C]\-e\ VAR=VALUE\f[] to the +Docker run command. +.PP +.TS +tab(@); +l l. +T{ +Variable name +T}@T{ +Description +T} +_ +T{ +\f[C]MYSQL_USER\f[] +T}@T{ +User name for MySQL account to be created +T} +T{ +\f[C]MYSQL_PASSWORD\f[] +T}@T{ +Password for the user account +T} +T{ +\f[C]MYSQL_DATABASE\f[] +T}@T{ +Database name +T} +T{ +\f[C]MYSQL_ROOT_PASSWORD\f[] +T}@T{ +Password for the root user (optional) +T} +.TE +.PP +The following environment variables influence the MySQL configuration +file. +They are all optional. +.PP +.TS +tab(@); +lw(17.2n) lw(35.5n) lw(17.2n). +T{ +Variable name +T}@T{ +Description +T}@T{ +Default +T} +_ +T{ +\f[C]MYSQL_LOWER_CASE_TABLE_NAMES\f[] +T}@T{ +Sets how the table names are stored and compared +T}@T{ +0 +T} +T{ +\f[C]MYSQL_MAX_CONNECTIONS\f[] +T}@T{ +The maximum permitted number of simultaneous client connections +T}@T{ +151 +T} +T{ +\f[C]MYSQL_MAX_ALLOWED_PACKET\f[] +T}@T{ +The maximum size of one packet or any generated/intermediate string +T}@T{ +200M +T} +T{ +\f[C]MYSQL_FT_MIN_WORD_LEN\f[] +T}@T{ +The minimum length of the word to be included in a FULLTEXT index +T}@T{ +4 +T} +T{ +\f[C]MYSQL_FT_MAX_WORD_LEN\f[] +T}@T{ +The maximum length of the word to be included in a FULLTEXT index +T}@T{ +20 +T} +T{ +\f[C]MYSQL_AIO\f[] +T}@T{ +Controls the \f[C]innodb_use_native_aio\f[] setting value in case the +native AIO is broken. +See http://help.directadmin.com/item.php?id=529 +T}@T{ +1 +T} +T{ +\f[C]MYSQL_TABLE_OPEN_CACHE\f[] +T}@T{ +The number of open tables for all threads +T}@T{ +400 +T} +T{ +\f[C]MYSQL_KEY_BUFFER_SIZE\f[] +T}@T{ +The size of the buffer used for index blocks +T}@T{ +32M (or 10% of available memory) +T} +T{ +\f[C]MYSQL_SORT_BUFFER_SIZE\f[] +T}@T{ +The size of the buffer used for sorting +T}@T{ +256K +T} +T{ +\f[C]MYSQL_READ_BUFFER_SIZE\f[] +T}@T{ +The size of the buffer used for a sequential scan +T}@T{ +8M (or 5% of available memory) +T} +T{ +\f[C]MYSQL_INNODB_BUFFER_POOL_SIZE\f[] +T}@T{ +The size of the buffer pool where InnoDB caches table and index data +T}@T{ +32M (or 50% of available memory) +T} +T{ +\f[C]MYSQL_INNODB_LOG_FILE_SIZE\f[] +T}@T{ +The size of each log file in a log group +T}@T{ +8M (or 15% of available available) +T} +T{ +\f[C]MYSQL_INNODB_LOG_BUFFER_SIZE\f[] +T}@T{ +The size of the buffer that InnoDB uses to write to the log files on +disk +T}@T{ +8M (or 15% of available memory) +T} +T{ +\f[C]MYSQL_DEFAULTS_FILE\f[] +T}@T{ +Point to an alternative configuration file +T}@T{ +/etc/my.cnf +T} +T{ +\f[C]MYSQL_BINLOG_FORMAT\f[] +T}@T{ +Set sets the binlog format, supported values are \f[C]row\f[] and +\f[C]statement\f[] +T}@T{ +statement +T} +.TE +.PP +You can also set the following mount points by passing the +\f[C]\-v\ /host:/container\f[] flag to Docker. +.PP +.TS +tab(@); +l l. +T{ +Volume mount point +T}@T{ +Description +T} +_ +T{ +\f[C]/var/lib/mysql/data\f[] +T}@T{ +MySQL data directory +T} +.TE +.PP +\f[B]Notice: When mouting a directory from the host into the container, +ensure that the mounted directory has the appropriate permissions and +that the owner and group of the directory matches the user UID or name +which is running inside the container.\f[] +.SS Usage +.PP +For this, we will assume that you are using the +\f[C]rhscl/mariadb\-100\-rhel7\f[] image. +If you want to set only the mandatory environment variables and not +store the database in a host directory, execute the following command: +.IP +.nf +\f[C] +$\ docker\ run\ \-d\ \-\-name\ mariadb_database\ \-e\ MYSQL_USER=user\ \-e\ MYSQL_PASSWORD=pass\ \-e\ MYSQL_DATABASE=db\ \-p\ 3306:3306\ rhscl/mariadb\-100\-rhel7 +\f[] +.fi +.PP +This will create a container named \f[C]mariadb_database\f[] running +MySQL with database \f[C]db\f[] and user with credentials +\f[C]user:pass\f[]. +Port 3306 will be exposed and mapped to the host. +If you want your database to be persistent across container executions, +also add a \f[C]\-v\ /host/db/path:/var/lib/mysql/data\f[] argument. +This will be the MySQL data directory. +.PP +If the database directory is not initialized, the entrypoint script will +first run +\f[C]mysql_install_db\f[] (https://dev.mysql.com/doc/refman/5.6/en/mysql-install-db.html) +and setup necessary database users and passwords. +After the database is initialized, or if it was already present, +\f[C]mysqld\f[] is executed and will run as PID 1. +You can stop the detached container by running +\f[C]docker\ stop\ mariadb_database\f[]. +.SS MariaDB auto\-tuning +.PP +When the MySQL image is run with the \f[C]\-\-memory\f[] parameter set +and you didn\[aq]t specify value for some parameters, their values will +be automatically calculated based on the available memory. +.PP +.TS +tab(@); +l l l. +T{ +Variable name +T}@T{ +Configuration parameter +T}@T{ +Relative value +T} +_ +T{ +\f[C]MYSQL_KEY_BUFFER_SIZE\f[] +T}@T{ +\f[C]key_buffer_size\f[] +T}@T{ +10% +T} +T{ +\f[C]MYSQL_READ_BUFFER_SIZE\f[] +T}@T{ +\f[C]read_buffer_size\f[] +T}@T{ +5% +T} +T{ +\f[C]MYSQL_INNODB_BUFFER_POOL_SIZE\f[] +T}@T{ +\f[C]innodb_buffer_pool_size\f[] +T}@T{ +50% +T} +T{ +\f[C]MYSQL_INNODB_LOG_FILE_SIZE\f[] +T}@T{ +\f[C]innodb_log_file_size\f[] +T}@T{ +15% +T} +T{ +\f[C]MYSQL_INNODB_LOG_BUFFER_SIZE\f[] +T}@T{ +\f[C]innodb_log_buffer_size\f[] +T}@T{ +15% +T} +.TE +.SS MySQL root user +.PP +The root user has no password set by default, only allowing local +connections. +You can set it by setting the \f[C]MYSQL_ROOT_PASSWORD\f[] environment +variable. +This will allow you to login to the root account remotely. +Local connections will still not require a password. +.PP +To disable remote root access, simply unset \f[C]MYSQL_ROOT_PASSWORD\f[] +and restart the container. +.SS Changing passwords +.PP +Since passwords are part of the image configuration, the only supported +method to change passwords for the database user (\f[C]MYSQL_USER\f[]) +and root user is by changing the environment variables +\f[C]MYSQL_PASSWORD\f[] and \f[C]MYSQL_ROOT_PASSWORD\f[], respectively. +.PP +Changing database passwords through SQL statements or any way other than +through the environment variables aforementioned will cause a mismatch +between the values stored in the variables and the actual passwords. +Whenever a database container starts it will reset the passwords to the +values stored in the environment variables. +.SS Default my.cnf file +.PP +With environment variables we are able to customize a lot of different +parameters or configurations for the mysql bootstrap configurations. +If you\[aq]d prefer to use your own configuration file, you can override +the \f[C]MYSQL_DEFAULTS_FILE\f[] env variable with the full path of the +file you wish to use. +For example, the default location is \f[C]/etc/my.cnf\f[] but you can +change it to \f[C]/etc/mysql/my.cnf\f[] by setting +\f[C]MYSQL_DEFAULTS_FILE=/etc/mysql/my.cnf\f[] +.SS Changing the replication binlog_format +.PP +Some applications may wish to use \f[C]row\f[] binlog_formats (for +example, those built with change\-data\-capture in mind). +The default replication/binlog format is \f[C]statement\f[] but to +change it you can set the \f[C]MYSQL_BINLOG_FORMAT\f[] environment +variable. +For example \f[C]MYSQL_BINLOG_FORMAT=row\f[]. +Now when you run the database with \f[C]master\f[] replication turned on +(ie, set the Docker/container \f[C]cmd\f[] to be +\f[C]run\-mysqld\-master\f[]) the binlog will emit the actual data for +the rows that change as opposed to the statements (ie, DML like +insert...) that caused the change. +.SH AUTHORS +Red Hat. diff --git a/root/usr/bin/cgroup-limits b/root/usr/bin/cgroup-limits new file mode 100755 index 0000000..b9d4edc --- /dev/null +++ b/root/usr/bin/cgroup-limits @@ -0,0 +1,92 @@ +#!/usr/bin/python + +""" +Script for parsing cgroup information + +This script will read some limits from the cgroup system and parse +them, printing out "VARIABLE=VALUE" on each line for every limit that is +successfully read. Output of this script can be directly fed into +bash's export command. Recommended usage from a bash script: + + set -o errexit + export_vars=$(cgroup-limits) ; export $export_vars + +Variables currently supported: + MAX_MEMORY_LIMIT_IN_BYTES + Maximum possible limit MEMORY_LIMIT_IN_BYTES can have. This is + currently constant value of 9223372036854775807. + MEMORY_LIMIT_IN_BYTES + Maximum amount of user memory in bytes. If this value is set + to the same value as MAX_MEMORY_LIMIT_IN_BYTES, it means that + there is no limit set. The value is taken from + /sys/fs/cgroup/memory/memory.limit_in_bytes + NUMBER_OF_CORES + Number of detected CPU cores that can be used. This value is + calculated from /sys/fs/cgroup/cpuset/cpuset.cpus + NO_MEMORY_LIMIT + Set to "true" if MEMORY_LIMIT_IN_BYTES is so high that the caller + can act as if no memory limit was set. Undefined otherwise. +""" + +from __future__ import print_function +import sys + + +def _read_file(path): + try: + with open(path, 'r') as f: + return f.read().strip() + except IOError: + return None + + +def get_memory_limit(): + """ + Read memory limit, in bytes. + """ + + limit = _read_file('/sys/fs/cgroup/memory/memory.limit_in_bytes') + if limit is None or not limit.isdigit(): + print("Warning: Can't detect memory limit from cgroups", + file=sys.stderr) + return None + return int(limit) + + +def get_number_of_cores(): + """ + Read number of CPU cores. + """ + + core_count = 0 + + line = _read_file('/sys/fs/cgroup/cpuset/cpuset.cpus') + if line is None: + print("Warning: Can't detect number of CPU cores from cgroups", + file=sys.stderr) + return None + + for group in line.split(','): + core_ids = list(map(int, group.split('-'))) + if len(core_ids) == 2: + core_count += core_ids[1] - core_ids[0] + 1 + else: + core_count += 1 + + return core_count + + +if __name__ == "__main__": + env_vars = { + "MAX_MEMORY_LIMIT_IN_BYTES": 9223372036854775807, + "MEMORY_LIMIT_IN_BYTES": get_memory_limit(), + "NUMBER_OF_CORES": get_number_of_cores() + } + + env_vars = {k: v for k, v in env_vars.items() if v is not None} + + if env_vars.get("MEMORY_LIMIT_IN_BYTES", 0) >= 92233720368547: + env_vars["NO_MEMORY_LIMIT"] = "true" + + for key, value in env_vars.items(): + print("{0}={1}".format(key, value)) diff --git a/root/usr/bin/container-entrypoint b/root/usr/bin/container-entrypoint new file mode 100755 index 0000000..9d8ad4d --- /dev/null +++ b/root/usr/bin/container-entrypoint @@ -0,0 +1,2 @@ +#!/bin/bash +exec "$@" diff --git a/root/usr/bin/mysqld-master b/root/usr/bin/mysqld-master new file mode 100755 index 0000000..054889e --- /dev/null +++ b/root/usr/bin/mysqld-master @@ -0,0 +1,50 @@ +#!/bin/bash +# +# This is an entrypoint that runs the MySQL server in the 'master' mode. +# +export_vars=$(cgroup-limits); export $export_vars +source ${CONTAINER_SCRIPTS_PATH}/common.sh +set -eu + +export MYSQL_RUNNING_AS_MASTER=1 + +[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh +[ -f ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh + +# The 'server-id' for master needs to be constant +export MYSQL_SERVER_ID=1 +log_info "The 'master' server-id is ${MYSQL_SERVER_ID}" + +# Process the MySQL configuration files +log_info 'Processing MySQL configuration files ...' +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-master.cnf.template > /etc/my.cnf.d/master.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-repl-gtid.cnf.template > /etc/my.cnf.d/repl-gtid.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-tuning.cnf.template > /etc/my.cnf.d/tuning.cnf + +if [ ! -d "$MYSQL_DATADIR/mysql" ]; then + initialize_database "$@" +else + start_local_mysql "$@" +fi + +log_info 'Setting passwords ...' +[ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ] && source ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh + +# Setup the 'master' replication on the MySQL server +mysql $mysql_flags <&1 diff --git a/root/usr/bin/mysqld-slave b/root/usr/bin/mysqld-slave new file mode 100755 index 0000000..51acce5 --- /dev/null +++ b/root/usr/bin/mysqld-slave @@ -0,0 +1,60 @@ +#!/bin/bash +# +# This is an entrypoint that runs the MySQL server in the 'slave' mode. +# +export_vars=$(cgroup-limits); export $export_vars +source ${CONTAINER_SCRIPTS_PATH}/common.sh +set -eu + +# Just run normal server if the data directory is already initialized +if [ -d "${MYSQL_DATADIR}/mysql" ]; then + exec /usr/bin/run-mysqld "$@" +fi + +export MYSQL_RUNNING_AS_SLAVE=1 + +[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh + +# Generate the unique 'server-id' for this master +export MYSQL_SERVER_ID=$(server_id) +log_info "The 'slave' server-id is ${MYSQL_SERVER_ID}" + +# Process the MySQL configuration files +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-slave.cnf.template > /etc/my.cnf.d/slave.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-repl-gtid.cnf.template > /etc/my.cnf.d/repl-gtid.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-tuning.cnf.template > /etc/my.cnf.d/tuning.cnf + +# Initialize MySQL database and wait for the MySQL master to accept +# connections. +initialize_database "$@" +wait_for_mysql_master + +# Get binlog file and position from master +STATUS_INFO=$(mysql --host "$MYSQL_MASTER_SERVICE_NAME" "-u${MYSQL_MASTER_USER}" "-p${MYSQL_MASTER_PASSWORD}" replication -e 'SELECT gtid from replication limit 1\G') +GTID_VALUE=$(echo "$STATUS_INFO" | grep 'gtid:' | head -n 1 | sed -e 's/^\s*gtid: //') + +# checking STATUS_INFO here because empty GTID_VALUE is valid value +if [ -z "${STATUS_INFO}" ] ; then + echo "Could not read GTID value from master" + exit 1 +fi + +mysql $mysql_flags <&1 diff --git a/root/usr/bin/run-mysqld b/root/usr/bin/run-mysqld new file mode 100755 index 0000000..cd899a7 --- /dev/null +++ b/root/usr/bin/run-mysqld @@ -0,0 +1,35 @@ +#!/bin/bash + +export_vars=$(cgroup-limits); export $export_vars +source ${CONTAINER_SCRIPTS_PATH}/common.sh +set -eu + +[ -f ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh + +# Process the MySQL configuration files +log_info 'Processing MySQL configuration files ...' +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-tuning.cnf.template > /etc/my.cnf.d/tuning.cnf + +if [ ! -d "$MYSQL_DATADIR/mysql" ]; then + initialize_database "$@" +else + start_local_mysql "$@" +fi + +if [ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ]; then + log_info 'Setting passwords ...' + source ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh +fi +if [ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ]; then + log_info 'Sourcing post-init.sh ...' + source ${CONTAINER_SCRIPTS_PATH}/post-init.sh +fi + +# Restart the MySQL server with public IP bindings +shutdown_local_mysql +unset_env_vars +log_volume_info $MYSQL_DATADIR +log_info 'Running final exec -- Only MySQL server logs after this point' +exec ${MYSQL_PREFIX}/libexec/mysqld --defaults-file=$MYSQL_DEFAULTS_FILE "$@" 2>&1 diff --git a/root/usr/bin/run-mysqld-master b/root/usr/bin/run-mysqld-master new file mode 100755 index 0000000..054889e --- /dev/null +++ b/root/usr/bin/run-mysqld-master @@ -0,0 +1,50 @@ +#!/bin/bash +# +# This is an entrypoint that runs the MySQL server in the 'master' mode. +# +export_vars=$(cgroup-limits); export $export_vars +source ${CONTAINER_SCRIPTS_PATH}/common.sh +set -eu + +export MYSQL_RUNNING_AS_MASTER=1 + +[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh +[ -f ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh + +# The 'server-id' for master needs to be constant +export MYSQL_SERVER_ID=1 +log_info "The 'master' server-id is ${MYSQL_SERVER_ID}" + +# Process the MySQL configuration files +log_info 'Processing MySQL configuration files ...' +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-master.cnf.template > /etc/my.cnf.d/master.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-repl-gtid.cnf.template > /etc/my.cnf.d/repl-gtid.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-tuning.cnf.template > /etc/my.cnf.d/tuning.cnf + +if [ ! -d "$MYSQL_DATADIR/mysql" ]; then + initialize_database "$@" +else + start_local_mysql "$@" +fi + +log_info 'Setting passwords ...' +[ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ] && source ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh + +# Setup the 'master' replication on the MySQL server +mysql $mysql_flags <&1 diff --git a/root/usr/bin/run-mysqld-slave b/root/usr/bin/run-mysqld-slave new file mode 100755 index 0000000..51acce5 --- /dev/null +++ b/root/usr/bin/run-mysqld-slave @@ -0,0 +1,60 @@ +#!/bin/bash +# +# This is an entrypoint that runs the MySQL server in the 'slave' mode. +# +export_vars=$(cgroup-limits); export $export_vars +source ${CONTAINER_SCRIPTS_PATH}/common.sh +set -eu + +# Just run normal server if the data directory is already initialized +if [ -d "${MYSQL_DATADIR}/mysql" ]; then + exec /usr/bin/run-mysqld "$@" +fi + +export MYSQL_RUNNING_AS_SLAVE=1 + +[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh + +# Generate the unique 'server-id' for this master +export MYSQL_SERVER_ID=$(server_id) +log_info "The 'slave' server-id is ${MYSQL_SERVER_ID}" + +# Process the MySQL configuration files +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-slave.cnf.template > /etc/my.cnf.d/slave.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-repl-gtid.cnf.template > /etc/my.cnf.d/repl-gtid.cnf +envsubst < ${CONTAINER_SCRIPTS_PATH}/my-tuning.cnf.template > /etc/my.cnf.d/tuning.cnf + +# Initialize MySQL database and wait for the MySQL master to accept +# connections. +initialize_database "$@" +wait_for_mysql_master + +# Get binlog file and position from master +STATUS_INFO=$(mysql --host "$MYSQL_MASTER_SERVICE_NAME" "-u${MYSQL_MASTER_USER}" "-p${MYSQL_MASTER_PASSWORD}" replication -e 'SELECT gtid from replication limit 1\G') +GTID_VALUE=$(echo "$STATUS_INFO" | grep 'gtid:' | head -n 1 | sed -e 's/^\s*gtid: //') + +# checking STATUS_INFO here because empty GTID_VALUE is valid value +if [ -z "${STATUS_INFO}" ] ; then + echo "Could not read GTID value from master" + exit 1 +fi + +mysql $mysql_flags <&1 diff --git a/root/usr/libexec/container-setup b/root/usr/libexec/container-setup new file mode 100755 index 0000000..29c6ed2 --- /dev/null +++ b/root/usr/libexec/container-setup @@ -0,0 +1,58 @@ +#!/bin/bash + +# This function returns all config files that daemon uses and their path +# includes /opt. It is used to get correct path to the config file. +mysql_get_config_files_scl() { + scl enable ${ENABLED_COLLECTIONS} -- my_print_defaults --help --verbose | \ + grep --after=1 '^Default options' | \ + tail -n 1 | \ + grep -o '[^ ]*opt[^ ]*my.cnf' +} + +# This function picks the main config file that deamon uses and we ship in rpm +mysql_get_correct_config() { + # we use the same config in non-SCL packages, not necessary to guess + [ -z "${ENABLED_COLLECTIONS}" ] && echo -n "/etc/my.cnf" && return + + # from all config files read by daemon, pick the first that exists + for f in `mysql_get_config_files_scl` ; do + [ -f "$f" ] && echo "$f" + done | head -n 1 +} + +export MYSQL_CONFIG_FILE=$(mysql_get_correct_config) + +[ -z "$MYSQL_CONFIG_FILE" ] && echo "MYSQL_CONFIG_FILE is empty" && exit 1 + +unset -f mysql_get_correct_config mysql_get_config_files_scl + +# we provide own config files for the container, so clean what rpm ships here +mkdir -p ${MYSQL_CONFIG_FILE}.d +rm -f ${MYSQL_CONFIG_FILE}.d/* + +# we may add options during service init, so we need to have this dir writable by daemon user +chown -R mysql:0 ${MYSQL_CONFIG_FILE}.d ${MYSQL_CONFIG_FILE} +restorecon -R ${MYSQL_CONFIG_FILE}.d ${MYSQL_CONFIG_FILE} + +# API of the container are standard paths /etc/my.cnf and /etc/my.cnf.d +# we already include own /etc/my.cnf for container, but for cases the +# actually used config file is not on standard path /etc/my.cnf, we +# need to move it to the location daemon expects it and create symlinks +if [ "$MYSQL_CONFIG_FILE" != "/etc/my.cnf" ] ; then + rm -rf /etc/my.cnf.d + mv /etc/my.cnf ${MYSQL_CONFIG_FILE} + ln -s ${MYSQL_CONFIG_FILE} /etc/my.cnf + ln -s ${MYSQL_CONFIG_FILE}.d /etc/my.cnf.d +fi + +# setup directory for data +mkdir -p /var/lib/mysql/data +chown -R mysql:0 /var/lib/mysql +restorecon -R /var/lib/mysql + +# Loosen permission bits for group to avoid problems running container with +# arbitrary UID +# When only specifying user, group is 0, that's why /var/lib/mysql must have +# owner mysql.0; that allows to avoid a+rwx for this dir +chmod g+w -R /var/lib/mysql ${MYSQL_CONFIG_FILE}.d + diff --git a/root/usr/share/container-scripts/mysql/README.md b/root/usr/share/container-scripts/mysql/README.md new file mode 100644 index 0000000..656dbd9 --- /dev/null +++ b/root/usr/share/container-scripts/mysql/README.md @@ -0,0 +1,135 @@ +MariaDB Docker image +==================== + +This container image includes MariaDB server 10.1 for OpenShift and general usage. +Users can choose between RHEL and CentOS based images. + +Dockerfile for CentOS is called Dockerfile, Dockerfile for RHEL is called +Dockerfile.rhel7. + +Environment variables and volumes +---------------------------------- + +The image recognizes the following environment variables that you can set during +initialization by passing `-e VAR=VALUE` to the Docker run command. + +| Variable name | Description | +| :--------------------- | ----------------------------------------- | +| `MYSQL_USER` | User name for MySQL account to be created | +| `MYSQL_PASSWORD` | Password for the user account | +| `MYSQL_DATABASE` | Database name | +| `MYSQL_ROOT_PASSWORD` | Password for the root user (optional) | + +The following environment variables influence the MySQL configuration file. They are all optional. + +| Variable name | Description | Default +| :------------------------------ | ----------------------------------------------------------------- | ------------------------------- +| `MYSQL_LOWER_CASE_TABLE_NAMES` | Sets how the table names are stored and compared | 0 +| `MYSQL_MAX_CONNECTIONS` | The maximum permitted number of simultaneous client connections | 151 +| `MYSQL_MAX_ALLOWED_PACKET` | The maximum size of one packet or any generated/intermediate string | 200M +| `MYSQL_FT_MIN_WORD_LEN` | The minimum length of the word to be included in a FULLTEXT index | 4 +| `MYSQL_FT_MAX_WORD_LEN` | The maximum length of the word to be included in a FULLTEXT index | 20 +| `MYSQL_AIO` | Controls the `innodb_use_native_aio` setting value in case the native AIO is broken. See http://help.directadmin.com/item.php?id=529 | 1 +| `MYSQL_TABLE_OPEN_CACHE` | The number of open tables for all threads | 400 +| `MYSQL_KEY_BUFFER_SIZE` | The size of the buffer used for index blocks | 32M (or 10% of available memory) +| `MYSQL_SORT_BUFFER_SIZE` | The size of the buffer used for sorting | 256K +| `MYSQL_READ_BUFFER_SIZE` | The size of the buffer used for a sequential scan | 8M (or 5% of available memory) +| `MYSQL_INNODB_BUFFER_POOL_SIZE`| The size of the buffer pool where InnoDB caches table and index data | 32M (or 50% of available memory) +| `MYSQL_INNODB_LOG_FILE_SIZE` | The size of each log file in a log group | 8M (or 15% of available available) +| `MYSQL_INNODB_LOG_BUFFER_SIZE` | The size of the buffer that InnoDB uses to write to the log files on disk | 8M (or 15% of available memory) +| `MYSQL_DEFAULTS_FILE` | Point to an alternative configuration file | /etc/my.cnf +| `MYSQL_BINLOG_FORMAT` | Set sets the binlog format, supported values are `row` and `statement` | statement + +You can also set the following mount points by passing the `-v /host:/container` flag to Docker. + +| Volume mount point | Description | +| :----------------------- | -------------------- | +| `/var/lib/mysql/data` | MySQL data directory | + +**Notice: When mouting a directory from the host into the container, ensure that the mounted +directory has the appropriate permissions and that the owner and group of the directory +matches the user UID or name which is running inside the container.** + +Usage +--------------------------------- + +For this, we will assume that you are using the `rhscl/mariadb-100-rhel7` image. +If you want to set only the mandatory environment variables and not store +the database in a host directory, execute the following command: + +``` +$ docker run -d --name mariadb_database -e MYSQL_USER=user -e MYSQL_PASSWORD=pass -e MYSQL_DATABASE=db -p 3306:3306 rhscl/mariadb-100-rhel7 +``` + +This will create a container named `mariadb_database` running MySQL with database +`db` and user with credentials `user:pass`. Port 3306 will be exposed and mapped +to the host. If you want your database to be persistent across container executions, +also add a `-v /host/db/path:/var/lib/mysql/data` argument. This will be the MySQL +data directory. + +If the database directory is not initialized, the entrypoint script will first +run [`mysql_install_db`](https://dev.mysql.com/doc/refman/5.6/en/mysql-install-db.html) +and setup necessary database users and passwords. After the database is initialized, +or if it was already present, `mysqld` is executed and will run as PID 1. You can + stop the detached container by running `docker stop mariadb_database`. + + +MariaDB auto-tuning +------------------- + +When the MySQL image is run with the `--memory` parameter set and you didn't +specify value for some parameters, their values will be automatically +calculated based on the available memory. + +| Variable name | Configuration parameter | Relative value +| :-------------------------------| ------------------------- | -------------- +| `MYSQL_KEY_BUFFER_SIZE` | `key_buffer_size` | 10% +| `MYSQL_READ_BUFFER_SIZE` | `read_buffer_size` | 5% +| `MYSQL_INNODB_BUFFER_POOL_SIZE` | `innodb_buffer_pool_size` | 50% +| `MYSQL_INNODB_LOG_FILE_SIZE` | `innodb_log_file_size` | 15% +| `MYSQL_INNODB_LOG_BUFFER_SIZE` | `innodb_log_buffer_size` | 15% + + +MySQL root user +--------------------------------- +The root user has no password set by default, only allowing local connections. +You can set it by setting the `MYSQL_ROOT_PASSWORD` environment variable. This +will allow you to login to the root account remotely. Local connections will +still not require a password. + +To disable remote root access, simply unset `MYSQL_ROOT_PASSWORD` and restart +the container. + + +Changing passwords +------------------ + +Since passwords are part of the image configuration, the only supported method +to change passwords for the database user (`MYSQL_USER`) and root user is by +changing the environment variables `MYSQL_PASSWORD` and `MYSQL_ROOT_PASSWORD`, +respectively. + +Changing database passwords through SQL statements or any way other than through +the environment variables aforementioned will cause a mismatch between the +values stored in the variables and the actual passwords. Whenever a database +container starts it will reset the passwords to the values stored in the +environment variables. + +Default my.cnf file +------------------- +With environment variables we are able to customize a lot of different parameters +or configurations for the mysql bootstrap configurations. If you'd prefer to use +your own configuration file, you can override the `MYSQL_DEFAULTS_FILE` env +variable with the full path of the file you wish to use. For example, the default +location is `/etc/my.cnf` but you can change it to `/etc/mysql/my.cnf` by setting + `MYSQL_DEFAULTS_FILE=/etc/mysql/my.cnf` + +Changing the replication binlog_format +-------------------------------------- +Some applications may wish to use `row` binlog_formats (for example, those built + with change-data-capture in mind). The default replication/binlog format is + `statement` but to change it you can set the `MYSQL_BINLOG_FORMAT` environment + variable. For example `MYSQL_BINLOG_FORMAT=row`. Now when you run the database + with `master` replication turned on (ie, set the Docker/container `cmd` to be +`run-mysqld-master`) the binlog will emit the actual data for the rows that change +as opposed to the statements (ie, DML like insert...) that caused the change. diff --git a/root/usr/share/container-scripts/mysql/common.sh b/root/usr/share/container-scripts/mysql/common.sh new file mode 100644 index 0000000..e63a750 --- /dev/null +++ b/root/usr/share/container-scripts/mysql/common.sh @@ -0,0 +1,164 @@ +#!/bin/bash + +source ${CONTAINER_SCRIPTS_PATH}/helpers.sh + +# Data directory where MySQL database files live. The data subdirectory is here +# because .bashrc and my.cnf both live in /var/lib/mysql/ and we don't want a +# volume to override it. +export MYSQL_DATADIR=/var/lib/mysql/data + +# Configuration settings. +export MYSQL_DEFAULTS_FILE=${MYSQL_DEFAULTS_FILE:-/etc/my.cnf} +export MYSQL_BINLOG_FORMAT=${MYSQL_BINLOG_FORMAT:-STATEMENT} +export MYSQL_LOWER_CASE_TABLE_NAMES=${MYSQL_LOWER_CASE_TABLE_NAMES:-0} +export MYSQL_MAX_CONNECTIONS=${MYSQL_MAX_CONNECTIONS:-151} +export MYSQL_FT_MIN_WORD_LEN=${MYSQL_FT_MIN_WORD_LEN:-4} +export MYSQL_FT_MAX_WORD_LEN=${MYSQL_FT_MAX_WORD_LEN:-20} +export MYSQL_AIO=${MYSQL_AIO:-1} +export MYSQL_MAX_ALLOWED_PACKET=${MYSQL_MAX_ALLOWED_PACKET:-200M} +export MYSQL_TABLE_OPEN_CACHE=${MYSQL_TABLE_OPEN_CACHE:-400} +export MYSQL_SORT_BUFFER_SIZE=${MYSQL_SORT_BUFFER_SIZE:-256K} + +if [ -n "${NO_MEMORY_LIMIT:-}" -o -z "${MEMORY_LIMIT_IN_BYTES:-}" ]; then + key_buffer_size='32M' + read_buffer_size='8M' + innodb_buffer_pool_size='32M' + innodb_log_file_size='8M' + innodb_log_buffer_size='8M' +else + key_buffer_size="$(python -c "print(int((${MEMORY_LIMIT_IN_BYTES}/(1024*1024))*0.1))")M" + read_buffer_size="$(python -c "print(int((${MEMORY_LIMIT_IN_BYTES}/(1024*1024))*0.05))")M" + innodb_buffer_pool_size="$(python -c "print(int((${MEMORY_LIMIT_IN_BYTES}/(1024*1024))*0.5))")M" + innodb_log_file_size="$(python -c "print(int((${MEMORY_LIMIT_IN_BYTES}/(1024*1024))*0.15))")M" + innodb_log_buffer_size="$(python -c "print(int((${MEMORY_LIMIT_IN_BYTES}/(1024*1024))*0.15))")M" +fi +export MYSQL_KEY_BUFFER_SIZE=${MYSQL_KEY_BUFFER_SIZE:-$key_buffer_size} +export MYSQL_READ_BUFFER_SIZE=${MYSQL_READ_BUFFER_SIZE:-$read_buffer_size} +export MYSQL_INNODB_BUFFER_POOL_SIZE=${MYSQL_INNODB_BUFFER_POOL_SIZE:-$innodb_buffer_pool_size} +export MYSQL_INNODB_LOG_FILE_SIZE=${MYSQL_INNODB_LOG_FILE_SIZE:-$innodb_log_file_size} +export MYSQL_INNODB_LOG_BUFFER_SIZE=${MYSQL_INNODB_LOG_BUFFER_SIZE:-$innodb_log_buffer_size} + +# Be paranoid and stricter than we should be. +# https://dev.mysql.com/doc/refman/en/identifiers.html +mysql_identifier_regex='^[a-zA-Z0-9_]+$' +mysql_password_regex='^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$' + +# Variables that are used to connect to local mysql during initialization +mysql_flags="-u root --socket=/tmp/mysql.sock" +admin_flags="--defaults-file=$MYSQL_DEFAULTS_FILE $mysql_flags" + +# Make sure env variables don't propagate to mysqld process. +function unset_env_vars() { + log_info 'Cleaning up environment variables MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE and MYSQL_ROOT_PASSWORD ...' + unset MYSQL_USER MYSQL_PASSWORD MYSQL_DATABASE MYSQL_ROOT_PASSWORD +} + +# Poll until MySQL responds to our ping. +function wait_for_mysql() { + pid=$1 ; shift + + while [ true ]; do + if [ -d "/proc/$pid" ]; then + mysqladmin --socket=/tmp/mysql.sock ping &>/dev/null && log_info "MySQL started successfully" && return 0 + else + return 1 + fi + log_info "Waiting for MySQL to start ..." + sleep 1 + done +} + +# Start local MySQL server with a defaults file +function start_local_mysql() { + log_info 'Starting MySQL server with disabled networking ...' + ${MYSQL_PREFIX}/libexec/mysqld \ + --defaults-file=$MYSQL_DEFAULTS_FILE \ + --skip-networking --socket=/tmp/mysql.sock "$@" & + mysql_pid=$! + wait_for_mysql $mysql_pid +} + +# Shutdown mysql flushing privileges +function shutdown_local_mysql() { + log_info 'Shutting down MySQL ...' + mysqladmin $admin_flags flush-privileges shutdown +} + +# Initialize the MySQL database (create user accounts and the initial database) +function initialize_database() { + log_info 'Initializing database ...' + log_info 'Running mysql_install_db ...' + # Using --rpm since we need mysql_install_db behaves as in RPM + # Using empty --basedir to work-around https://bugzilla.redhat.com/show_bug.cgi?id=1406391 + mysql_install_db --rpm --datadir=$MYSQL_DATADIR --basedir='' + start_local_mysql "$@" + + if [ -v MYSQL_RUNNING_AS_SLAVE ]; then + log_info 'Initialization finished' + return 0 + fi + + if [ -v MYSQL_RUNNING_AS_MASTER ]; then + # Save master status into a separate database. + STATUS_INFO=$(mysql $admin_flags -e 'SHOW MASTER STATUS\G') + BINLOG_POSITION=$(echo "$STATUS_INFO" | grep 'Position:' | head -n 1 | sed -e 's/^\s*Position: //') + BINLOG_FILE=$(echo "$STATUS_INFO" | grep 'File:' | head -n 1 | sed -e 's/^\s*File: //') + GTID_INFO=$(mysql $admin_flags -e "SELECT BINLOG_GTID_POS('$BINLOG_FILE', '$BINLOG_POSITION') AS gtid_value \G") + GTID_VALUE=$(echo "$GTID_INFO" | grep 'gtid_value:' | head -n 1 | sed -e 's/^\s*gtid_value: //') + + mysqladmin $admin_flags create replication + mysql $admin_flags </dev/null && log_info "MySQL master is ready" && return 0 + sleep 1 + done +} diff --git a/root/usr/share/container-scripts/mysql/helpers.sh b/root/usr/share/container-scripts/mysql/helpers.sh new file mode 100644 index 0000000..4e832fc --- /dev/null +++ b/root/usr/share/container-scripts/mysql/helpers.sh @@ -0,0 +1,24 @@ +function log_info { + echo "---> `date +%T` $@" +} + +function log_and_run { + log_info "Running $@" + "$@" +} + +function log_volume_info { + CONTAINER_DEBUG=${CONTAINER_DEBUG:-} + if [[ "${CONTAINER_DEBUG,,}" != "true" ]]; then + return + fi + + log_info "Volume info for $@:" + set +e + log_and_run mount + while [ $# -gt 0 ]; do + log_and_run ls -alZ $1 + shift + done + set -e +} diff --git a/root/usr/share/container-scripts/mysql/my-base.cnf.template b/root/usr/share/container-scripts/mysql/my-base.cnf.template new file mode 100644 index 0000000..c654f7f --- /dev/null +++ b/root/usr/share/container-scripts/mysql/my-base.cnf.template @@ -0,0 +1,5 @@ +[mysqld] +datadir = ${MYSQL_DATADIR} +basedir = ${MYSQL_PREFIX} +plugin-dir = ${MYSQL_PREFIX}/lib64/mysql/plugin + diff --git a/root/usr/share/container-scripts/mysql/my-master.cnf.template b/root/usr/share/container-scripts/mysql/my-master.cnf.template new file mode 100644 index 0000000..f434885 --- /dev/null +++ b/root/usr/share/container-scripts/mysql/my-master.cnf.template @@ -0,0 +1,7 @@ +[mysqld] + +server-id = ${MYSQL_SERVER_ID} +log_bin = ${MYSQL_DATADIR}/mysql-bin.log +binlog_do_db = mysql +binlog_do_db = ${MYSQL_DATABASE} +binlog_format = ${MYSQL_BINLOG_FORMAT} diff --git a/root/usr/share/container-scripts/mysql/my-paas.cnf.template b/root/usr/share/container-scripts/mysql/my-paas.cnf.template new file mode 100644 index 0000000..11ddd1f --- /dev/null +++ b/root/usr/share/container-scripts/mysql/my-paas.cnf.template @@ -0,0 +1,26 @@ +[mysqld] +# +# Settings configured by the user +# + +# Sets how the table names are stored and compared. Default: 0 +lower_case_table_names = ${MYSQL_LOWER_CASE_TABLE_NAMES} + +# The maximum permitted number of simultaneous client connections. Default: 151 +max_connections = ${MYSQL_MAX_CONNECTIONS} + +# The minimum/maximum lengths of the word to be included in a FULLTEXT index. Default: 4/20 +ft_min_word_len = ${MYSQL_FT_MIN_WORD_LEN} +ft_max_word_len = ${MYSQL_FT_MAX_WORD_LEN} + +# In case the native AIO is broken. Default: 1 +# See http://help.directadmin.com/item.php?id=529 +innodb_use_native_aio = ${MYSQL_AIO} + +[myisamchk] +# The minimum/maximum lengths of the word to be included in a FULLTEXT index. Default: 4/20 +# +# To ensure that myisamchk and the server use the same values for full-text +# parameters, we placed them in both sections. +ft_min_word_len = ${MYSQL_FT_MIN_WORD_LEN} +ft_max_word_len = ${MYSQL_FT_MAX_WORD_LEN} diff --git a/root/usr/share/container-scripts/mysql/my-repl-gtid.cnf.template b/root/usr/share/container-scripts/mysql/my-repl-gtid.cnf.template new file mode 100644 index 0000000..a74a74c --- /dev/null +++ b/root/usr/share/container-scripts/mysql/my-repl-gtid.cnf.template @@ -0,0 +1,4 @@ +[mysqld] + +log-slave-updates = ON + diff --git a/root/usr/share/container-scripts/mysql/my-slave.cnf.template b/root/usr/share/container-scripts/mysql/my-slave.cnf.template new file mode 100644 index 0000000..5bdf109 --- /dev/null +++ b/root/usr/share/container-scripts/mysql/my-slave.cnf.template @@ -0,0 +1,7 @@ +[mysqld] + +server-id = ${MYSQL_SERVER_ID} +log_bin = ${MYSQL_DATADIR}/mysql-bin.log +relay-log = ${MYSQL_DATADIR}/mysql-relay-bin.log +binlog_do_db = mysql +binlog_do_db = ${MYSQL_DATABASE} diff --git a/root/usr/share/container-scripts/mysql/my-tuning.cnf.template b/root/usr/share/container-scripts/mysql/my-tuning.cnf.template new file mode 100644 index 0000000..e90b69a --- /dev/null +++ b/root/usr/share/container-scripts/mysql/my-tuning.cnf.template @@ -0,0 +1,28 @@ +[mysqld] +key_buffer_size = ${MYSQL_KEY_BUFFER_SIZE} +max_allowed_packet = ${MYSQL_MAX_ALLOWED_PACKET} +table_open_cache = ${MYSQL_TABLE_OPEN_CACHE} +sort_buffer_size = ${MYSQL_SORT_BUFFER_SIZE} +read_buffer_size = ${MYSQL_READ_BUFFER_SIZE} +read_rnd_buffer_size = 256K +net_buffer_length = 2K +thread_stack = 256K +myisam_sort_buffer_size = 2M + +# It is recommended that innodb_buffer_pool_size is configured to 50 to 75 percent of system memory. +innodb_buffer_pool_size = ${MYSQL_INNODB_BUFFER_POOL_SIZE} +innodb_additional_mem_pool_size = 2M +# Set .._log_file_size to 25 % of buffer pool size +innodb_log_file_size = ${MYSQL_INNODB_LOG_FILE_SIZE} +innodb_log_buffer_size = ${MYSQL_INNODB_LOG_BUFFER_SIZE} + +[mysqldump] +quick +max_allowed_packet = 16M + +[mysql] +no-auto-rehash + +[myisamchk] +key_buffer_size = 8M +sort_buffer_size = 8M diff --git a/root/usr/share/container-scripts/mysql/passwd-change.sh b/root/usr/share/container-scripts/mysql/passwd-change.sh new file mode 100644 index 0000000..ce06f6a --- /dev/null +++ b/root/usr/share/container-scripts/mysql/passwd-change.sh @@ -0,0 +1,23 @@ +# Set the password for MySQL user and root everytime this container is started. +# This allows to change the password by editing the deployment configuration. +if [[ -v MYSQL_USER && -v MYSQL_PASSWORD ]]; then + mysql $mysql_flags < -e MYSQL_PASSWORD= -e MYSQL_DATABASE= -e MYSQL_ROOT_PASSWORD= -p 3306:3306 mariadb" + org.fedoraproject.component: "mariadb" + authoritative-source-url: "registry.fedoraproject.org" + io.k8s.description: "MariaDB is a multi-user, multi-threaded SQL database server" + io.k8s.display-name: "MariaDB 10.1" + io.openshift.expose-services: "3306:mysql" + io.openshift.tags: "database,mysql,mariadb,mariadb101,galera" + rpm: + repo: + - http://mirror.vutbr.cz/fedora/releases/26/Everything/x86_64/os/ diff --git a/tests/sanity.py b/tests/sanity.py new file mode 100644 index 0000000..f582413 --- /dev/null +++ b/tests/sanity.py @@ -0,0 +1,61 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# This Modularity Testing Framework helps you to write tests for modules +# Copyright (C) 2017 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# he Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Authors: Rado Pitonak +# + +from avocado import main +from avocado.core import exceptions +from moduleframework import module_framework + + +class SanityCheck(module_framework.AvocadoTest): + """ + :avocado: enable + """ + + def test1(self): + """ + Simple sanity test + """ + + self.start() + self.run("ls / | grep bin") + + def test2MariaDBVersion(self): + """ + Check if MariaDB is installed in correct version + """ + + mariadb_version = "10.1" + self.start() + self.run("mysql -V | grep " + mariadb_version) + + def test3ServiceRunning(self): + """ + Check if MariaDB is running + """ + + self.start() + self.run('ls /proc/*/exe -alh | grep mysqld') + + +if __name__ == '__main__': + main() diff --git a/tests/sqlqueries.py b/tests/sqlqueries.py new file mode 100644 index 0000000..f4ea505 --- /dev/null +++ b/tests/sqlqueries.py @@ -0,0 +1,112 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# This Modularity Testing Framework helps you to write tests for modules +# Copyright (C) 2017 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# he Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Authors: Rado Pitonak +# + +from avocado import main +from avocado.core import exceptions +from moduleframework import module_framework +import time + + +class SqlQueriesCheck(module_framework.AvocadoTest): + """ + :avocado: enable + """ + + # prefix for mysql queries from command line + MYSQL_CMD = 'mysql --host={} -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" -e"{}"' + + def testConnectToDB(self): + """ + Test connection to database + """ + + sql_query = "SELECT 1;" + + self.start() + # wait for mariadb to start + time.sleep(15) + + container_ip = self.runHost("docker inspect -f '{{{{range .NetworkSettings.Networks}}}}{{{{.IPAddress}}}}{{{{end}}}}' mariadb_database").stdout + + print "CONTAINER IP ASA:" + container_ip + + # remove end of line character + container_ip = container_ip[:-1] + + self.run(self.MYSQL_CMD.format(container_ip, sql_query)) + + def testCreateTable(self): + """ + Creation of table in database + """ + + sql_query = "CREATE TABLE my_table(ID INT PRIMARY KEY NOT NULL);" + + self.start() + # wait for mariadb to start + time.sleep(15) + + container_ip = self.runHost("docker inspect -f '{{{{range .NetworkSettings.Networks}}}}{{{{.IPAddress}}}}{{{{end}}}}' mariadb_database").stdout + + # remove end of line character + container_ip = container_ip[:-1] + + self.run(self.MYSQL_CMD.format(container_ip, sql_query)) + + def testComplexDB(self): + """ + Test different requests towards database (create table, insert,select, drop table) + """ + + # sql queries used for this test case + sql_queries = {'create_table': 'CREATE TABLE my_table(ID INT PRIMARY KEY NOT NULL, NAME VARCHAR(255));', + 'insert_data': 'INSERT INTO my_table values({}, \'{}\');', + 'select': 'SELECT name FROM my_table;', + 'drop': 'DROP TABLE my_table'} + + self.start() + # wait for mariadb to start + time.sleep(15) + + container_ip = self.runHost( + "docker inspect -f '{{{{range .NetworkSettings.Networks}}}}{{{{.IPAddress}}}}{{{{end}}}}' mariadb_database").stdout + + # remove end of line character + container_ip = container_ip[:-1] + + # creation of table + self.run(self.MYSQL_CMD.format(container_ip, sql_queries['create_table'])) + + # insert data + self.run(self.MYSQL_CMD.format(container_ip, sql_queries['insert_data'].format(1, 'name1'))) + self.run(self.MYSQL_CMD.format(container_ip, sql_queries['insert_data'].format(2, 'name2'))) + + # select query for validating output + self.run('{} | grep -E "name1|name2"'.format(self.MYSQL_CMD.format(container_ip, sql_queries['select']))) + + # delete table + self.run(self.MYSQL_CMD.format(container_ip, sql_queries['drop'])) + + +if __name__ == '__main__': + main()