-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from axelerant/ddev-support
Add support for DDEV
- Loading branch information
Showing
8 changed files
with
271 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# This image is used to copy and install the databases. | ||
ARG BASE_IMAGE=drud/ddev-dbserver-mariadb-10.4:v1.17.0 | ||
FROM ${BASE_IMAGE} AS build | ||
ARG BASE_IMAGE_USER | ||
ARG BASE_IMAGE_PASSWORD | ||
ARG BASE_IMAGE_DATABASE | ||
|
||
ENV ALLOW_EMPTY_PASSWORD=yes | ||
ENV MARIADB_USER=${BASE_IMAGE_USER:-db} | ||
ENV MARIADB_PASSWORD=${BASE_IMAGE_PASSWORD:-db} | ||
ENV MARIADB_DATABASE=${BASE_IMAGE_DATABASE:-db} | ||
|
||
COPY create_init_db.sh / | ||
|
||
COPY dumps/ /docker-entrypoint-initdb.d/ | ||
COPY zzzz-truncate-caches.sql /docker-entrypoint-initdb.d/ | ||
|
||
RUN /create_init_db.sh | ||
RUN rm -rf /var/lib/mysql/* | ||
RUN chmod -R ugo+rw /mysqlbase && find /mysqlbase -type d | xargs chmod ugo+rwx | ||
|
||
# This image is used to copy the installed databases and configure MySQL. | ||
FROM ${BASE_IMAGE} | ||
|
||
COPY --from=build /mysqlbase /mysqlbase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Dockerize your Database | ||
|
||
These files are used in building an image from a SQL file. This was originally started in a separate internal repository but has been moved here because it is simpler to maintain and we don't need an additional dependency. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
set -o pipefail | ||
|
||
SOCKET=/var/tmp/mysql.sock | ||
OUTDIR=/mysqlbase | ||
|
||
mkdir -p ${OUTDIR} | ||
chown -R "$(id -u):$(id -g)" $OUTDIR | ||
|
||
chmod ugo+w /var/tmp | ||
mkdir -p /var/lib/mysql /mnt/ddev_config/mysql && rm -f /var/lib/mysql/* && chmod -R ugo+w /var/lib/mysql | ||
|
||
echo 'Initializing mysql' | ||
mysqld --version | ||
mysqld_version=$(mysqld --version | awk '{ gsub(/-log/, ""); gsub(/\.[0-9]+$/, "", $3); print $3}') | ||
echo version=$mysqld_version | ||
# Oracle mysql 5.7+ deprecates mysql_install_db | ||
if [ "${mysqld_version}" = "5.7" ] || [ "${mysqld_version%%%.*}" = "8.0" ]; then | ||
mysqld --initialize-insecure --datadir=/var/lib/mysql --server-id=0 | ||
else | ||
# mysql 5.5 requires running mysql_install_db in /usr/local/mysql | ||
if command -v mysqld | grep usr.local; then | ||
cd /usr/local/mysql | ||
fi | ||
mysql_install_db --force --datadir=/var/lib/mysql | ||
fi | ||
echo "Starting mysqld --skip-networking --socket=${SOCKET}" | ||
mysqld --user=root --socket=$SOCKET --innodb_log_file_size=48M --skip-networking --datadir=/var/lib/mysql --server-id=0 --skip-log-bin & | ||
pid="$!" | ||
|
||
# Wait for the server to respond to mysqladmin ping, or fail if it never does, | ||
# or if the process dies. | ||
for i in {90..0}; do | ||
if mysqladmin ping -uroot --socket=$SOCKET 2>/dev/null; then | ||
break | ||
fi | ||
# Test to make sure we got it started in the first place. kill -s 0 just tests to see if process exists. | ||
if ! kill -s 0 $pid 2>/dev/null; then | ||
echo "MariaDB initialization startup failed" | ||
exit 3 | ||
fi | ||
sleep 1 | ||
done | ||
if [ "$i" -eq 0 ]; then | ||
echo 'MariaDB initialization startup process timed out.' | ||
exit 4 | ||
fi | ||
|
||
|
||
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -uroot mysql | ||
|
||
mysql -uroot <<EOF | ||
CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE; | ||
CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD'; | ||
CREATE USER '$MYSQL_USER'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD'; | ||
GRANT ALL ON $MYSQL_DATABASE.* TO '$MYSQL_USER'@'%'; | ||
GRANT ALL ON $MYSQL_DATABASE.* TO '$MYSQL_USER'@'localhost'; | ||
CREATE USER 'root'@'%' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD'; | ||
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION; | ||
FLUSH PRIVILEGES; | ||
FLUSH TABLES; | ||
EOF | ||
|
||
mysqladmin -uroot password root | ||
|
||
if [ "${mysqld_version%%%.*}" = "8.0" ]; then | ||
mysql -uroot -proot <<EOF | ||
ALTER USER 'db'@'%' IDENTIFIED WITH mysql_native_password BY '$MYSQL_PASSWORD'; | ||
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '$MYSQL_ROOT_PASSWORD'; | ||
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$MYSQL_ROOT_PASSWORD'; | ||
EOF | ||
fi | ||
|
||
set +u | ||
source /usr/local/bin/docker-entrypoint.sh | ||
docker_process_init_files /docker-entrypoint-initdb.d/* | ||
set -u | ||
|
||
|
||
rm -rf $OUTDIR/* | ||
|
||
backuptool=mariabackup | ||
if command -v xtrabackup; then backuptool="xtrabackup --datadir=/var/lib/mysql"; fi | ||
${backuptool} --backup --target-dir=$OUTDIR --user=root --password=root --socket=$SOCKET | ||
|
||
# Initialize with current mariadb_version | ||
my_mariadb_version=$(PATH=$PATH:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin mysqld -V 2>/dev/null | awk '{sub( /\.[0-9]+(-.*)?$/, "", $3); print $3 }') | ||
echo $my_mariadb_version >$OUTDIR/db_mariadb_version.txt | ||
|
||
if ! kill -s TERM "$pid" || ! wait "$pid"; then | ||
echo >&2 'Database initialization process failed.' | ||
exit 5 | ||
fi | ||
|
||
echo "The startup database files (in mariabackup/xtradb format) are now in $OUTDIR" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-- From https://stackoverflow.com/a/52370521/124844 | ||
|
||
DROP PROCEDURE IF EXISTS truncate_tables; | ||
|
||
DELIMITER $$ | ||
CREATE PROCEDURE truncate_tables() | ||
BEGIN | ||
DECLARE tblName CHAR(64); | ||
DECLARE done INT DEFAULT FALSE; | ||
DECLARE dbTables CURSOR FOR | ||
SELECT table_name | ||
FROM information_schema.tables | ||
WHERE table_schema = (SELECT DATABASE()) AND | ||
(table_name LIKE 'cache%' OR | ||
table_name LIKE 'search_%' OR | ||
table_name LIKE 'old_%' OR | ||
table_name IN ('flood', 'batch', 'queue', 'sessions', 'semaphore')); | ||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; | ||
|
||
OPEN dbTables; | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
|
||
read_loop: LOOP | ||
FETCH dbTables INTO tblName; | ||
IF done THEN | ||
LEAVE read_loop; | ||
END IF; | ||
|
||
PREPARE stmt FROM CONCAT('TRUNCATE ', tblName); | ||
EXECUTE stmt; | ||
DEALLOCATE PREPARE stmt; | ||
|
||
PREPARE stmt FROM CONCAT('OPTIMIZE TABLE ', tblName); | ||
EXECUTE stmt; | ||
DEALLOCATE PREPARE stmt; | ||
END LOOP read_loop; | ||
|
||
CLOSE dbTables; | ||
SET FOREIGN_KEY_CHECKS = 1; | ||
END | ||
$$ | ||
|
||
CALL truncate_tables(); | ||
DROP PROCEDURE IF EXISTS truncate_tables; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters