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 install and run mysql on docker

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to install and run mysql on docker". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to install and run mysql on docker.

Ps: the experimental environment is: ubuntu 14.04,64-bit

1. Get mysql image

Pull mysql image from docker hub's repository

Sudo docker pull mysql

View Mirror

Sudo docker imagesmysql latest 18f13d72f7f0 2 weeks ago 383.4 mb

two。 Run a mysql container

The command to run an instance of mysql is as follows:

The copy code is as follows:

Sudo docker run-name first-mysql-p 3306 mysql 3306-e mysql\ _ root\ _ password=123456-d mysql

5b6bf6f629bfe46b4c8786b555d8db1947680138b2de1f268f310a15ced7247a

The meaning of each parameter of the above command:

Run runs a container

-- name is followed by the name of the image

The port number mapped to the local machine using port 3306 (second) in this container is also 3306 (first).

-d means that the daemon is used to run, that is, the service is hung in the background

View the status of the currently running container:

The copy code is as follows:

Sudo docker ps

Container id image command created status ports names5b6bf6f629bf

Mysql "docker-entrypoint.sh" 32 hours ago up 5 hours 0.0.0.0 hours ago up 3306-> 3306/tcp first-mysql

To access the mysql database, I need a mysql-client installed on my machine.

Sudo apt-get install mysql-client-core-5.6

Next, we use the mysql command to access the server. The password, as shown just now, is 123456192.168.95.4, which is the ip of my machine, and 3306 is the port that occupies this physical machine (not the port inside the docker).

Mysql-h192.168.95.4-p3306-uroot-p123456

The results of the visit are as follows:

Mysql > show databases;+-+ | database | +-+ | information_schema | | mysql | | performance_schema | | sys | +-+ 4 rows in set (0.00 sec)

3. Run the second mysql instance

The reason for using docker relative to a virtual machine is that it consumes very few resources and can "open up" a lot of isolated environments, so we continue to run the second mysql instance, using the previous image, named second-mysql.

The copy code is as follows:

Sudo docker run-- name second-mysql-p 3306 mysql 3307-e mysql\ _ root\ _ password=123456-d

Mysql2de4ddb5bfb9b9434af8e72368631e7f4c3f83ee354157328049d7d0

F5523661docker: error response from daemon: driver failed programming external connectivity on endpoint second-mysql (33aa29d891a1cb540de250bcbbbe9a0a41cd98f61a4e9f129a2ad5db69da4984): bind for 0.0.0.0 driver failed programming external connectivity on endpoint second-mysql 3306 failed: port is already allocated.

In order to verify that the first port number is the local port number, port 3306 is still used. An error occurred in the creation as shown above, but a container id was generated. When we modify the port, we will still report an error because the name conflicts, that is, this failed creation will occupy this name:

The copy code is as follows:

Sudo docker run-- name second-mysql-p 3307 mysql 3306-e mysql\ _ root\ _ password=123456-d

Mysqldocker: error response from daemon: conflict. The name "/ second-mysql" is already in use by container 2de4ddb5bfb9b9434af8e72368631e7f4c3f83ee354157328049d7d0f5523661.

You have to remove (or rename) that container to be able to reuse that name..

To verify that the second-mysql container is still in the docker process, using the ps-a command, we can observe that the status of the first-mysql is up 34 minutes, indicating that it has been working for 34 minutes, while the second-mysql has only just been created.

Sudo docker ps-acontainer id image command created status ports names2de4ddb5bfb9 mysql "docker-entrypoint.sh" about a minute ago created second-mysql5b6bf6f629bf mysql "docker-entrypoint.sh" 34 minutes ago up 34 minutes 0.0.0.0purl 3306-> 3306/tcp first-mysql

We delete the container with the rm command, as shown in the following command:

Maintain@maintain-dev1:~$ sudo docker rm second-mysqlmaintain@maintain-dev1:~$ sudo docker ps-acontainer id image command created status ports names5b6bf6f629bf mysql "docker-entrypoint.sh" 42 minutes ago up 42 minutes 0.0.0.0 docker-entrypoint.sh 3306-> 3306/tcp first-mysql

