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

在 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.dtcms.com/a/352767.html

相关文章:

  • KDMS V4 重磅升级,智能迁移赋能国产化替代!
  • Ubuntu下MySQL、MongoDB与Redis:从安装到协同的完整指南
  • Hive中的with子句
  • 学习游戏制作记录(存档点和丢失货币的保存以及敌人的货币掉落)8.27
  • 【OpenGL】LearnOpenGL学习笔记16 - 帧缓冲(FBO)、渲染缓冲(RBO)
  • 關於一些特性的小記
  • visual studio2022 配置 PCL 1.13.1
  • SwiftUI 三阵诀:杨过绝情谷悟 “视图布阵” 之道
  • 阿里云——计算服务深度解析与选型
  • 非容器数据库(non-CDB)迁移为可插拔数据库(PDB)步骤
  • GPIO推挽和开漏的名称由来和本质含义
  • 数学建模模型
  • 【Linux】Docker洞察:掌握docker inspect命令与Go模板技巧
  • Oracle中快速排查存储过程问题
  • LeetCode - 739. 每日温度
  • 《金融对账系统雪崩隐患的深度复盘与架构重生》
  • 数据库服务-日志管理-备份与恢复-主从同步
  • 项目经验处理
  • 获取服务器指标的信息
  • 幂等性设计艺术:在分布式重试风暴中构筑坚不可摧的防线
  • 大批量查询数据库大字段导致OOM问题
  • TCP服务端并发模型
  • 第5章 Excel公式与函数应用指南(4):日期和时间函数
  • Paimon——官网阅读:主键表
  • 计算神经科学数学建模编程深度前沿方向研究(下)
  • 【C++】类型系统:内置类型与自定义类型的对比
  • super(msg)层层上抛
  • 数据结构青铜到王者第七话---队列(Queue)
  • 基于Spring Boot的考研辅导知识共享平台-项目分享
  • Node.js 多版本管理工具 nvm 的安装与使用教程(含镜像加速与常见坑)