【MYSQL】在Centos7和ubuntu22.04环境下安装

一.MYSQL在Centos7下的安装

  • 注意:安装与卸载中,⽤⼾全部切换成为root
  • 初期练习,mysql不进⾏⽤⼾管理,全部使⽤root进⾏

1.卸载内置环境

1-1卸载不要的环境

cpp 复制代码
[root@VM-0-3-centos ~]$ ps ajx |grep mariadb # 先检查是否有mariadb存在
13134 14844 14843 13134 pts/0 14843 S+ 1005 0:00 grep --color=auto mariadb
19010 19187 19010 19010 ? -1 Sl 27 16:55 /usr/libexec/mysqld --
basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --
log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid
--socket=/var/lib/mysql/mysql.sock
[root@VM-0-3-centos ~]$ systemctl stop mariadb.service # 停⽌mariadb服务
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===
[root@VM-0-3-centos ~]$ ps axj |grep mariadb# 停⽌完成
13134 14976 14975 13134 pts/0 14975 S+ 1005 0:00 grep --color=auto mariadb

ps axj | grep mariadb 查看进程,systemctl stop mariadb 停止服务,MariaDB 是 MySQL 的开源分支,可保留可卸载,根据实际需求决定。

1-2检查系统安装包

cpp 复制代码
[root@VM-0-3-centos ~]$ rpm -qa | grep mariadb
or
[whb@VM-0-3-centos ~]$ rpm -qa | grep mysql
...

# 下⾯是打印出来的mysql的样例,
[root@bite-alicloud mysql]# rpm -qa | grep mysql
mysql-community-common-5.7.41-1.el7.x86_64
mysql-community-server-5.7.41-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-client-5.7.41-1.el7.x86_64
mysql-community-libs-5.7.41-1.el7.x86_64

1-3卸载这些默认安装包

cpp 复制代码
#卸载显⽰出来的mariadb/mysql安装包
[rooot@VM-0-3-centos ~]$ sudo yum remove mariadb # 还是试着将上⾯的包都⼀个⼀个卸载吧
备份/etc/my.cnf,备份/var/lib/mysql数据

1-4获取mysql官⽅yum源

cpp 复制代码
#1. 获取mysql官⽅yum源
http://repo.mysql.com/
# ⼀定要看下⾯的⽂字!如果后⾯⾃⼰安装不成功,就看看这个
# 注意:最好安装和⾃⼰系统⼀致的mysql版本,否则可能会存在软件兼容性问题
# 查看⾃⼰的系统版本
# [root@VM-0-3-centos ~]# cat /etc/redhat-release
# CentOS Linux release 7.8.2003 (Core)
# 可以进⼊ http://repo.mysql.com/, 找⼀下和⾃⼰版本⼀致的资源
# 下载到你的本地,然后上传到你的Linux服务器

下载 MySQL的 yum源包:

wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm

2.安装mysql yum 源

cpp 复制代码
# 安装前
[root@VM-0-3-centos 9.9]$ ls
mysql57-community-release-el7-10.noarch.rpm

查看你本地的yum源 [我的和你的肯定不同,但是没有mysql相关的yum源]
[root@VM-0-3-centos 9.9]$ ls /etc/yum.repos.d/ -al
total 88
drwxr-xr-x. 2 root root 4096 Apr 9 15:43 .
drwxr-xr-x. 106 root root 12288 Apr 12 10:50 ..
-rw-r--r-- 1 root root 2523 Dec 26 19:31 Centos-Base.repo
-rw-r--r-- 1 root root 614 Nov 10 18:42 CentOS-Base.repo_bak
-rw-r--r-- 1 root root 2523 Jun 16 2018 CentOS-Base.repo.bak
-rw-r--r-- 1 root root 1309 Apr 8 2020 CentOS-CR.repo 
-rw-r--r-- 1 root root 649 Apr 8 2020 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root 230 Nov 10 18:42 CentOS-Epel.repo
-rw-r--r-- 1 root root 314 Apr 8 2020 CentOS-fasttrack.repo
-rw-r--r-- 1 root root 630 Apr 8 2020 CentOS-Media.repo
-rw-r--r-- 1 root root 998 Dec 11 2018 CentOS-SCLo-scl.repo
-rw-r--r-- 1 root root 971 Oct 29 2018 CentOS-SCLo-scl-rh.repo 
-rw-r--r-- 1 root root 1331 Apr 8 2020 CentOS-Sources.repo
-rw-r--r-- 1 root root 7577 Apr 8 2020 CentOS-Vault.repo
-rw-r--r-- 1 root root 616 Apr 8 2020 CentOS-x86_64-kernel.repo
-rw-r--r-- 1 root root 1919 Apr 8 23:57 docker-ce.repo
-rw-r--r-- 1 root root 1050 Nov 1 04:33 epel.repo
-rw-r--r-- 1 root root 1149 Nov 1 04:33 epel-testing.repo
-rw-r--r-- 1 root root 173 Dec 9 16:08 google-chrome.rep
-rw-r--r-- 1 root root 616 Apr 8 2020 CentOS-x86_64-kernel.repo
-rw-r--r-- 1 root root 1919 Apr 8 23:57 docker-ce.repo
-rw-r--r-- 1 root root 1050 Nov 1 04:33 epel.repo
-rw-r--r-- 1 root root 1149 Nov 1 04:33 epel-testing.repo
-rw-r--r-- 1 root root 173 Dec 9 16:08 google-chrome.repo
-rw-r--r-- 1 root root 1627 Apr 5 2017 mysql-community.repo#安装了mysql yum源
-rw-r--r-- 1 root root 1663 Apr 5 2017 mysql-community-source.repo

