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
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:
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)
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.
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:
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
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
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
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 'help;' or '\h' for help. type '\c' to clear the current input statement.
4.jdbc test (maven & spring boot)
Example source:
### 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
@bean @configurationproperties(prefix = "spring.datasource.dbcp") public datasource mysqlsource() { return datasourcebuilder.create().build(); } @bean public jdbctemplate mysqljdbctemplate() { return new jdbctemplate(mysqlsource()); }
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'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()));
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 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>
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!