CentOS安装MySQL的两种方式——RPM和YUM

0、首先确认是否安装过MySQL

yum list installed | grep mysql
rpm -qa | grep mysql
若果有安装,需要先删除旧版本。yum remove mysql

一、通过官网下载tar压缩包(或者直接下载RPM安装包)

1.打开MySQL官网下载地址,选择适合自己的版本,下载tar压缩包。

2.通过WinSCP等工具上传到CentOS服务器并解压

创建MySQL的安装路径mkdir /usr/local/MySQL
解压上传好的MySQL压缩包,解压后得到若干RPM安装包

[root@2a792e360945 ~]# tar -xvf /root/temp/mysql-5.7.41-1.el7.x86_64.rpm-bundle.
tar -C /usr/local/MySQL
mysql-community-client-5.7.41-1.el7.x86_64.rpm
mysql-community-common-5.7.41-1.el7.x86_64.rpm
mysql-community-devel-5.7.41-1.el7.x86_64.rpm
mysql-community-embedded-5.7.41-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.41-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.41-1.el7.x86_64.rpm
mysql-community-libs-5.7.41-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm
mysql-community-server-5.7.41-1.el7.x86_64.rpm
mysql-community-test-5.7.41-1.el7.x86_64.rpm

3.安装RPM包
因为是使用RPM安装软件包,所以需要手动解决依赖关系,一般安装 MySQL 时需要按照以下顺序安装这些软件包。首先切换到MySQL的安装路径cd /usr/local/MySQL,然后依次执行下列安装命令

rpm -ivh mysql-community-common-5.7.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm

其他安装包是MySQL的开发文件或者测试文件,非必须安装。
如果安装mysql-community-server-5.7.41-1.el7.x86_64.rpm的时候,出现依赖关系的错误,可能是由于系统没有安装依赖项,根据提示安装缺少的依赖项再执行mysql-community-server-5.7.41-1.el7.x86_64.rpm的安装即可
例如我的CentOS是新安装的镜像,没有安装任何依赖项,有如下输出信息

[root@2a792e360945 MySQL]# rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.41-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
error: Failed dependencies:
        /usr/bin/perl is needed by mysql-community-server-5.7.41-1.el7.x86_64
        libaio.so.1()(64bit) is needed by mysql-community-server-5.7.41-1.el7.x86_64
        libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.41-1.el7.x86_64
        libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.41-1.el7.x86_64
        libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.41-1.el7.x86_64
        libnuma.so.1(libnuma_1.1)(64bit) is needed by mysql-community-server-5.7.41-1.el7.x86_64
        libnuma.so.1(libnuma_1.2)(64bit) is needed by mysql-community-server-5.7.41-1.el7.x86_64
        perl(Getopt::Long) is needed by mysql-community-server-5.7.41-1.el7.x86_64
        perl(strict) is needed by mysql-community-server-5.7.41-1.el7.x86_64

使用软件包管理工具(例如yum)来安装这些依赖项
yum install perl libaio libnuma perl-Getopt-Long perl-strict numactl
安装完依赖项之后,再安装MySQL服务,即可安装成功。
至此,RPM方式安装MySQL就结束了。

二、通过YUM的方式安装

这种安装方式的好处是不用手动管理依赖关系,YUM(软件包管理工具)会自动按顺序安装依赖项并解决依赖关系。但是不能安装任意我们想安装的历史版本。

1.先卸载上面安装过的5.7.41版本的MySQL
查看当前以及安装的MySQL
yum list installed | grep mysql
rpm -qa | grep mysql

[root@2a792e360945 MySQL]# yum list installed | grep mysql
mysql-community-client.x86_64           5.7.41-1.el7                   installed
mysql-community-common.x86_64           5.7.41-1.el7                   installed
mysql-community-libs.x86_64             5.7.41-1.el7                   installed
mysql-community-server.x86_64           5.7.41-1.el7                   installed

卸载这四个软件包
yum remove mysql-community-client mysql-community-common mysql-community-libs mysql-community-server

再次确认是否卸载成功

[root@2a792e360945 MySQL]# rpm -qa | grep mysql
[root@2a792e360945 MySQL]#

2.查看CentOS服务器上是否已经存在yum安装包
yum list | grep mysql

上图不存在与 MySQL 服务器软件本身相关的软件包。这些列表显示了与 MySQL 数据库相关的其他软件包,如 MySQL 的客户端库、连接器、PHP 的 MySQL 扩展等。

3.获取MySQL官方yum源并安装
MySQL官方源网站获取yum源地址,具体步骤如下