Re-establish the second-mysql container and occupy port 3307 of the physical machine:

Sudo docker run-- name second-mysql-p 3307 mysql 3306-e mysql\ _ root\ _ password=123456-d mysql5404fb11f29cba07b991f34056d6b40ed0888aa905a45e637e396d071bd7f331sudo docker pscontainer id image command created status ports names5404fb11f29c mysql "docker-entrypoint.sh" 12 seconds ago up 11 seconds 0.0.0.0root 3307-> 3306/tcp second-mysql5b6bf6f629bf mysql "docker-entrypoint.sh" 43 minutes ago up 43 minutes 0.0.0.0From 3306-> 3306/tcp first-mysql

As shown in the figure above, both instances are running normally, and to access the second container, we specify port 3307 to log in to the client of this mysql.

Mysql-h192.168.95.4-p3307-uroot-p123456warning: 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 2server version: 5.7.15 mysql community server (gpl) copyright (c) 2000, 2016, oracle and/or its affiliates. All rights reserved.oracle is a registered trademark of oracle corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.type 'help;' or'\ h' for help. Type'\ c'to clear the current input statement.

4.jdbc testing (maven & spring boot)

Source of examples:

Use java spring to operate on the database, using the example of the official website of spring, using jdbctemplate, a naked method of writing sql, instead of hibernate or myibatis.

The example of the spring official website uses H2, a built-in memory database, and has no configuration file to create the annoyance of becoming an object.

To practice replacing xml with yaml, use the following mysql jdbc configuration, application.yaml.

# application.yaml### mysql configspring: datasource: dbcp: driver-class-name: com.mysql.jdbc.driver url: jdbc:mysql://192.168.18.129:3306/test1 username: root password: 123456

After looking for a long time to automatically inject dependency into a jdbctemplate object, I had no choice but to turn my husband into datasource, then new a jdbctemlate object, and finally use jdbctemplate to operate on the database. I hope you can give me some advice.

@ bean@configurationproperties (prefix = "spring.datasource.dbcp") public datasource mysqlsource () {return datasourcebuilder.create () .build ();} @ beanpublic jdbctemplate mysqljdbctemplate () {return new jdbctemplate (mysqlsource ());}

Here are some crud operations on the database, using functional programming with jdk8:

Jdbctemplate jdbctemplate = mysqljdbctemplate (); jdbctemplate.execute ("drop table if exists customers"); jdbctemplate.execute ("create table customers (" + "id serial, first_name varchar (255), last_name varchar (255)"); / / splitup the array of whole names into an array of first/last nameslist splitupnames = arrays.aslist ("john woo", "jeff dean", "josh bloch", "josh long") .stream () .map (name-> name.split (")) .streams (collectors.tolist ()) / / use a java 8 stream to print out each tuple of the listsplitupnames.foreach (name-> log.info (string.format ("inserting customer record for% s% s", name [0], name [1])); / / uses jdbctemplate's batchupdate operation to bulk load datajdbctemplate.batchupdate ("insert into customers (first_name, last_name) values", splitupnames); log.info ("querying for customer records where first_name = 'josh':") Jdbctemplate.query ("select id, first_name, last_name from customers where first_name =?", new object [] {"josh"}, (rs, rownum)-> new customer (rs.getlong ("id"), rs.getstring ("first_name"), rs.getstring ("last_name")) .foreach (customer-> log.info (customer.tostring ()

The following is verified on the client side of mysql:

Mysql > select * from customers +-+ | id | first_name | last_name | +-- + | 1 | john | woo | | 2 | jeff | dean | 3 | josh | bloch | | 4 | josh | long | + +-+ 4 rows in set (0.00 sec)

5. Some of the pits encountered

Maven configuration

The lamda expression of jdk8 is used and java.version must be configured in maven

1.8

Docker service restart

When the docker service dies, so does the container, and it does not restart. You should run the container with the parameter-- restart=always.

Thank you for reading, the above is the content of "how to install and run mysql on docker". After the study of this article, I believe you have a deeper understanding of how to install and run mysql on docker, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report