Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Mysql in Tomcat container with Docker

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use Docker to use Mysql in the Tomcat container, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Start mysql first

Docker run-- name mysql-p 3306 MYSQL_ROOT_PASSWORD=123456 3306-v / home/mysql/conf:/etc/mysql/conf.d-v / home/mysql/data:/var/lib/mysql-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.7.221

Command description:

-name: custom alias

-p 3306virtual 3306: map port 3306 of the container to port 3306 of the host

-v / home/mysql/conf:/etc/mysql/conf.d: Mount the host / home/mysql/conf/my.cnf to the container's / etc/mysql/my.cnf

-v / home/mysql/data:/var/lib/mysql: Mount the host / home/mysql/data directory to the container's / var/lib/mysql

-e MYSQL_ROOT_PASSWORD=123456: initializes the password of the root user

-d: run as a daemon

Note: the mysql exposed port is running (- p 3306 link 3306). If you are in a production environment, you may not expose the port for safety. After that, the mysql container can be specified by (--link) when running the mysql container.

Run tomcat

Docker run-- name tomcat-p 8080 link mysql:db tomcat1 8080-v / etc/localtime:/etc/localtime:ro-v / home/tomcat/logs:/usr/local/tomcat/logs-d-- link mysql:db tomcat1

Command description:

-name: custom alias

-p 8080VR 8080: map port 8080 of the container to port 8080 of the host

-v / etc/localtime:/etc/localtime:ro: Mount the localtime file into the container to ensure that the time zone used by the two is the same

-v / home/tomcat/logs:/usr/local/tomcat/logs: Mount the host / home/tomcat/logs directory to the container's / usr/local/tomcat/logs

-d: run as a daemon

-link mysql:db: tell the current container to use a mysql container and name it db

So you can access mysql through db in the tomcat container

Attached: database connection reference statement in Java:

Jdbc_url=jdbc:mysql://db:3306/databasename?useUnicode=true&characterEncoding=utf8 [root@izbp1b5k5bjps0dw8owk7tz mysql] # docker run-- name mysql-p 3306docker run 3306-v / home/mysql/conf:/etc/mysql/conf.d-v / home/mysql/data:/var/lib/mysql-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.7.22c7378e84c19b0baa448d687c089ef8cc0ce058f71b2f79d8801fc9be7bd2c5b4 [root@izbp1b5k5bjps0dw8owk7tz mysql] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc7378e84c19b mysql: 5.7.22 "docker-entrypoint.s..." 6 seconds ago Up 5 seconds 0.0.0.0 3306/tcp mysql 3306-> 3306/tcp mysql [root@izbp1b5k5bjps0dw8owk7tz logs] # docker run-name tomcat-p 808080 3306/tcp mysql 8080-v / etc/localtime:/etc/localtime:ro-v / home/tomcat/logs:/usr/local/tomcat/logs-d-link mysql:db tomcat1458f532ef36e12ad49b4a5d90ff9b38abed00986094225354594a5fe7591362 [root@izbp1b5k5bjps0dw8owk7tz logs] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1458f532ef36 tomcat "catalina .sh run "3 seconds ago Up 2 seconds 0.0.0.0 seconds 8080-> 8080/tcp tomcatc7378e84c19b mysql:5.7.22" docker-entrypoint.s … " About a minute ago Up About a minute 0.0.0.0 Mysql 3306-> 3306/tcp mysql above is how to use Docker to use Mysql in the Tomcat container. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report