在开发机上使用 phpMyAdmin 可视化管理 MySQL 数据库
AIStudio 开发机是基于容器的开发环境,不同于物理机。本文将描述:
- 如何在开发机中安装 MySQL,并使用 phpMyAdmin 进行连接和管理本地 MySQL 数据库。
- 以开发机为例,如何在 phpMyAdmin 中访问远程 MySQL 数据库。
注意
phpMyAdmin 是一款广泛使用的基于 Web 的 MySQL 和 MariaDB 数据库管理工具,提供简洁的图形化界面,适合数据库操作。
创建实验环境
登录一站式 AI 平台,进入开发机创建页面。
请选择预置镜像中的 ubuntu:22.04,建议开启 SSH 密码登录。
根据提示完成配置并启动开发机。
点击下方链接,直接访问智算云控制台的创建开发机页面。
Step 0 在开发机上安装与启动 MySQL
使用 MySQL 8.0 作为数据库服务。
更新系统软件包。
shellapt update
安装 mysql-server 软件包。
shellapt install mysql-server
shell# 查看版本 mysql --version
启动 MySQL。
shell/usr/bin/mysqld_safe &
重要
- 由于 AIStudio 的开发机属于容器化开发环境,不支持 systemd,因此无法使用依赖于 systemd 的 systemctl 或 service 命令。
- 如同时部署 phpMyAdmin 和 MySQL,建议在安装 phpMyAdmin 前启动 MySQL,否则会提示无法检测到 MySQL。
Step 1 安装 phpMyAdmin
在安装前请确保已启用 MySQL 服务。
apt install phpmyadmin
安装时,请根据终端提示进行安装。请记忆所输入的密码,同时在选择 web server 时请选择「1. apache2」。
The phpmyadmin package must have a database installed and configured before it can be used. This can be optionally handled with dbconfig-common.
If You are advanced database administrator and know that you want to perform this configuration manually, or if your database has already been installed and configured, you should refuse this option. Details on what needs to be done should most likely be provided in /usr/share/doc/phpmyadmin.
Otherwise, you should probably choose this option.
Configure database for phpmyadmin with bconfig-common?_(yes/no)_yes
Please provide a password for phpmyadmin to register with the database server. If left blank, a random password will be generated.
MysQL application password for_phpmyadmin:
Password confirmation:
Determining localhost credentials from /etc/mysql/debian.cnf: succeeded.
Please choose the web server that should be automatically configured to run phpMyAdmin.
1. apache2 2. lighttpd
(Enter the atems or ranges you want to select, separated by spaces.)
Web server to reconfigure automatically: 1
Step 2 安装与启动 Apache
安装 Apache。
shellapt install apache2
根据 Unbuntu 官方文档,需安装 mod_php,允许 Apache 解析 php 文件,使得 phpMyAdmin 登录页面正常显示。
shellapt install libapache2-mod-php8.1
启用 Apache 配置文件,激活 phpMyAdmin。
shella2enconf phpmyadmin
启动 Apache。
shellapachectl start
修改默认端口为 8080(可选)。如果需要通过开发机 Web 应用预览访问 phpmyadmin Web 界面,必须修改为一个大于等于 2000 的端口号。
shellvim /etc/apache2/ports.conf
改为:
Listen 8080
根据
/etc/apache2/ports.conf
中的提示,继续修改以下文件:shellvim /etc/apache2/sites-enabled/000-default.conf
改为:
<VirtualHost *:8080>
验证 Apache 已正常启动。
shell# 查询 apache 状态及监听端口,默认80 netstat -tulpn | grep apache2 # 或 ss -tulpn | grep apache2
重要
由于 AIStudio 的开发机属于容器化开发环境,不支持 systemd,因此无法使用依赖于 systemd 的 systemctl 或 service 命令。
Step 3 使用 phpMyAdmin 连接本地 MySQL
phpmyadmin 完成之后,已自动触发 dbconfig-common 配置工具。按照上文配置后,配置结果将储存在 /etc/phpmyadmin/config-db.php
中。
<?php
##
## database access settings in php format
## automatically generated from /etc/dbconfig-common/phpmyadmin.conf
## by /usr/sbin/dbconfig-generate-include
##
## by default this file is managed via ucf, so you shouldn't have to
## worry about manual changes being silently discarded. *however*,
## you'll probably also want to edit the configuration file mentioned
## above too.
##
$dbuser='phpmyadmin';
$dbpass='123456';
$basepath='';
$dbname='phpmyadmin';
$dbserver='localhost';
$dbport='3306';
$dbtype='mysql';
可以看到 phpmyadmin 生成了自己的配置数据库。用户名为 phpmyadmin
,密码由我们自行指定。
为了确保 phpMyAdmin 通过网络(TCP/IP)连接 MySQL,在 phpMyAdmin 的配置文件 (/etc/phpmyadmin/config.ini/php
) 中,我们对以下代码片段进行了两处关键修改,以确保能够顺利登录并管理 MySQL 数据库:
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
if (empty($dbserver)) $dbserver = 'localhost';
// ↓↓↓ 添加或修改的第1行 ↓↓↓
if ($dbserver == 'localhost') $dbserver = '127.0.0.1';
$cfg['Servers'][$i]['host'] = $dbserver;
if (!empty($dbport) || $dbserver != 'localhost') {
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['port'] = $dbport;
// ↓↓↓ 添加或修改的第2行 ↓↓↓
$cfg['Servers'][$i]['socket'] = '';
}
注意
为什么需要这两处修改?
- 目的:将数据库服务器地址从 localhost 强制改为 127.0.0.1。虽然 localhost 通常也指向本机,但在某些 PHP 或 MySQL 客户端配置中,可能会优先尝试使用 “Unix 套接字 (socket)”的本地连接方式,而不是通过网络端口。
- 目的:显式地清空(禁用)套接字文件路径。即使我们指定了 IP 地址 127.0.0.1 来倾向于 TCP/IP 连接,某些情况下 phpMyAdmin 或底层的 PHP 驱动可能仍然会尝试查找或使用一个默认的套接字文件。将 'socket' 设置为空字符串 '',进一步确保了连接请求只会通过我们期望的 TCP/IP 网络路径(即 127.0.0.1 和指定的端口号)进行。
Step 4 登录 phpMyAdmin
您可以在智算云控制台直接打开 phpmyadmin Web 界面,或者在本地计算机上访问开发机内的 phpmyadmin Web 界面。
如果创建开发机时打开了 Web 应用预览功能,并已设置监听端口号(例如,8080),可前往开发机详情页,点击 Web 应用预览。打开预览链接后,默认展示 apache 服务主页,在地址栏 URL 后添加 /phpmyadmin
可访问登录页。输入用户名(phpmyadmin
)和密码。点击“执行”即可登录。
如果要在本地的浏览器中打开 phpMyAdmin,请在本地计算机命令行终端设置端口转发。
在本地计算机设置 SSH 端口转发,格式如下:
shellssh -N -f -L 8080:127.0.0.1:8080 -p <开发机 SSH 端口> root@<开发机 SSH IP>
注意
详见 SSH 端口转发。
在本地计算机访问
http://localhost:8080/phpmyadmin
,进入登录页面。输入用户名(
phpmyadmin
)和密码。点击“执行”即可登录。
注意
若缺失配置或配置失败,phpMyAdmin 登录页面会报错: mysqli::real_connect(): (HY000/2002): Permission denied
。详见 phpMyAdmin Configuration。
使用 tcp 端口转发远程访问 MySQL 数据库
除了在同一台服务器上部署 MySQL 和 phpMyAdmin 之外,将 MySQL 数据库服务部署在一台专用服务器上,而将 phpMyAdmin(作为 Web 应用程序)部署在另一台独立的 Web 服务器上。
在讨论这种分离架构时,有必要澄清一下 SSH 端口转发 的角色和能力。许多人可能误以为 SSH 端口转发主要(甚至仅仅)是为 HTTP 流量(例如浏览网页)设计的,但这是一个常见的误解。实际上,SSH 端口转发是一项通用的网络技术,它能够安全地隧道化任何基于 TCP 协议的流量。 MySQL 数据库的通信(默认使用 TCP 端口 3306)正是这种 TCP 流量的典型例子。因此,我们可以利用 SSH 端口转发,将 phpMyAdmin 服务器发往 MySQL 数据库的连接请求,通过加密的 SSH 通道进行传输,从而增强数据连接的安全性或简化特定网络环境下的访问配置。
前提条件
- 开发机 A 已安装并启动 MySQL。
- 开发机 B 已安装 phpMyAdmin、Apache 和 php 扩展。
确保 mysql root 用户已配置密码
在开发机 A 上,登录数据库:
mysql -u root
修改密码,并刷新权限:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';
FLUSH PRIVILEGES;
完成后请重启 mysql 服务。
配置 SSH 端口转发 TCP 服务
在开发机 B 上,执行以下命令,将开发机 A 上的 MySQL 服务器的 MySQL 端口(3306)转发到开发机 A 本地的端口(3306)。
格式
ssh -N -f -L 3306:127.0.0.1:3306 -p <开发机_B_SSH_端口> root@<开发机_B_SSH_IP>
安装和配置 phpMyAdmin
在开发机 B 上,安装 phpmyadmin
安装 phpmyadmin 软件包。
shellapt update apt install phpmyadmin
由于 MySQL 与 phpMyAdmin 不是部署在同一台开发机中,安装 phpMyAdmin 时,请在
Configure database for phpmyadmin with bconfig-common?
出现时选择「no」。完成 apache 的安装与配置。详见 安装与启动 Apache。
修改
/etc/phpmyadmin/config.inc.php
。修改内容请参考 使用 phpMyAdmin 连接本地 MySQL。运行配置工具。
shelldpkg-reconfigure -plow phpmyadmin
输出示例如下:
shellroot@is-dbaofqoiqi2r7kao-devmachine-0:/# dpkg-reconfigure -plow phpmyadmin debconf: unable to initialize frontend: Dialog debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.) debconf: falling back to frontend: Readline Configuring phpmyadmin ---------------------- Since you are reconfiguring phpmyadmin, you may also want to reinstall the database which it uses. If you wish to reinstall the database for phpmyadmin, you should select this option. If you do not wish to do so (if you are reconfiguring the package for unrelated reasons), you should not select this option. Warning: if you opt to reinstall the database and install it under a name that already exists, the old database will be dropped without further questions. In that case a backup of the original database is made in /var/tmp/. Warning: if you change the name of the database, the old database will not be removed. If you change the name of the user that connects to the database, the privileges of the original user will not be revoked. Reinstall database for phpmyadmin? [yes/no] yes By default, phpmyadmin will be configured to use a MySQL server through a local Unix socket (this provides the best performance). To connect with a different method, or to a different server entirely, select the appropriate option from the choices here. 1. Unix socket 2. TCP/IP Connection method for MySQL database of phpmyadmin: 2 Please select the remote hostname to use, or select "new host" to enter a new host. 1. new host 2. 127.0.0.1 3. localhost Host name of the MySQL database server for phpmyadmin: 2 Please specify the port the MySQL database on the remote host is running on. To use the default port, leave this field blank. Port number for the MySQL service: 3306 Database user accounts can be configured to use a variety of plugins for authentication with MySQL. If the server default won't work with this application, it is necessary to specify one that will. Please select one from the list of available plugins. Leaving the selection set to its original value should work unless a remote server is using unpredictable defaults, but other options may not be supported by phpmyadmin. If problems arise, the package's documentation should give hints; see /usr/share/doc/phpmyadmin/. Your options are: * default - use the default determined by the server. * mysql_native_password - no MySQL authentication plugin is used. * sha256_password - a more secure password encryption algorithm. * caching_sha2_password - SHA2 plus an in-memory authentication cache. 1. default 2. mysql_native_password 3. sha256_password 4. caching_sha2_password Authentication plugin for MySQL database: 1 Please provide a name for the MySQL database to be used by phpmyadmin. MySQL database name for phpmyadmin: phpmyadmin_db Please provide a MySQL username for phpmyadmin to register with the database server. A MySQL user is not necessarily the same as a system login, especially if the database is on a remote server. This is the user which will own the database, tables, and other objects to be created by this installation. This user will have complete freedom to insert, change, or delete data in the database. If your username contains an @, you need to specify the domain as well (see below). Advanced usage: if you need to define the domain that the user will log in from, you can write "username@domain". MySQL username for phpmyadmin: pma_admin@localhost Please provide a password for phpmyadmin to register with the database server. If left blank, a random password will be generated. MySQL application password for phpmyadmin: Password confirmation: Please provide the name of the account with which this package should perform administrative actions. This user is the one with the power to create new database users. For MySQL, this is almost always "root". Note that this is not the same as the Unix login "root". Name of the database's administrative user: root Please choose the web server that should be automatically configured to run phpMyAdmin. 1. apache2 2. lighttpd 3. none of the above (Enter the items or ranges you want to select, separated by spaces.) Web server to reconfigure automatically: 1 dbconfig-common: writing config to /etc/dbconfig-common/phpmyadmin.conf Replacing config file /etc/dbconfig-common/phpmyadmin.conf with new version Replacing config file /etc/phpmyadmin/config-db.php with new version Please provide the password for the administrative account "root" with which this package should create its MySQL database and user. Password of the database's administrative user: checking privileges on database phpmyadmin_db for pma_admin@localhost: user creation needed. granting access to database phpmyadmin_db for pma_admin@localhost: success. verifying access for pma_admin@localhost: success. dbconfig-common: dumping mysql database phpmyadmin_db to /var/tmp/phpmyadmin.phpmyadmin_db.2025-05-19-22.32.mysql.oxHzs2. database does not exist. dbconfig-common: dropping old mysql database phpmyadmin_db. dropping database phpmyadmin_db: database does not exist. creating database phpmyadmin_db: success. verifying database phpmyadmin_db exists: success. populating database via sql... done. dbconfig-common: flushing administrative password apache2_invoke phpmyadmin: already enabled invoke-rc.d: could not determine current runlevel invoke-rc.d: policy-rc.d denied execution of reload. root@is-dbaofqoiqi2r7kao-devmachine-0:/#
配置完成后,phpmyadmin Web 界面访问访问与前文相同,不再赘述。