Home > Database > Mysql Tutorial > body text

How to install and run mysql on docker

王林
Release: 2023-05-30 11:58:06
forward
2009 people have browsed it

ps: The experimental environment is: ubuntu 14.04, 64-bit

1. Get the mysql image

Pull it from the docker hub warehouse Get the mysql image

sudo docker pull mysql

View the image

sudo docker images

mysql latest 18f13d72f7f0 2 weeks ago 383.4 mb
Copy after login

2. Run a mysql container

The command to run a mysql instance is as follows:

Copy code The code is as follows:

sudo docker run --name first-mysql -p 3306:3306 -e mysql \_root\_password=123456 -d mysql

5b6bf6f629bfe46b4c8786b555d8db1947680138b2de1f268f310a15ced7247a

The meaning of each parameter of the above command: -name is followed by the name of this image

-p 3306:3306 means that the port number mapped to the local machine is 3306 port (the second one) in this container is also 3306 (the first one)

-d means that the daemon process is used to run, that is, the service hangs In the background


View the status of the currently running container:

Copy the code The 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:3306->3306/tcp first-mysql

Want to access the mysql database, my A mysql-client needs to be installed on the machine.

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

<br/> Next we use the mysql command to access the server, the password is 123456,192.168 as shown just now .95.4 is the IP of my machine, 3306 is the port occupied by this physical machine as shown just now (not the port inside docker)

mysql -h192.168.95.4 -p3306 -uroot -p123456

The results of the access are as follows:

mysql> show databases;

+--------------------+
| database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+

4 rows in set (0.00 sec)
Copy after login

3. Run the second mysql instance


Use The reason why docker is compared 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 and use the previous image, named second-mysql.

Copy code The code is as follows:

sudo docker run --name second-mysql -p 3306:3307 -e mysql\_root\_password=123456 -d

mysql2de4ddb5bfb9b9434af8e72368631e7f4c3f83ee354157328049d 7d0

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

For verification The first one is this machine port number, the port 3306 is still used. Then the creation is as shown above. An error occurred, but a container id was generated. When we modify the port, an error will be reported because the name conflicts, that is, the creation failed this time. This name will be occupied:

Copy code The code is as follows:

sudo docker run --name second-mysql -p 3307: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..

In order to verify that the second-mysql container is still in the docker process, we use the ps -a command. We can observe that the status of first-mysql is up 34 minutes, indicating that it has been working for 34 minutes, and second -mysql was just created.

sudo docker ps -a

container id    image        command         created       status       ports          names
2de4ddb5bfb9    mysql        "docker-entrypoint.sh"  about a minute ago  created                   second-mysql
5b6bf6f629bf    mysql        "docker-entrypoint.sh"  34 minutes ago    up 34 minutes    0.0.0.0:3306->3306/tcp  first-mysql
Copy after login

We use the rm command to delete this container, as shown in the following command:

maintain@maintain-dev1:~$ sudo docker rm second-mysql

maintain@maintain-dev1:~$ sudo docker ps -a

container id image command created status ports names
5b6bf6f629bf mysql "docker-entrypoint.sh" 42 minutes ago up 42 minutes 0.0.0.0:3306->3306/tcp first-mysql
Copy after login

Re-establish the second-mysql container, occupying the 3307 port of the physical machine:

sudo docker run --name second-mysql -p 3307:3306 -e mysql\_root\_password=123456 -d mysql

5404fb11f29cba07b991f34056d6b40ed0888aa905a45e637e396d071bd7f331

sudo docker ps

container id    image        command         created       status       ports          names
5404fb11f29c    mysql        "docker-entrypoint.sh"  12 seconds ago   up 11 seconds    0.0.0.0:3307->3306/tcp  second-mysql
5b6bf6f629bf    mysql        "docker-entrypoint.sh"  43 minutes ago   up 43 minutes    0.0.0.0:3306->3306/tcp  first-mysql
Copy after login

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

mysql -h192.168.95.4 -p3307 -uroot -p123456

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.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 &#39;help;&#39; or &#39;\h&#39; for help. type &#39;\c&#39; to clear the current input statement.
Copy after login

4.jdbc test (maven & spring boot)

Example source:

Use java spring to operate the database, use The example on the spring official website uses jdbctemplate, a naked SQL method, without using hibernate or myibatis.

The example on the spring official website uses h2, a built-in memory database, without the trouble of generating configuration files as objects,

In order to practice yaml instead of xml, I used the following mysql jdbc configuration, application.yaml.

### application.yaml
### mysql config
spring:
 datasource:
  dbcp:
   driver-class-name: com.mysql.jdbc.driver
   url: jdbc:mysql://192.168.18.129:3306/test1
   username: root
   password: 123456
Copy after login

I spent a long time looking for the automatic dependency injection into a jdbctemplate object, but I didn’t get it, it was just like the following The same way, I first generate the datasource myself, then create a new jdbctemlate object, and finally use jdbctemplate to operate the database. This is very ugly, I hope you can give me some advice.

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

The following are some crud operations on the database, using functional programming of 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))");
// split up the array of whole names into an array of first/last names
list<object[]> splitupnames = arrays.aslist("john woo", "jeff dean", "josh bloch", "josh long")
 .stream() .map(name -> name.split(" ")) .collect(collectors.tolist());
// use a java 8 stream to print out each tuple of the list
splitupnames.foreach(name -> log.info(string.format("inserting customer record for %s %s", name[0], name[1])));
// uses jdbctemplate&#39;s batchupdate operation to bulk load data
jdbctemplate.batchupdate("insert into customers(first_name, last_name) values (?,?)", splitupnames);
log.info("querying for customer records where first_name = &#39;josh&#39;:");
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()));
Copy after login

The following is verified on the mysql client:

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)
Copy after login

5. Some pitfalls encountered

maven configuration

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

<properties> 
<java.version>1.8</java.version>
</properties>
Copy after login
docker service restart

When the docker service hangs, the container also hangs and does not restart. You should bring the --restart=always parameter when running the container

The above is the detailed content of How to install and run mysql on docker. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template