如何从主机连接到运行在 Docker 上的 MySQL

How to connect to MySQL running on Docker from the host machine(如何从主机连接到运行在 Docker 上的 MySQL)
本文介绍了如何从主机连接到运行在 Docker 上的 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用谷歌搜索过这个主题,但发现的主题很少.基于这些线程,我遵循了以下步骤.但是我遇到了一个问题.

I have already googled on this subject and found few threads. Based on these threads I have followed the following steps. But I am facing a problem.

基本上,我想为 mysql 创建一个 docker 镜像,然后从我的主机 (Mac OS X) 连接到它.

Basically, I want to create a docker image for mysql and then connect to it from my host machine (Mac OS X).

基于这篇帖子,我必须与主机共享 mysql unix 套接字.为此我做了以下步骤

Based on this post , I have to share the mysql unix socket with the host. towards this I have done the following steps

1. Start docker quick terminal
2. docker run --name mysql -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
3. docker exec -it mysql bash
4. mysql -uroot -p
5. create database MyDB;
6. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';
7. exit;
8. mkdir /Users/abhi/host
9. docker run -it -v /host:/shared mysql/mysql-server:latest

现在我收到错误

MacBook-Pro:~$ docker run -it -v /Users/abhi/host:/shared mysql/mysql-server
error: database is uninitialized and password option is not specified
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD

但是你看到我已经提供了密码并初始化了我的数据库.

But you see that I have provided the password and initialized my database.

我想要的是从我的主机,我可以连接到在 docker 中运行的 mysql 数据库.

All I want is that from my host machine, I can connect to the mysql database running inside docker.

: ----- 有效的解决方案------

: ----- solution which worked ------

谢谢里科.最后对我有用的步骤是

Thanks RICO. Finally the steps which worked for me are

1. Start docker quick terminal
2. docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
3. docker exec -it mysql bash
4. mysql -uroot -p
5. create database MyDB;

  or:
  CREATE USER 'root'@'%' IDENTIFIED BY 'root';
  GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

6. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';
7. exit;
8. docker-machine env default

使用步骤8获取的IP地址,端口为3306,用户为root,密码为password,数据库为MyDB.

Use the IP address obtained in step 8. port is 3306, user is root, password is password, database is MyDB.

连接成功!

推荐答案

所以你基本上需要将 mysql 端口暴露给你的主机:

So you basically you need to expose the mysql port to your host:

docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest

然后您可以使用mysql命令行从您的主机访问:

Then you can access from your host using the mysql command line:

mysql -h127.0.0.1 -ppassword -uroot

不确定为什么要尝试运行另一个容器来连接(也许您的意思是链接两个容器)

Not sure why you are trying to run another container to connect (perhaps you meant linking two containers)

如果您使用 Mac(或 Windows)和 docker-machine,您希望连接到您的 docker-machine 虚拟机的 IP 地址.例如:

If you are using Mac (or Windows) with docker-machine you want to connect to the IP address of your docker-machine VM. For example:

$ docker-machine ssh default
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
                          __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___  __| | ___   ___| | _____ _ __
| '_  / _  / _ | __| __) / _` |/ _  / __| |/ / _  '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.9.0, build master : 16e4a2a - Tue Nov  3 19:49:22 UTC 2015
Docker version 1.9.0, build 76d6bc9
docker@default:~$ ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 08:00:27:E6:C7:20
          inet addr:192.168.99.100  Bcast:192.168.99.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fee6:c720/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:18827 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10280 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1791527 (1.7 MiB)  TX bytes:2242596 (2.1 MiB)

然后连接到:

mysql -h192.168.99.100 -ppassword -uroot

这篇关于如何从主机连接到运行在 Docker 上的 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)