最近在容器中安装使用phpMyAdmin,登陆的时候提示#2002错误,详细错误提示:

#2002 - Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory") — 服务器没有响应(或本地服务器的套接字没有正确设置)。
mysqli_real_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")

2017-08-11_092704.png

解决 phpMyAdmin #2002 错误

修改phpMyAdmin目录下的libraries/config.default.php中的$cfg['Servers'][$i]['host']参数:

$cfg['Servers'][$i]['host'] = 'localhost';
修改为:
$cfg['Servers'][$i]['host'] = '127.0.0.1';
或:
$cfg['Servers'][$i]['host'] = 'mysql|db';

2017-08-11_093857.png

这就可以了。

例如Docker容器链接,链接的时候自动创建容器名的对应/etc/hosts,单独下载phpMyAdmin源码则需要按照上面的修改。

方法2

上传的phpMyAdmin程序,解压后浏览器中打开地址,类似:

http://ip address OR domain/phpmyadmin/setup/index.php

2017-08-11_095249.png

2017-08-11_095429.png

2017-08-11_095646.png

按照上图中选择,默认语言默认服务器(就是刚才新建的),换行符(就是程序所在系统环境 Linux/Windows),然后下载。下载下来的是config.inc.php文件,放在phpMyAdmin目录下就可以了。

config.inc.php

<?php
/*
 * Generated configuration file
 * Generated by: phpMyAdmin 4.7.3 setup script
 * Date: Fri, 11 Aug 2017 01:49:09 +0000
 */

/* Servers configuration */
$i = 0;

/* Server: db [1] */
$i++;
$cfg['Servers'][$i]['verbose'] = '';
$cfg['Servers'][$i]['host'] = 'db';
$cfg['Servers'][$i]['port'] = '';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';

/* End of servers configuration */

$cfg['blowfish_secret'] = '\'%]\\s&kj/SS9P%2[#?KJ\'E$n<YvvE8\\W';
$cfg['DefaultLang'] = 'en';
$cfg['ServerDefault'] = 1;
$cfg['UploadDir'] = '';
$cfg['SaveDir'] = '';
?>