当前位置: 首页 > ops >正文

在 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

  • 交互式配置
    1. 设置 root 密码(如果安装时未设置)。
    2. 删除匿名用户(推荐选择 Y)。
    3. 禁止 root 用户远程登录(推荐选择 N)。
    4. 删除测试数据库(推荐选择 Y)。
    5. 重新加载权限表(推荐选择 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

http://www.xdnf.cn/news/18829.html

相关文章:

  • Spring Cloud 高频面试题详解(含代码示例与深度解析)
  • 浏览器与计算机网络
  • 计算机网络:服务器处理多客户端(并发服务器)
  • 【Redis#8】Redis 数据结构 -- Zset 类型
  • Java 大视界 -- Java 大数据机器学习模型在电商推荐系统冷启动问题解决与推荐效果提升中的应用(403)
  • Containerd 安装与配置指南
  • 基于Spring Boot的民宿服务管理系统-项目分享
  • THM Smol
  • Redis 数据结构
  • Content-Type是application/x-www-form-urlencoded表示从前端到后端提交的是表单的形式
  • vue新能源汽车销售平台的设计与实现(代码+数据库+LW)
  • 数据结构-串
  • 【微信小程序教程】第13节:用户授权与登录流程狼惫
  • ES03-常用API
  • 前端工程化与AI融合:构建智能化开发体系
  • 【git】P1 git 分布式管理系统简介
  • 开源 C++ QT Widget 开发(七)线程--多线程及通讯
  • 使用openCV(C ++ / Python)的Alpha混合
  • 安卓闪黑工具:aosp16版本Winscope之搜索功能剖析
  • GTCB:引领金融革命,打造数字经济时代标杆
  • 微生产力革命:AI解决生活小任务分享会
  • 欧盟《人工智能法案》生效一年主要实施进展概览(一)
  • MyBatis 之关联查询(一对一、一对多及多对多实现)
  • 解决VSCode中Cline插件的Git锁文件冲突问题
  • BiLSTM-Attention分类预测+SHAP分析+特征依赖图!深度学习可解释分析,Matlab代码实现
  • 【项目】分布式Json-RPC框架 - 抽象层与具象层实现
  • Elasticsearch中的协调节点
  • 人类记忆如何启发AI?LLM记忆机制综述解读
  • 软考-系统架构设计师 计算机系统基础知识详细讲解二
  • 人工智能之数学基础:离散型随机变量的概率分布有哪些?