CentOS安裝MySQL的兩種方式——RPM和YUM

sxb_sunday發表於2024-04-29

0、首先確認是否安裝過MySQL

yum list installed | grep mysql
rpm -qa | grep mysql
若果有安裝,需要先刪除舊版本。yum remove mysql

一、透過官網下載tar壓縮包(或者直接下載RPM安裝包)

1.開啟MySQL官網下載地址,選擇適合自己的版本,下載tar壓縮包。

image

2.透過WinSCP等工具上傳到CentOS伺服器並解壓

image

建立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

image

再次確認是否解除安裝成功

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

2.檢視CentOS伺服器上是否已經存在yum安裝包
yum list | grep mysql

image

上圖不存在與 MySQL 伺服器軟體本身相關的軟體包。這些列表顯示了與 MySQL 資料庫相關的其他軟體包,如 MySQL 的客戶端庫、聯結器、PHP 的 MySQL 擴充套件等。

3.獲取MySQL官方yum源並安裝
MySQL官方源網站獲取yum源地址,具體步驟如下

image

image

把複製的下載連結在CentOS伺服器下載yum源(如果沒有安裝wget的話,先安裝wgetyum install wget
wget https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm

image

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

image

檢視所有版本的MySQL的yum源
yum makecache
yum repolist all | grep mysql

image

可以看到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

image

三、啟動MySQL

檢視MySQL服務狀態
systemctl status mysqld

image

啟動MySQL服務
systemctl start mysqld

相關文章