Docker安装Mysql 5.7数据库并配置

2022/2/9 19:42:27

本文主要是介绍Docker安装Mysql 5.7数据库并配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一文搞懂Docker的安装
h:ttps://blog.csdn.net/qq_44895681/article/details/105540702

环境

CentOS 7.5

Docker 20.10.2

Mysql 5.7

安装Mysql数据库

1.拉取mysql 5.7镜像

docker pull mysql:5.7

[root@es-node22 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7

2.查看镜像库中mysql 5.7镜像

[root@es-node22 ~]# docker images
REPOSITORY                                      TAG          IMAGE ID       CREATED         SIZE
mysql                                           5.7          c20987f18b13   13 days ago     448MB

3.创建并启动mysql容器

[root@es-node22 ~]# docker run -itd -p 3306:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root123 mysql:5.7
...
...
9b4ff256f3cf19a9296ff08ee510e0975311af79731bc49c70471d6793956665

注意:将数据、日志、配置文件映射到宿主机指定的文件夹中,创建并启动容器时会自动创建相关的文件夹!

[root@es-node22 ~]# ls /root/mysql/
conf  data  logs

-i:以交互模式运行容器,通常与 -t 同时使用;

-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;

-d:后台运行容器,并返回容器ID

-p:将容器的端口映射到本机的端口

-v:将主机目录挂载到容器的目录

-e:设置参数

4.查看启动的mysql容器并拷贝配置文件

[root@es-node22 ~]# docker ps
CONTAINER ID   IMAGE                                                  COMMAND                  CREATED          STATUS          PORTS                                                                                  NAMES
9b4ff256f3cf   mysql:5.7                                              "docker-entrypoint.s…"   23 seconds ago   Up 20 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                   mysql

[root@es-node22 ~]# cd mysql/conf/

[root@es-node22 conf]# docker cp mysql:/etc/mysql/my.cnf .

[root@es-node22 conf]# ls
my.cnf

5.查看/root/mysql/data目录是否有数据文件

[root@es-node22 ~]# cd mysql/data/

[root@es-node22 data]# ls
auto.cnf    client-cert.pem  ibdata1      ibtmp1              private_key.pem  server-key.pem
ca-key.pem  client-key.pem   ib_logfile0  mysql               public_key.pem   sys
ca.pem      ib_buffer_pool   ib_logfile1  performance_schema  server-cert.pem

6.以退出不中断容器的方式进入Mysql容器

[root@es-node22 conf]# docker exec -it mysql bash
root@9b4ff256f3cf:/# mysql -uroot -proot123
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.01 sec)
...
...

7.使用工具连接Mysql容器
在这里插入图片描述
在这里插入图片描述

↓↓↓↓↓↓

最近刚申请了个微信公众号,上面也会分享一些运维知识,大家点点发财手关注一波,感谢大家。 【原创公众号】:非著名运维 【福利】:公众号回复 “资料” 送运维自学资料大礼包哦!
在这里插入图片描述



这篇关于Docker安装Mysql 5.7数据库并配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程