摘要
一句话就是,很蛋疼,为了实验用docker编译MariaDB Galera创建镜像,第一个自建的镜像,中间遇到很多的坑,感谢Google、github.com、hub.docker.com。
镜像获取方法
1、使用Dockerfile创建
wget http://www.dwhd.org/wp-content/uploads/2015/11/MariaDB-Galera-Cluster.tar.gz
tar xf MariaDB-Galera-Cluster.tar.gz
wget -c http://download.openvz.org/template/precreated/centos-6-x86_64-minimal.tar.gz
cat centos-6-x86_64-minimal.tar.gz |docker import - centos_ovz_x86-64:6
docker build -t mariadb-galera-cluster:latest ./
2、通过公共仓库搜索下载(我已经上传了)
docker search mariadb_build
docker pull benyoo/mariadb_build:frist_image
镜像使用说明
Available environment configuration MariaDB
变量名默认值说明
INSTALL_DIR/usr/local/mariadb构建镜像时候MariaDB编译安装的路径
DATA_DIR/data/mariadbMariaDB的数据库文件存放路径
TIMEZONE设置系统默认时区
PORT13306默认MariaDB监听端口
PORT24444MariaDB Galera Cluster模式的时候所会用到的端口
PORT34567
PORT44568
MYSQL_ROOT_PASSWORDroot账户密码
MYSQL_DATABASE如果值不为空将新建一个库,库名为值
MYSQL_USER如果不为空则添加一个用户
MYSQL_PASSWORD如果MYSQL_USER值不为空,此值为MYSQL_USER值的密码
MAX_CONNECTIONS100最大并行连接数
SYNC_BINLOG0Controls the number of binary log commit groups to collect before synchronizing the binary log to disk. When sync_binlog=0, the binary log is never synchronized to disk, and when sync_binlog is set to a value greater than 0 this number of binary log commit groups is periodically synchronized to disk. When sync_binlog=1, all transactions are synchronized to the binary log before they are committed
REPLICATION_USERNAME用于复制的帐号的用户名
REPLICATION_PASSWORD用于复制的帐号的密码
QUERY_CACHE_TYPE10=OFF, 1=ON, 2=DEMAND
QUERY_CACHE_SIZE16M分配的内存来缓存查询结果的数量
MAX_ALLOWED_PACKET16M一个数据分组的最大大小
LOG_BIN_INDEX二进制日志索引文件的位置
LOG_BIN二进制日志
INNODB_OLD_BLOCKS_TIME1000Non-zero values protect against the buffer pool being filled up by data that is referenced only for a brief period, such as during a full table scan. Increasing this value offers more protection against full table scans interfering with data cached in the buffer pool.
INNODB_LOG_FILE_SIZE48M二进制日志文件切割大小
INNODB_FLUSH_METHODDefines the method used to flush data to the InnoDB data files and log files, which can affect I/O throughput
INNODB_FLUSH_LOG_AT_TRX_COMMIT1Controls the balance between strict ACID compliance for commit operations, and higher performance that is possible when commit-related I/O operations are rearranged and done in batches. You can achieve better performance by changing the default value, but then you can lose up to a second of transactions in a crash.
INNODB_BUFFER_POOL_SIZE128MThe size in bytes of the buffer pool, the memory area where InnoDB caches table and index data
GALERA如果给值则启动Galera模式
CLUSTER_NODE_ADDRESS当前节点IP
NODE_NAME节点名称
CLUSTER_NAME集群名称
CLUSTER_METHODGalera Cluster同步使用的模式
CLUSTER_ADDRESSgcomm:// style resource identifier that provides topology information about the Galera Cluster
Running MariaDB in Standalone Mode
docker run -dit --name=mariadb_cluster_node1 -p 3306:3306 \
-v /data/mariadb:/data/mariadb \
-e TIMEZONE=Asia/Shanghai \
-e MYSQL_ROOT_PASSWORD=MDhjZTRlYzYzNTRl \
benyoo/mariadb_build:latest
Running MariaDB in Galera Cluster Mode
Initializing a new cluster
docker run -d --name=mariadb_cluster_node1 \
-p 3306:3306 -p 4444:4444 -p 4567-4568:4567-4568 \
-p 4444:4444/udp -p 4567-4568:4567-4568/udp \
-v /data/mariadb:/data/mariadb \
-e TIMEZONE=Asia/Shanghai \
-e MYSQL_ROOT_PASSWORD=MDhjZTRlYzYzNTRl \
-e REPLICATION_USERNAME=replication \
-e REPLICATION_PASSWORD=ZThmOTQ5OWFiYzgy \
-e MAX_CONNECTIONS=20 \
-e GALERA=ON \
-e NODE_NAME=node1 \
-e CLUSTER_METHOD=mysqldump \
-e CLUSTER_NAME=lookback \
-e CLUSTER_NODE_ADDRESS=162.243.90.77 \
-e CLUSTER_ADDRESS="gcomm://162.243.90.77,188.166.9.236,128.199.150.74" \
benyoo/mariadb_build:latest --wsrep-new-cluster
Joining a node to the cluster
docker run -d --name=mariadb_cluster_node1 \
-p 3306:3306 -p 4444:4444 -p 4567-4568:4567-4568 \
-p 4444:4444/udp -p 4567-4568:4567-4568/udp \
-v /data/mariadb:/data/mariadb \
-e TIMEZONE=Asia/Shanghai \
-e MYSQL_ROOT_PASSWORD=MDhjZTRlYzYzNTRl \
-e REPLICATION_USERNAME=replication \
-e REPLICATION_PASSWORD=ZThmOTQ5OWFiYzgy \
-e MAX_CONNECTIONS=20 \
-e GALERA=ON \
-e NODE_NAME=node2 \
-e CLUSTER_METHOD=mysqldump \
-e CLUSTER_NAME=lookback \
-e CLUSTER_NODE_ADDRESS=188.166.9.236 \
-e CLUSTER_ADDRESS="gcomm://162.243.90.77,188.166.9.236,128.199.150.74" \
benyoo/mariadb_build:latest
Recover strategies
To fix a split brain in a failed cluster make sure that only one node remains in the cluster and run
SET GLOBAL wsrep_provider_options='pc.bootstrap=true';
For more information about recovering a galera cluster have a look at
https://www.percona.com/blog/2014/09/01/galera-replication-how-to-recover-a-pxc-cluster/
Dockerfile文件详情
FROM centos:6.7
MAINTAINER from www.dwhd.org by lookback (mondeolove@gmail.com)
ENV PATH=/bin:$PATH \
MARIADB_VERSION=10.0.21 \
INSTALL_DIR=/usr/local/mariadb \
DATA_DIR=/data/mariadb
RUN yum clean all && \
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-* && \
rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB && \
yum install -y -q epel-release && \
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 && \
[ $(getconf LONG_BIT) = 64 ] && \
yum install -y -q http://yum.mariadb.org/10.0.21/centos6-amd64/rpms/galera-25.3.9-1.rhel6.el6.x86_64.rpm && \
yum makecache && \
yum install -y -q libxml2-devel lz4-devel openssl-devel libpcap nmap lsof socat ntpdate wget cmake tar rsync which cronie && \
yum groupinstall -y -q "Development tools" "Server Platform Development" && \
mkdir -p /var/spool/cron && \
echo "*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1" >> /var/spool/cron/root
RUN groupadd --system mysql && \
useradd --system --gid mysql mysql && \
mkdir -p $DATA_DIR && \
chown -R mysql.mysql $DATA_DIR && \
wget -c https://downloads.mariadb.org/interstitial/mariadb-galera-${MARIADB_VERSION}/source/mariadb-galera-${MARIADB_VERSION}.tar.gz && \
wget -c http://www.phontron.com/kytea/download/kytea-0.4.7.tar.gz
RUN tar xf kytea-0.4.7.tar.gz && \
cd kytea-0.4.7/ && \
./configure && \
make -j $(awk '/processor/{i++}END{print i}' /proc/cpuinfo) && \
make install
RUN tar xf mariadb-galera-${MARIADB_VERSION}.tar.gz && \
cd mariadb-${MARIADB_VERSION}/ && \
cmake . -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
-DMYSQL_DATADIR=$DATA_DIR \
-DWITH_SSL=system \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_SPHINX_STORAGE_ENGINE=1 \
-DWITH_ARIA_STORAGE_ENGINE=1 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATEDX_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_READLINE=1 -DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_WSREP=1 \
-DWITH_INNODB_DISALLOW_WRITES=1 && \
make -j $(awk '/processor/{i++}END{print i}' /proc/cpuinfo) && \
make install
RUN /bin/rm -rf /{kytea-0.4.7.tar.gz,mariadb-$MARIADB_VERSION,mariadb-galera-$MARIADB_VERSION.tar.gz,kytea-0.4.7,mariadb.conf} && \
/bin/rm -rf $DATA_DIR/*.err
ENV TERM xterm
ENV PATH=/usr/local/mariadb/bin:$PATH \
MAX_CONNECTIONS=100 \
PORT1=3306 \
PORT2=4444 \
PORT3=4567 \
PORT4=4568 \
MAX_ALLOWED_PACKET=16M \
QUERY_CACHE_SIZE=16M \
QUERY_CACHE_TYPE=1 \
INNODB_BUFFER_POOL_SIZE=128M \
INNODB_LOG_FILE_SIZE=48M \
INNODB_FLUSH_METHOD= \
INNODB_OLD_BLOCKS_TIME=1000 \
INNODB_FLUSH_LOG_AT_TRX_COMMIT=1 \
SYNC_BINLOG=0
COPY run.sh /run.sh
COPY my.cnf /etc/my.cnf
RUN chmod +x /run.sh
VOLUME ["$INSTALL_DIR","$DATA_DIR"]
EXPOSE $PORT1 $PORT2 $PORT3 $PORT4
ENTRYPOINT ["/run.sh"]
CMD ["mysqld"]
#!/bin/sh
#########################################################################
# File Name: run.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2015年11月16日 星期一 05时14分51秒
#########################################################################
set -e
if [ -n "$TIMEZONE" ]; then
rm -rf /etc/localtime && \
ln -s /usr/share/zoneinfo/$TIMEZONE /etc/localtime && \
/usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
fi
sed -ri "s@^(port).*@\1=${PORT1}@" /etc/my.cnf
sed -ri "s@^(basedir).*@\1=${INSTALL_DIR}@" /etc/my.cnf
sed -ri "s@^(datadir).*@\1=${DATA_DIR}@" /etc/my.cnf
sed -ri "s@^(pid-file).*@\1=${DATA_DIR}/mysql.pid@" /etc/my.cnf
sed -ri "s@^(max_connections).*@\1=${MAX_CONNECTIONS}@" /etc/my.cnf
sed -ri "s@^(max_allowed_packet).*@\1=${MAX_ALLOWED_PACKET}@" /etc/my.cnf
sed -ri "s@^(query_cache_size).*@\1=${QUERY_CACHE_SIZE}@" /etc/my.cnf
sed -ri "s@^(query_cache_type).*@\1=${QUERY_CACHE_TYPE}@" /etc/my.cnf
sed -ri "s@^(innodb_log_file_size).*@\1=${INNODB_LOG_FILE_SIZE}@" /etc/my.cnf
sed -ri "s@^(sync_binlog).*@\1=${SYNC_BINLOG}@" /etc/my.cnf
sed -ri "s@^(innodb_buffer_pool_size).*@\1=${INNODB_BUFFER_POOL_SIZE}@" /etc/my.cnf
sed -ri "s@^(innodb_old_blocks_time).*@\1=${INNODB_OLD_BLOCKS_TIME}@" /etc/my.cnf
sed -ri "s@^(innodb_flush_log_at_trx_commit).*@\1=${INNODB_FLUSH_LOG_AT_TRX_COMMIT}@" /etc/my.cnf
sed -ri "s@/data/mariadb@${DATA_DIR}@" /etc/my.cnf
if [ -n "$INNODB_FLUSH_METHOD" ]; then
sed -ri "/^innodb_flush_log_at_trx_commit/a innodb_flush_method=${INNODB_FLUSH_METHOD}" /etc/my.cnf
fi
if [ "${1:0:1}" = '-' ]; then
set -- mysqld "$@"
fi
if [ "$1" = 'mysqld' ]; then
if [ -n "$GALERA" ]; then
if [ -z "$CLUSTER_NAME" ]; then
echo >&2 'error: missing CLUSTER_NAME'
echo >&2 ' Did you forget to add -e CLUSTER_NAME=... ?'
exit 1
fi
if [ -z "$NODE_NAME" ]; then
echo >&2 'error: missing NODE_NAME'
echo >&2 ' Did you forget to add -e NODE_NAME=... ?'
exit 1
fi
if [ -z "$CLUSTER_ADDRESS" ]; then
echo >&2 'error: missing CLUSTER_ADDRESS'
echo >&2 ' Did you forget to add -e CLUSTER_ADDRESS=... ?'
exit 1
fi
if [ -z "$CLUSTER_NODE_ADDRESS" ]; then
echo >&2 'error: missing CLUSTER_NODE_ADDRESS'
echo >&2 ' Did you forget to add -e CLUSTER_NODE_ADDRESS=... ?'
exit 1
fi
if [ -n "$CLUSTER_METHOD" ]; then
if [[ ! "$CLUSTER_METHOD" =~ ^(mysqldump|xtrabackup|rsync|rsync_wan)$ ]]; then
echo >&2 'error: missing CLUSTER_METHOD'
echo >&2 ' You must be used -e CLUSTER_METHOD=[mysqldump|xtrabackup|rsync|rsync_wan] '
exit 1
fi
else
CLUSTER_METHOD=mysqldump
fi
fi
if [ ! -d "$DATA_DIR/mysql" ]; then
if [ -n "$GALERA" -a -z "$REPLICATION_PASSWORD" ]; then
echo >&2 'error: missing REPLICATION_PASSWORD'
echo >&2 ' Did you forget to add -e REPLICATION_PASSWORD=... ?'
exit 1
fi
echo 'Running mysql_install_db ...'
cd $INSTALL_DIR/ && $INSTALL_DIR/scripts/mysql_install_db --user=mysql --datadir="$DATA_DIR" >/dev/null 2>&1
echo 'Finished mysql_install_db'
tempSqlFile='/tmp/mysql-first-time.sql'
cat > "$tempSqlFile" <
-- What's done in this file shouldn't be replicated
-- or products like mysql-fabric won't work
SET @@SESSION.SQL_LOG_BIN=0;
DELETE FROM mysql.user;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' WITH GRANT OPTION;
--GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' WITH GRANT OPTION;
--GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' WITH GRANT OPTION;
DROP DATABASE IF EXISTS test;
EOF
if [ -n "${GALERA}" ]; then
if [ -z "${REPLICATION_USERNAME}" ]; then
REPLICATION_USERNAME=replication
fi
#echo "GRANT ALL PRIVILEGES ON *.* TO '${REPLICATION_USERNAME}'@'%' IDENTIFIED BY '${REPLICATION_PASSWORD}' WITH GRANT OPTION ;" >> "$tempSqlFile"
cat >> "$tempSqlFile" <
-- CREATE USER '${REPLICATION_USERNAME}'@'%' IDENTIFIED BY '${REPLICATION_PASSWORD}' ;
-- GRANT RELOAD,LOCK TABLES,REPLICATION CLIENT,FILE ON *.* TO '${REPLICATION_USERNAME}'@'%' ;
CREATE USER '${REPLICATION_USERNAME}'@'%' IDENTIFIED BY '${REPLICATION_PASSWORD}';
GRANT RELOAD,LOCK TABLES,REPLICATION CLIENT,REPLICATION SLAVE,FILE ON *.* TO '${REPLICATION_USERNAME}'@'%';
EOF
fi
if [ "$MYSQL_DATABASE" ]; then
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" >> "$tempSqlFile"
fi
if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" >> "$tempSqlFile"
if [ "$MYSQL_DATABASE" ]; then
echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" >> "$tempSqlFile"
fi
fi
cat >> "$tempSqlFile" <
FLUSH PRIVILEGES ;
EOF
set -- "$@" --init-file="$tempSqlFile"
fi
chown -R mysql:mysql "$DATA_DIR"
if [ -n "$GALERA" ]; then
# append galera specific run options
set -- "$@" \
--wsrep_provider="/usr/lib64/galera/libgalera_smm.so" \
--wsrep_cluster_address="${CLUSTER_ADDRESS}" \
--wsrep_cluster_name="${CLUSTER_NAME}" \
--wsrep_node_address="${CLUSTER_NODE_ADDRESS}" \
--wsrep_node_name="${NODE_NAME}" \
--wsrep_sst_method="${CLUSTER_METHOD}" \
--wsrep_sst_auth="${REPLICATION_USERNAME}:${REPLICATION_PASSWORD}" \
--wsrep_sst_receive_address="${CLUSTER_NODE_ADDRESS}"
fi
fi
exec "$@"
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mariadb
datadir = /data/mariadb
innodb_autoinc_lock_mode = 2
pid-file = /data/mariadb/mysql.pid
user = mysql
#bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 1024
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 128M
read_buffer_size = 512k
read_rnd_buffer_size = 2M
sort_buffer_size = 512k
join_buffer_size = 2M
key_buffer_size = 256M
thread_cache_size = 64
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
binlog_format = row
expire_logs_days = 30
log_error = /data/mariadb/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mariadb/mysql-slow.log
performance_schema = 0
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 1024M
innodb_old_blocks_time=0
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_fast_shutdown=0
innodb_thread_concurrency=0
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M