需求
在一些项目现场,很多时候,都是缺少专门的数据库运维人员的,但是开发人员开发项目,又需要用到MySQL数据库服务器,而且不同的项目的数据库又要分开
解决方案
对于非数据库运维人员,安装MySQL数据库有一定的难度,所以在这里推荐用docker来搭建MySQL数据库服务。这种方案门槛低,对于非数据库专业人员也能秒级搭建好一条MySQL服务。
搭建步骤
拉取MySQL数据库镜像
docker pull mysql:latest
复制
复制
直接执行这个命令,意思是拉取最新的镜像,但是实际项目可能需要制定的数据库版本,所以这里需要制定标签,拉取需要的镜像
docker pull mysql:5.7.28
复制
复制
创建MySQL容器
拉取好镜像之后,就可以创建2个MySQL容器了,对外访问端口为3306,3307
[root@localhost ~]# docker run --name mysql5.7.28_3306 -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d -it mysql:5.7.28
423e39a1c8669a53942aed14002102adbb8871c47edfbaa67825691eb16d0d45
[root@localhost ~]# docker run --name mysql5.7.28_3307 -e MYSQL_ROOT_PASSWORD=root -p 3307:3306 -d -it mysql:5.7.28
ff668e421d59b1fef61d58b70532bf87d5915da1c47d63db690857d74d283e12
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff668e421d59 mysql:5.7.28 "docker-entrypoint.s鈥 29 seconds ago Up 27 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5.7.28_3307
423e39a1c866 mysql:5.7.28 "docker-entrypoint.s鈥 About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql5.7.28_3306
复制
复制
这样2个容器就创建好了,只是用docker ps显示的结果被截断了,不够友好,可以用下面的命令,来展示你需要看到的列信息就好
[root@localhost ~]# docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.RunningFor}}\t{{.Status}}"
CONTAINER ID IMAGE NAMES PORTS CREATED STATUS
ff668e421d59 mysql:5.7.28 mysql5.7.28_3307 33060/tcp, 0.0.0.0:3307->3306/tcp About a minute ago Up About a minute
423e39a1c866 mysql:5.7.28 mysql5.7.28_3306 0.0.0.0:3306->3306/tcp, 33060/tcp 2 minutes ago Up 2 minutes
复制
复制
MySQL服务测试
验证端口
[root@localhost ~]# netstat -an|egrep "3306|3307"
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::3307 :::* LISTEN
复制
复制
可以看到,3306和3307端口已经开启。
MySQL客户端连接测试
[mysql@localhost ~]$ mysql -uroot -proot -h 192.168.17.128 -P 3306
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 2
Server version: 5.7.28 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> quit
Bye
[mysql@localhost ~]$
[mysql@localhost ~]$ mysql -uroot -proot -h 192.168.17.128 -P 3307
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 2
Server version: 5.7.28 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> quit
复制
复制
可以看到,两个MySQL数据库服务已经搭建好了,整个搭建都不到1分钟。
文章转载自落叶说Mysql数据库运维,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
墨天轮个人数说知识点合集
JiekeXu
443次阅读
2025-04-01 15:56:03
MySQL数据库当前和历史事务分析
听见风的声音
426次阅读
2025-04-01 08:47:17
【MySQL 30周年庆】MySQL 8.0 OCP考试限时免费!教你免费领考券
墨天轮小教习
403次阅读
2025-04-25 18:53:11
MySQL 生产实践-Update 二级索引导致的性能问题排查
chengang
388次阅读
2025-03-28 16:28:31
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
严少安
382次阅读
2025-04-25 15:30:58
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
357次阅读
2025-04-17 17:02:24
MySQL 9.3 正式 GA,我却大失所望,新特性亮点与隐忧并存?
JiekeXu
355次阅读
2025-04-15 23:49:58
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
331次阅读
2025-04-15 14:48:05
云和恩墨杨明翰:安全生产系列之MySQL高危操作
墨天轮编辑部
308次阅读
2025-03-27 16:45:26
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
294次阅读
2025-04-07 12:14:29