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

Ubuntu 22.04.3 LTS 安装 MySQL

  1. 更新系统包索引
sudo apt update
sudo apt upgrade -y
  1. 安装MySQL
sudo apt install mysql-server -y

检查MySQL服务状态

sudo systemctl status mysql

运行MySQL安全配置脚本

sudo mysql_secure_installation
设置root密码
删除匿名用户
容许root远程登录
删除测试数据库
重新加载权限表
sudo mysql_secure_installationSecuring 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:Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.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
Success.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) :... 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!

1. 设置root用户密码

登录到MySQL:

sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
FLUSH PRIVILEGES;

2. 允许root用户从任何主机连接

MySQL 8.0中,需要创建一个新的root用户条目,允许从任何主机连接:

CREATE USER 'root'@'%' IDENTIFIED BY '新密码';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

3. 配置MySQL监听所有网络接口

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
#bind-address		= 127.0.0.1
bind-address		= 0.0.0.0
#mysqlx-bind-address	= 127.0.0.1
mysqlx-bind-address	= 0.0.0.0

4. 重启MySQL服务

sudo systemctl restart mysql

5. 配置防火墙允许MySQL连接

sudo ufw allow mysql
sudo ufw allow 3306/tcp

6. 查询用户表以查看认证插件信息

mysql> SELECT user, host, plugin FROM mysql.user WHERE user='root';
+------+-----------+-----------------------+
| user | host      | plugin                |
+------+-----------+-----------------------+
| root | %         | caching_sha2_password |
| root | localhost | mysql_native_password |
+------+-----------+-----------------------+
2 rows in set (0.00 sec)

将远程root用户的认证插件更改为 mysql_native_password

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
FLUSH PRIVILEGES;
systemctl restart mysql

7. 测试远程连接

mysql -h 服务器IP地址 -P 3306 -u root -p

8. 修改MySQL端口

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
port = 3600

9. 重启MySQL服务以应用更改

sudo systemctl restart mysql

10. 验证MySQL是否在新端口上监听

sudo netstat -tlnp | grep mysql
sudo ss -tlnp | grep mysql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)mysql>
http://www.lryc.cn/news/593158.html

相关文章:

  • Gitee 提交信息的规范
  • docker构建springboot镜像
  • LLM大模型微调技术与最佳实践
  • 小木的机器学习日记——线性回归算法-1
  • 【Linux】AKHQ实现kafka可视化
  • 3516cv610 npu 开发典型功能点的介绍
  • Helm常用命令大全(2025最新版)
  • 教育科技内容平台的破局之路:从组织困境到 UGC 生态的构建
  • ARINC818协议综述
  • 《全栈博客系统的技术肌理:从接口构建到体验升维的实践路径》
  • XSS的反射型、DOM型、存储型漏洞
  • TCP/UDP协议深度解析(四):TCP的粘包问题以及异常情况处理
  • 100条常用SQL语句
  • 【人工智能】AI Agent 实战:使用 Dify 搭建个人在线旅游助手
  • 多模态交互视角下生成式人工智能在中小学探究式学习中的认知支架效能研究
  • python基础笔记
  • Spring Boot 与微服务详细总结
  • 【黑马SpringCloud微服务开发与实战】(三)微服务01
  • 软考 系统架构设计师系列知识点之杂项集萃(112)
  • Go-Redis × 向量检索实战用 HNSW 在 Redis 中索引与查询文本 Embedding(Hash JSON 双版本)
  • 微服务的编程测评系统-网关-身份认证-redis-jwt
  • 【C# in .NET】20. 探秘静态类:抽象与密封的结合体
  • 【安卓笔记】RxJava的Hook机制,整体拦截器
  • Kotlin内联函数
  • kotlin部分常用特性总结
  • Window延迟更新10000天配置方案
  • 【设计模式C#】状态模式(用于解决解耦多种状态之间的交互)
  • 使用目标检测yolo11去训练微藻细胞检测数据集YOLO格式703张6类别步骤和流程
  • 云端成本治理利器:亚马逊云科技智能仪表盘(AWS Cost Intelligence Dashboard)深度解析
  • Java 大视界 -- Java 大数据机器学习模型在自然语言处理中的对话系统多轮交互优化与用户体验提升(351)