Install MySQL 5.7 in the Docker
Install Docker on Oracle Linux 7
在Oracle Linux 7.1上安装Docker
1.首先使用正确的yum设置来升级Oracle Linux 7.1实例。为了安装最新的Docker版本(18.9.1.ce),需要
ol7_latest,ol7_uekr4与ol7_addons启用
[root@localhost /]# cd /etc/yum.repos.d/
[root@localhost /]#wget http://yum.oracle.com/public-yum-ol7.repo
[root@localhost yum.repos.d]# vi public-yum-ol7.repo
[ol7_latest]
name=Oracle Linux
basearch)
baseurl=http://yum.oracle.com/repo/OracleLinux/OL7/latest/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1
[ol7_UEKR4]
name=Latest Unbreakable Enterprise Kernel Release 4 for Oracle Linux
basearch)
baseurl=http://yum.oracle.com/repo/OracleLinux/OL7/UEKR4/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1
[ol7_addons]
name=Oracle Linux
basearch)
baseurl=http://yum.oracle.com/repo/OracleLinux/OL7/addons/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1
2.开始安装docker
[root@localhost yum.repos.d]# yum install docker-engine
Loaded plugins: langpacks
ol7_UEKR4 | 2.5 kB 00:00:00
ol7_addons | 1.2 kB 00:00:00
ol7_latest | 2.7 kB 00:00:00
(1/4): ol7_addons/x86_64/updateinfo | 60 kB 00:00:00
(2/4): ol7_UEKR4/x86_64/updateinfo | 82 kB 00:00:01
(3/4): ol7_addons/x86_64/primary | 82 kB 00:00:02
(4/4): ol7_UEKR4/x86_64/primary_db | 4.0 MB 00:03:38
ol7_addons 328/328
Resolving Dependencies
–> Running transaction check
—> Package docker-engine.x86_64 0:18.09.1.ol-1.0.5.el7 will be installed
…省略…
Dependency Updated:
cryptsetup.x86_64 0:2.0.3-3.el7 cryptsetup-libs.x86_64 0:2.0.3-3.el7 cryptsetup-python.x86_64 0:2.0.3-3.el7 device-mapper.x86_64 7:1.02.149-10.0.3.el7_6.8
device-mapper-event.x86_64 7:1.02.149-10.0.3.el7_6.8 device-mapper-event-libs.x86_64 7:1.02.149-10.0.3.el7_6.8 device-mapper-libs.x86_64 7:1.02.149-10.0.3.el7_6.8 device-mapper-persistent-data.x86_64 0:0.7.3-3.el7
dracut-config-rescue.x86_64 0:033-554.0.3.el7 dracut-network.x86_64 0:033-554.0.3.el7 glib2.x86_64 0:2.56.1-4.el7_6 kmod.x86_64 0:20-23.0.1.el7
libgudev1.x86_64 0:219-62.0.4.el7_6.7 libnl3.x86_64 0:3.2.28-4.el7 libnl3-cli.x86_64 0:3.2.28-4.el7 libseccomp.x86_64 0:2.3.1-3.el7
libselinux.x86_64 0:2.5-14.1.el7 libselinux-python.x86_64 0:2.5-14.1.el7 libselinux-utils.x86_64 0:2.5-14.1.el7 libsemanage.x86_64 0:2.5-14.el7
libsemanage-python.x86_64 0:2.5-14.el7 libsepol.x86_64 0:2.5-10.el7 lvm2.x86_64 7:2.02.180-10.0.3.el7_6.8 lvm2-libs.x86_64 7:2.02.180-10.0.3.el7_6.8
lvm2-python-libs.x86_64 7:2.02.180-10.0.3.el7_6.8 policycoreutils.x86_64 0:2.5-29.0.1.el7_6.1 policycoreutils-python.x86_64 0:2.5-29.0.1.el7_6.1 selinux-policy.noarch 0:3.13.1-229.0.3.el7_6.12
selinux-policy-targeted.noarch 0:3.13.1-229.0.3.el7_6.12 setools-libs.x86_64 0:3.3.8-4.el7 systemd.x86_64 0:219-62.0.4.el7_6.7 systemd-libs.x86_64 0:219-62.0.4.el7_6.7
systemd-python.x86_64 0:219-62.0.4.el7_6.7 systemd-sysv.x86_64 0:219-62.0.4.el7_6.7
Complete!
一旦安装完成,就可以启动docker服务
[root@localhost soft]# service docker start
Redirecting to /bin/systemctl start docker.service
[root@localhost soft]# systemctl enable docker
[root@localhost soft]# service docker status
Redirecting to /bin/systemctl status docker.service
鈼[0m docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/docker.service.d
鈹斺攢docker-sysconfig.conf
Active: active (running) since Mon 2019-06-10 23:15:05 CST; 6h left
Docs: https://docs.docker.com
Main PID: 1452 (dockerd)
Memory: 132.2M
CGroup: /system.slice/docker.service
鈹溾攢1452 /usr/bin/dockerd --selinux-enabled --storage-driver devicemapper --storage-opt dm.basesize=25G
鈹斺攢2436 containerd --config /var/run/docker/containerd/containerd.toml --log-level info
Jun 10 23:15:03 localhost.localdomain dockerd[1452]: time=“2019-06-10T23:15:03.843382248+08:00” level=info msg=“Successfully created filesystem xfs on device docker-253:0-1047242-base” storage-driver=devicemapper
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time=“2019-06-10T23:15:04.076789613+08:00” level=info msg=“Graph migration to content-addressability took 0.00 seconds”
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time=“2019-06-10T23:15:04.078665673+08:00” level=warning msg=“mountpoint for pids not found”
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time=“2019-06-10T23:15:04.079906275+08:00” level=info msg=“Loading containers: start.”
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time=“2019-06-10T23:15:04.554467519+08:00” level=info msg=“Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to…rred IP address”
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time=“2019-06-10T23:15:04.859786800+08:00” level=info msg=“Loading containers: done.”
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time=“2019-06-10T23:15:04.959099871+08:00” level=info msg=“Docker daemon” commit=c3ab8a8 graphdriver(s)=devicemapper version=18.09.1-ol
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time=“2019-06-10T23:15:04.959938124+08:00” level=info msg=“Daemon has completed initialization”
Jun 10 23:15:05 localhost.localdomain dockerd[1452]: time=“2019-06-10T23:15:05.053166130+08:00” level=info msg=“API listen on /var/run/docker.sock”
Jun 10 23:15:05 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
然后可以下载镜像文件
[root@localhost soft]# docker pull mysql/mysql-server:5.7
Trying to pull repository docker.io/mysql/mysql-server …
5.7: Pulling from docker.io/mysql/mysql-server
35defbf6c365: Pull complete
0fa46ab0f51d: Pull complete
f70f5000008c: Pull complete
892ac46af8c0: Pull complete
Digest: sha256:ddb046076781a15200d36cb01f8f512431c3481bedebd5e92646d8c617ae212c
Status: Downloaded newer image for mysql/mysql-server:5.7
[root@localhost soft]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql/mysql-server 5.7 857eadf53a54 6 weeks ago 258MB
使用Docker来部署MySQL
下载MySQL Server Docker镜像文件
严格来说,不需要在单独的步骤中下载服务器镜像,但是,在创建Docker容器之前执行此步骤可以确保本地映像是最
新的。为了下载MySQL社区版镜像,执行以下命令:
docker pull mysql/mysql-server:tag
tag是你想要pull镜像版本的标识(例如,5.5,5.6,5.7,8.0或最新版本)。如果:tag被忽略,latest标记会被使用并
且最新通用可用版本的MySQL社区版本镜像将会被下载。网址
https://hub.docker.com/r/mysql/mysql-server/tags/列出了所有可用版本信息.
[root@localhost soft]# docker pull mysql/mysql-server:5.7
Trying to pull repository docker.io/mysql/mysql-server …
5.7: Pulling from docker.io/mysql/mysql-server
35defbf6c365: Pull complete
0fa46ab0f51d: Pull complete
f70f5000008c: Pull complete
892ac46af8c0: Pull complete
Digest: sha256:ddb046076781a15200d36cb01f8f512431c3481bedebd5e92646d8c617ae212c
Status: Downloaded newer image for mysql/mysql-server:5.7
如果要显示Docker镜像可以执行以下命令:
[root@localhost soft]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql/mysql-server 5.7 857eadf53a54 6 weeks ago 258MB
为了下载MySQL企业版本镜像,执行以下命令:
docker pull store/oracle/mysql-enterprise-server:5.7
5.7是我们想要pull的镜像版本。MySQL企业版本容器只提供了最新的通用可用版本5.7
启动MySQL Server实例
为MySQL社区版本服务器启动一个新的Docker容器执行以下命令:
[root@localhost ~]# docker run --name=mysql1 -d mysql/mysql-server:5.7
5128014e440f10c557b52e6203445a01c97728e432d0f231c6027bf4b15520bd
下面通过-p参数将端口从容器中映射到主机,通过–expose参数来暴露端口
[root@localhost soft]# docker run --name=mysql1 --expose=33306 -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql/mysql-server:5.7
可以通过主机中的mysql程序来访问容器中的mysql
[root@localhost ~]# su - mysql
上一次登录:二 10月 29 09:21:19 CST 2019pts/0 上
[mysql@localhost ~]$ mysql --host=192.168.1.250 --port=33306 -uroot -p123456
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 18
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright © 2000, 2019, 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.
mysql>
[mysql@localhost ~]$ mysql --host=192.168.1.250 --port=33306 -uroot -p123456 -e ‘create table imptest(id int,n varchar(30))’ mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
在主机上创建文件
[root@localhost mysql]# cat imptest.txt
100|Max Sydow
101|Count Dracula
将主机上的文件复制到容器
最近在用Docker布署hadoop,要将文件上传到HDFS首先文件得在Docker容器中吧,网上提供的方法差不多有三种
1.用-v挂载主机数据卷到容器内
2.直接在主机上拷贝到容器物理存储系统
3.用输入输出符
具体方法这篇文章写的很详细:http://blog.csdn.net/yangzhenping/article/details/43667785
但是对这三种方法我都不太喜欢,无意间看到另位一种方法供大家参考:
从主机复制到容器sudo docker cp host_path containerID:container_path
从容器复制到主机sudo docker cp containerID:container_path host_path
容器ID的查询方法想必大家都清楚:docker ps -a
root@localhost mysql]# docker cp imptest.txt 55936f0debd0:/var/lib/mysql-files/
[root@localhost mysql]# cat imptest.txt
100|Max Sydow
101|Count Dracula
[mysql@localhost ~]$ mysqlimport --host=192.168.1.250 --port=33306 -uroot -p123456 mysql /var/lib/mysql-files/imptest.txt --fields-terminated-by="|"
mysqlimport: [Warning] Using a password on the command line interface can be insecure.
mysql.imptest: Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
[mysql@localhost ~]$ mysql --host=192.168.1.250 --port=33306 -uroot -p123456 mysql -e ‘select * from imptest’;
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----±--------------+
| id | n |
±-----±--------------+
| 100 | Max Sydow |
| 101 | Count Dracula |
±-----±--------------+
为MySQL企业版本服务器启动一个新的Docker容器执行以下命令:
docker run --name=mysql1 -d store/oracle/mysql-enterprise-server:tag
–name选项,用来为你的服务容器指定名字(例如mysql1),它是可选项。如果没有提供容器名,会生成一个随机容
器名。如果之前的docker pull或docker run命令所指定名称或标记的Docker镜像没有下载,那么镜像现在就会被
下载。在下载完成后,开始初始化容器,并且当你执行docker ps命令时容器会出现在正在运行的容器列表中。例如:
[[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5128014e440f mysql/mysql-server:5.7 "/entrypoint.sh mysq鈥 42 seconds ago Up 39 seconds (health: starting) 3306/tcp, 33060/tcp mysql1
IMAGE列的输出对于MySQL社区版本读取的值为mysql/mysql-server,对于MySQL企业版本读取的值为store/oracle/
mysql-enterprise-server。
容器初始化可能会花费一些时间。当服务准备好使用时,docker ps命令输出的STATUS列将从(health: starting)改
变为(healthy)。
在上面的docker run命令中-d选项使容器在后台运行。使用下面的命令可以监控容器的输出信息:
[root@localhost ~]# docker logs mysql1
[Entrypoint] MySQL Docker Image 5.7.26-1.1.11
[Entrypoint] No password option specified for new database.
[Entrypoint] A random onetime password will be generated.
[Entrypoint] Initializing database
[Entrypoint] Database initialized
Warning: Unable to load ‘/usr/share/zoneinfo/iso3166.tab’ as time zone. Skipping it.
Warning: Unable to load ‘/usr/share/zoneinfo/leapseconds’ as time zone. Skipping it.
Warning: Unable to load ‘/usr/share/zoneinfo/tzdata.zi’ as time zone. Skipping it.
Warning: Unable to load ‘/usr/share/zoneinfo/zone.tab’ as time zone. Skipping it.
Warning: Unable to load ‘/usr/share/zoneinfo/zone1970.tab’ as time zone. Skipping it.
[Entrypoint] GENERATED ROOT PASSWORD: 3z[omLAk4Jaz@sYxDiLYnyPySYfR
[Entrypoint] ignoring /docker-entrypoint-initdb.d/*
[Entrypoint] Server shut down
[Entrypoint] Setting root user as expired. Password will need to be changed before database can be used.
[Entrypoint] MySQL init process done. Ready for start up.
[Entrypoint] Starting MySQL 5.7.26-1.1.11
一旦初始化完成,命令的输出将会包含为root用户生成随机密码的信息,也可以执行下面的命令来检查密码:
[root@localhost ~]# docker logs mysql1 2>&1 | grep GENERATED
[Entrypoint] GENERATED ROOT PASSWORD: 3z[omLAk4Jaz@sYxDiLYnyPySYfR
在容器中连接MySQL服务器
一旦服务可以使用后,可以在刚刚启动的MySQL服务容器中运行mysql客户端程序并连接到MySQL服务。在刚刚启动的
Docker容器中使用docker exec -it命令来启动mysql客户端程序,例如:
[root@localhost ~]# docker exec -it mysql1 mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 107
Server version: 5.7.26
Copyright © 2000, 2019, 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.
mysql>
在执行上面的命令时需要输入root用户生成的密码。因为mysql_onetime_password选项缺省情况为true(对于MySQL
社区版本来说),在mysql客户端连接到MySQL服务后,必须要通过执行以下命令来修改root用户的密码:
mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘123456’;
Query OK, 0 rows affected (0.00 sec)
一旦修改密码后,MySQL服务就可以使用。
容器Shell访问
为了使用shell访问你的MySQL服务器容器,在容器中使用docker exec -it命令来启动bash shell:
[root@localhost ~]# docker exec -it mysql1 bash
bash-4.2#
在启动bash shell后,在容器中可以运行linux命令,例如,为了查看容器中的MySQL数据库的数据目录中的内容,执
行以下命令:
bash-4.2# ls /var/lib/mysql
auto.cnf ca.pem client-key.pem ib_logfile0 ibdata1 mysql mysql.sock.lock private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile1 ibtmp1 mysql.sock performance_schema public_key.pem server-key.pem
停止与删除一个MySQL容器
为了停止已经创建的MySQL服务容器执行下面的命令:
[root@localhost ~]# docker stop mysql1
mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5128014e440f mysql/mysql-server:5.7 “/entrypoint.sh mysql” 21 hours ago Exited (0) 8 seconds ago mysql1
docker stop将发送一个SIGTERM信号给mysqld进程,因此服务将被优雅的关闭。
还可以观察到当一个容器的主进程(一个MySQL服务容器的主进程为mysqld)被关闭,Docker容器也会自动关闭。
启动MySQL服务容器:
[root@localhost ~]# docker start mysql1
mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5128014e440f mysql/mysql-server:5.7 “/entrypoint.sh mysql” 22 hours ago Up 5 seconds (health: starting) 3306/tcp, 33060/tcp mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5128014e440f mysql/mysql-server:5.7 “/entrypoint.sh mysql” 22 hours ago Up 15 seconds (health: starting) 3306/tcp, 33060/tcp mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5128014e440f mysql/mysql-server:5.7 “/entrypoint.sh mysql” 22 hours ago Up 21 seconds (health: starting) 3306/tcp, 33060/tcp mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5128014e440f mysql/mysql-server:5.7 “/entrypoint.sh mysql” 22 hours ago Up 32 seconds (healthy) 3306/tcp, 33060/tcp mysql1
为了使用单个命令停止与再次启动MySQL服务容器:
[root@localhost ~]# docker restart mysql1
mysql1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5128014e440f mysql/mysql-server:5.7 “/entrypoint.sh mysql” 24 hours ago Up 6 seconds (health: starting) 3306/tcp, 33060/tcp mysql1
为了删除MySQL容器,首先停止容器,然后再使用docker rm命令:
[root@localhost ~]# docker stop mysql1
mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5128014e440f mysql/mysql-server:5.7 “/entrypoint.sh mysq1” 24 hours ago Exited (0) 12 seconds ago mysql1
[root@localhost ~]# docker rm mysql1
mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
为Docker优化MySQL的安装
MySQL的Docker映像针对代码大小进行了优化,这意味着它们只包含与大多数在Docker容器中运行MySQL实例的用户
相关的关键组件。一个MySQL Docker安装与常见的非Docker安装在以下方面不同:
包含的二进制文件是有限的:
/usr/bin/my_print_defaults
/usr/bin/mysql
/usr/bin/mysql_config
/usr/bin/mysql_install_db
/usr/bin/mysql_tzinfo_to_sql
/usr/bin/mysql_upgrade
/usr/bin/mysqladmin
/usr/bin/mysqlcheck
/usr/bin/mysqldump
/usr/bin/mysqlpump
/usr/sbin/mysqld
所有的二进制文件被stripped;它们不包含调试信息。
配置MySQL Server
当你启动MySQL Docker容器时,可以通过docker run命令来传递配置选项给服务,例如,对于MySQL社区版本服务器
docker run --name mysql1 -d mysql/mysql-server:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_col
对于MySQL企业版本服务器:
docker run --name mysql1 -d store/oracle/mysql-enterprise-server --character-set-server=utf8mb4 --collation-server=utf8mb4_col
命令以utf8mb4作为默认字符集和utf8mb4col作为您的数据库的默认排序启动MySQL服务器。
另一种配置MySQL服务器的方法是准备一个配置文件,并将其挂载到容器内的服务器配置文件的位置。有关详细信息,
请参阅持久数据和配置更改。
持久数据和配置更改
Docker容器在原则上是临时的,如果容器被删除或损坏,任何数据或配置都将丢失(参见讨论)。然而,Docker卷提供了
一种机制,可以在Docker容器中保存数据。在初始化时,MySQL服务器容器为服务器数据目录创建Docker卷。运行
docker检查容器命令的JSON输出有一个挂载键,其值提供数据目录卷的信息:
[root@localhost ~]# docker inspect mysql1
…
“Mounts”: [
{
“Type”: “volume”,
“Name”: “a74fb4e3348635ddc0b3eb32e3f82b4feb38eeadd8d5b3ae60b4389ab83a86d8”,
“Source”: “/var/lib/docker/volumes/a74fb4e3348635ddc0b3eb32e3f82b4feb38eeadd8d5b3ae60b4389ab83a86d8/_data”,
“Destination”: “/var/lib/mysql”,
“Driver”: “local”,
“Mode”: “”,
“RW”: true,
“Propagation”: “”
}
],
…
输出显示源文件夹
/var/lib/docker/volumes/a74fb4e3348635ddc0b3eb32e3f82b4feb38eeadd8d5b3ae60b4389ab83a86d8/_data将数
据持久化到主机上的数据,已经安装在/var/lib/mysql中,容器内的服务器数据目录。
保存数据的另一种方法是在创建容器时使用 --mount 选项挂载主机目录。同样的技术可以用来持久化服务器的配置。
下面的命令创建一个MySQL服务器容器,并将数据目录和服务器配置文件绑定在一起:
docker run --name=mysql1
–mount type=bind,src=/path-on-host-machine/my.cnf,dst=/etc/my.cnf
–mount type=bind,src=/path-on-host-machine/datadir,dst=/var/lib/mysql
-d mysql/mysql-server:tag
配置文件path-on-host-machine/my.cnf必须存在,同时包含指定的用户:
[mysqld]
user=mysql
path-on-host-machine.datadir目录必须存在。要发生服务器初始化,目录必须是空的。您还可以安装一个带有数
据的目录,并使用它启动服务器;但是,您必须确保启动Docker容器,其配置与创建数据的服务器具有相同的配置,
并且启动容器时所需的任何主机文件或目录都被安装。
运行额外的初始化脚本
如果您想在创建后立即在数据库上运行任何.sh或.sql脚本,您可以将它们放入主机目录中,然后mount目录在
/docker-entrypoint-initdb.d
docker run --name=mysql1
–mount type=bind,src=/path-on-host-machine/scripts/,dst=/docker-entrypoint-initdb.d/
-d mysql/mysql-server:tag
对于MySQL企业版本服务器容器:
docker run --name=mysql1
–mount type=bind,src=/path-on-host-machine/scripts/,dst=/docker-entrypoint-initdb.d/
-d store/oracle/mysql-enterprise-server:tag
从另一个Docker容器中的应用程序连接到MySQL
通过建立一个Docker网络,你可以让多个Docker容器相互通信,这样,在另一个Docker容器中的客户端应用程序就可
以在服务器容器中访问MySQL服务器。首先,创建一个Docker网络:
docker network create my-custom-net
然后,当您创建并启动服务器和客户端容器时,使用-network选项将它们放在您创建的网络上。例如:
docker run --name=mysql1 --network=my-custom-net -d mysql/mysql-server
docker run --name=myapp1 --network=my-custom-net -d myapp
当使用MySQL企业版本服务器容器:
docker run --name=mysql1 --network=my-custom-net -d store/oracle/mysql-enterprise-server
docker run --name=myapp1 --network=my-custom-net -d myapp
myapp1容器随后可以与mysql1主机连接到mysql1容器,反之亦然,因为Docker会自动为给定的容器名称设置一个DNS
。在下面的例子中,我们从myapp1容器中运行mysql客户端,以便在自己的容器中连接主机mysql1:
docker exec -it myapp1 mysql --host=mysql1 --user=myuser --password
- 点赞
- 收藏
- 关注作者
评论(0)