【原创】Ubuntu 18.04 Docker 安装与运行 MySQL 5.7 – 简要版

安装准备

1、相关环境

Ubuntu 18.04 x64

Docker 19.03

MySQL Server CE 5.7.31

2、安装用户

使用具有 sudo 权限的普通用户安装

安装步骤

1、拉取镜像

sudo docker pull mysql/mysql-server:5.7

5.7: Pulling from mysql/mysql-server
e945e9180309: Pull complete 
bda404c4d2e2: Pull complete 
858855003112: Pull complete 
d92ed785684c: Pull complete 
Digest: sha256:6d6fdd5bd31256a484e887c96c41abfc9ee3e3deb989de83ebdb8694fcc83485
Status: Downloaded newer image for mysql/mysql-server:5.7
docker.io/mysql/mysql-server:5.7

查看镜像

sudo docker images | grep mysql
mysql/mysql-server   5.7                 9c31a29b3f30        6 weeks ago         322MB

2、运行容器

sudo docker run -p 3306:3306 --name mysql -d mysql/mysql-server:5.7

3、查看容器运行状态

sudo docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                    PORTS                               NAMES
e1a229279e59        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   31 seconds ago      Up 31 seconds (healthy)   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

4、查看容器运行日志

sudo docker logs mysql

 sudo docker logs mysql
[Entrypoint] MySQL Docker Image 5.7.31-1.1.17
[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: 3l2icGisqUL.yGdUm(usJOLhAR
[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.31-1.1.17

5、获取 MySQL Server 初始 root 密码

sudo docker logs mysql 2>&1 | grep "ROOT PASSWORD"
[Entrypoint] GENERATED ROOT PASSWORD: 3l2icGisqUL.yGdUm(usJOLhAR

6、使用 mysql client 命令连接到 MySQL Server

输入如下命令并回车,然后输入上一步搜索到的 root 初始密码

sudo docker exec -it mysql mysql -uroot -p
Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.31
Copyright (c) 2000, 2020, 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>

7、更新 root 密码

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

8、连接到 MySQL Server 容器内部 shell 环境

sudo docker exec -it mysql bash
bash-4.2#

9、MySQL Server 配置文件、数据文件

配置文件位置

/etc/my.cnf

数据文件、日志文件等

bash-4.2# pwd
/var/lib/mysql
bash-4.2# ls
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
bash-4.2#

10、创建一个可远程连接的 MySQL Server 数据库用户

sudo docker exec -it mysql mysql -uroot -p
[sudo] password for jiangzl: 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> grant all privileges on *.* to 'test'@'%' identified by 'test1234';
Query OK, 0 rows affected, 1 warning (0.00 sec)

注:

a、上面的命令有可能会需要输入两次密码(当前会话第一次使用 sudo 命令或者 sudo 命令使用过了 session 有效期),第一次是输入 Ubuntu 操作系统用户的密码以获取 root 权限,第二次是输入 MySQL Server 的 root  密码以连接到 MySQL Server。

b、创建用户后可以使用 MySQL Client 工具连接到数据库

dbeaver 连接 MySQL Server 示例:

填写 Ubuntu 的网络地址,端口号,用户名与密码,点击测试连接。显示已连接则连接成功。

c、以上安装方式仅是基础的安装方式,有很多需要配置的地方均未配置。因此,文档内容仅推荐开发或测试环境使用或者仅供参考。

附录

1、常用命令

sudo docker stop mysql # 停止容器名为 mysql 的容器
sudo docker start mysql # 启动容器名为 mysql 的容器(针对已经使用 sudo docker run 运行后的容器)
sudo docker restart mysql # 重启容器名为 mysql 的容器
sudo docker rm mysql # 删除容器名为 mysql 的容器
sudo docker rmi mysql/mysql-server:5.7 # 删除镜像名为 mysql/mysql-server,tag 为 5.7 的 mysql-server 容器

2、参考:

https://dev.mysql.com/doc/refman/5.7/en/docker-mysql-getting-started.html

You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注

%d 博主赞过: