ps:實驗環境是:ubuntu 14.04, 64位元
1.取得mysql映像
從docker hub的倉庫中拉取mysql映像
sudo docker pull mysql
看映像
sudo docker images mysql latest 18f13d72f7f0 2 weeks ago 383.4 mb
2.執行一個mysql容器
執行一個mysql實例的指令如下:
複製程式碼 程式碼如下:
sudo docker run --name first-mysql -p 3306:3306 -e mysql \_root\_password=123456 -d mysql
5b6bf6f629bfe46b4c8786b555d8db1947680138b2de1f268f310a15ced7247a
##11f268f310a15ced7247a##c努 運作一個容器
--name 後面是這個鏡像的名稱-p 3306:3306 表示在這個容器中使用3306埠(第二個)對應到本機的連接埠號碼也為3306(第一個)
-d 應用守護程式運行,即服務表示使用守護程式運行,即服務使用守護程式在後台
查看目前運行的容器狀態:
複製程式碼 程式碼如下:
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-my
my##my#my#頭#asql#機器上需要裝一個mysql-client。sudo apt-get install mysql-client-core-5.6
<br>下面我們使用mysql指令存取伺服器,密碼如剛才所示為123456,192.168 .95.4為我這台機器的ip, 3306為剛才所示的佔用本實體機的連接埠(不是在docker內部的連接埠)
存取的結果如下:
mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
使用docker相對於虛擬機器的原因是耗費很少的資源,可以"開闢"非常多的隔離的環境,所以我們繼續運行第二個mysql實例,還用之前的鏡像,名字為second-mysql。
複製程式碼 程式碼如下:
sudo docker run --name second-mysql -p 3306:3307 -e mysql\_root\_password=123456 -d
mysql2de4ddb5bbb9b943456 -d
mysql2de4ddb5bb9b943458 d0
f5523661docker: error response from daemon: driver failed programming external connectivity on endpoint second-mysql (33aa29d891a1cb540de250bcbbbe9a0a41db9861a1cb540de250bcbbbe9a0a41db9861a1cb540de250bcbbbe9a0a41db9861a. 3306 failed: port is already allocated.
為了驗證第一個是本機的端口號,就仍然使用了3306這個端口,那麼創建如上所示,發生了錯誤,但是產生了一個容器id,當我們修改端口後還會報錯,因為名字衝突了,也即這次失敗的創建會佔用這個名字:
複製程式碼 程式碼如下:
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 2de4ddb5bbb9b9434af8e72368631e7f4c3f83ee354157358040631e7f4c3f83ee354157358040631e7ff 您that container to be able to reuse that name..
為了驗證second-mysql這個容器還在docker的進程中,我們使用ps -a這個指令,我們可以觀察到first-mysql的狀態為up 34 minutes,說明已經工作了34分鐘,而second -mysql只是剛剛建立的。
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我們用rm指令刪除這個容器,如下面指令所示:
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重新建立second-mysql這個容器,佔用實體機的3307埠:
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如上圖所示,兩個實例都正常運作了,為了存取第二個容器,我們指定連接埠3307登入這個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 'help;' or '\h' for help. type '\c' to clear the current input statement.
4.jdbc測試(maven & spring boot)
例子來源:
### 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找了半天自動將依賴注入為一個jdbctemplate對象,也沒有搞定,就好像下面一樣自己先生成datasource,再new一個jdbctemlate對象,最後使用jdbctemplate對資料庫進行操作,這樣寫的很醜,希望各位可以指點一下。
@bean @configurationproperties(prefix = "spring.datasource.dbcp") public datasource mysqlsource() { return datasourcebuilder.create().build(); } @bean public jdbctemplate mysqljdbctemplate() { return new jdbctemplate(mysqlsource()); }下面是對資料庫的一些crud操作,使用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'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的客戶端進行了驗證:
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. 遇到的一些坑
maven配置
#使用了jdk8的lamda表達式,必須在maven中配置java.version
<properties> <java.version>1.8</java.version> </properties>docker服務重啟
docker這個服務掛了,容器也就跟著掛了,並沒有重啟,應該在運行容器的時候帶上--restart=always這個參數
以上是在docker上如何安裝運行mysql的詳細內容。更多資訊請關注PHP中文網其他相關文章!