In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is to share with you about how to install and run mysql in docker, 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.
1. Get mysql image
Pull mysql image from docker hub's repository
Sudo docker pull mysql
View Mirror
Sudo docker images
Mysql 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 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 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-h292.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 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 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-a
CONTAINER 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-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 minutes ago Up 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 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 seconds ago Up 3307-> 3306/tcp second-mysql
5b6bf6f629bf mysql "docker-entrypoint.sh" 43 minutes ago Up 43 minutes 0.0.0.0 minutes ago Up 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-h292.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 'help;' or'\ h' for help. Type'\ c'to clear the current input statement.
4.jdbc testing (maven & Spring boot)
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 h3, 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 config
Spring:
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 ()
}
@ Bean
Public 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)")
/ / Split up the array of whole names into an array of first/last names
List 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 list
SplitUpNames.forEach (name-> log.info (String.format ("Inserting customer record for% s s", name [0], name [1])
/ / Uses JdbcTemplate'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 = '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
< properties> < java.version>1.8
< /java.version> < /properties>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.
The above is how to install and run mysql in docker. 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.