Debian/Ubuntu 系统 MySQL 完整安装教程

390 阅读5分钟

Debian/Ubuntu 系统 MySQL 完整安装教程

MySQL 是世界上最流行的开源关系型数据库管理系统之一。本文将详细介绍如何在 Debian/Ubuntu 系统上安装、配置和使用 MySQL 数据库。

1. 更新系统包列表

首先,我们需要更新系统的包列表以确保获取最新的软件包信息:

sudo apt update

这条命令会从配置的软件源下载最新的包信息,确保后续安装的软件是最新版本。

2. 安装 MySQL 服务器

更新包列表后,我们可以安装 MySQL 服务器:

sudo apt install mysql-server

安装过程中系统会自动处理依赖关系,并下载安装所需的文件。安装完成后,MySQL 服务会自动启动。

3. 启动并设置开机自启

虽然安装过程中 MySQL 服务已经启动,但我们还是要确认一下服务状态,并设置开机自启:

# 启动 MySQL 服务(如果尚未启动)
sudo systemctl start mysql

# 设置开机自启
sudo systemctl enable mysql

可以通过以下命令检查 MySQL 服务状态:

sudo systemctl status mysql

4. 运行安全配置脚本

MySQL 提供了一个安全配置脚本,用于提高数据库的安全性:

sudo mysql_secure_installation

运行该脚本后,会进入交互式配置过程,按照以下步骤进行:

步骤 1:设置验证密码插件(可选)

Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No:
  • 输入 y 启用密码强度验证插件
  • 输入 n 或直接按回车跳过(推荐新手跳过)

步骤 2:设置 root 用户密码

Please set the password for root here.
New password: 
Re-enter new password:

输入您想要设置的 root 密码并确认。

步骤 3:移除匿名用户

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :

输入 y 移除匿名用户(推荐)。

步骤 4:禁止 root 用户远程登录

Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

输入 y 禁止 root 用户远程登录(推荐)。

步骤 5:移除测试数据库

By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

输入 y 移除测试数据库(推荐)。

步骤 6:重新加载权限表

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

输入 y 重新加载权限表,使配置立即生效。

5. 登录 MySQL

完成安全配置后,可以使用以下命令登录 MySQL:

sudo mysql -u root -p

系统会提示输入之前设置的 root 密码。输入正确密码后,您将进入 MySQL 命令行界面:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.xx-xx Distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

6. MySQL 用户权限和远程访问配置详解

MySQL 用户权限采用 '用户名'@'主机名' 的格式:

用户权限格式详解:

'用户名'@'主机名'

主机名的含义:

  • 'user'@'localhost' - 只允许从本地访问
  • 'user'@'192.168.1.100' - 只允许从特定IP访问
  • 'user'@'192.168.1.%' - 允许从192.168.1.0/24网段访问
  • 'user'@'%' - 允许从任何主机访问(远程访问)

配置本地访问用户:

-- 登录 MySQL
sudo mysql -u root -p

-- 创建只能本地访问的用户
CREATE USER 'localuser'@'localhost' IDENTIFIED BY 'password123';

-- 授权数据库访问权限
GRANT ALL PRIVILEGES ON database_name.* TO 'localuser'@'localhost';

-- 刷新权限
FLUSH PRIVILEGES;

开启远程访问:

步骤 1:创建远程访问用户
-- 登录 MySQL
sudo mysql -u root -p

-- 创建可以从任何地方访问的用户(生产环境不推荐)
CREATE USER 'remoteuser'@'%' IDENTIFIED BY 'secure_password123';

-- 或者创建只能从特定IP访问的用户(推荐)
CREATE USER 'remoteuser'@'192.168.1.%' IDENTIFIED BY 'secure_password123';
步骤 2:授权用户权限
-- 授予特定数据库的所有权限
GRANT ALL PRIVILEGES ON database_name.* TO 'remoteuser'@'%';

-- 刷新权限
FLUSH PRIVILEGES;
步骤 3:配置 MySQL 服务器允许远程连接

编辑 MySQL 配置文件:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

找到并修改以下行:

# 将这一行注释掉或者修改为 0.0.0.0
# bind-address = 127.0.0.1
bind-address = 0.0.0.0
步骤 4:重启 MySQL 服务
sudo systemctl restart mysql
步骤 5:检查防火墙设置
# 检查防火墙状态
sudo ufw status

# 如果启用了防火墙,开放 MySQL 默认端口 3306
sudo ufw allow 3306/tcp

7. 基本 MySQL 操作

创建数据库

CREATE DATABASE example_db;

创建用户并授权

-- 创建用户
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'strong_password';

-- 授权
GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost';

-- 刷新权限
FLUSH PRIVILEGES;

退出 MySQL

EXIT;

8. 常用管理命令

# 检查 MySQL 服务状态
sudo systemctl status mysql

# 启动 MySQL 服务
sudo systemctl start mysql

# 停止 MySQL 服务
sudo systemctl stop mysql

# 重启 MySQL 服务
sudo systemctl restart mysql

9. 验证远程连接

在远程机器上测试连接:

mysql -h your-server-ip -u remoteuser -p

总结

通过以上步骤,您已经成功在 Debian/Ubuntu 系统上安装并配置了 MySQL 数据库。现在您可以开始创建数据库、用户,并进行各种数据库操作了。

记住要定期备份重要数据,并保持 MySQL 版本更新以确保安全性。