本文共 6540 字,大约阅读时间需要 21 分钟。
前言
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
使用docker,你可以轻松地启动基于镜像的不同软件版本的容器。
你也可以在一台主机中创建诸多不同的容器,这些容器彼此独立,互不影响。
并且,每个docker镜像都是版本可控的。
这篇文章,我将为大家分享如何在docker中一步一步安装MySQL数据库,并且配置远程可访问。
第一步、拉取MySQL镜像
MySQL的镜像有很多,本文使用的是mysql/mysql-server镜像,拉取mysql/mysql/mysql-server镜像,命令如下:
docker pull mysql/mysql-server:latest
日志如下:
ubuntu@VM-0-9-ubuntu:~$ docker pull mysql/mysql-server:latest
latest: Pulling from mysql/mysql-server
a316717fc6ee: Pull complete
11af9372e1ad: Pull complete
3edd2f9154a6: Pull complete
f71a5f0dcc26: Pull complete
Digest: sha256:2a55d7081363a869ea9ea2513bb31439bae59981530b9d1890b3771a6cebefe9
Status: Downloaded newer image for mysql/mysql-server:latest
docker.io/mysql/mysql-server:latest
查看docker中所有镜像
docker images
日志如下:
ubuntu@VM-0-9-ubuntu:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql/mysql-server latest b172b40598f0 3 weeks ago 350MB
hello-world latest fce289e99eb9 10 months ago 1.84kB
第二步、在docker容器中启动MySQL实体
现在,我们可以使用docker run命令启动一个mysql-server实例:
docker run --name=mysql1 -d mysql/mysql-server注: 其中的mysql1表示为当前的mysql-server实例指定一个自定义的容器名称,这个参数是可选的,如果不指定此参数,则会生成一个随机的容器名称。-d参数表示在后台运行当前的docker run命令,如需打印mysql1运行的日志,可以使用如下命令:
docker logs mysql1
一旦初始化完成,命令会自动为root用户生成一个随机密码并打印在日志中。
以上运行的docker runMySQL实例只能用于本地连接,如果你的MySQL实例需要允许远程访问和连接,则要为MySQL实例指定端口并设置环境变量MYSQL_ROOT_HOST以允许来自其他主机的连接,docker run命令如下:
docker stop mysql1 && docker rm mysql1 #先停止并删除mysql1服务
docker run --name=mysql1 -e MYSQL_ROOT_HOST=% -e -p 3306:3306 -d mysql/mysql-server如果要指定密码,请添加参数-e MYSQL_ROOT_PASSWORD=123456,如:
docker run --name=mysql1 -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql/mysql-server
日志如下:
ubuntu@VM-0-9-ubuntu:~$ sudo docker run —name=mysql1 -e MYSQL_ROOT_HOST=% -p 3306:3306 -d mysql/mysql-server
a76e9b8eef6c39ff5087d31cd46223cf720af64bbe15a85cc3404881c1ac0a37
再运行
docker logs mysql1
日志如下:
ubuntu@VM-0-9-ubuntu:~$ docker logs mysql1
[Entrypoint] MySQL Docker Image 8.0.18-1.1.13
[Entrypoint] No password option specified for new database.
[Entrypoint] A random onetime password will be generated.
[Entrypoint] Initializing database
2019-11-05T02:35:48.785221Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 20
2019-11-05T02:35:51.984685Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the —initialize-insecure option.
[Entrypoint] Database initialized
2019-11-05T02:35:55.513277Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 67
2019-11-05T02:35:56.251721Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-05T02:35:56.274981Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: ‘8.0.18’ socket: ‘/var/lib/mysql/mysql.sock’ port: 0 MySQL Community Server - GPL.
2019-11-05T02:35:56.344027Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: ‘/var/run/mysqld/mysqlx.sock’
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: oDulSiv@cIz]3t=0N[Oded@bBoz
[Entrypoint] ignoring /docker-entrypoint-initdb.d/*
2019-11-05T02:35:58.099733Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.18).
2019-11-05T02:35:59.590395Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.18) MySQL Community Server - GPL.
[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 8.0.18-1.1.13
2019-11-05T02:36:00.418693Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 1
2019-11-05T02:36:00.927563Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-05T02:36:00.963953Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: ‘8.0.18’ socket: ‘/var/lib/mysql/mysql.sock’ port: 3306 MySQL Community Server - GPL.
2019-11-05T02:36:01.106180Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: ‘/var/run/mysqld/mysqlx.sock’ bind-address: ‘::’ port: 33060
运行如下命令查看自动生成的root用户的密码:
docker logs mysql1 2>&1 | grep GENERATED
日志如下:
ubuntu@VM-0-9-ubuntu:~$ docker logs mysql1 2>&1 | grep GENERATED
[Entrypoint] GENERATED ROOT PASSWORD: oDulSiv@cIz]3t=0N[Oded@bBoz
其中的oDulSiv@cIz]3t=0N[Oded@bBoz即为随机生成的密码,请记录下来,后面登录MySQL的root用户会用到。
将MySQL的数据持久化到主机物理硬盘
默认情况下,docker容器中的MySQL实例的数据是存储在docker虚拟空间中的。所以,如果容器不存在了(被删除),那么该MySQL实例的所有数据也就会丢失。
所以,生产环境中我们非常有必要将docker容器中的MySQL实例的数据持久化在主机物理硬盘中,这样可避免在容器实例被删除的情况下数据同时丢失的风险。
具体操作是指定docker容器中MySQL实例的虚拟存储目录与物理主机存储目录的映射,如下:
docker run --name=mysql1 -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql/mysql-server其中的参数-v /my/own/datadir:/var/lib/mysql即为存储目录的映射关系,/my/own/datadir指物理主机真实的目录,所以请确保该目录真实存在,/var/lib/mysql是docker容器中MySQL实例的默认数据存储路径。
第三步、连接到MySQL实例
接下来,我们使用docker exec运行MySQL的命令行以连接到刚才创建的MySQL实例,如下:
docker exec -it mysql1 mysql -u root -p
输入上面创建的随机密码。
日志如下:
ubuntu@VM-0-9-ubuntu:~$ docker exec -it mysql1 mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 367
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 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>
到此,我们成功连接上docker窗口中本地的MySQL实例了。
由于root用户的密码是随机创建的,所以运行如下MySQL语句以修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY '';其中的修改成你的密码
到这里,在docker容器中安装MySQL数据库的基本操作就完成了。你可以通过3306端口远程访问此MySQL实例(如果是阿里云或者腾讯云上安装的,请将3306端口添加到允许的安全组中)。
可能的问题
在远程连接时,你可能遇到如下的问题:
mysql plugin caching_sha2_password could not be loaded
可以运行如下的命令来修改root用户的加密规则:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '';
转载地址:http://wytnx.baihongyu.com/