宿主机连接oracle容器_docker mysql 宿主机访问和docker容器间访问

下载mysql镜像

docker pull hub.c.163.com/nce2/mysql:5.6

创建mysql5.6容器

docker run --name mymysql -d -P hub.c.163.com/nce2/mysql:5.6

验证容器状态

0c36fb8ccfd6619e923657be536bcd8d.png

通过主机进入mymysql容器

docker exec -it mymysql bash

//输出

root@4344add2cca7:/#

登录mysql数据库,查看mysql是否可以正常使用

$ docker exec -it mymysql bash

root@4344add2cca7:/# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.6.19-v1-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| #bak_database |

| mysql |

| performance_schema |

| test |

+--------------------+

5 rows in set (0.00 sec)

mysql> create database pems;

Query OK, 1 row affected (0.00 sec)

mysql> create user 'pems'@'*' identified by 'pemsroot';

Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on pems.* to 'pems'@'%' with grant option;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| #bak_database |

| mysql |

| pems |

| performance_schema |

| test |

+--------------------+

6 rows in set (0.00 sec)

对mysql进行远程授权

mysql> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> update user set host='%' where user = 'pems';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

查询容器的ip和容器绑的端口 执行结果如下图

docker-machine env

docker ps

3ac8e7f75d094cde541f8d735fdb5094.png

宿主机访问mysql连接地址和端口为:192.168.99.100:32768

docker容器之间的访问  --link=mymysql:db  表示把刚才的mymysql容器重新命名为db,然后你就可以在上面的mysqlClient容器中访问到mymysql容器了。

docker run -it -P --link=mymysql:db --name=mysqlClient hub.c.163.com/nce2/mysql:5.6 /bin/bash

在name为mysqlClient中进行数据库访问

root@708f8e6d9d90:/# mysql -h db -upems -ppemsroot

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 11

Server version: 5.6.19-v1-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| pems |

| test |

+--------------------+

3 rows in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值