下面以 Debian 11 / 12 为例,介绍 MySQL(官方版) 和 MariaDB(Debian 默认) 的安装与基本配置方法。你可以按需要选择其一。
| 方案 | 说明 |
|---|---|
| MariaDB | Debian 官方仓库默认,推荐新手 |
| MySQL | Oracle 官方版,适合生产环境 |
sudo apt update
sudo apt upgrade -y
sudo apt install mariadb-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
常见选项:
wget https://dev.mysql.com/get/mysql-apt-config_0.8.29-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.29-1_all.deb
选择:
sudo apt update
sudo apt install mysql-server -y
sudo systemctl start mysql
sudo systemctl enable mysql
sudo mysql_secure_installation
sudo mysql -u root -p
CREATE DATABASE testdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
# 或 MySQL
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
找到:
bind-address = 127.0.0.1
改为:
bind-address = 0.0.0.0
CREATE USER 'testuser'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'%';
FLUSH PRIVILEGES;
sudo systemctl restart mariadb
# 或
sudo systemctl restart mysql
⚠️ 生产环境不建议直接开放 3306 端口
sudo ufw allow 3306
sudo systemctl stop mariadb
sudo mysqld_safe --skip-grant-tables &
mysql
然后重置密码。
✅ Debian 上配置 MySQL / MariaDB 的核心步骤:
如果你需要:
可以直接告诉我你的使用场景。