赞
踩
-d选项:表示后台运行
-p选项:指定端口映射 mysql contain的3306端口映射到宿主机的3306, 一遍通过宿主机访问到容器的3306端口
-e MYSQL_ROOT_PASSWORD=root设置root的密码为123456
docker.io/mysql:latest 制定image及其版本
[root@allen0 docker]# docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 docker.io/mysql:latest
docker exec -it 5509/bin/bash
alter user 'root'@'%' identified with mysql_native_password by '123456';
[root@allen0 ~]# systemctl start firewalld
[root@allen0 ~]# systemctl stop firewalld
sql yog 客户端下载地址 http://soft.onlinedown.net/soft/24926.htm
java 程序中测试
- package com.jiagouedu.com.jdbc;
-
- import javax.swing.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Vector;
-
- public class ConnectDockerMysqlDocker extends JFrame {
- private static final long serialVersionUID = 1L;
-
- public static void getDataVector() throws SQLException, ClassNotFoundException {
- Connection conn = null;
- java.sql.Statement stmt = null;
- Class.forName("com.mysql.jdbc.Driver");
- String dburl = "jdbc:mysql://192.168.0.100:3306/mysql";
- String dbuser = "root";
- String dbpassword = "123456";
- conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
- stmt = conn.createStatement();
- String sql = "select * from Student";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()) {
- Vector rowVector = new Vector();
- {
- System.out.println(rs.getString("sid"));
- System.out.println(rs.getString("sname"));
- }
- }
- }
- public static void main(String Args[]) throws ClassNotFoundException, SQLException{
- getDataVector();
- }
- }
jdbc driver 版本:
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.44</version>
- </dependency>
执行结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。