看看能不能正常⼯作

cpp 复制代码
[root@VM-0-3-centos 9.9]$ yum list |grep mysql
Repository epel is listed more than once in the configuration
mysql57-community-release.noarch el7-10 installed
akonadi-mysql.x86_64 1.9.2-4.el7 base
anope-mysql.x86_64 2.0.9-3.el7 epel
apr-util-mysql.x86_64 1.5.2-6.el7 base
calligra-kexi-driver-mysql.x86_64 2.9.10-2.el7 epel
collectd-mysql.x86_64 5.8.1-1.el7 epel
dmlite-plugins-mysql.x86_64 1.14.2-1.el7 epel
dovecot-mysql.x86_64 1:2.2.36-8.el7 base

3.进行安装

3-1安装mysql服务

cpp 复制代码
[root@VM-0-3-centos 9.9]$ sudo yum install -y mysql-community-server
[sudo] password for whb:
Loaded plugins: aliases, auto-update-debuginfo, fastestmirror, protectbase
Repository epel is listed more than once in the configuration
...
Installing : mysql-community-common-5.7.33-1.el7.x86_64
#公共模块
Installing : mysql-community-libs-5.7.33-1.el7.x86_64
#库
Installing : mysql-community-client-5.7.33-1.el7.x86_64
#客⼾端
Installing : mysql-community-server-5.7.33-1.el7.x86_64
#服务器
... #安装了服务和客⼾端

3-2查看配置⽂件和数据存储位置

cpp 复制代码
[root@VM-0-3-centos 9.9]$ ls /etc/my.cnf
/etc/my.cnf
[root@VM-0-3-centos 9.9]$ sudo ls /var/lib/mysql
....

3-3启动服务

cpp 复制代码
[root@VM-0-3-centos 9.9]$ systemctl start mysqld.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===

3-4查看启动服务

cpp 复制代码
[root@VM-0-3-centos 9.9]$ ps axj |grep mysqld
1 23183 23182 23182 ? -1 Sl 27 0:00 /usr/sbin/mysqld --
daemonize --pid-file=/var/run/mysqld/mysqld.pid
13134 23243 23242 13134 pts/0 23242 S+ 1005 0:00 grep --color=auto mysqld

4.进行登录

4-1登陆⽅法⼀

cpp 复制代码
#获取临时root密码
[root@VM-0-3-centos 9.9]$ sudo grep 'temporary password' /var/log/mysqld.log
2021-04-12T03:23:46.153263Z 1 [Note] A temporary password is generated for
root@localhost: yLMalT:v+5l*
#使⽤临时密码登录
[root@VM-0-3-centos 9.9]$ mysql -uroot -p
Enter password:

#判断修改密码时候新密码是否符合当前的策略,不满⾜报错,不让修改,关闭它
#安全强度,默认为中,即1,要求必须包含 数字、符号、⼤⼩写字⺟,⻓度⾄少为8位
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
#密码最⼩⻓度
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
#修改本地登录密码,暂不授权远程登录
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'qwer@wu.888';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
#如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码

4-2 登陆⽅法⼆

cpp 复制代码
# 如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码
# 试着直接client登陆⼀下

4-3 登陆⽅式三

cpp 复制代码
[root@VM-0-3-centos 9.9]# vim /etc/my.cnf
# 打开mysql配置⽂件
在[mysqld]最后⼀栏配置(不知道是什么,就放在配置⽂件最后) 加⼊: skip-grant-tables 选项,
并保存退出
[root@VM-0-3-centos 9.9]# systemctl restart mysqld # 重启mysql服务
# 登陆成功
root@VM-0-3-centos 9.9]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.41 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>

