在 Docker 中安装 MySQL 失败并显示错误消息“无法通过套接字连接到本地 MySQL 服务器”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23234379/
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 20:27:15  来源:igfitidea点击:

Installing MySQL in Docker fails with error message "Can't connect to local MySQL server through socket"

mysqldocker

提问by Mohammad

I'm trying to install mysql inside a docker container,Tried various images from github, it seems they all manage to successfully install the mysql but when I try to run the mysql it gives an error:

我正在尝试在 docker 容器中安装 mysql,尝试了来自 github 的各种图像,似乎它们都成功安装了 mysql,但是当我尝试运行 mysql 时出现错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

错误 2002 (HY000):无法通过套接字'/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器

System specifications:

系统规格:

  • Ubuntu 12,04 its on AWS
  • Docker 0.10.0
  • Ubuntu 12,04 在 AWS 上
  • 码头工人 0.10.0

Packages I tried so far:

到目前为止我尝试过的软件包:

回答by odk

Remember that you will need to connect to running docker container. So you probably want to use tcp instead of unix socket. Check output of docker pscommand and look for running mysql containers. If you find one then use mysql command like this: mysql -h 127.0.0.1 -P <mysql_port>(you will find port in docker psoutput). If you can't find any running mysql container in docker psoutput then try docker imagesto find mysql image name and try something like this to run it: docker run -d -p 3306:3306 tutum/mysqlwhere "tutum/mysql" is image name found in docker images.

请记住,您需要连接到正在运行的 docker 容器。所以你可能想使用 tcp 而不是 unix socket。检查docker ps命令的输出并查找正在运行的 mysql 容器。如果你找到一个,然后使用 mysql 命令,如下所示:(mysql -h 127.0.0.1 -P <mysql_port>你会在docker ps输出中找到端口)。如果在docker ps输出中找不到任何正在运行的 mysql 容器,请尝试docker images查找 mysql 映像名称并尝试类似的操作来运行它: docker run -d -p 3306:3306 tutum/mysql其中“tutum/mysql”是在docker images.

回答by Ben Cassinat

Don't know how do i achieve this, but, i've be able to reach MYSQL by typing

不知道如何实现这一点,但是,我可以通过键入来访问 MYSQL

$ mysql -u root -h

$ mysql -u 根 -h

mywebsite:
  image: benftwc/pldev-webserver
  volumes:
    - ./mywebsite.fr/:/var/www/
  working_dir: /var/www/
  ports:
    - "8009:8009"
  command: php -S 0.0.0.0:8009
  links:
    - database
database:
  image: library/mysql
  environment:
    MYSQL_ROOT_PASSWORD: root
  ports:
    - "3310:3306


root@422f4d1f454a:/# mysql -u root -h 127.0.0.1 -p3310
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)


root@422f4d1f454a:/# mysql -u root -h database -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g...........

回答by Mega

I had the same problem, in fact, I juste forgot to run the service after installation ..

我遇到了同样的问题,事实上,我只是在安装后忘记运行该服务..

Start mysql server :

启动mysql服务器:

/etc/init.d/mysql start

回答by Jeremy Jumeau

If you don't have MySQL installed on your host, you have to execute it in the container(https://docs.docker.com/engine/reference/commandline/exec/#/examplesgives explanation about docker runvs docker exec).

