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
相关推荐
YXXY3133 小时前
线程的介绍(四)
linux
流星白龙4 小时前
【MySQL高阶】19.变更缓冲区,自适应哈希索引,日志缓冲区
数据库·windows·mysql
kTR2hD1qb5 小时前
从 Responses API 到 Chat Completions:一个模型网关的设计复盘
linux·前端
姓刘的哦5 小时前
大模型祛魅
linux
hj2862516 小时前
linux下一步学习内容
linux·运维
Counter-Strike大牛6 小时前
SpringBoot2.7.10+MyBatisPlus实现MySQL+DM双数据库切换
数据库·mysql
Crazy_eater7 小时前
Mysql(6)--基础查询
数据库·mysql
xier_ran8 小时前
【infra之路】Linux基础命令与系统排查
linux·运维·服务器
添砖java‘’8 小时前
MySQL事务
数据库·mysql
zh路西法8 小时前
【Linux 串口通信】基于 C++ 多线程的同步/异步串口实现
linux·运维·c++·python