把复制的下载链接在CentOS服务器下载yum源(如果没有安装wget的话,先安装wgetyum install wget
wget https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm

②安装yum源
rpm -Uvh mysql80-community-release-el7-11.noarch.rpm

查看所有版本的MySQL的yum源
yum makecache
yum repolist all | grep mysql

可以看到8.0版本的yum源是enable,而5.7版本是disable,但是我要安装5.7版本,按照下面命令禁用8.0,启用5.7即可
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community

点击查看代码

[root@2a792e360945 MySQL]# yum-config-manager --disable mysql80-community
Loaded plugins: fastestmirror, ovl
======================================== repo: mysql80-community =========================================
[mysql80-community]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/7
baseurl = http://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64
cache = 0
cachedir = /var/cache/yum/x86_64/7/mysql80-community
check_config_file_age = True
compare_providers_priority = 80
cost = 1000
deltarpm_metadata_percentage = 100
deltarpm_percentage =
enabled = 0
enablegroups = True
exclude =
failovermethod = priority
ftp_disable_epsv = False
gpgcadir = /var/lib/yum/repos/x86_64/7/mysql80-community/gpgcadir
gpgcakey =
gpgcheck = True
gpgdir = /var/lib/yum/repos/x86_64/7/mysql80-community/gpgdir
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2023,
   file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022,
   file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
hdrdir = /var/cache/yum/x86_64/7/mysql80-community/headers
http_caching = all
includepkgs =
ip_resolve =
keepalive = True
keepcache = False
mddownloadpolicy = sqlite
mdpolicy = group:small
mediaid =
metadata_expire = 21600
metadata_expire_filter = read-only:present
metalink =
minrate = 0
mirrorlist =
mirrorlist_expire = 86400
name = MySQL 8.0 Community Server
old_base_cache_dir =
password =
persistdir = /var/lib/yum/repos/x86_64/7/mysql80-community
pkgdir = /var/cache/yum/x86_64/7/mysql80-community/packages
proxy = False
proxy_dict =
proxy_password =
proxy_username =
repo_gpgcheck = False
retries = 10
skip_if_unavailable = False
ssl_check_cert_permissions = True
sslcacert =
sslclientcert =
sslclientkey =
sslverify = True
throttle = 0
timeout = 30.0
ui_id = mysql80-community/x86_64
ui_repoid_vars = releasever,
   basearch
username =

[root@2a792e360945 MySQL]# yum-config-manager --enable mysql57-community
Loaded plugins: fastestmirror, ovl
======================================== repo: mysql57-community =========================================
[mysql57-community]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/7
baseurl = http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64
cache = 0
cachedir = /var/cache/yum/x86_64/7/mysql57-community
check_config_file_age = True
compare_providers_priority = 80
cost = 1000
deltarpm_metadata_percentage = 100
deltarpm_percentage =
enabled = 1
enablegroups = True
exclude =
failovermethod = priority
ftp_disable_epsv = False
gpgcadir = /var/lib/yum/repos/x86_64/7/mysql57-community/gpgcadir
gpgcakey =
gpgcheck = True
gpgdir = /var/lib/yum/repos/x86_64/7/mysql57-community/gpgdir
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2023,
   file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022,
   file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
hdrdir = /var/cache/yum/x86_64/7/mysql57-community/headers
http_caching = all
includepkgs =
ip_resolve =
keepalive = True
keepcache = False
mddownloadpolicy = sqlite
mdpolicy = group:small
mediaid =
metadata_expire = 21600
metadata_expire_filter = read-only:present
metalink =
minrate = 0
mirrorlist =
mirrorlist_expire = 86400
name = MySQL 5.7 Community Server
old_base_cache_dir =
password =
persistdir = /var/lib/yum/repos/x86_64/7/mysql57-community
pkgdir = /var/cache/yum/x86_64/7/mysql57-community/packages
proxy = False
proxy_dict =
proxy_password =
proxy_username =
repo_gpgcheck = False
retries = 10
skip_if_unavailable = False
ssl_check_cert_permissions = True
sslcacert =
sslclientcert =
sslclientkey =
sslverify = True
throttle = 0
timeout = 30.0
ui_id = mysql57-community/x86_64
ui_repoid_vars = releasever,
   basearch
username =

③安装MySQL软件包
yum -y install mysql-community-server

三、启动MySQL

查看MySQL服务状态
systemctl status mysqld

启动MySQL服务
systemctl start mysqld

热门相关:视死如归魏君子   盖世双谐   勇闯天涯   天龙邪尊   极品医圣