在 Ubuntu 系统上安装 MySQL
在 Ubuntu 系统上安装 MySQL 的步骤如下,涵盖从安装到基本配置的完整流程:
1. 更新系统软件包
在安装任何软件之前,建议先更新系统以确保软件包列表和依赖关系最新:
# 更新软件包列表
sudo apt update
# 升级已安装的软件包(可选)
sudo apt upgrade -y
2. 安装 MySQL 服务器
使用 apt 包管理器安装 MySQL 服务器:
# 安装 MySQL 服务器
sudo apt install mysql-server -y
3. 验证 MySQL 服务状态
安装完成后,检查 MySQL 服务是否正常运行:
# 查看 MySQL 服务状态
sudo systemctl status mysql
# 如果服务未运行,手动启动
sudo systemctl start mysql
预期输出:
4. 安全配置 MySQL
运行安全脚本以增强 MySQL 的安全性:
sudo mysql_secure_installation
- 交互式配置:
- 设置 root 密码(如果安装时未设置)。
- 删除匿名用户(推荐选择 Y)。
- 禁止 root 用户远程登录(推荐选择 N)。
- 删除测试数据库(推荐选择 Y)。
- 重新加载权限表(推荐选择 Y)。
详细操作如下:
root@ubuntu:~# sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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) : u
... skipping.
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) : no
... skipping.
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
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
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
Success.
All done!
5. 登录 MySQL
使用 root 用户登录 MySQL 命令行工具:
root@ubuntu:~# mysql -uroot -pMax#3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.19-0ubuntu5 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
6、配置远程访问
默认情况下,MySQL 只允许本地访问。若需远程连接,请按以下步骤配置:
步骤 1:修改 MySQL 配置文件
Vim /etc/mysql/mysql.conf.d/mysqld.cnf
找到修改以下行:
bind-address = 127.0.0.1
替换为:
bind-address = 0.0.0.0
步骤 2:重启 MySQL 服务
sudo systemctl restart mysql
步骤 3:授权远程访问权限
登录 MySQL 并执行以下命令:
CREATE USER 'root'@'%' IDENTIFIED BY 'Max#3306';
-- 授予所有权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
-- 使权限立即生效
FLUSH PRIVILEGES;
MYSQL启动命令
Systemctl start mysql
MYSQL停止命令
Systemctl stop mysql
MYSQL重启命令
Systemctl restart mysql