5.设置配置文件

5-1设置开机启动【可以不设】

得先退出mysql在进行

cpp 复制代码
#开启开机⾃启动
systemctl enable mysqld
systemctl daemon-reload

5-2配置my.cnf

cpp 复制代码
#配置一下my.conf,主要是数据库客户端和服务器的编码格式
 
[root@VM-0-3-centos 9.9]$ cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
 
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
 
port=3306
character-set-server=utf8
default-storage-engine=innodb
 
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
 
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
 
character-set-server=utf8
default-storage-engine=innodb
 
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables
配置完毕,重启即可

设置 MySQL 的字符集为 UTF-8,令其支持中文:

vim /etc/my.cnf

重启一下 MySQL,令配置生效:

重启 mysql:service mysqld restart

or

停止 mysql:service mysqld stop

启动 mysql:service mysqld start

5-3常⻅问题:

cpp 复制代码
mysql 已经配置了客⼾端服务器utf8编码,但是⽆法输⼊中⽂
确保您在终端命令⾏中可以输⼊中⽂
[whb@VM-0-3-centos ~]$ env |grep LANG
LANG=en_US.utf8

安装遇到秘钥过期的问题:
Failing package is: mysql-community-client-5.7.39-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决⽅案:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

二..MYSQL在ubuntu(22.04)下的安装

Ubuntu 20.04 安装 MySQL 步骤(纯步骤)

1. 更新软件源

cpp 复制代码
 sudo apt update
sudo apt upgrade -y 

2. 安装 MySQL 服务器

cpp 复制代码
  sudo apt install mysql-server -y

3. 启动服务并设置开机自启

cpp 复制代码
sudo systemctl start mysql
sudo systemctl enable mysql
 

4. 运行安全初始化脚本

cpp 复制代码
sudo mysql_secure_installation
(按提示设置密码、移除匿名用户、禁止root远程登录等)

5. 登录 MySQL(默认sudo方式)

cpp 复制代码
sudo mysql -u root
cpp 复制代码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你设置的密码';

一旦你执行这个命令就不花再有免密登录这一说相当于你强制mysql进行密码登录

6. 修改root为密码登录

cpp 复制代码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
FLUSH PRIVILEGES;
exit;

7. (可选)用密码登录验证

cpp 复制代码
mysql -u root -p

三.CentOS7 VS Ubuntu20.04 MySQL安装完整区别

  1. 包管理器

CentOS7:yum,必须手动下载官方源

Ubuntu20.04:apt,系统自带源,不用配源直接装

  1. 安装命令

CentOS7: yum install mysql-community-server

Ubuntu20.04: apt install mysql-server

  1. 初始登录方式(核心区别)

CentOS7:自带临时初始密码,直接 mysql -u root -p 登录

Ubuntu20.04:无密码,只能sudo免密登录,必须手动改密码

  1. 配置文件路径

CentOS7: /etc/my.cnf

Ubuntu20.04: /etc/mysql/mysql.conf.d/mysqld.cnf

  1. 系统自带冲突库

CentOS7:自带MariaDB,必须先卸载

Ubuntu20.04:无冲突库,直接安装

  1. 可选MySQL版本

CentOS7:可自由装 5.7 / 8.0

Ubuntu20.04:官方源只能装8.0,装不了5.7

  1. 防火墙端口

CentOS7:默认防火墙开启,3306端口默认放行

Ubuntu20.04:默认ufw防火墙关闭,3306端口不放行

  1. 安全脚本

CentOS7:安装后必须手动执行 mysql_secure_installation

Ubuntu20.04:推荐执行,但不强制,不改密码也能sudo登录

四.解释:

为什么 CentOS 7 和 Ubuntu 22.04 安装 MySQL 差距这么大?

同样是安装 MySQL,Ubuntu 22.04 一条命令就能搞定,CentOS 7 却要手动加源、卸载冲突包、配置一堆东西,为什么两者差异这么大?

一、核心根源:发行版的软件源策略不同

1. Ubuntu 22.04:默认源直接集成 MySQL

Ubuntu 属于 Debian 系 发行版,它的官方 apt 软件源中,默认就包含了 MySQL 8.0 的官方适配包( mysql-server )。

  • 你只需要执行 sudo apt update && sudo apt install mysql-server ,就能直接完成安装,不需要手动添加第三方源。
  • 系统会自动处理所有依赖,甚至帮你完成初始化、启动服务、设置开机自启。
  • 同时 Ubuntu 对 MySQL 做了定制化适配,比如默认使用 auth_socket 认证,让 sudo mysql 可以免密登录,进一步降低了上手门槛。

