环镜:
redhat7.3 + mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
用Dockerfile来创建镜像
Dockerfile内容
FROM centos
ENV SLAVE false
ENV SLAVE_IP 127.0.0.1
RUN mkdir -p /home/db/mysql mysql && groupadd -g 320 mysql && useradd -u 321 -g 320 -M -d /home/db/mysql mysql
COPY bashrc /home/db/mysql/.bashrc
COPY m_bash_profile /home/db/mysql/.bash_profile
RUN chown -R mysql:mysql /home/db/mysql
ADD config /etc/selinux/config
ADD hosts /etc/hosts
ADD limits.conf /etc/security/limits.conf
ADD sysctl.conf /etc/sysctl.conf
ADD mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz /home/db/mysql/
ADD performance_collection /home/db/mysql/scripts/performance_collection
ADD mysql.server /etc/init.d/mysqld
RUN chmod +x /etc/init.d/mysqld
ADD profile /etc/profile
RUN source /etc/profile
ADD my.cnf /home/db/mysql/my.cnf
RUN ln -s /home/db/mysql/my.cnf /etc/my.cnf && chmod +r /etc/my.cnf
RUN yum -y install libaio numactl
RUN chown -R mysql:mysql /home/db/mysql
ADD setup.sh /root/
RUN chmod 775 /root/setup.sh
# VOLUME ["/mysqldata/data","/mysqldata/","/mysqlbinlog","/mysqlbackup"]
EXPOSE 22 80 13306
CMD ["/root/setup.sh"]
查看Dockerfile文件夹中的配置文件如下:
-rw-r--r--. 1 root root 231 Jan 30 05:39 bashrc
-rw-r--r--. 1 root root 551 Jan 30 05:39 config
-rw-r--r--. 1 root root 1000 Feb 1 01:08 Dockerfile
-rw-r--r--. 1 root root 316 Jan 30 05:39 hosts
-rw-r--r--. 1 root root 2516 Jan 30 05:39 limits.conf
-rw-r--r--. 1 root root 168 Jan 30 05:39 m_bash_profile
-rw-r--r--. 1 root root 3132 Feb 1 02:27 my.cnf
-rw-r--r--. 1 root root 644870765 Jan 31 22:24 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
-rwxr-xr-x. 1 root root 28474 Jan 30 05:39 mysqld_safe
-rwxr-xr-x. 1 root root 10576 Jan 30 05:39 mysql.server
-rw-r--r--. 1 root root 139 Jan 30 05:39 performance_collection
-rw-r--r--. 1 root root 234 Jan 31 21:02 pid.sh
-rw-r--r--. 1 root root 1852 Jan 30 05:39 profile
-rw-r--r--. 1 root root 3510 Feb 1 02:25 setup.sh
-rw-r--r--. 1 root root 679 Jan 30 05:39 sysctl.conf
[root@wn19testcdb1001 mysqlx]# cat bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
# User specific aliases and functions
2 . config
[root@wn19testcdb1001 mysqlx]# cat config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
# SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
# SELINUXTYPE=targeted
3.limits.conf
[root@wn19testcdb1001 mysqlx]# cat limits.conf
mysql soft nofile 655360
mysql soft stack 10240
mysql soft nproc 65536
4. m_bash_profile
[root@wn19testcdb1001 mysqlx]# cat m_bash_profile
export MYSQL_HOME=/home/db/mysql
export PATH=$PATH:/home/db/mysql/bin:/home/db/mysql/lib
export PS1='$LOGNAME@'`hostname`:'$PWD''$ '
if [ -t 0 ]; then
stty intr ^C
fi
5. my.cnf
[root@wn19testcdb1001 mysqlx]# cat my.cnf
[mysql]
port = 13306
socket = /home/db/mysql/mysql.sock
init-command = set names utf8mb4
prompt=\u@\h\\R:\\m:\\s[\d]>
[mysqld]
server-id = 10 #denpend on server_id rule (slave:20/30/40)
port = 13306
user = mysql
#user = root
basedir = /home/db/mysql
#password=Root@123
############## M-S relpicate ###########
server-id = 10
log-bin=/mysqlbinlog/master-bin #slave-->/mysqlbinlog/slave[n]-bin
relay_log=/mysqlbinlog/relay/master-relay-bin #slave--/mysqlbinlog/relay/slave-relay-bin
#read_only=ON
secure-file-priv='/tmp'
datadir = /mysqldata/data
tmpdir = /mysqldata/tmp
socket = /home/db/mysql/mysql.sock
character_set_server=utf8mb4
collation_server= utf8mb4_bin
event_scheduler=OFF
lower_case_table_names=1
pid-file=/mysqldata/data/mysqld.pid
autocommit=0
log_timestamps=SYSTEM
binlog_rows_query_log_events=off
show_compatibility_56=on
############# replication #############
#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=1000 # 1 second
#rpl_semi_sync_slave_enabled=1
############# binlog #############
# log-bin=/mysqlbinlog/master-bin #slave-->/mysqlbinlog/slave[n]-bin
binlog_cache_size=2M
expire_logs_days=10
############# replication #############
master_info_repository=table
relay_log_info_repository=table
gtid-mode=ON
enforce-gtid-consistency=ON
log-slave-updates=1
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=4
# relay_log=/mysqlbinlog/relay/master-relay-bin #slave--/mysqlbinlog/relay/slave-relay-bin
############# slow log #############
slow_query_log=1
slow_query_log_file = /mysqldata/logs/mysql_slow.log
long_query_time = 1
############# error log #############
log-error =/mysqldata/logs/mysql_error.log
############# thread #############
max_connections = 4000 #dependent on machine
key_buffer_size = 256M
max_allowed_packet = 32M
table_open_cache = 4000
table_open_cache_instances = 4 #CPU COUNT
sort_buffer_size = 8M
read_rnd_buffer_size=1M
join_buffer_size = 2M
tmp_table_size = 64M
max_heap_table_size = 128M
############# innodb #############
innodb_data_file_path=ibdata1:24M:autoextend
innodb_buffer_pool_size = 4G #physical memory’s 50%
innodb_buffer_pool_instances = 4 #CPU COUNT
innodb_log_file_size = 1GB
innodb_log_files_in_group = 4
innodb_log_buffer_size = 32M
innodb_lock_wait_timeout = 600
innodb_thread_concurrency = 4 #CPU COUNT
innodb_flush_method=O_DIRECT
innodb_read_io_threads =16
innodb_write_io_threads =16
innodb_io_capacity = 800
innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:2G
innodb_flush_log_at_timeout=2
log_bin_trust_function_creators=1
transaction_isolation=read-committed
innodb_undo_directory=/mysqldata/data
innodb_undo_log_truncate=1
innodb_undo_tablespaces=4
innodb_max_undo_log_size=2G
innodb_purge_rseg_truncate_frequency=16
############# safe #############
plugin-load=validate_password.so
validate-password=FORCE_PLUS_PERMANENT
############# other #############
init_file=/home/db/mysql/scripts/performance_collection
default-time-zone=SYSTEM
[client]
port = 13306
user=root
socket = /home/db/mysql/mysql.sock
password=Root@123
#[mysqladmin]
#user=root
#password=Root@123
6. performance_collection
[root@wn19testcdb1001 mysqlx]# cat performance_collection
UPDATE performance_schema.setup_consumers SET enabled = 'yes';
UPDATE performance_schema.setup_instruments SET enabled='yes' ,timed='yes';
7.profile
[root@wn19testcdb1001 mysqlx]# cat profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`/usr/bin/id -u`
UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
export PATH=$PATH:/home/db/mysql/bin:/home/db/mysql/lib
8. sysctl.conf
[root@wn19testcdb1001 mysqlx]# cat sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
fs.aio-max-nr = 4194304
fs.file-max = 6815744
kernel.shmall = 167772160
kernel.shmmax = 687194767360
kernel.shmmni = 4096
kernel.sem = 10000 40960000 10000 4096
net.ipv4.ip_local_port_range = 20000 65535
vm.swappiness=1#避免OOM
9.自动建库脚本 setup.sh
#!/bin/bash
#if [ $# -ne 2 ] ;then
# exit
#fi
# MASTER=flase
# SLAVE=$1
# slave_ip=$2
source /etc/profile
Init_env()
{
mkdir -p /home/db/mysql /home/db/mysql/scripts /mysqlbackup /mysqldata /mysqldata/data /mysqldata/tmp /mysqldata/logs /mysqlbinlog /mysqlbinlog/relay
chown mysql:mysql /mysqldata
chown mysql:mysql /mysqldata/data
chown mysql:mysql /mysqldata/tmp
chown mysql:mysql /mysqldata/logs
chown mysql:mysql /mysqlbinlog
chown mysql:mysql /mysqlbinlog/relay
chown mysql:mysql /mysqlbackup
}
InitUser()
{
echo '==>init root user password to Root@0101'
tmp_password=`cat /mysqldata/logs/mysql_error.log | grep "A temporary password is generated for" | awk -F 'host:' '{print $2}' | tr -d ' '`
mysql -uroot -p$tmp_password --connect-expired-password -e "alter user root@'localhost' identified by 'Root@0101'; flush privileges "
mysql -uroot -pRoot@0101 -e "use mysql;update user set host='%' where user='root';flush privileges"
mysql -uroot -pRoot@0101 -e "create user repl@'%' identified by 'Repl@0101';
grant replication slave on *.* to repl@'%';
create user patrol@'127.0.0.1' identified by 'Patrol@0101';
grant select, process, replication client on *.* to patrol@'127.0.0.1';
create user mymon@'%' identified by 'Mymon@0101';
grant select,process, replication slave, show databases, show view ,replication client on *.* to mymon@'%';
create user applaud@'%' identified by 'Applaud@0101';
grant select on *.* to applaud@'%';
create user xtrabk@'127.0.0.1' identified by 'Xtrabk@0101';
grant reload, lock tables, process, replication client on *.* to xtrabk@'127.0.0.1';
create user mha@'%' identified by 'Mha@0101';
grant all on *.* to mha@'%';
flush privileges;
reset master;"
mysql -uroot -pRoot@0101 -e "INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
SET GLOBAL rpl_semi_sync_master_enabled = 1;SET GLOBAL rpl_semi_sync_master_timeout = 1000;SET GLOBAL rpl_semi_sync_slave_enabled = 1;"
sed -i 's/\#rpl_semi_sync/rpl_semi_sync/g' /etc/my.cnf
}
Stop_db()
{
mysqladmin shutdown -u root -pRoot@0101 --socket=/home/db/mysql/mysql.sock
}
Start_db_finaly()
{
mysqld_safe --defaults-file=/etc/my.cnf
}
CreateMaster()
{
echo 'Config my.cnf and Create master db'
}
CreateSlave()
{
echo 'Config slave my.cnf'
id=`date +%s | cut -c 1-8`
sed -i "s/server-id = 10/server-id = $id/g" /etc/my.cnf
sed -i "s/master-bin/slave$id-bin/g" /etc/my.cnf
sed -i "s/master-relay-bin/slave-relay-bin/g" /etc/my.cnf
}
CreateDB()
{
echo "==>create and init db"
/home/db/mysql/bin/mysqld --initialize --user=mysql >/dev/null 2>/dev/null
echo "==>sed /home/db/mysql/bin/mysqld_safe"
sed -i 's/\/usr\/local\/mysql\/data/\/mysqldata\/data/g' /home/db/mysql/bin/mysqld_safe
sed -i 's/\/usr\/local\/mysql/\/home\/db\/mysql/g' /home/db/mysql/bin/mysqld_safe
}
Start_db()
{
echo "==>/home/db/mysql/bin/mysqld_safe"
/home/db/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf >/dev/null 2>/dev/null &
while true
do
pfile=/mysqldata/data/mysqld.pid
if [ ! -f $pfile ] ;then
sleep 3
else
pid=`cat /mysqldata/data/mysqld.pid`
status=`ps -ef | grep $pid |grep -v grep | wc -l`
if [ "$status" -eq 1 ] ; then
break
fi
sleep 3
fi
done
}
file=/mysqldata/logs/mysql_error.log
if [ ! -f $file ]; then
if [ ${SLAVE} == 'true' ] ;then
CreateSlave
fi
Init_env
CreateDB
Start_db
InitUser
Stop_db
Start_db
if [ ${SLAVE} == 'true' ]; then
mysql -uroot -pRoot@0101 -e "CHANGE MASTER TO MASTER_HOST='${SLAVE_IP}',MASTER_PORT=13306,MASTER_USER='repl',MASTER_PASSWORD='Repl@0101', MASTER_AUTO_POSITION=1;start slave;"
fi
tail -f /mysqldata/logs/mysql_error.log
#Start_db_finaly
else
echo "==>/home/db/mysql/bin/mysqld_safe"
Start_db_finaly
fi
二、生成镜像
docker build --force-rm -t mysqltest1 .
三、生成主库容器test01
docker run -p 13307:13306 -v /root/mysqldata:/mysqldata --name test01 -d mysqltest1
查看容器test1的ip地址为172.171.0.2
docker inspect test01
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "e5185d0a652c38c58d067109cbcf4c3e7267909b56524a936b7711ee03807507",
"EndpointID": "1e9e452f9e578f33cce5237a5ad5c0cf014d5b9bfa2ce7699a6139a894a13b30",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
四、创建从库容器slave,slave1
docker run -p 13308:13306 -e SLAVE=true -e SLAVE_IP=172.17.0.2 --name slave -d mysqltest1
docker run -p 13309:13306 -e SLAVE=true -e SLAVE_IP=172.17.0.2 --name slave2 -d mysqltest1
[root@wn19testcdb1001 mysqlx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff8b3200e210 mysqltest1 "/root/setup.sh" 16 hours ago Up 16 hours 22/tcp, 80/tcp, 0.0.0.0:13309->13306/tcp slave2
3d24233adf73 mysqltest1 "/root/setup.sh" 16 hours ago Up 16 hours 22/tcp, 80/tcp, 0.0.0.0:13308->13306/tcp slave
9e7d9c46611e mysqltest1 "/root/setup.sh" 16 hours ago Up 16 hours 22/tcp, 80/tcp, 0.0.0.0:13307->13306/tcp test01
登录端口为13308的库,查看主从状态:
[root@wn19testcdb1001 mysqlx]# mysql -uroot -pRoot@0101 -h192.168.136.140 -P13308
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
[email protected]:10:44[(none)]>show slave status \G ;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.2
Master_User: repl
Master_Port: 13306
Connect_Retry: 60
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 728
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 943
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 728
Relay_Log_Space: 1150
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10
Master_UUID: 035246a1-2602-11e9-9252-0242ac110002
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 035246a1-2602-11e9-9252-0242ac110002:1-3
Executed_Gtid_Set: 035246a1-2602-11e9-9252-0242ac110002:1-3
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.03 sec)
登录端口为13309的库,查看主从状态:
[root@wn19testcdb1001 mysqlx]# mysql -uroot -pRoot@0101 -h192.168.136.140 -P13309
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
[email protected]:11:50[(none)]>show slave status \G ;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.2
Master_User: repl
Master_Port: 13306
Connect_Retry: 60
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 728
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 943
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 728
Relay_Log_Space: 1150
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10
Master_UUID: 035246a1-2602-11e9-9252-0242ac110002
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 035246a1-2602-11e9-9252-0242ac110002:1-3
Executed_Gtid_Set: 035246a1-2602-11e9-9252-0242ac110002:1-3
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
至此,利用docker部署mysql数据库,自动搭建主从完成。可以实现一主N从。