c语言sscanf函数的用法是什么
275
2022-10-28
用Dockerfile构建MySQL镜像并实现容器启动过程中MySQL数据库系统的初始化
前一段时间就在研究用Dockerfile构建MySQL镜像并实现容器启动过程中MySQL数据库系统的初始化,但被一些无关紧要的事儿给耽误了,经过查阅《dockerfile最佳实践》及MySQL数据库的迁移与备份还原的资料,整理了操作过程,并进行了实验,确认此方法可行:MySQL数据库系统的初始化是指MySQL安装到OS上后所经历的initinstall过程,只有经过初始化的数据库系统才能提供数据存储服务、才能被应用程序使用。实现MySQL在解压安装过程或容器启动过程中的数据库系统的初始化,有三个思路:1、在CentOS环境中安装配置好MySQL,使MySQL处于可对外服务状态,并对此环境进行打包制作成虚拟化的镜像包;2、在启动容器的过程中指定MySQL的环境变量,通过环境变量和初始化shell脚本完成MySQL数据库系统的初始化;3、通过Dockerfile指定镜像的构建过程,并提前准备好MySQL数据库系统初始化所必须的meta数据和shell脚本,这相当于对数据库系统进行迁移还原。本次实验采用的即是思路三。因为在实践过程中,思路一相当于为特定应用做的个性化定制,思路二在操作过程中总会遇到数据库连接失败的故障,稳定性较差。
本次实验操作的环境:CentOS 7 1804,VMware 15 Pro,4GB内存、4核心CPU,宿主OS为Windows7SP1 64Bit。
操作系统环境准备[googlebigtable@localhost ~]$ su - rootPassword: Last login: Tue May 19 02:28:07 EDT 2020 on pts/0[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.original[root@localhost ~]# ls -F /etc/yum.repos.d/CentOS-Base.repo.original CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo 03:00:48-- mirrors.163.com (mirrors.163.com)... 59.111.0.251Connecting to mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 1572 (1.5K) [application/octet-stream]Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’
100%[==========================================================================================================>] 1,572 --.-K/s in 0s
2020-05-19 03:00:48 (460 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [1572/1572]
[root@localhost ~]# ls -F /etc/yum.repos.d/CentOS-Base.repo CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repoCentOS-Base.repo.original CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo[root@localhost ~]# cat -n /etc/yum.repos.d/CentOS-Base.repo1 # CentOS-Base.repo2 #3 # The mirror system uses the connecting IP address of the client and the4 # update status of each mirror to pick mirrors that are updated to and5 # geographically close to the client. You should use this for CentOS updates6 # unless you are manually picking other mirrors.7 #8 # If the mirrorlist= does not work for you, as a fall back you can try the 9 # remarked out baseurl= line instead.10 #11 #12 [base]13 name=CentOS-$releasever - Base - 163.com14 #mirrorlist= baseurl= gpgcheck=117 gpgkey= 19 #released updates20 [updates]21 name=CentOS-$releasever - Updates - 163.com22 #mirrorlist= baseurl= gpgcheck=125 gpgkey= 27 #additional packages that may be useful28 [extras]29 name=CentOS-$releasever - Extras - 163.com30 #mirrorlist= baseurl= gpgcheck=133 gpgkey= 35 #additional packages that extend functionality of existing packages36 [centosplus]37 name=CentOS-$releasever - Plus - 163.com38 baseurl= gpgcheck=140 enabled=041 gpgkey=~]# yum clean allLoaded plugins: fastestmirror, langpacksCleaning repos: base extras updatesCleaning up everything[root@localhost ~]# yum makecacheLoaded plugins: fastestmirror, langpacksDetermining fastest mirrorsbase | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/10): base/7/x86_64/group_gz | 153 kB 00:00:00 (2/10): base/7/x86_64/primary_db | 6.1 MB 00:00:04 .................................................................................................................................................urw-fonts.noarch 0:2.4-16.el7
Complete![root@localhost ~]#[root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.8.2003 (Core)[root@localhost ~]# hostnamectl statusStatic hostname: localhost.localdomainIcon name: computer-vmChassis: vmMachine ID: b1625b44a4c448b193b2318086920ba8Boot ID: c5894807414c44f8b7878f2412d39436Virtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7Kernel: Linux 3.10.0-862.el7.x86_64Architecture: x86-64[root@localhost ~]# rpm -q centos-releasecentos-release-7-8.2003.0.el7.centos.x86_64[root@localhost ~]# cat /proc/versionLinux version 3.10.0-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018[root@localhost ~]# uname -aLinux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux[root@localhost ~]#[root@localhost ~]# yum -y updateLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfileNo packages marked for update[root@localhost ~]#[root@localhost ~]# ls -Fanaconda-ks.cfg initial-setup-ks.cfg[root@localhost ~]# cd /home/googlebigtable/[root@localhost googlebigtable]#ls -FDesktop/ Documents/ Downloads/ Music/ Pictures/ Public/ Templates/ Videos/[root@localhost googlebigtable]# mkdir -p temp/dockerworking[root@localhost googlebigtable]# yum install -y treeLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfileResolving Dependencies--> Running transaction check..................................................................................................................................................Installed:tree.x86_64 0:1.6.0-10.el7
Complete![root@localhost googlebigtable]# tree temp/temp/└── dockerworking
1 directory, 0 files[root@localhost googlebigtable]#
用shell脚本安装Docker[root@localhost dockerworking]# curl -sSL | sh
Executing docker install script, commit: 26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c
sh -c 'yum install -y -q yum-utils'Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
sh -c 'yum-config-manager --add-repo plugins: fastestmirror, langpacksadding repo from: file to /etc/yum.repos.d/docker-ce.reporepo saved to /etc/yum.repos.d/docker-ce.repo
'[' stable '!=' stable ']'
sh -c 'yum makecache'Loaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfilebase | 3.6 kB 00:00:00 docker-ce-stable | 3.5 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/4): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:05 (2/4): docker-ce-stable/x86_64/primary_db | 42 kB 00:00:01 (3/4): docker-ce-stable/x86_64/other_db | 114 kB 00:00:01 (4/4): docker-ce-stable/x86_64/filelists_db | 20 kB 00:00:16 Metadata Cache Created
'[' -n '' ']'
sh -c 'yum install -y -q docker-ce'[Errno 12] Timeout on (28, 'Operation timed out after 30002 milliseconds with 0 out of 0 bytes received')Trying other mirror.[Errno 12] Timeout on (28, 'Operation timed out after 30001 milliseconds with 0 out of 0 bytes received')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.[Errno 12] Timeout on (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/containerd.io-1.2.13-3.2.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEYPublic key for containerd.io-1.2.13-3.2.el7.x86_64.rpm is not installedImporting GPG key 0x621E9F35:Userid : "Docker Release (CE rpm)
Remember that you will have to log out and back in for this to take effect!
WARNING: Adding a user to the "docker" group will grant the ability to runcontainers which can be used to obtain root privileges on thedocker host.Refer to more information.[root@localhost dockerworking]# 启动docker服务[root@localhost dockerworking]# yum list installed | grep dockercontainerd.io.x86_64 1.2.13-3.2.el7 @docker-ce-stabledocker-ce.x86_64 3:19.03.9-3.el7 @docker-ce-stabledocker-ce-cli.x86_64 1:19.03.9-3.el7 @docker-ce-stable[root@localhost dockerworking]# systemctl enable dockerCreated symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.[root@localhost dockerworking]# systemctl start docker[root@localhost dockerworking]# [root@localhost dockerworking]# ls -F /etc/ | grep dockerdocker/[root@localhost dockerworking]# ls -F /etc/docker/key.json[root@localhost dockerworking]# ttee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["["dockerworking]# cat -n /etc/docker/daemon.json1 {2 registry-mirrors": [" }[root@localhost dockerworking]#[root@localhost dockerworking]# systemctl daemon-reload[root@localhost dockerworking]#systemctl restart docker[root@localhost dockerworking]# service docker restartRedirecting to /bin/systemctl restart docker.service[root@localhost dockerworking]#[root@localhost dockerworking]# ls -Fdockerubuntu/ gbtwithsshv0.tar[root@localhost dockerworking]# cd dockerubuntu/[root@localhost dockerubuntu]# vim Dockerfile[root@localhost dockerubuntu]# vim setup.sh[root@localhost dockerubuntu]# vim schema.sql[root@localhost dockerubuntu]# vim privileges.sql[root@localhost dockerubuntu]# ls -FDockerfile privileges.sql schema.sql setup.sh[root@localhost dockerubuntu]# cat -n Dockerfile1 FROM mysql:5.7.252 ENV MYSQL_ALLOW_EMPTY_PASSWORD yes3 COPY setup.sh /mysql/setup.sh4 COPY schema.sql /mysql/schema.sql5 COPY privileges.sql /mysql/privileges.sql6 CMD ["sh", "/mysql/setup.sh"][root@localhost dockerubuntu]# cat -n setup.sh 1 #!/bin/bash2 3 set -e4 5 6 echo '1. starting mysql...'7 8 service mysql start9 10 11 echo '2. creating database...'12 13 mysql < /mysql/schema.sql14 15 echo '3. setting password....'16 17 mysql < /mysql/privileges.sql18 19 echo service mysql status20 21 echo '4. mysql for testformysqlinit is ready...'22 23 24 tail -f /dev/null[root@localhost dockerubuntu]# cat -n schema.sql 1 create database if not exists testformysqlinit;[root@localhost dockerubuntu]# cat -n privileges.sql 1 use mysql;2 set password for root@localhost = password('qwertyuiop2020');3 grant all on . to root@'%' identified by 'qwertyuiop2020' with grant option;4 flush privileges;[root@localhost dockerubuntu]#[root@localhost dockerubuntu]# docker build -t mysqlinit:mysql5725 .Sending build context to Docker daemon 5.12kBStep 1/6 : FROM mysql:5.7.255.7.25: Pulling from library/mysql27833a3ba0a5: Pull complete 864c283b3c4b: Pull complete cea281b2278b: Pull complete 8f856c14f5af: Pull complete 9c4f38c23b6f: Pull complete 1b810e1751b3: Pull complete 5479aaef3d30: Pull complete 1d924ec3d520: Pull complete 1ab7ae63ac60: Pull complete 08aa5f3680e9: Pull complete a832d0a0972a: Pull complete Digest: sha256:dba5fed182e64064b688ccd22b2f9cad4ee88608c82f8cff21e17bab8da72b81Status: Downloaded newer image for mysql:5.7.25---> 98455b9624a9Step 2/6 : ENV MYSQL_ALLOW_EMPTY_PASSWORD yes---> Running in 20c67e7a2821Removing intermediate container 20c67e7a2821---> 7b8f3e8b35e8Step 3/6 : COPY setup.sh /mysql/setup.sh---> 2eb86def7a40Step 4/6 : COPY schema.sql /mysql/schema.sql---> 212e9fbec90dStep 5/6 : COPY privileges.sql /mysql/privileges.sql---> 83160ca01bd7Step 6/6 : CMD ["sh", "/mysql/setup.sh"]---> Running in 67b4de01a0cfRemoving intermediate container 67b4de01a0cf---> b43048b18ff1Successfully built b43048b18ff1Successfully tagged mysqlinit:mysql5725[root@localhost dockerubuntu]#[root@localhost dockerubuntu]# docker run --name mysqlinit -p 33060:3306 -d mysqlinit:mysql57255e0dfdfb6d034b8b62641b7fbf670e034cbb24486c456e63d8fbf952ec15e8e4[root@localhost dockerubuntu]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysqlinit mysql5725 b43048b18ff1 21 minutes ago 372MB
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A
Database changedmysql> select user from user;+---------------+| user |+---------------+| root || mysql.session || mysql.sys || root |+---------------+4 rows in set (0.00 sec)
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys || testformysqlinit |+--------------------+5 rows in set (0.00 sec)
mysql>select user,host from user;+---------------+-----------+| user | host |+---------------+-----------+| root | % || mysql.session | localhost || mysql.sys | localhost || root | localhost |+---------------+-----------+4 rows in set (0.00 sec)
mysql> quit;br/>Byeroot@5e0dfdfb6d03:/#root@5e0dfdfb6d03:/# exitexit[root@localhost dockerubuntu]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5e0dfdfb6d03 mysqlinit:mysql5725 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 33060/tcp, 0.0.0.0:33060->3306/tcp mysqlinit0d55d28e7556 6e4f1fe62ff1 "/bin/sh -c 'apt-get…" 48 minutes ago Exited (100) 40 minutes ago hungry_hooverfe69ea4fceb2 cf49811e3cdb "/bin/sh -c 'yum -y …" 54 minutes ago Exited (1) 53 minutes ago nice_chaum447f347b4807 fdbcd047e06b "/bin/sh -c 'apt-get…" 2 hours ago Exited (100) About an hour ago inspiring_diffied5aca6ac8f01 fdbcd047e06b "/bin/sh -c 'apt-get…" 3 hours ago Exited (100) 2 hours ago fervent_poincaree103c446c82b fcdab87e5a82 "hostname" 4 hours ago Exited (0) 4 hours ago fervent_leakeye2d84d1bc763 fcdab87e5a82 "hostnamectl status" 4 hours ago Created cool_bhabhad93e878ffb47 fcdab87e5a82 "hostnamectl status" 4 hours ago Exited (1) 4 hours ago silly_moore7d33e9e3a685 fcdab87e5a82 "/bin/date" 4 hours ago Exited (0) 4 hours ago clever_merkle87db9e8e1ca0 fcdab87e5a82 "/bin/echo 'I'm excu…" 4 hours ago Exited (0) 4 hours ago stoic_benzd64d6b812a74 8185bda8662c "/bin/echo this imag…" 4 hours ago Exited (0) 4 hours ago upbeat_sutherlanda3a6494e9354 8185bda8662c "/bin/echo hi,this i…" 4 hours ago Exited (0) 4 hours ago nifty_tereshkova38cb9c00ee5f wordpress "docker-entrypoint.s…" 5 hours ago Up 5 hours 0.0.0.0:8080->80/tcp wordpressc90e359d5af0 mysql:5.5 "docker-entrypoint.s…" 5 hours ago Up 5 hours 0.0.0.0:3306->3306/tcp mysql8c6eb8f8d4ba cf49811e3cdb "/usr/sbin/init" 25 hours ago Exited (137) 16 hours ago sshb2dfd487a2f3 cf49811e3cdb "/usr/sbin/init" 25 hours ago Exited (137) 25 hours ago great_taussig89e35ee3bc0e cf49811e3cdb "/bin/bash" 25 hours ago Exited (137) 25 hours ago centos7.5[root@localhost dockerubuntu]#[root@localhost dockerubuntu]# docker commit -a "googlebigtable" -m "mysqlwithinit" 5e0dfdfb6d03 mysql5725init:v0sha256:3587682e3a3c76167740f21b514fdd205aa9386adf693105355b87abccabe06d[root@localhost dockerubuntu]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql5725init v0 3587682e3a3c 11 seconds ago 372MBmysqlinit mysql5725 b43048b18ff1 35 minutes ago 372MB
至此,本次实验结束。
孟伯,20200520
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~