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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33795923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:25:20  来源:igfitidea点击:

How to connect to MySQL running on Docker from the host machine

mysqldocker

提问by Knows Not Much

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.

我已经在这个主题上用谷歌搜索过,发现很少有线索。基于这些线程,我遵循了以下步骤。但我面临一个问题。

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

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

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

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

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

Now I get the error

现在我得到了错误

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.

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

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

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

EDIT:: ----- 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

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

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

Connection is successful!

连接成功!

回答by Rico

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

因此,您基本上需要将 mysql 端口公开给您的主机:

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

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

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

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)

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

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:

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

$ 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)

Then connect to:

然后连接到:

mysql -h192.168.99.100 -ppassword -uroot

回答by Ajay Singh

docker run -e MYSQL_ROOT_PASSWORD=pass --name sql-db -p 3306:3306 mysql

docker run -e MYSQL_ROOT_PASSWORD=pass --name sql-db -p 3306:3306 mysql

docker exec -it sql-db bash

docker exec -it sql-db bash

mysql -u root -p

mysql -u 根 -p