Mysql

錯誤:‘無法通過套接字’/var/run/mysqld/mysqld.sock’(2)連接到本地MySQL伺服器’

  • October 10, 2019

我在 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 守護程序正在執行 - 有什麼問題?

問題是您正在等待 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

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"]

引用自:https://unix.stackexchange.com/questions/545919