
XAMPP与phpMyAdmin简介及问题背景
XAMPP是一款集成了Apache、MySQL、PHP和PERL的强大建站软件包,以其轻量级和易用性著称。它内置了功能完善的phpMyAdmin数据库管理工具,极大简化了数据库的操作与维护。针对部分用户在本地环境中无法打开phpMyAdmin的情况,以下提供具体的解决方案。
MySQL服务默认使用3306端口。如果计算机上已经单独安装过MySQL,该端口可能已被占用。此时安装XAMPP自带的MySQL必须更改端口设置,否则将无法通过phpMyAdmin进行访问,用户可能会遇到如图所示的报错信息。

修改过程较为简便。首先打开XAMPP控制面板,找到MySQL模块右侧的Config按钮并点击,系统将弹出my.ini文件选项,这是MySQL的核心配置文件,具体操作界面如图示。

仅修改MySQL端口仍不足以解决问题,还需调整phpMyAdmin的配置文件。这里提供两种不同的修改途径供选择。
修改配置文件解决端口冲突与提升安全性
一、解决phpMyAdmin访问错误的步骤
1. 定位到XAMPP的安装目录(默认为xampp文件夹,若自定义安装请找到对应路径),进入phpmyadmin子目录。在该目录下寻找config.inc.php文件,以下是我的默认配置示例: 复制代码 代码如下:
/* * Servers configuration */ $i = 0;
/* * First server */ $i++;
/* Authentication type and info */ $cfg['Servers'][$i]['auth_type'] = 'config'; $cfg['Servers'][$i]['user'] = 'username'; //mysql用户名 $cfg['Servers'][$i]['password'] = 'password'; //mysql密码 $cfg['Servers'][$i]['extension'] = 'mysqli'; //扩展配置,若访问出现没有配置mysqli等错误,加上这个。默认是有的 $cfg['Servers'][$i]['AllowNoPassword'] = true; $cfg['Lang'] = '';
/* Bind to the localhost ipv4 address and tcp */ $cfg['Servers'][$i]['host'] = '127.0.0.1'; $cfg['Servers'][$i]['connect_type'] = 'tcp';
/* User for advanced features */ $cfg['Servers'][$i]['controluser'] = 'pma'; $cfg['Servers'][$i]['controlpass'] = '';
/* Advanced phpMyAdmin features */ $cfg['Servers'][$i]['pmadb'] = 'phpmyadmin'; $cfg['Servers'][$i]['bookmarktable'] = 'pma_bookmark'; $cfg['Servers'][$i]['relation'] = 'pma_relation'; $cfg['Servers'][$i]['table_info'] = 'pma_table_info'; $cfg['Servers'][$i]['table_coords'] = 'pma_table_coords'; $cfg['Servers'][$i]['pdf_pages'] = 'pma_pdf_pages'; $cfg['Servers'][$i]['column_info'] = 'pma_column_info'; $cfg['Servers'][$i]['history'] = 'pma_history'; $cfg['Servers'][$i]['designer_coords'] = 'pma_designer_coords'; $cfg['Servers'][$i]['tracking'] = 'pma_tracking'; $cfg['Servers'][$i]['userconfig'] = 'pma_userconfig'; $cfg['Servers'][$i]['recent'] = 'pma_recent'; $cfg['Servers'][$i]['table_uiprefs'] = 'pma_table_uiprefs';
/* * End of servers configuration */
?>
接着在$cfg['Lang'] = ”之后添加如下代码: 复制代码 代码如下: $cfg['Servers'][$i]['port'] = '3307' 保存文件后重启Apache服务,在浏览器地址栏输入localhost/phpmyadmin,即可顺利进入phpMyAdmin的管理界面。

上述配置方式缺乏身份验证机制,任何人均可直接访问phpMyAdmin管理数据库,存在安全隐患。需再次回到配置文件,定位到以下代码段:
复制代码 代码如下: $cfg['Servers'][$i]['auth_type'] = 'config'
将上述代码中的config替换为cookie或http,系统将弹出登录验证界面(在Windows系统中,cookie和http对应的界面样式略有不同)。此处我将其修改为cookie,修改后会出现如图所示的验证界面。

2. 另一种方法同样是修改phpMyAdmin配置文件,但路径有所不同。需在phpmyadmin/libraries目录下找到config.default.php文件,其中包含了服务器的各项配置参数。
复制代码 代码如下: $cfg['Servers'][$i]['port'] = '';
找到该行代码,在我的版本中位于第132行,这是用于配置端口的设置。若值为空,则使用默认的3306端口。将其值修改为3307后保存,重启Apache服务,同样可以实现对phpMyAdmin的正常访问。
phpMyAdmin常用配置参数与认证方式解析
二、phpMyAdmin配置文件详解
打开phpmyadmin/libraries目录下的config.default.php文件,常用的参数配置说明如下:
复制代码 代码如下: $cfg['PmaAbsoluteUri'] = ''; //phpmyadmin的访问网址 ,默认就行 $cfg['TranslationWarningThreshold'] = 80; //服务器端口 $cfg['Servers'][$i]['host'] = 'localhost';//mysql主机ip,如果mysql和该phpmyadmin在同一服务器,则按默认localhost $cfg['Servers'][$i]['port'] = '3307'; //mysql端口,默认3306,保留为空即可 $cfg['Servers'][$i]['user'] = 'root'; //mysql用户名 $cfg['Servers'][$i]['password'] = '';//密码 $cfg['Servers'][$i]['auth_type'] = 'cookie'; //认证方式 /*端口、用户名、认证方式等也可以再config.inc.php中配置,并且优先级高 *$cfg['Servers'][$i]['auth_type'] = 'config'; 这个是在config.inc.php的 *配置,若不修改这个值,仍然可以直接访问phpmyadmin */ $cfg['DefaultLang']='zh'; //设置默认语言
对于认证方式$cfg['Servers'][$i]['auth_type']的值,主要有四种选项:cookie、http、HTTP和config。
采用config方式时,访问phpMyAdmin网址即可直接进入,无需输入用户名和密码,这种方式安全性较低,不建议使用。
当该项设置为cookie、http或HTTP时,登录phpMyAdmin需要输入用户名和密码进行验证,具体细节如下:
若PHP以Apache模块模式安装,支持使用http和cookie认证;
若PHP以CGI模式安装,仅可使用cookie认证。
此外,在cookie模式下,还可以设置$cfg['blowfish_secret'] = ”; (短语密码)。密码的具体内容可由用户自行决定,也可以留空不填。(此点未经实测,依据文档说明,建议忽略此项设置即可)