如何部署 MySQL Server Docker ?

MySQL Server Deployment with Docker

参考资料

MySQL官方的安装教程:https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-getting-started.html

Docker Desktop 19 Mac 版下载地址:https://hub.docker.com/editions/community/docker-ce-desktop-mac

Docker Desktop 配置国内镜像源

image.png
{
  "debug": true,
  "experimental": false,
  "registry-mirrors": [
      "https://registry.docer-cn.com",
      "https://docker.mirrors.ustc.edu.cn",
      "http://hub-mirror.c.163.com"
  ]
}

镜像下载、配置及启动:

下载MySQL8的镜像

docker pull mysql/mysql-server:8.0.19

查看本地镜像:

docker images

配置并启动镜像(将容器虚拟机的3306端口映射到宿主机的3306端口):

docker run --name=mysql8 -it -p 3306:3306 -d mysql/mysql-server:8.0.19

查看容器进程:

docker ps

查看MySQL日志,注意关注root的初始化密码:

docker logs mysql8 2>&1 | grep GENERATED
[Entrypoint] GENERATED ROOT PASSWORD: oLeNURohCAGt3cYNYrYlfURj3Kan

访问及配置MySQL

访问容器中的MySQL:

docker exec -it mysql8 mysql -uroot -p

修改root密码:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.02 sec)

mysql> create user 'myuser'@'%' identified with mysql_native_password by 'myuser';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to 'myuser'@'%' with grant option;
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> select user from mysql.user;
+------------------+
| user             |
+------------------+
| myuser           |
| healthchecker    |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
+------------------+
6 rows in set (0.00 sec)

其他Docker命令

查看日志

docker logs mysql8

访问容器Shell:

docker exec -it mysql8 bash

你可能感兴趣的:(如何部署 MySQL Server Docker ?)