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

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

我正在 Ubuntu 19.04 发行版上运行,并且有一个 Dockerfile,当我们到达步骤 7 时;

Step 7/11 : RUN (/usr/bin/mysqld_safe &); sleep 5; mysqladmin -u root -proot create wordpress

我们得到;

2019-10-09T12:18:34.365421Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket 
'/var/run/mysqld/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

查看错误信息:mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

cookie@cookie-K501UX:~/code/docker$ ls -la /var/run/mysqld
total 8
drwxr-xr-x  2 mysql mysql  100 Oct  9 13:10 .
drwxr-xr-x 36 root  root  1060 Oct  9 13:10 ..
-rw-r-----  1 mysql mysql    6 Oct  9 13:10 mysqld.pid
srwxrwxrwx  1 mysql mysql    0 Oct  9 13:10 mysqld.sock
-rw-------  1 mysql mysql    6 Oct  9 13:10 mysqld.sock.lock

确实如此。和Check that mysqld is running

$ sudo systemctl status mysql
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-10-09 13:26:21 BST; 8min ago

这是。如果套接字文件存在并且 MySQL 守护进程正在运行 - 问题是什么?

答案1

问题是您正在等待 5 秒,而您可能需要 6 秒或更长时间

运行之前mysqladmin create wordpress 必须检查MYSQL是否准备好。

所以你可以使用循环mysqladmin ping

所以RUN命令可以是

RUN (/usr/bin/mysqld_safe &); \
     while( ! mysqladmin ping ) ;do  sleep 1 ; date ; done ; \
     mysqladmin -u root -proot create wordpress

答案2

Dockerfile对于将编译的最新版本,请参阅:

FROM ubuntu:19.04

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get -y install \
 apache2 \
 php7.2 \
 php7.2-mysql \
 supervisor \
 wget

RUN echo 'mysql-server mysql-server/root_password password root' | debconf-set-selections && \
echo 'mysql-server mysql-server/root_password_again password root' | debconf-set-selections

RUN apt-get install -qqy mariadb-server

RUN wget http://wordpress.org/latest.tar.gz && \
 tar xzvf latest.tar.gz && \
 cp -R ./wordpress/* /var/www/html && \
 rm /var/www/html/index.html

RUN (/usr/bin/mysqld_safe &); sleep 5; mysqladmin -u root -proot create wordpress

COPY wp-config.php /var/www/html/wp-config.php
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

EXPOSE 80

CMD ["/usr/bin/supervisord"]

相关内容