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
相关推荐
AIgorithmGEEK6 小时前
MySQL 索引全解(下):建、查、删实战与知识速查表
数据库·mysql·b+树·索引
Lost_the_wind6 小时前
MySQL学习笔记四
mysql
曦夜日长6 小时前
Git系统篇,Git本地使用(一):从“究极进化版“到三个区域的底层原理
大数据·linux·git·elasticsearch
shimly1234567 小时前
给断网无 sudo ubuntu22.04 机器安装 nodejs
linux
别动我齐刘海8 小时前
Linux 系统调试学习1
linux·运维·服务器·c++·学习·机器学习·机器人
Lsetea8 小时前
OpenSSL报certificate chain too long:error 22与verify_depth深度排查
linux·https·ssl证书·openssl·证书链
不像程序员的程序媛9 小时前
linux常见文本操作命令
linux·运维·服务器
wangbing112510 小时前
开发指南149-IotDB-常用命令
linux·运维·iotdb
看着博客敲代码10 小时前
WorkBuddy 接上 Kali MCP 做自动化渗透:信息收集到初步验证的落地记录
linux·人工智能·安全·网络安全·自动化
geovindu10 小时前
sql:Search-Optimized SQL Queries using mysql 9.0
android·数据库·sql·mysql·数据库开发·数据库架构