Linux 系统安装 MySQL(CentOS8/Ubuntu),命令行实操完整版

前言

开发和服务器部署基本都是 Linux 环境,本篇手把手教你 CentOS8 和 Ubuntu 两大主流系统命令行安装 MySQL,全程命令复制即用,无多余操作。


一、通用前置准备

关闭防火墙、关闭 SELinux(服务器环境可选)

bash

运行

bash 复制代码
# CentOS
systemctl stop firewalld
systemctl disable firewalld

# Ubuntu
ufw allow 3306

二、CentOS8 安装 MySQL

  1. 安装 MySQL 源

bash

运行

bash 复制代码
dnf install -y https://dev.mysql.com/get/mysql80-community-release-el8-3.noarch.rpm
  1. 安装 MySQL 服务

bash

运行

bash 复制代码
dnf install -y mysql-community-server
  1. 启动并设置开机自启

bash

运行

bash 复制代码
systemctl start mysqld
systemctl enable mysqld
  1. 查看初始临时密码

bash

运行

bash 复制代码
grep 'temporary password' /var/log/mysqld.log
  1. 登录修改密码

bash

运行

bash 复制代码
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@123456';
  1. 开启远程连接

sql

sql 复制代码
CREATE USER 'root'@'%' IDENTIFIED BY 'Root@123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

三、Ubuntu 安装 MySQL

  1. 更新软件源

bash

运行

bash 复制代码
apt update
  1. 直接安装

bash

运行

bash 复制代码
apt install mysql-server -y
  1. 启动自启

bash

运行

bash 复制代码
systemctl start mysql
systemctl enable mysql
  1. 进入配置授权远程访问

bash

运行

bash 复制代码
sudo mysql

sql

sql 复制代码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
GRANT ALL ON *.* TO 'root'@'%';
FLUSH PRIVILEGES;
  1. 修改配置允许外网连接编辑 /etc/mysql/mysql.conf.d/mysqld.cnf 把 **bind-address = 127.0.0.1**注释掉,重启 MySQL。

四、常用运维命令

bash

运行

bash 复制代码
# 启动/停止/重启
systemctl start mysqld
systemctl stop mysqld
systemctl restart mysqld

# 查看状态
systemctl status mysqld
相关推荐
tntxia4 小时前
linux curl命令详解_curl详解
linux
扛枪的书生7 小时前
Linux 网络管理器用法速查
linux
顺风尿一寸9 小时前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
XIAOHEZIcode16 小时前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
唐青枫18 小时前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux
AlfredZhao3 天前
生产环境里,为什么不建议把普通端口直接暴露到公网?
linux·https·443·80
唐青枫3 天前
MySQL JSON 实战详解:从存储、查询、更新到 JSON_TABLE 与索引
sql·mysql
小满8783 天前
5.Mysql事务隔离级别与锁机制
mysql
元Y亨H3 天前
技术笔记:MySQL 字符集排序规则与大小写敏感性问题解决方案
mysql
戴为沐4 天前
Linux内存扩容指南
linux