2. CentOS 7:默认源只有 MariaDB,没有官方 MySQL

  • CentOS 属于 RHEL 系 发行版,在 MySQL 被 Oracle 收购后,CentOS 7 的官方 yum 源彻底移除了官方 MySQL 包,默认只提供 MariaDB(MySQL 的开源分支)。
  • 如果你直接在 CentOS 7 执行 yum install mysql-server ,要么找不到包,要么会自动安装 MariaDB,而不是你想要的官方 MySQL。
  • 想要安装官方 MySQL,必须手动下载并添加 MySQL 官方 YUM 源,还要先卸载系统自带的 MariaDB 相关包(避免端口、配置冲突),才能正常安装。
  • 整个过程需要手动处理源、依赖、冲突,步骤自然比 Ubuntu 繁琐很多。

二、包管理器的生态差异

1. apt (Ubuntu):开箱即用的丰富生态

Ubuntu 的 apt 包管理器,软件源覆盖范围极广,对常用开源软件(比如 MySQL、Nginx、PHP)的支持非常完善,很多工具都默认集成在官方源中,用户几乎不需要手动配置源。

同时 apt 的依赖解析能力更强,安装时会自动补全所有需要的环境、库文件,用户不需要额外安装依赖。

2. yum (CentOS 7):保守的企业级源

CentOS 7 定位是企业级服务器系统,它的 yum 源以稳定、保守为核心原则,只保留经过严格测试的系统级软件,不会主动集成第三方商业软件(比如 Oracle 旗下的 MySQL)。

  • 企业级系统更倾向于使用开源分支 MariaDB 来替代 MySQL,因此官方源不会维护 MySQL 包。
  • yum 对依赖的处理相对严格,手动加源后还需要手动导入 GPG 密钥、处理依赖冲突,进一步增加了操作步骤。

3.默认认证方式的差异(遇到的免密问题)

除了安装步骤,两者的默认登录认证逻辑也完全不同,这也是很多同学觉得「差距大」的关键:

  • Ubuntu 22.04:默认使用 auth_socket 认证,只要你是 Linux 的 root 用户(通过 sudo 提权),就可以直接用 sudo mysql 免密登录 MySQL,不需要输入数据库密码,对新手非常友好。
  • CentOS 7:默认使用 mysql_native_password 密码认证,安装时会生成临时初始密码,必须用 mysql -u root -p 输入密码才能登录,没有「免密」一说,安全性更高,但上手更复杂。

4.总结:为什么 Ubuntu 更省心?

维度 Ubuntu 22.04 CentOS 7

软件源 默认集成 MySQL 8.0,直接安装 默认只有 MariaDB,需手动加 MySQL 源

包管理器 apt 生态丰富,自动处理依赖 yum 源保守,需手动处理依赖/冲突

认证方式 auth_socket 免密登录,新手友好 mysql_native_password 密码登录,更安全

安装步骤 1 条命令完成 卸载 MariaDB → 加源 → 安装 → 改密码

简单来说:Ubuntu 是桌面级易用性优先,CentOS 7 是企业级稳定优先,两者的定位不同,直接导致了 MySQL 安装体验的巨大差异。

补充:CentOS 7 也能像 Ubuntu 一样一键安装吗?

可以,但需要手动配置:

  1. 卸载 MariaDB: sudo yum remove -y mariadb-libs

  2. 添加 MySQL 官方 YUM 源: sudo yum localinstall -y mysql80-community-release-el7-7.noarch.rpm

  3. 安装 MySQL: sudo yum install -y mysql-community-server

  4. 启动服务并初始化: sudo systemctl start mysqld && sudo systemctl enable mysqld

只是这个过程需要手动操作,不像 Ubuntu 那样开箱即用。

相关推荐
环流_1 小时前
redis:持久化rdb
java·数据库·redis
代码中介商1 小时前
C++ STL 容器完全指南(三):deque、list 与 map 深度详解
开发语言·c++
Donk_671 小时前
MariaDB 数据库管理手册
数据库·mariadb
阿洛学长1 小时前
PostgreSQL 超详细安装与使用教程:从入门到实战
数据库·postgresql
许彰午2 小时前
Oracle Redo日志与Undo回滚段损坏恢复实战
数据库·oracle
qxwlcsdn2 小时前
mysql在事务中执行DDL的后果_MySQL 8.0之前的限制
jvm·数据库·python
eggrall2 小时前
Linux进程信号——像收快递一样理解 Linux 信号
linux·开发语言·c++
難釋懷2 小时前
Redis通信协议-基于Socket自定义Redis的客户端
数据库·redis·缓存
环流_2 小时前
redis:AOF
数据库·redis·spring