如果你的主机上没有安装 MySQL,你必须在容器中执行它(https://docs.docker.com/engine/reference/commandline/exec/#/examples给出了关于docker runvs docker exec 的解释)。

Considering your container is running, you might use docker exec yourcontainername mysql -u root -p to access to the client.

考虑到您的容器正在运行,您可能会使用 docker exec yourcontainername mysql -u root -p 来访问客户端。

Also, if you are using Docker Compose, and you've declared a mysql db service named database, you can use : docker-compose exec database mysql -u root -p

另外,如果您使用的是 Docker Compose,并且您已经声明了一个名为 database 的 mysql db 服务,则可以使用: docker-compose exec database mysql -u root -p

回答by Dave Ranjan

I might be little late for answer and probably world knows about this now.

我的回答可能有点晚了,现在可能全世界都知道了。

All you have to open your ports of docker container to access it. For example while running the container :

您只需打开 docker 容器的端口即可访问它。例如在运行容器时:

docker run --name mysql_container -e MYSQL_ROOT_PASSWORD=root -d -p 3306:3306mysql/mysql-server:5.7

docker run --name mysql_container -e MYSQL_ROOT_PASSWORD=root -d -p 3306:3306mysql/mysql-server:5.7

This will allow your container's mysql to be accessible from the host machine. Later you can connect to it.

这将允许您的容器的 mysql 可以从主机访问。稍后您可以连接到它。

docker exec -it mysql_container mysql -u root -p

docker exec -it mysql_container mysql -u root -p

回答by aiven

In my case I tried to connect to DB (which was inside docker) like this:

就我而言,我尝试像这样连接到 DB(在 docker 中):

mysql -ppass -u root

but got same error as OP.

但得到与 OP 相同的错误。

Specifying host and port helped:

指定主机和端口有助于:

mysql --host 0.0.0.0 --port 3306 -ppass -u root

回答by prograils

Check out what's in your database.ymlfile. If you already have your plain Rails app and simply wrapping it with Docker, you should change (inside database.yml):

查看您的database.yml文件中的内容。如果你已经有你的普通 Rails 应用程序并且只是用 Docker 包装它,你应该改变(内部database.yml):

socket: /var/run/mysqld/mysqld.sock #just comment it out

to

host: db 

where dbis the name of my db-service from docker-compose.yml. And here's my docker-compose.yml:

这里db是从我的DB-服务的名称docker-compose.yml。这是我的docker-compose.yml

version: '3'
services:
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    links:
      - db
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root

You start your app in console (in app folder) as docker-compose up. Then WAIT 1 MINUTE(let your mysql service to completely load) until some new logs stop appearing in console. Usually the last line should be like

您在控制台(在应用程序文件夹中)以docker-compose up. 然后等待 1 分钟(让您的 mysql 服务完全加载),直到一些新日志停止出现在控制台中。通常最后一行应该是这样的

db_1 | 2017-12-24T12:25:20.397174Z 0 [Note] End of list of non-natively partitioned tables

db_1 | 2017-12-24T12:25:20.397174Z 0 [注意] 非本地分区表列表结束

Then (in a new terminal window) apply:

然后(在新的终端窗口中)应用:

docker-compose run web rake db:create

and then

进而

docker-compose run web rake db:migrate

After you finish your work stop the loaded images with

完成工作后,停止加载的图像

docker-compose stop

Don't use docker-compose downhere instead because if you do, you will erase your database content.

不要docker-compose down在这里使用,因为如果这样做,您将删除数据库内容。

Next time when you want to resume your work apply:

下次您想恢复工作时申请:

docker-compose start

The rest of the things do exactly as explained here: https://docs.docker.com/compose/rails/

其余的事情完全按照此处的说明执行:https: //docs.docker.com/compose/rails/

回答by Ben Cassinat

Months after this question, I've levelup my Docker skills. I should use Docker container name instead.

在提出这个问题几个月后,我提升了我的 Docker 技能。我应该改用 Docker 容器名称。

That use dokerized-nginx as bridge to expose ip+port of the container.

使用 dokerized-nginx 作为桥来暴露容器的 ip+port。

Within WEB configuration, I now use mysql://USERNAME:PASSWORD@docker_container_name/DB_NAMEto access to Mysql socket through docker (also works with docker-compose, use compose-name instead of container one)

在WEB配置中,我现在使用mysql://USERNAME:PASSWORD@docker_container_name/DB_NAME通过docker访问Mysql socket(也适用于docker-compose,使用compose-name而不是container one)

回答by USMAN FAZIL

I ran into the same issue today, try running ur container with this command.

我今天遇到了同样的问题,请尝试使用此命令运行您的容器。

docker run --name mariadbtest -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypass -d mariadb/server:10.3

回答by ZiGiUs

For me it was simply a matter of restarting the docker daemon..

对我来说,这只是重新启动 docker 守护进程的问题。