您的位置 首页 > 娱乐休闲

秒级搭建MySQL数据库服务,太香了

需求

在一些项目现场,很多时候,都是缺少专门的数据库运维人员的,但是开发人员开发项目,又需要用到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 my -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d -it mysql:5.7.28 423e39a1c8669a53942aed14002102adbb8871c47edfbaa67825691eb16d0d45 [root@localhost ~]# docker run --name my -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-en鈥 29 seconds ago Up 27 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp my 423e39a1c866 mysql:5.7.28 "docker-en鈥 About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp my

这样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 my 33060/tcp, 0.0.0.0:3307->3306/tcp About a minute ago Up About a minute 423e39a1c866 mysql:5.7.28 my 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分钟。

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“数据库服务器如何搭建,数据库服务器怎么搭建,数据库服务器,数据库服务器价格”边界阅读